Ask questions in plain English, get SQL back instantly. Rate queries with π/π, provide optional feedback on rejections, watch the system correct itself and get smarter with every interaction.
User question
β
βΌ
FastAPI backend
β
βββΊ pgvector similarity search β fetch top-k approved past queries
β
βββΊ Prompt builder (injects few-shot examples)
β
βββΊ Azure OpenAI GPT-4o β generates SQL
β
βββΊ asyncpg β saves query + embedding to PostgreSQL
β
βββΊ Returns SQL to frontend
β
βββΊ User runs it β results table shown
β
βββΊ User rates it
β
βββΊ π β stored as future few-shot example
β
βββΊ π β optional comment β background correction
β
βββΊ GPT-4o corrects using feedback signal
β
βββΊ corrected SQL stored & shown
β
βββΊ user can run corrected SQL too
| Layer | Tech |
|---|---|
| Backend | FastAPI + Python 3.12 |
| LLM | Azure OpenAI GPT-4o |
| Embeddings | Azure OpenAI text-embedding-3-small |
| Database | PostgreSQL 16 + pgvector |
| DB driver | asyncpg (MCP-compatible) |
| Frontend | React 18 + Vite |
- Docker + Docker Compose
- Azure OpenAI resource with GPT-4o and text-embedding-3-small deployed
cp backend/.env.example backend/.env
# Edit backend/.env with your Azure OpenAI credentialsdocker-compose up- Frontend: http://localhost:5173
- Backend API: http://localhost:8000
- API docs: http://localhost:8000/docs
Backend:
cd backend
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
pip install -r requirements.txt
cp .env.example .env # Fill in your credentials
uvicorn main:app --reloadPostgreSQL with pgvector:
# Using Docker just for the DB
docker run -d \
-e POSTGRES_PASSWORD=password \
-e POSTGRES_DB=sqlassistant \
-p 5432:5432 \
pgvector/pgvector:pg16Frontend:
cd frontend
npm install
npm run dev| Method | Path | Description |
|---|---|---|
| POST | /generate-sql |
Generate SQL from natural language |
| POST | /execute-sql |
Run a SELECT query, get rows back |
| POST | /feedback |
Submit π or π with optional comment |
| GET | /correction/{id} |
Poll for background correction result |
| GET | /history |
Recent query history |
- First run: No examples β raw GPT-4o generation
- Thumbs up: Query stored with embedding β becomes few-shot example for similar future questions
- Thumbs down + comment: LLM corrects using your feedback β corrected SQL stored alongside the rejection reason
- Future similar questions: Both approved originals and corrected queries are retrieved by vector similarity and injected as few-shot examples into the prompt
- Over time: The example bank grows richer β generation quality improves without any weight updates or retraining
The system uses two separate databases:
DATABASE_URLβ stores query feedback, embeddings, corrections (the learning store)TARGET_DATABASE_URLβ the database users are actually querying against
They can be the same DB, or separate ones. Set TARGET_DATABASE_URL to your actual application database.
- Only
SELECTstatements are permitted for execution (enforced server-side) - Results are capped at 500 rows
- Schema context is provided by the user β never auto-discovered from the target DB
- Use a read-only database user for
TARGET_DATABASE_URLin production
