A serverless backend that enables natural language querying of a Postgres database, converting user questions into SQL and returning structured, UI-friendly responses.
This project is a serverless FastAPI backend for a chatbot that generates and executes SQL queries on a Postgres database using OpenAI's GPT models, then returns structured, UI-friendly responses. It is designed to run on AWS Lambda via AWS SAM, but can also be run locally or in Docker.
/ask
endpoint├── main.py # Main FastAPI app and Lambda handler
├── requirements.txt # Python dependencies
├── template.yaml # AWS SAM template for Lambda deployment
├── samconfig.toml # AWS SAM deployment config
├── Dockerfile # For local/Docker deployment
├── .gitignore # Files to ignore in git
└── .env # (Not committed) Environment variables
git clone cd mcp-chat-3
python -m venv .venv source .venv/bin/activate # or .venv\Scripts\activate on Windows pip install -r requirements.txt
Create a .env
file (not committed to git):
OPENAI_API_KEY=your-openai-key
SUPABASE_DB_NAME=your-db
SUPABASE_DB_USER=your-user
SUPABASE_DB_PASSWORD=your-password
SUPABASE_DB_HOST=your-host
SUPABASE_DB_PORT=your-port
uvicorn main:app --reload --port 8080
docker build -t mcp-chat-backend . docker run -p 8080:8080 --env-file .env mcp-chat-backend
sam build sam deploy --guided
template.yaml
or via the AWS Console.https://xxxxxx.execute-api.region.amazonaws.com/Prod/ask
).{ "question": "your question here" }
{ "messages": [ { "type": "text", "content": "Sample 588 has a resistance of 1.2 ohms.", "entity": { "entity_type": "sample", "id": "588" } }, { "type": "list", "items": ["Item 1", "Item 2"] } ] }
main.py
for the full schema and more details.OPENAI_API_KEY
: Your OpenAI API keySUPABASE_DB_NAME
, SUPABASE_DB_USER
, SUPABASE_DB_PASSWORD
, SUPABASE_DB_HOST
, SUPABASE_DB_PORT
: Your Postgres database credentialsMIT (or your license here)
Discover shared experiences
Shared threads will appear here, showcasing real-world applications and insights from the community. Check back soon for updates!