A scalable FastAPI-based chatbot designed for production deployment on AWS ECS with intelligent context management using vector databases.
- Backend: FastAPI with JWT authentication via AWS Cognito
- Database: PostgreSQL with pgvector extension for semantic search
- Context Management: Hybrid approach with recent messages + vector similarity search
- Deployment: Containerized for AWS ECS with external RDS
- Development: Docker with hot reload for rapid iteration
- Recent Messages: Last 20 messages for immediate context
- Semantic Search: Vector embeddings for relevant historical context
- Intelligent Retrieval: Combines recent + relevant messages for LLM prompts
- API: FastAPI containers on ECS behind ALB
- Database: RDS PostgreSQL with pgvector
- Authentication: AWS Cognito User Pools
- LLM: OpenAI API integration
A-Simple-Chatbot/
βββ app/ # FastAPI application
β βββ api/ # API routes and dependencies
β βββ core/ # Core configurations
β βββ models/ # SQLAlchemy models
β βββ schemas/ # Pydantic schemas
β βββ services/ # Business logic
βββ infra/ # Infrastructure
β βββ docker/ # Docker configurations
β βββ Dockerfile # Multi-stage build
β βββ docker-compose.yml # Development setup
β βββ docker-compose.ci.yml # CI/CD testing
β βββ scripts/ # Management scripts
βββ tests/ # Comprehensive test suite
βββ alembic/ # Database migrations
βββ scripts/ # Utility scripts
βββ docs/ # Architecture documentation
βββ pyproject.toml # Project dependencies & tool configs
βββ .flake8 # Flake8 configuration
βββ Makefile # Convenience commands
- Docker & Docker Compose
- Git
- Note: Python is NOT required locally - everything runs in Docker!
-
Clone and setup environment:
git clone <repo-url> cd A-Simple-Chatbot
-
Configure environment variables:
# Copy example env file cp infra/docker/.env.example .env # Edit the .env file nano .env
Required variables:
DATABASE_URL(auto-configured for development)COGNITO_*(your AWS Cognito configuration)OPENAI_API_KEY(your OpenAI API key)
-
Start development environment:
make dev
This starts:
- FastAPI with hot reload on http://localhost:8000
- PostgreSQL with pgvector on localhost:5432
- Real-time code changes via volume mounting
First-time setup: The first run will automatically pull the pre-built base image (
hero7hero/simple-chatbot:latest) from Docker Hub, which contains all Python dependencies. This is a one-time download. -
Run database migrations:
uv run alembic upgrade head
-
Access the application:
- API: http://localhost:8000
- Interactive docs: http://localhost:8000/docs
- Health check: http://localhost:8000/health
make dev # Start development environment (API + DB)
make test # Run test suite in CI environment
make build # Build the backend application
make clean # Clean up Docker resourcesmake format # Format code with black and isort
make lint # Run flake8 linter
make lint-check # Check formatting without modifying
make fix # Auto-fix code formatting and importsNote: All code quality commands run inside Docker containers, so you don't need Python installed locally!
make build-base # Build base image with all dependencies
make push-base # Build and push base image to Docker Hub# Development
./infra/docker/scripts/dev.sh # Linux/Mac
.\infra\docker\scripts\dev.bat # Windows
# Testing
./infra/docker/scripts/test.sh
.\infra\docker\scripts\test.bat
# Build application
./infra/docker/scripts/build.sh
.\infra\docker\scripts\build.bat
# Base image (maintainers only)
./infra/docker/scripts/build-base.sh
.\infra\docker\scripts\build-base.batThis project uses a pre-built base image (hero7hero/simple-chatbot:latest) that contains all heavy Python dependencies (langchain, sentence-transformers, etc.). This significantly speeds up builds for both development and CI/CD.
- You don't need to build the base image! It's automatically pulled from Docker Hub when you run
make devormake test. - All builds are fast because they use the cached base image.
pyproject.toml.
# 1. Update dependencies in pyproject.toml
# 2. Rebuild the base image locally
make build-base
# 3. Test that everything works
make test
# 4. Push to Docker Hub (requires authentication)
docker login
make push-baseGuidelines:
- DO rebuild when: Adding new packages, upgrading major dependencies (langchain, transformers, etc.)
- DON'T rebuild for: Minor version bumps, code changes, or configuration updates
- Always test locally before pushing to Docker Hub
- Coordinate with team before pushing new base images to avoid breaking CI/CD
The base image includes:
- Python 3.12-slim
- System dependencies (build-essential, libpq-dev, curl)
- uv package manager
- All Python packages from
pyproject.toml(production + dev)
# Create new migration
uv run alembic revision --autogenerate -m "description"
# Apply migrations
uv run alembic upgrade head
# Rollback migration
uv run alembic downgrade -1- Users: Cognito integration with user profiles
- Conversations: Chat sessions with metadata
- Messages: Individual messages with embeddings
- Message Embeddings: Vector representations for semantic search
Uses AWS Cognito for JWT-based authentication:
- Configure Cognito settings in
.env - Use signup/login scripts:
# Signup (requires real email for confirmation) uv run python -m scripts.cognito.signup # Login (returns JWT token) uv run python -m scripts.cognito.login
- Use token in API calls:
curl -H "Authorization: Bearer <token>" http://localhost:8000/api/v1/secure
Comprehensive test suite with different environments:
# Run all tests
make test
# Run specific test files
uv run pytest tests/test_api.py -v
# Run tests with coverage
uv run pytest --cov=app tests/Test environments:
- Development: Full integration tests with real database
- CI/CD: Automated testing with temporary containers
- Unit Tests: Fast isolated tests for business logic
- Health Checks: Built-in health endpoint for ECS
- Logging: Structured JSON logs for CloudWatch
- Metrics: Application performance monitoring ready
- Error Tracking: Sentry integration available
- Setup development environment (see Quick Start)
- Create feature branch:
git checkout -b feature/new-feature - Format and lint your code:
make format && make lint - Run tests:
make test - Submit pull request with tests and documentation
This project uses automated code quality tools to maintain consistency:
- Black (v25.9.0+): Code formatter with 100 char line length
- isort (v6.1.0+): Import statement organizer (black-compatible profile)
- flake8 (v7.3.0+): Style guide enforcer with max complexity of 10
All tools are configured in pyproject.toml and .flake8:
- Line length: 100 characters
- Target: Python 3.12+
- Excludes: alembic migrations, cache directories, vendor code
# Before committing
make format # Auto-format code with black and isort
make lint # Check for linting issues with flake8
# In CI/CD pipelines
make lint-check # Verify formatting without changes
# Quick fix
make fix # Format and fix all issues- Type Hints: Full typing coverage required for new code
- Testing: Comprehensive test coverage expected
- Documentation: Update relevant docs with changes
- Linting: All code must pass
make lint-checkbefore merging
- API Documentation: Available at
/docswhen running - Architecture Docs: See
docs/directory - Docker Guide: See
infra/docker/README.md - Database Schema: Generated diagrams in
docs/