Skip to content

agilityfeat/serverless-ai-chatbot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Serverless AI Chatbot MVP

A scalable, serverless AI chatbot backend built with AWS SAM, featuring hexagonal architecture for maintainability and portability.

⚠️ Security Disclaimer

This code is for demonstration purposes only and lacks the authorization and security measures required for a production MVP. Before deploying to production, implement at least:

  • API authentication (API keys, JWT tokens, or AWS Cognito)
  • Rate limiting and throttling
  • Input validation and sanitization
  • Proper IAM roles with least privilege
  • Monitoring and logging for security events

Architecture

  • API Gateway: REST endpoint (⚠️ No authentication in demo)
  • Lambda Function: Stateless chat orchestrator
  • DynamoDB: Conversation history storage
  • Amazon Bedrock: AI model integration (Claude 3 Haiku)

Project Structure

src/
├── app.py                    # Lambda handler (orchestrator)
├── core/
│   └── chat_service.py      # Framework-agnostic business logic
├── adapters/
│   ├── database_adapter.py  # DynamoDB operations
│   └── llm_adapter.py       # Bedrock AI integration
└── requirements.txt

Prerequisites

  • AWS CLI configured with appropriate permissions
  • AWS SAM CLI installed
  • AWS Account with anthropic.claude-3-haiku-20240307-v1:0 model enabled in Amazon Bedrock. (If you prefer to use a different one make appropriate changes in src/adapters/llm_adapter.py)
  • Python 3.11+

Deployment

  1. Build the application:

    sam build
  2. Deploy to AWS:

    sam deploy --guided
  3. Note the API endpoint URL from the deployment output.

API Usage

Endpoint

POST /chat

Request Body

{
  "userId": "user123",
  "message": "Hello, how are you?"
}

Success Response (200)

{
  "aiResponse": "Hello! I'm doing well, thank you for asking. How can I help you today?"
}

Error Responses

  • 400 Bad Request: Invalid input
  • 500 Internal Server Error: System error

Testing

Using curl

curl -X POST https://your-api-id.execute-api.region.amazonaws.com/Prod/chat \
  -H "Content-Type: application/json" \
  -d '{"userId": "test-user", "message": "Hello!"}'

Using Postman

  1. Create a new POST request
  2. Set URL to your API endpoint + /chat
  3. Set Content-Type header to application/json
  4. Add request body with userId and message

Performance Features

  • Warm Initialization: AWS SDK clients initialized outside handler
  • Efficient Queries: DynamoDB queries limited to recent messages
  • Stateless Design: No memory state between invocations

Scaling Considerations

For high-traffic scenarios, consider:

  • Enabling Provisioned Concurrency in template.yaml
  • Implementing DynamoDB auto-scaling
  • Adding CloudWatch monitoring and alarms

Frontend

An example web interface is available in the frontend/ directory:

  1. Deploy backend first (see Deployment section above)
  2. Update API endpoint in frontend/script.js
  3. Serve locally:
    cd frontend
    python -m http.server 8000
  4. Open: http://localhost:8000

Clean Up

To remove all resources:

sam delete

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published