A production-grade backend for GST (Goods and Services Tax) Chartered Accountant Copilot, built with FastAPI, Supabase, and OpenAI.
- Stateless Backend: The backend is completely stateless and fetches conversation history from Supabase on each request.
- Single Endpoint:
POST /chat- handles all chat interactions - Frontend Integration: Frontend stores all conversations in Supabase. Backend only processes messages and updates the database.
- FastAPI: Python web framework
- Supabase: PostgreSQL database for conversation storage
- OpenAI API: GPT models for GST law reasoning and conversation summarization
pip install -r requirements.txtCopy .env.example to .env and fill in your credentials:
cp .env.example .envRequired environment variables:
SUPABASE_URL: Your Supabase project URLSUPABASE_KEY: Your Supabase anon/service keyOPENAI_API_KEY: Your OpenAI API key
Create a conversations table in your Supabase database:
CREATE TABLE conversations (
id TEXT PRIMARY KEY,
messages JSONB NOT NULL DEFAULT '[]'::jsonb,
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);uvicorn main:app --reloadThe API will be available at http://localhost:8000
Once the server is running, visit:
- Swagger UI:
http://localhost:8000/docs - ReDoc:
http://localhost:8000/redoc
Main endpoint for chat interactions.
Request Body:
{
"chat_id": "unique-conversation-id",
"message": "What is the GST rate for software services?"
}Response:
{
"chat_id": "unique-conversation-id",
"reply": "According to GST Act Section 2(102)...",
"success": true,
"error": null
}- Frontend creates a conversation record in Supabase on first message
- Every subsequent message calls
/chatwithchat_id+ latest user message - Backend fetches existing
conversation.messagesfrom Supabase - Backend builds a CA-safe prompt with deterministic GST rules
- Backend calls OpenAI API
- Backend appends assistant response to
conversation.messages - Backend updates Supabase and returns the reply
- ✅ Supabase DB client for conversation management
- ✅ Conversation summarization for long histories (gpt-4o-mini)
- ✅ Prompt builder with GST-specific rules
- ✅ OpenAI API integration with GPT-4
- ✅ Message append & save logic
- ✅ Comprehensive error handling
- ✅ CORS enabled for frontend integration
The AI assistant follows these rules:
- Never hallucinates GST law
- Always mentions GST Act Section / Rule when answering legal questions
- Asks for missing info instead of assuming
- Keeps responses concise and professional
.
├── main.py # FastAPI application
├── config.py # Configuration management
├── models.py # Pydantic models
├── services/
│ ├── supabase_service.py # Supabase client
│ ├── openai_service.py # OpenAI client & prompt builder
├── requirements.txt # Python dependencies
├── .env.example # Environment variables template
└── README.md # This file
MIT