Level: Intermediate | Duration: 60 minutes | Cost: ~$3.00
Build an intelligent chatbot with conversation memory using Amazon Bedrock, LangChain, and Streamlit. Deploy it on AWS ECS with complete infrastructure as code.
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
โ User Browser โโโโโบโ Application โโโโโบโ ECS Fargate โ
โ โ โ Load Balancer โ โ Container โ
โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโ
โ Amazon Bedrock โ
โ (Claude/Nova) โ
โโโโโโโโโโโโโโโโโโโ
# AWS CLI v2
aws --version
# AWS CDK
npm install -g aws-cdk
cdk --version
# Python 3.11+
python --version
# Docker
docker --version
anthropic.claude-3-sonnet-20240229-v1:0 or amazon.nova-pro-v1:005-chatbot/
โโโ source/ # Application source code
โ โโโ chatbot_backend.py # Backend logic with LangChain
โ โโโ chatbot_frontend.py # Streamlit UI
โโโ chatbot/ # CDK infrastructure
โ โโโ app.py # CDK app entry point
โ โโโ chatbot/
โ โโโ chatbot_ecr_stack.py # ECR repository
โ โโโ chatbot_ecs_stack.py # ECS service & ALB
โโโ Dockerfile # Container configuration
โโโ docker-compose.yml # Local development
โโโ requirements.txt # Python dependencies
โโโ README.md # This guide
chatbot_backend.py)# Singleton pattern for efficient resource usage
_bedrock_llm = None
def get_bedrock_client():
global _bedrock_llm
if _bedrock_llm is None:
_bedrock_llm = ChatBedrockConverse(
region_name='us-east-1',
model='amazon.nova-pro-v1:0',
temperature=0.1, # Low randomness for consistent responses
max_tokens=1000 # Reasonable response length
)
return _bedrock_llm
def create_chat_memory():
return ConversationSummaryBufferMemory(
llm=get_bedrock_client(),
max_token_limit=2000 # Prevents context overflow
)
Memory Flow:
def get_ai_response(user_input, chat_memory):
conversation_chain = ConversationChain(
llm=get_bedrock_client(),
memory=chat_memory,
verbose=True # Debug logging
)
return conversation_chain.invoke(user_input)['response']
chatbot_frontend.py)# Initialize persistent memory across page reloads
if 'chat_memory' not in st.session_state:
st.session_state.chat_memory = backend.create_chat_memory()
# Initialize chat history for UI display
if 'chat_history' not in st.session_state:
st.session_state.chat_history = []
Step-by-Step User Interaction:
User Input Capture
user_input = st.chat_input("Ask me anything...")
Display User Message
with st.chat_message("user"):
st.markdown(user_input)
Store in Session History
st.session_state.chat_history.append({
"role": "user",
"text": user_input
})
Generate AI Response
ai_response = backend.get_ai_response(
user_input,
st.session_state.chat_memory
)
Display AI Response
with st.chat_message("assistant"):
st.markdown(ai_response)
Update Session History
st.session_state.chat_history.append({
"role": "assistant",
"text": ai_response
})
chatbot_ecr_stack.py)chatbot_ecs_stack.py)# Clone repository
git clone https://github.com/anveshmuppeda/amazon-bedrock.git
cd amazon-bedrock/05-chatbot
# Create virtual environment
python -m venv chatbot-env
source chatbot-env/bin/activate # Windows: chatbot-env\Scripts\activate
# Install dependencies
pip install -r requirements.txt
# Configure AWS CLI
aws configure
# Enter: Access Key, Secret Key, Region (us-east-1), Output format (json)
# Verify configuration
aws sts get-caller-identity
# List available models
aws bedrock list-foundation-models --region us-east-1
# Test model access
aws bedrock invoke-model \
--model-id "amazon.nova-pro-v1:0" \
--body '{"messages":[{"role":"user","content":[{"text":"Hello"}]}],"inferenceConfig":{"maxTokens":100}}' \
--cli-binary-format raw-in-base64-out \
--region us-east-1 \
output.json
# Start the application
docker-compose up --build
# Access application
open http://localhost:8501
# Navigate to source directory
cd source
# Set environment variables
export AWS_DEFAULT_REGION=us-east-1
export BEDROCK_MODEL_ID=amazon.nova-pro-v1:0
# Run Streamlit
streamlit run chatbot_frontend.py
Test Scenario:
cd chatbot
cdk bootstrap aws://$(aws sts get-caller-identity --query Account --output text)/us-east-1
# Deploy ECR stack
cdk deploy ChatbotEcrStack
# Note the ECR repository URI from output
# Get ECR login token
aws ecr get-login-password --region us-east-1 | \
docker login --username AWS --password-stdin \
$(aws sts get-caller-identity --query Account --output text).dkr.ecr.us-east-1.amazonaws.com
# Build for Linux/AMD64 (required for ECS)
docker build --platform linux/amd64 -t chatbot-ecr-repository:latest .
# Tag for ECR
docker tag chatbot-ecr-repository:latest \
$(aws sts get-caller-identity --query Account --output text).dkr.ecr.us-east-1.amazonaws.com/chatbot-ecr-repository:latest
# Push to ECR
docker push \
$(aws sts get-caller-identity --query Account --output text).dkr.ecr.us-east-1.amazonaws.com/chatbot-ecr-repository:latest
# Deploy ECS stack
cdk deploy ChatbotEcsStack
# Note the LoadBalancer DNS from output
# Get ALB DNS name
aws elbv2 describe-load-balancers \
--names chatbot-alb \
--query 'LoadBalancers[0].DNSName' \
--output text
# Access application
open http://[ALB-DNS-NAME]
User: "Remember that I work as a software engineer at AWS"
AI: "I'll remember that you work as a software engineer at AWS."
User: "What's my job?"
AI: "You work as a software engineer at AWS."
User: "I have a meeting at 3 PM about the new project"
AI: "Got it, you have a meeting at 3 PM about the new project."
User: "What time is my meeting?"
AI: "Your meeting is at 3 PM."
User: "What's it about?"
AI: "It's about the new project."
# Test API response time
curl -w "@curl-format.txt" -s -o /dev/null http://[ALB-DNS]/
# Simulate multiple users
for i in {1..5}; do
curl -X POST http://[ALB-DNS]/ &
done
# Check container status
docker ps
# Check health endpoint
curl http://localhost:8501/_stcore/health
# Check ECS service status
aws ecs describe-services \
--cluster chatbot-ecs-cluster \
--services chatbot-ecs-service
Error: AccessDeniedException
Solution: Enable model access in Bedrock console
Error: Token limit exceeded
Solution: Reduce max_token_limit in ConversationSummaryBufferMemory
# Check logs
docker logs [container-id]
# Common issues:
# - Missing AWS credentials
# - Wrong model ID
# - Network connectivity
# Check ECS logs
aws logs get-log-events \
--log-group-name "/chatbot/logs" \
--log-stream-name [stream-name]
# Test Bedrock connectivity
aws bedrock list-foundation-models --region us-east-1
# Check ECR repository
aws ecr describe-repositories --repository-names chatbot-ecr-repository
# Verify ECS service
aws ecs list-services --cluster chatbot-ecs-cluster
# Check ALB health
aws elbv2 describe-target-health --target-group-arn [target-group-arn]
| Component | Cost | Usage |
|---|---|---|
| ECS Fargate | ~$15 | 1 task, 0.5 vCPU, 1GB RAM |
| Application Load Balancer | ~$18 | Standard ALB |
| Bedrock API Calls | ~$5 | 1000 requests/month |
| CloudWatch Logs | ~$1 | Standard logging |
| Total | ~$39/month | Light usage |
# Destroy ECS stack
cdk destroy ChatbotEcsStack
# Destroy ECR stack (after confirming no images needed)
cdk destroy ChatbotEcrStack
# Verify cleanup
aws ecs list-clusters
aws ecr describe-repositories
After completing this guide, you'll understand:
Ready to build your intelligent chatbot? Start with local developmentโ and work your way up to production deployment! ๐
Content type
Image
Digest
sha256:0eaa467f8โฆ
Size
245.7 MB
Last updated
12 months ago
docker pull anvesh35/bedrock-chatbot