Spaces:
Running
Running
Melika Kheirieh
commited on
Commit
Β·
602cae0
1
Parent(s):
ba06dd4
docs(readme): add evolution note and improve intro narrative
Browse files
README.md
CHANGED
|
@@ -1,7 +1,18 @@
|
|
| 1 |
# π§© NL2SQL Copilot
|
| 2 |
|
| 3 |
-
A modular **Text-to-SQL Copilot** that converts natural
|
| 4 |
-
Built with **FastAPI**, **LangGraph**, and **SQLAlchemy**, designed for read-only databases and
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
---
|
| 7 |
|
|
@@ -21,6 +32,52 @@ docker run --rm -p 8000:8000 nl2sql-copilot
|
|
| 21 |
```
|
| 22 |
|
| 23 |
Then open [http://localhost:8000/docs](http://localhost:8000/docs) π
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
---
|
| 26 |
|
|
@@ -30,69 +87,112 @@ Then open [http://localhost:8000/docs](http://localhost:8000/docs) π
|
|
| 30 |
nl2sql-copilot/
|
| 31 |
β
|
| 32 |
βββ app/ # FastAPI app, routers, schemas
|
| 33 |
-
βββ nl2sql/ # Core pipeline (
|
| 34 |
βββ adapters/ # Database and LLM adapters
|
| 35 |
βββ benchmarks/ # Evaluation scripts and results
|
| 36 |
-
βββ ui/ # Streamlit dashboard
|
|
|
|
| 37 |
β
|
| 38 |
βββ Dockerfile
|
| 39 |
-
βββ requirements.in
|
| 40 |
-
βββ requirements.txt
|
| 41 |
βββ README.md
|
| 42 |
```
|
| 43 |
|
| 44 |
---
|
| 45 |
|
| 46 |
-
##
|
| 47 |
-
|
| 48 |
-
### Install dependencies
|
| 49 |
|
| 50 |
-
|
| 51 |
|
| 52 |
-
```
|
| 53 |
-
|
|
|
|
|
|
|
| 54 |
```
|
| 55 |
|
| 56 |
-
|
|
|
|
| 57 |
|
| 58 |
-
|
| 59 |
-
pytest -q
|
| 60 |
-
```
|
| 61 |
|
| 62 |
-
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
---
|
| 70 |
|
| 71 |
-
##
|
| 72 |
|
| 73 |
-
* β
Modular
|
| 74 |
-
* π‘οΈ SQL safety filters (SELECT-only,
|
| 75 |
-
* π Self-repair loop
|
| 76 |
-
*
|
| 77 |
-
*
|
| 78 |
-
*
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
---
|
| 81 |
|
| 82 |
## π§° Tech Stack
|
| 83 |
|
| 84 |
-
| Layer
|
| 85 |
-
|
|
| 86 |
-
| Backend API
|
| 87 |
-
| Pipeline Core
|
| 88 |
-
| LLM Interface
|
| 89 |
-
| Database
|
| 90 |
-
| Evaluation
|
| 91 |
-
| UI
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
---
|
| 95 |
|
| 96 |
## π License
|
| 97 |
|
| 98 |
-
MIT Β© 2025 Melika Kheirieh
|
|
|
|
| 1 |
# π§© NL2SQL Copilot
|
| 2 |
|
| 3 |
+
A modular **Text-to-SQL Copilot** that converts natural-language questions into **safe, verified SQL queries**.
|
| 4 |
+
Built with **FastAPI**, **LangGraph**, and **SQLAlchemy**, designed for **read-only databases** and benchmarked on **Spider** and **Dr.Spider** datasets.
|
| 5 |
+
|
| 6 |
+
---
|
| 7 |
+
|
| 8 |
+
> π‘ **Why it matters**
|
| 9 |
+
> In real analytics teams, analysts often need quick insights without writing SQL.
|
| 10 |
+
> **NL2SQL Copilot** bridges that gap by translating plain-English questions into validated, read-only SQL β reducing query errors and saving hours of analyst time.
|
| 11 |
+
>
|
| 12 |
+
> 𧬠**Evolution Note**
|
| 13 |
+
> This repository is the next-generation version of [NL2SQL Copilot Prototype](https://github.com/melika-kheirieh/nl2sql-copilot-prototype).
|
| 14 |
+
> It refactors the original prototype into a **production-grade, modular architecture** β
|
| 15 |
+
> adding configuration-driven pipelines, safety layers, benchmarks, and a Streamlit UI for evaluation.
|
| 16 |
|
| 17 |
---
|
| 18 |
|
|
|
|
| 32 |
```
|
| 33 |
|
| 34 |
Then open [http://localhost:8000/docs](http://localhost:8000/docs) π
|
| 35 |
+
Or launch the [Streamlit Demo](http://localhost:7860) to test it interactively.
|
| 36 |
+
|
| 37 |
+
---
|
| 38 |
+
|
| 39 |
+
## π§ Demo
|
| 40 |
+
|
| 41 |
+
π― **Live Demo:** [Try it on Hugging Face Spaces β](https://huggingface.co/spaces/melika-kheirieh/nl2sql-copilot)
|
| 42 |
+
|
| 43 |
+
You can ask a question in plain English β the Copilot plans, generates, verifies, and safely executes an SQL query.
|
| 44 |
+
|
| 45 |
+
**User Query**
|
| 46 |
+
|
| 47 |
+
> show top 5 albums by total sales
|
| 48 |
+
|
| 49 |
+
**Generated SQL**
|
| 50 |
+
|
| 51 |
+
```sql
|
| 52 |
+
SELECT albums.Title, SUM(invoice_items.UnitPrice * invoice_items.Quantity) AS total_sales
|
| 53 |
+
FROM albums
|
| 54 |
+
JOIN tracks ON albums.AlbumId = tracks.AlbumId
|
| 55 |
+
JOIN invoice_items ON invoice_items.TrackId = tracks.TrackId
|
| 56 |
+
GROUP BY albums.Title
|
| 57 |
+
ORDER BY total_sales DESC
|
| 58 |
+
LIMIT 5;
|
| 59 |
+
```
|
| 60 |
+
|
| 61 |
+
**Execution Result (preview)**
|
| 62 |
+
|
| 63 |
+
| Album | Total Sales |
|
| 64 |
+
| ----------------- | ----------- |
|
| 65 |
+
| Greatest Hits | 155.34 |
|
| 66 |
+
| Let There Be Rock | 133.09 |
|
| 67 |
+
| Big Ones | 128.44 |
|
| 68 |
+
|
| 69 |
+
**Trace**
|
| 70 |
+
|
| 71 |
+
```json
|
| 72 |
+
[
|
| 73 |
+
{"stage": "planner", "duration_ms": 38, "summary": "Identified SQL intent"},
|
| 74 |
+
{"stage": "generator", "duration_ms": 201, "summary": "LLM generated SQL"},
|
| 75 |
+
{"stage": "safety", "duration_ms": 6, "summary": "Validated SELECT-only"},
|
| 76 |
+
{"stage": "executor", "duration_ms": 92, "summary": "Executed successfully"}
|
| 77 |
+
]
|
| 78 |
+
```
|
| 79 |
+
|
| 80 |
+

|
| 81 |
|
| 82 |
---
|
| 83 |
|
|
|
|
| 87 |
nl2sql-copilot/
|
| 88 |
β
|
| 89 |
βββ app/ # FastAPI app, routers, schemas
|
| 90 |
+
βββ nl2sql/ # Core pipeline (Planner β Generator β Safety β Executor β Verifier)
|
| 91 |
βββ adapters/ # Database and LLM adapters
|
| 92 |
βββ benchmarks/ # Evaluation scripts and results
|
| 93 |
+
βββ ui/ # Streamlit dashboard (demo + benchmark)
|
| 94 |
+
βββ configs/ # Pipeline configs (YAML-based)
|
| 95 |
β
|
| 96 |
βββ Dockerfile
|
| 97 |
+
βββ requirements.in / .txt
|
|
|
|
| 98 |
βββ README.md
|
| 99 |
```
|
| 100 |
|
| 101 |
---
|
| 102 |
|
| 103 |
+
## βοΈ How It Works
|
|
|
|
|
|
|
| 104 |
|
| 105 |
+
The Copilot runs a **multi-stage pipeline** ensuring every SQL query is both correct and safe:
|
| 106 |
|
| 107 |
+
```
|
| 108 |
+
Natural Language
|
| 109 |
+
β
|
| 110 |
+
[ Planner ] β [ Generator (LLM) ] β [ Safety ] β [ Executor ] β [ Verifier ] β [ Repair ]
|
| 111 |
```
|
| 112 |
|
| 113 |
+
Each stage is modular and configurable via `configs/pipeline.yaml`.
|
| 114 |
+
All queries execute inside a **read-only sandbox**.
|
| 115 |
|
| 116 |
+
---
|
|
|
|
|
|
|
| 117 |
|
| 118 |
+
## π Safety Layer
|
| 119 |
|
| 120 |
+
Before execution, every SQL statement is validated:
|
| 121 |
+
|
| 122 |
+
| Rule | Example Blocked |
|
| 123 |
+
| ------------------ | ----------------------------- |
|
| 124 |
+
| DML not allowed | `DELETE FROM users` |
|
| 125 |
+
| Multi-statement | `SELECT *; DROP TABLE users` |
|
| 126 |
+
| Forbidden keywords | `ALTER`, `TRUNCATE`, `UPDATE` |
|
| 127 |
+
|
| 128 |
+
β
Only safe, single-statement `SELECT` queries are executed.
|
| 129 |
+
|
| 130 |
+
---
|
| 131 |
+
|
| 132 |
+
## π Benchmark (sample)
|
| 133 |
+
|
| 134 |
+
Evaluated on a subset of the [Spider](https://yale-lily.github.io/spider) dataset using `gpt-4o-mini`:
|
| 135 |
+
|
| 136 |
+
| Query | Type | Correct | Latency (ms) | Model |
|
| 137 |
+
| --------------------------- | ------------- | ------- | ------------ | ----------- |
|
| 138 |
+
| list all artists | simple select | β
| 118 | gpt-4o-mini |
|
| 139 |
+
| total invoices per country | aggregation | β
| 127 | gpt-4o-mini |
|
| 140 |
+
| top 3 customers by spending | aggregation | β
| 141 | gpt-4o-mini |
|
| 141 |
+
| albums released before 2000 | filter | β
| 122 | gpt-4o-mini |
|
| 142 |
+
| top 5 sales by genre | join | β
| 149 | gpt-4o-mini |
|
| 143 |
+
|
| 144 |
+
*(see `benchmarks/results.csv` for detailed results)*
|
| 145 |
|
| 146 |
---
|
| 147 |
|
| 148 |
+
## π§© Key Features
|
| 149 |
|
| 150 |
+
* β
**Modular pipeline** (Planner β Generator β Safety β Executor β Verifier β Repair)
|
| 151 |
+
* π‘οΈ **SQL safety filters** (SELECT-only, blacklist, AST validation)
|
| 152 |
+
* π **Self-repair loop** for failed executions
|
| 153 |
+
* π§ **LLM-driven generator** (OpenAI / Ollama / Anthropic)
|
| 154 |
+
* π **Evaluation toolkit** for latency / accuracy / cost
|
| 155 |
+
* βοΈ **Config-driven architecture** (`Pipeline.from_config("configs/pipeline.yaml")`)
|
| 156 |
+
* π§° **PostgreSQL + SQLite adapters**
|
| 157 |
+
* ποΈ **Streamlit UI** for interactive demo & benchmark
|
| 158 |
+
* π§© Built with **FastAPI**, **LangGraph**, **Pydantic-AI**, **SQLAlchemy**
|
| 159 |
|
| 160 |
---
|
| 161 |
|
| 162 |
## π§° Tech Stack
|
| 163 |
|
| 164 |
+
| Layer | Tools / Libraries |
|
| 165 |
+
| ------------- | ----------------------------------------- |
|
| 166 |
+
| Backend API | FastAPI, Uvicorn |
|
| 167 |
+
| Pipeline Core | Python 3.12, Pydantic, SQLGlot |
|
| 168 |
+
| LLM Interface | Pydantic-AI (OpenAI / Anthropic / Ollama) |
|
| 169 |
+
| Database | SQLite (default), PostgreSQL |
|
| 170 |
+
| Evaluation | Spider / Dr.Spider |
|
| 171 |
+
| UI | Streamlit + Plotly |
|
| 172 |
+
| CI/CD | GitHub Actions, Makefile, Docker |
|
| 173 |
+
|
| 174 |
+
---
|
| 175 |
+
|
| 176 |
+
## π§ͺ Development
|
| 177 |
+
|
| 178 |
+
```bash
|
| 179 |
+
pip install -r requirements.txt
|
| 180 |
+
pytest -q
|
| 181 |
+
ruff check .
|
| 182 |
+
mypy .
|
| 183 |
+
```
|
| 184 |
+
|
| 185 |
+
---
|
| 186 |
+
|
| 187 |
+
## π§ Roadmap
|
| 188 |
+
|
| 189 |
+
* [ ] Add multilingual query support (Persian / English)
|
| 190 |
+
* [ ] Improve self-repair accuracy
|
| 191 |
+
* [ ] Add cost tracking per query
|
| 192 |
+
* [ ] Integrate Prometheus metrics
|
| 193 |
|
| 194 |
---
|
| 195 |
|
| 196 |
## π License
|
| 197 |
|
| 198 |
+
MIT Β© 2025 [Melika Kheirieh](https://github.com/melika-kheirieh)
|