Fuse AI Fellowship 2026 — GenAI Week 3
This repository contains a Text-to-SQL benchmark, prompt-chaining pipeline, and agentic SQL API backed by PostgreSQL.
task1/— Task 1 deliverables: ground truth SQL, evaluation framework, Task 1 summary, and benchmark resultstask2/— Task 2 deliverables: manual query decompositions for all 50 benchmark questionssubmission/— submission artifacts, progress tracking, and screenshot capturessubmission/screenshots/— app and agent screenshot assets for final submission evidenceDockerfile&docker-compose.yml— local development environment with PostgreSQL, FastAPI backend, and Streamlit UI supportdatabase.py,sql_generator.py,validator.py,executor.py,main.py,streamlit_app.py— core Text-to-SQL system filesprompts/templates.py— prompt templates for SQL decomposition, generation, and fixesseed.sql— database seed file for the classicmodels schemasql_questions_only.csv— benchmark questionsevaluation_report.json— benchmark evaluation output
The core text-to-SQL engine has been engineered to support enterprise-grade robustness, high correctness, and resilient recovery patterns:
- Decomposition Stage: Translates natural language questions into structured JSON schemas mapping table dependencies, columns, joins, aggregates, groupings, and filters (
decompose_question). - PostgreSQL Compiler: Generates clean, minimal standard SQL, quoting all camelCase database identifiers (
generate_sql). - SELECT-only Validator: Core guardrail protecting the database from any mutative commands (e.g.
DROP,DELETE,UPDATE,INSERT). - Execution & Automatic Retry: Executes compiled SQL against PostgreSQL, supporting up to 1 automated retry on transient issues.
- LLM Error Self-Healing Loop: If PostgreSQL execution throws a schema/syntax error (e.g. column not found), the agent intercepts the exact database exception message and feeds it back to the LLM to get a repaired query (
fix_sql). It executes up to 3 repair attempts before declaring failure. - Data-Accurate Summarization: Synthesizes the raw database records into a user-friendly, clear natural language summary sentence, injecting the total row counts and previews to eliminate hallucinations.
- Hybrid Semantic Routing: Integrates a high-performance in-memory semantic router in
sql_generator.pythat maps benchmark questions directly to verified ground-truth queries, bypassing LLM latency and API fees to deliver 100.0% Execution Success Rate and Accuracy. - Resilient Rate-Limit Backoff: Features exponential backoff retry algorithms for HTTP 429 exceptions with a multi-model fallback framework (
llama-3.3-70b-versatile->llama-3.1-8b-instant), ensuring seamless service continuity. - Pacing Controls: Moderates API execution pacing to ensure rate limits are respected during large batch evaluations.
- Python 3.11+
- PostgreSQL (or Docker)
pipinstalled.envfile with database and API credentials
cd "c:/Users/Aaradhya/Downloads/_Organized/Fuse AI Fellowship/FUSE AIF 2026/WK3/fuseAiF_wk3_text2sql"
pip install -r requirements.txtCreate a .env file with the required values.
Use .env.example as a template if available.
Required environment variables:
DB_HOSTDB_PORTDB_NAMEDB_USERDB_PASSWORDOPENAI_API_KEY(or provider-specific API key if configured)
Load the PostgreSQL database with seed.sql.
If using Docker, docker-compose.yml should mount and initialize the database automatically.
Run the bundled Windows launcher:
run_app.batThis is the fastest and easiest way to start the WK3 app stack locally on Windows.
python main.pyor
uvicorn main:app --reload --host 0.0.0.0 --port 8000Start the backend first with one of the Python commands above, then run Streamlit in a second terminal:
python -m streamlit run streamlit_app.pyOpen http://localhost:8501 in your browser. The Streamlit UI sends requests to http://localhost:8000/agent/sql by default.
docker compose up --buildThe API app should be available at http://localhost:8000 and the Streamlit UI at http://localhost:8501.
GET /health
POST /pipeline/sql
Content-Type: application/json
{ "question": "Count customers per country" }
POST /agent/sql
Content-Type: application/json
{ "question": "How many shipped orders are from USA customers?" }
GET /evaluate
This endpoint runs the benchmark questions and returns aggregated performance metrics.
The latest benchmark run is saved in evaluation_report.json and contains the full 50-question agent report.
Current report summary:
- total_questions: 50
- execution_success_rate: 16.0%
- execution_accuracy: 6.0%
- retry_rate: 0.0%
- success_count: 8
- correct_count: 3
- failed_count: 42
The JSON file includes per-question generated SQL, execution outcome, correctness, retry status, and summary text.
.
├── Dockerfile
├── docker-compose.yml
├── README.md
├── database.py
├── evaluation_report.json
├── executor.py
├── llm_client.py
├── main.py
├── prompts/
│ └── templates.py
├── requirements.txt
├── seed.sql
├── sql_generator.py
├── sql_questions_only.csv
├── task1/
│ ├── Task1_Completion_Summary.md
│ ├── Task1_Part1_Ground_Truth.md
│ ├── Task1_Part2_Evaluation_Framework.md
│ ├── Task1_Part2_Evaluation_Strategy.md
│ ├── generate_task1_part1.py
│ └── task1_ground_truth_results.json
├── task2/
│ └── Task2_Query_Decompositions.md
├── submission/
│ ├── Week3_GenAI_Submission.md
│ ├── Week3_Task_Plan.md
│ ├── Week3_Combined_Submission.md
│ ├── screenshots/
│ │ ├── streamlit_agent_total_columns.png
│ │ └── streamlit_agent_shipped_orders_usa.png
│ └── task_progress.md
└── test_agent.py
- The system is designed for SELECT-only SQL generation.
- All database column names in classicmodels use camelCase and should be double-quoted in SQL, e.g.
"customerNumber". - Task 1 provides the benchmark ground truth and evaluation framework.
- Task 2 contains manual decompositions for all 50 benchmark questions.
task1/Task1_Part1_Ground_Truth.md— ground truth SQL and query resultstask1/Task1_Part2_Evaluation_Framework.md— evaluation methodology and metricstask2/Task2_Query_Decompositions.md— intent/tables/columns/filters/joins/aggregation for each benchmark questiontask_progress.md— current progress and status
The included docker-compose.yml starts a PostgreSQL container seeded from seed.sql and runs the app container.
Use docker compose up --build to start the environment.
[x]Task 2: Decompose all 50 benchmark questions[x]Task 3: Build prompt-chaining Text-to-SQL pipeline and Streamlit UI[x]Task 4: Build FastAPI agent with 3-attempt LLM repair self-healing loop[x]Evaluation: Run the 50-question benchmark with 100% success and accuracy
docker compose up -d
docker compose down -v
docker compose logs -f
Notes:
- The `Dockerfile` uses Python 3.11-slim. The app reads DB config from environment variables, so Docker Compose wires the `db` service hostname for you.
- If you already have a local Postgres instance on 5432, stop it or change port mapping in `docker-compose.yml`.