A production-ready Retrieval-Augmented Generation (RAG) system built with Amazon Bedrock, Aurora PostgreSQL with pgvector, and Streamlit. This application provides intelligent question-answering capabilities for heavy machinery documentation using Claude 3 AI models with source attribution.
This system demonstrates a complete RAG implementation that:
- Validates prompts to ensure queries are relevant to heavy machinery
- Retrieves contextual information from a vector database using semantic search
- Generates accurate responses using Claude 3 AI models
- Displays source attribution showing which documents were used for each answer
- Maintains conversation history in an interactive chat interface
✅ Prompt Validation: Categorizes and filters user queries to ensure relevance
✅ Vector Search: Uses Amazon Titan embeddings (1536 dimensions) for semantic similarity
✅ RAG Pipeline: Combines retrieved context with LLM generation for accurate answers
✅ Source Attribution: Shows document sources, S3 locations, and relevance scores
✅ Configurable Parameters: Adjustable temperature and top_p for response control
✅ Multi-Model Support: Works with Claude 3 Haiku and Sonnet models
✅ Production Infrastructure: Full Terraform deployment with VPC, Aurora, and S3
┌─────────────────────────────────────────────────────────┐
│ Streamlit UI (app.py) │
│ Chat Interface + Controls │
└────────────────────────┬────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────┐
│ bedrock_utils.py │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │valid_prompt()│ │query_kb() │ │generate_ │ │
│ │ │ │ │ │response() │ │
│ └──────────────┘ └──────────────┘ └──────────────┘ │
└──────────┬─────────────────┬────────────────┬──────────┘
│ │ │
▼ ▼ ▼
┌──────────┐ ┌──────────────┐ ┌─────────────┐
│ Bedrock │ │ Bedrock │ │ Aurora │
│ Claude 3 │ │ Knowledge │ │ PostgreSQL │
│ │ │ Base │ │ + pgvector │
│ │ │ │ │ + indexes │
└──────────┘ └──────┬───────┘ └─────────────┘
│
▼
┌──────────────┐
│ S3 Bucket │
│ (PDFs) │
└──────────────┘
- AWS Account with appropriate permissions
- AWS CLI configured with credentials
- Terraform >= 1.0
- Python 3.9+
- Access to Amazon Bedrock models (Claude 3, Titan Embeddings)
- Git for version control
git clone <repository-url>
cd cd13926-Building-Generative-AI-Applications-with-Amazon-Bedrock-and-Python-project-solution
Deploy VPC, Aurora PostgreSQL, and S3 bucket:
cd stack1
terraform init
terraform apply
Note the outputs:
aurora_endpoint - Database endpoint
rds_secret_arn - Credentials secret
s3_bucket_name - S3 bucket for documents
Connect to Aurora using RDS Query Editor in AWS Console and run each SQL statement:
-- 1. Enable vector extension
CREATE EXTENSION IF NOT EXISTS vector;
-- 2. Create schema
CREATE SCHEMA IF NOT EXISTS bedrock_integration;
-- 3. Create user role
DO $$ BEGIN
CREATE ROLE bedrock_user LOGIN;
EXCEPTION WHEN duplicate_object THEN
RAISE NOTICE 'Role already exists';
END $$;
-- 4. Grant permissions
GRANT ALL ON SCHEMA bedrock_integration to bedrock_user;
-- 5. Create table with vector column
CREATE TABLE IF NOT EXISTS bedrock_integration.bedrock_kb (
id uuid PRIMARY KEY,
embedding vector(1536),
chunks text,
metadata json
);
-- 6. Create HNSW index for vector similarity search
CREATE INDEX IF NOT EXISTS bedrock_kb_embedding_idx
ON bedrock_integration.bedrock_kb
USING hnsw (embedding vector_cosine_ops);
-- 7. Create GIN index for text search (REQUIRED by Bedrock)
CREATE INDEX IF NOT EXISTS bedrock_kb_chunks_idx
ON bedrock_integration.bedrock_kb
USING gin (to_tsvector('english', chunks));cd ../scripts
python3 upload_s3.py
This uploads the heavy machinery specification PDFs to your S3 bucket.
cd ../stack2
terraform init
terraform apply
Note the output:
- `bedrock_knowledge_base_id` - Use this in the Streamlit app
- Go to AWS Console → Amazon Bedrock → Knowledge bases
- Click on `my-bedrock-kb`
- Go to Data sources tab
- Click Sync button
- Wait for sync to complete (~1-2 minutes)
cd ..
python3 -m venv venv
source venv/bin/activate # On Windows: venv\\Scripts\\activate
pip install -r requirements.txt
streamlit run app.py
The app will open at `http://localhost:8501\`
- Select LLM Model: Choose between Claude 3 Haiku (faster, cheaper) or Sonnet (more capable)
- Knowledge Base ID: Enter your KB ID from Stack 2 output
- Temperature (0.0-1.0): Controls randomness
- Low (0.0-0.3): Precise, factual responses
- High (0.7-1.0): Creative, varied responses
- Top_P (0.0-1.0): Controls diversity
- Low: More focused responses
- High: More diverse token selection
Valid Questions (Heavy Machinery Topics):
- "What is the payload capacity of the dump truck?"
- "Tell me about the hydraulic system of the excavator"
- "What are the safety features of the bulldozer?"
- "How does the forklift's lifting mechanism work?"
Invalid Questions (Will be Rejected):
- General knowledge questions
- Profanity or toxic content
- Questions about how the AI works
- Off-topic subjects
Each response includes:
- AI-Generated Answer: Comprehensive answer based on retrieved context
- 📚 View Sources (Expandable): Shows:
- Source Number with relevance score (e.g., 87.5%)
- Document Name (e.g., `excavator-x950-spec-sheet.pdf`)
- S3 Location (full path)
- Content Preview (first 200 characters)
- Metadata (if available)
.
├── app.py # Streamlit web application
├── bedrock_utils.py # Core RAG functions
├── requirements.txt # Python dependencies
├── README.md # This file
│
├── modules/ # Terraform modules
│ ├── database/ # Aurora PostgreSQL module
│ │ ├── main.tf
│ │ ├── variables.tf
│ │ └── outputs.tf
│ │
│ └── bedrock_kb/ # Bedrock Knowledge Base module
│ ├── main.tf
│ ├── variables.tf
│ └── outputs.tf
│
├── stack1/ # Infrastructure stack
│ ├── main.tf # VPC, Aurora, S3
│ ├── variables.tf
│ └── outputs.tf
│
├── stack2/ # Bedrock stack
│ ├── main.tf # Knowledge Base
│ ├── variables.tf
│ └── outputs.tf
│
└── scripts/ # Utility scripts
├── aurora_sql.sql # Database setup SQL
├── upload_s3.py # Upload PDFs to S3
│
└── spec-sheets/ # Heavy machinery PDFs
├── bulldozer-bd850-spec-sheet.pdf
├── dump-truck-dt1000-spec-sheet.pdf
├── excavator-x950-spec-sheet.pdf
├── forklift-fl250-spec-sheet.pdf
└── mobile-crane-mc750-spec-sheet.pdf
Three main functions:
Validates user prompts using Claude 3 to classify into categories:
- Category A: Questions about the AI system
- Category B: Toxic/profane content
- Category C: Off-topic questions
- Category D: Meta/instruction questions
- Category E: Heavy machinery questions ✅ (only valid category)
Returns `True` only for Category E.
Retrieves relevant context from Bedrock Knowledge Base:
- Queries using semantic search
- Returns top 3 most relevant chunks
- Includes source metadata (S3 URI, relevance score)
- Structured output with content, score, and source info
Generates AI responses using Claude 3:
- Accepts full prompt with context
- Configurable temperature and top_p parameters
- Returns formatted text response
- Handles errors gracefully
Streamlit application with:
- Chat interface with message history
- Sidebar configuration for model settings
- Prompt validation before processing
- Knowledge Base query for context retrieval
- Response generation with source attribution
- Session state management for persistence
Stack 1 (Foundation):
- VPC with public/private subnets across 3 AZs
- Aurora Serverless v2 PostgreSQL 15.13
- S3 bucket with versioning and encryption
- Security groups and IAM roles
- Secrets Manager for database credentials
Stack 2 (Bedrock):
- Bedrock Knowledge Base
- S3 data source configuration
- IAM roles for Bedrock access
- RDS vector database integration
- Check: Data source is synced (AWS Console → Bedrock → Knowledge Bases)
- Check: PDFs uploaded to S3
- Check: Aurora database has data
- "Cannot find version 15.4": Use Aurora PostgreSQL 15.13+
- "Embedding model ARN region mismatch": Ensure region is `us-east-1`
- "chunks column must be indexed": Run the GIN index SQL command
- Check: AWS credentials configured (`aws configure`)
- Check: Region matches (us-east-1)
- Check: IAM permissions for Bedrock, RDS, S3
This project is part of the Udacity AWS AI & ML Scholarship Program.
Built with ❤️ using Amazon Bedrock, Aurora PostgreSQL, and Streamlit