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