Spaces:
Running
Running
Melika Kheirieh
commited on
Commit
·
3ef53b4
1
Parent(s):
cc371b0
feat(api): expose executor result in NL2SQLResponse and return full result to UI
Browse files- README.md +144 -157
- app/routers/nl2sql.py +12 -0
- app/schemas.py +5 -4
README.md
CHANGED
|
@@ -6,18 +6,18 @@ colorTo: blue
|
|
| 6 |
sdk: docker
|
| 7 |
pinned: false
|
| 8 |
---
|
| 9 |
-
|
| 10 |
# 🧩 **NL2SQL Copilot — Natural-Language → Safe SQL**
|
| 11 |
[](https://github.com/melika-kheirieh/nl2sql-copilot/actions/workflows/ci.yml)
|
| 12 |
[](#)
|
| 13 |
[](LICENSE)
|
| 14 |
|
|
|
|
| 15 |
**Modular Text-to-SQL Copilot built with FastAPI & Pydantic-AI.**
|
| 16 |
-
Generates *safe, verified, executable SQL*
|
| 17 |
-
Includes: self-repair, Spider benchmarks, Prometheus metrics, and a full demo UI.
|
| 18 |
|
| 19 |
-
🚀 **Live Demo
|
| 20 |
-
👉
|
| 21 |
|
| 22 |
---
|
| 23 |
|
|
@@ -26,186 +26,190 @@ Includes: self-repair, Spider benchmarks, Prometheus metrics, and a full demo UI
|
|
| 26 |
```bash
|
| 27 |
git clone https://github.com/melika-kheirieh/nl2sql-copilot
|
| 28 |
cd nl2sql-copilot
|
| 29 |
-
make setup
|
| 30 |
-
make run
|
| 31 |
```
|
| 32 |
|
| 33 |
Open:
|
| 34 |
-
|
| 35 |
-
|
|
|
|
| 36 |
|
| 37 |
---
|
| 38 |
|
| 39 |
# **2) Demo (Gradio UI)**
|
| 40 |
|
| 41 |
-
The
|
| 42 |
|
| 43 |
-
* Uploading
|
| 44 |
* Asking natural-language questions
|
| 45 |
* Viewing generated SQL
|
| 46 |
-
* Viewing
|
| 47 |
-
* Full trace
|
| 48 |
-
*
|
| 49 |
-
*
|
|
|
|
| 50 |
|
| 51 |
-
Everything runs
|
| 52 |
|
| 53 |
---
|
| 54 |
|
| 55 |
-
# **3) Architecture
|
| 56 |
|
| 57 |
```
|
| 58 |
user query
|
| 59 |
↓
|
| 60 |
-
detector (
|
| 61 |
-
planner (
|
| 62 |
generator (SQL generation)
|
| 63 |
-
safety (SELECT-only
|
| 64 |
-
executor (
|
| 65 |
verifier (semantic + execution checks)
|
| 66 |
repair (minimal-diff SQL repair loop)
|
| 67 |
↓
|
| 68 |
final SQL + result + traces
|
| 69 |
```
|
| 70 |
|
| 71 |
-
###
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
-
*
|
| 74 |
-
*
|
| 75 |
-
*
|
| 76 |
-
*
|
| 77 |
-
*
|
| 78 |
-
*
|
|
|
|
|
|
|
| 79 |
|
| 80 |
-
|
| 81 |
|
| 82 |
---
|
| 83 |
|
| 84 |
-
# **
|
| 85 |
|
| 86 |
-
### ✔
|
| 87 |
|
| 88 |
Planner → Generator → Safety → Executor → Verifier → Repair.
|
| 89 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
### ✔ Safety by Design
|
| 91 |
|
| 92 |
-
*
|
| 93 |
* Column/table validation
|
| 94 |
-
* No multi-
|
| 95 |
-
*
|
| 96 |
|
| 97 |
-
### ✔ Repair
|
| 98 |
|
| 99 |
-
|
| 100 |
|
| 101 |
### ✔ Caching
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
* Miss/hit metrics
|
| 106 |
|
| 107 |
### ✔ Observability
|
| 108 |
|
| 109 |
* Per-stage latency
|
| 110 |
* Pipeline success ratio
|
| 111 |
* Repair success rate
|
| 112 |
-
* p95 latency
|
| 113 |
* Cache hit ratio
|
|
|
|
| 114 |
* Full Grafana dashboard
|
| 115 |
|
| 116 |
-
### ✔
|
| 117 |
-
|
| 118 |
-
Reproducible evaluation on Spider (dev split).
|
| 119 |
-
Comes with plotting utilities, histogram, latency per stage, and summary.json.
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
باشه—الان **همین بخش Benchmarks که ساختم** را برایت
|
| 124 |
-
**کاملاً آمادهٔ قرار گرفتن در README** میکنم:
|
| 125 |
-
|
| 126 |
-
* با heading درست
|
| 127 |
-
* با anchor مناسب
|
| 128 |
-
* با ساختار کاملاً همتراز با بقیهٔ README تو
|
| 129 |
-
* با badge
|
| 130 |
-
* بدون هیچ وابستگی اضافی
|
| 131 |
-
* ۱۰۰٪ کپیـپیست مستقیم
|
| 132 |
-
|
| 133 |
-
این نسخه **نهایی، آمادهٔ چسباندن** است:
|
| 134 |
|
| 135 |
---
|
| 136 |
|
| 137 |
-
#
|
| 138 |
|
| 139 |
-
[](#benchmarks
|
| 140 |
|
| 141 |
-
|
| 142 |
-
(focused on
|
| 143 |
|
| 144 |
### 🧮 Summary
|
| 145 |
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
|
| 152 |
-
|
| 153 |
-
the model optimizes for **
|
| 154 |
|
| 155 |
---
|
| 156 |
|
| 157 |
### ⏱ Latency
|
| 158 |
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
- **p50 latency:** ~**9229 ms**
|
| 163 |
-
- **p95 latency:** ~**14936 ms**
|
| 164 |
|
| 165 |
-
Latency
|
| 166 |
-
|
| 167 |
-
2) multi-hop reasoning dominated by the planner stage.
|
| 168 |
-
|
| 169 |
-
(See `latency_histogram.png` in the benchmark folder.)
|
| 170 |
|
| 171 |
---
|
| 172 |
|
| 173 |
-
### ⚙️ Per-Stage Latency
|
| 174 |
|
| 175 |
-
| Stage
|
| 176 |
-
|
| 177 |
-
| detector
|
| 178 |
-
| planner
|
| 179 |
-
| generator
|
| 180 |
-
| safety
|
| 181 |
-
| executor
|
| 182 |
-
| verifier
|
| 183 |
-
| repair
|
| 184 |
|
| 185 |
-
|
| 186 |
-
|
| 187 |
|
| 188 |
---
|
| 189 |
|
| 190 |
-
### ❌ Failure Modes (Why EM is low
|
| 191 |
|
| 192 |
-
Even when EM=0, **SM
|
| 193 |
|
| 194 |
Typical causes:
|
| 195 |
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
|
| 202 |
-
In real systems, **execution correctness
|
| 203 |
|
| 204 |
---
|
| 205 |
|
| 206 |
-
### 📂
|
| 207 |
-
|
| 208 |
-
Run the exact same benchmark:
|
| 209 |
|
| 210 |
```bash
|
| 211 |
export SPIDER_ROOT="$PWD/data/spider"
|
|
@@ -215,12 +219,12 @@ PYTHONPATH=$PWD \
|
|
| 215 |
|
| 216 |
PYTHONPATH=$PWD \
|
| 217 |
python benchmarks/plot_results.py
|
| 218 |
-
|
| 219 |
|
| 220 |
-
Artifacts
|
| 221 |
|
| 222 |
```
|
| 223 |
-
benchmarks/results_pro
|
| 224 |
summary.json
|
| 225 |
eval.jsonl
|
| 226 |
metrics_overview.png
|
|
@@ -229,11 +233,9 @@ benchmarks/results_pro/20251113-113600/
|
|
| 229 |
errors_overview.png
|
| 230 |
```
|
| 231 |
|
| 232 |
-
These plots are directly embedded into the README if needed.
|
| 233 |
-
|
| 234 |
---
|
| 235 |
|
| 236 |
-
# **
|
| 237 |
|
| 238 |
## 🔍 NL → SQL
|
| 239 |
|
|
@@ -247,29 +249,37 @@ curl -X POST "http://localhost:8000/api/v1/nl2sql" \
|
|
| 247 |
}'
|
| 248 |
```
|
| 249 |
|
| 250 |
-
Sample
|
| 251 |
|
| 252 |
```json
|
| 253 |
{
|
| 254 |
"ambiguous": false,
|
| 255 |
-
"sql": "...",
|
| 256 |
-
"rationale": "
|
| 257 |
-
"result":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 258 |
"traces": [
|
| 259 |
{"stage": "detector", "duration_ms": 1},
|
| 260 |
-
{"stage": "planner",
|
| 261 |
-
{"stage": "generator",
|
| 262 |
-
{"stage": "safety",
|
| 263 |
{"stage": "executor", "duration_ms": 1},
|
| 264 |
{"stage": "verifier", "duration_ms": 1},
|
| 265 |
-
{"stage": "repair",
|
| 266 |
]
|
| 267 |
}
|
| 268 |
```
|
| 269 |
|
| 270 |
---
|
| 271 |
|
| 272 |
-
##
|
| 273 |
|
| 274 |
```bash
|
| 275 |
curl -X POST "http://localhost:8000/api/v1/nl2sql/upload_db" \
|
|
@@ -277,15 +287,6 @@ curl -X POST "http://localhost:8000/api/v1/nl2sql/upload_db" \
|
|
| 277 |
-F "file=@/path/to/db.sqlite"
|
| 278 |
```
|
| 279 |
|
| 280 |
-
Response:
|
| 281 |
-
|
| 282 |
-
```json
|
| 283 |
-
{
|
| 284 |
-
"db_id": "9a21d49f-38d3-4ce0-a459-3688e02fd44a",
|
| 285 |
-
"message": "Database uploaded successfully."
|
| 286 |
-
}
|
| 287 |
-
```
|
| 288 |
-
|
| 289 |
---
|
| 290 |
|
| 291 |
## 📑 Schema Preview
|
|
@@ -297,57 +298,43 @@ curl "http://localhost:8000/api/v1/nl2sql/schema?db_id=<uuid>" \
|
|
| 297 |
|
| 298 |
---
|
| 299 |
|
| 300 |
-
|
| 301 |
|
| 302 |
-
| Variable | Purpose
|
| 303 |
-
| ---------------------- |
|
| 304 |
-
| `API_KEYS` | Comma-separated
|
| 305 |
-
| `
|
| 306 |
-
| `
|
| 307 |
-
| `
|
| 308 |
-
| `
|
| 309 |
-
| `
|
|
|
|
| 310 |
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
# **7) Future Work**
|
| 314 |
|
| 315 |
-
|
| 316 |
|
| 317 |
-
|
| 318 |
|
| 319 |
-
|
| 320 |
|
| 321 |
### 2) Redis Distributed Cache
|
| 322 |
|
| 323 |
-
Shared cache across replicas, eviction, warm-ups.
|
| 324 |
-
|
| 325 |
### 3) Multi-Model Planner/Generator
|
| 326 |
|
| 327 |
-
Support OpenAI, vLLM, LLaMA, hybrid pipelines.
|
| 328 |
-
|
| 329 |
### 4) A/B Testing Framework
|
| 330 |
|
| 331 |
-
Compare prompts/models with automated drift tracking.
|
| 332 |
-
|
| 333 |
### 5) Schema Embeddings
|
| 334 |
|
| 335 |
-
Vector-based reasoning for table/column retrieval.
|
| 336 |
-
|
| 337 |
### 6) Nightly CI Benchmarks
|
| 338 |
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
### 7) Stronger Diff-based Repair
|
| 342 |
-
|
| 343 |
-
Trace-aware SQL recovery with history logging.
|
| 344 |
-
|
| 345 |
-
### 8) Deployment Template
|
| 346 |
|
| 347 |
-
Helm
|
| 348 |
|
| 349 |
---
|
| 350 |
|
| 351 |
-
# **
|
| 352 |
|
| 353 |
MIT License.
|
|
|
|
| 6 |
sdk: docker
|
| 7 |
pinned: false
|
| 8 |
---
|
|
|
|
| 9 |
# 🧩 **NL2SQL Copilot — Natural-Language → Safe SQL**
|
| 10 |
[](https://github.com/melika-kheirieh/nl2sql-copilot/actions/workflows/ci.yml)
|
| 11 |
[](#)
|
| 12 |
[](LICENSE)
|
| 13 |
|
| 14 |
+
|
| 15 |
**Modular Text-to-SQL Copilot built with FastAPI & Pydantic-AI.**
|
| 16 |
+
Generates *safe, verified, executable SQL* through a multi-stage agentic pipeline.
|
| 17 |
+
Includes: schema introspection, self-repair, Spider benchmarks, Prometheus metrics, and a full demo UI.
|
| 18 |
|
| 19 |
+
🚀 **Live Demo:**
|
| 20 |
+
👉 **[https://huggingface.co/spaces/melika-kheirieh/nl2sql-copilot](https://huggingface.co/spaces/melika-kheirieh/nl2sql-copilot)**
|
| 21 |
|
| 22 |
---
|
| 23 |
|
|
|
|
| 26 |
```bash
|
| 27 |
git clone https://github.com/melika-kheirieh/nl2sql-copilot
|
| 28 |
cd nl2sql-copilot
|
| 29 |
+
make setup # install dependencies
|
| 30 |
+
make run # start API + Gradio UI
|
| 31 |
```
|
| 32 |
|
| 33 |
Open:
|
| 34 |
+
|
| 35 |
+
* [http://localhost:8000](http://localhost:8000) (FastAPI Swagger UI)
|
| 36 |
+
* [http://localhost:7860](http://localhost:7860) (Gradio Demo)
|
| 37 |
|
| 38 |
---
|
| 39 |
|
| 40 |
# **2) Demo (Gradio UI)**
|
| 41 |
|
| 42 |
+
The demo supports:
|
| 43 |
|
| 44 |
+
* Uploading any SQLite database
|
| 45 |
* Asking natural-language questions
|
| 46 |
* Viewing generated SQL
|
| 47 |
+
* Viewing query results
|
| 48 |
+
* Full multi-stage trace (detector → planner → generator → safety → executor → verifier → repair)
|
| 49 |
+
* Per-stage timings
|
| 50 |
+
* Example queries
|
| 51 |
+
* And a default demo DB (no upload required)
|
| 52 |
|
| 53 |
+
Everything runs on the same backend as the API.
|
| 54 |
|
| 55 |
---
|
| 56 |
|
| 57 |
+
# **3) Agentic Architecture**
|
| 58 |
|
| 59 |
```
|
| 60 |
user query
|
| 61 |
↓
|
| 62 |
+
detector (ambiguity, missing info)
|
| 63 |
+
planner (schema reasoning + task decomposition)
|
| 64 |
generator (SQL generation)
|
| 65 |
+
safety (SELECT-only validation)
|
| 66 |
+
executor (sandboxed DB execution)
|
| 67 |
verifier (semantic + execution checks)
|
| 68 |
repair (minimal-diff SQL repair loop)
|
| 69 |
↓
|
| 70 |
final SQL + result + traces
|
| 71 |
```
|
| 72 |
|
| 73 |
+
### ⚙️ Tech Stack
|
| 74 |
+
|
| 75 |
+
* FastAPI
|
| 76 |
+
* Pydantic-AI
|
| 77 |
+
* SQLiteAdapter
|
| 78 |
+
* Prometheus + Grafana
|
| 79 |
+
* pytest + mypy + Makefile
|
| 80 |
+
* Gradio UI
|
| 81 |
+
|
| 82 |
+
The pipeline is fully modular: each stage has a clean, swappable interface.
|
| 83 |
+
|
| 84 |
+
---
|
| 85 |
+
|
| 86 |
+
# **4) Evolution (Prototype → Copilot)**
|
| 87 |
+
|
| 88 |
+
This project is the **second-generation, production-grade** version of an earlier prototype:
|
| 89 |
+
👉 [https://github.com/melika-kheirieh/nl2sql-copilot-prototype](https://github.com/melika-kheirieh/nl2sql-copilot-prototype)
|
| 90 |
+
|
| 91 |
+
The prototype explored single-step, prompt-based SQL generation.
|
| 92 |
+
The current version is a **complete architectural redesign**, adding:
|
| 93 |
|
| 94 |
+
* multi-stage agentic pipeline
|
| 95 |
+
* schema introspection
|
| 96 |
+
* safety guardrails
|
| 97 |
+
* self-repair loop
|
| 98 |
+
* caching
|
| 99 |
+
* observability
|
| 100 |
+
* Spider benchmarks
|
| 101 |
+
* multi-DB support with upload + TTL handling
|
| 102 |
|
| 103 |
+
This repository is the first **end-to-end, production-oriented** version.
|
| 104 |
|
| 105 |
---
|
| 106 |
|
| 107 |
+
# **5) Key Features**
|
| 108 |
|
| 109 |
+
### ✔ Agentic Pipeline
|
| 110 |
|
| 111 |
Planner → Generator → Safety → Executor → Verifier → Repair.
|
| 112 |
|
| 113 |
+
### ✔ Schema-Aware
|
| 114 |
+
|
| 115 |
+
Automatic schema preview for any uploaded SQLite database.
|
| 116 |
+
|
| 117 |
### ✔ Safety by Design
|
| 118 |
|
| 119 |
+
* SELECT-only
|
| 120 |
* Column/table validation
|
| 121 |
+
* No multi-statement SQL
|
| 122 |
+
* Prevents schema hallucination
|
| 123 |
|
| 124 |
+
### ✔ Self-Repair
|
| 125 |
|
| 126 |
+
Automatic minimal-diff correction when SQL fails.
|
| 127 |
|
| 128 |
### ✔ Caching
|
| 129 |
|
| 130 |
+
TTL-based, with key = (db_id, normalized_query, schema_hash).
|
| 131 |
+
Hit/miss metrics included.
|
|
|
|
| 132 |
|
| 133 |
### ✔ Observability
|
| 134 |
|
| 135 |
* Per-stage latency
|
| 136 |
* Pipeline success ratio
|
| 137 |
* Repair success rate
|
|
|
|
| 138 |
* Cache hit ratio
|
| 139 |
+
* p95 latency
|
| 140 |
* Full Grafana dashboard
|
| 141 |
|
| 142 |
+
### ✔ Benchmarks
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
+
Reproducible Spider evaluation with plots + summary.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
|
| 146 |
---
|
| 147 |
|
| 148 |
+
# **6) Benchmarks (Spider dev, 20 samples)**
|
| 149 |
|
| 150 |
+
[](#benchmarks)
|
| 151 |
|
| 152 |
+
Evaluated on a curated 20-sample subset of the Spider **dev** split
|
| 153 |
+
(focused on `concert_singer`), using the full production pipeline.
|
| 154 |
|
| 155 |
### 🧮 Summary
|
| 156 |
|
| 157 |
+
* **Total samples:** 20
|
| 158 |
+
* **Successful runs:** 20/20 (**100%**)
|
| 159 |
+
* **Exact Match (EM):** **0.10**
|
| 160 |
+
* **Structural Match (SM):** **0.70**
|
| 161 |
+
* **Execution Accuracy:** **0.725**
|
| 162 |
|
| 163 |
+
This reflects a *production-oriented* NL2SQL system:
|
| 164 |
+
the model optimizes for **executable SQL**, not literal gold-string alignment.
|
| 165 |
|
| 166 |
---
|
| 167 |
|
| 168 |
### ⏱ Latency
|
| 169 |
|
| 170 |
+
* **Avg latency:** ~**8066 ms**
|
| 171 |
+
* **p50:** ~**9229 ms**
|
| 172 |
+
* **p95:** ~**14936 ms**
|
|
|
|
|
|
|
| 173 |
|
| 174 |
+
Latency is **bimodal**:
|
| 175 |
+
simple queries → fast, reasoning-heavy queries → planner-dominated.
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
---
|
| 178 |
|
| 179 |
+
### ⚙️ Per-Stage Latency
|
| 180 |
|
| 181 |
+
| Stage | Avg latency (ms) |
|
| 182 |
+
| --------- | ---------------- |
|
| 183 |
+
| detector | ~1 |
|
| 184 |
+
| planner | ~8360 |
|
| 185 |
+
| generator | ~1645 |
|
| 186 |
+
| safety | ~2 |
|
| 187 |
+
| executor | ~1 |
|
| 188 |
+
| verifier | ~1 |
|
| 189 |
+
| repair | ~1200 |
|
| 190 |
|
| 191 |
+
Planner is the main bottleneck (expected for schema-level reasoning).
|
| 192 |
+
Safety/executor/verifier stay **single-digit ms**.
|
| 193 |
|
| 194 |
---
|
| 195 |
|
| 196 |
+
### ❌ Failure Modes (Why EM is low)
|
| 197 |
|
| 198 |
+
Even when EM = 0, **SM and ExecAcc are often 1.0**.
|
| 199 |
|
| 200 |
Typical causes:
|
| 201 |
|
| 202 |
+
* Capitalization differences (`Age` vs `age`)
|
| 203 |
+
* Different column ordering
|
| 204 |
+
* LIMIT differences
|
| 205 |
+
* Alias mismatch
|
| 206 |
+
* Gold SQL is `EMPTY` but the model infers a valid SQL
|
| 207 |
|
| 208 |
+
In real-world systems, **execution correctness matters more than exact string match**.
|
| 209 |
|
| 210 |
---
|
| 211 |
|
| 212 |
+
### 📂 Reproducing the Benchmark
|
|
|
|
|
|
|
| 213 |
|
| 214 |
```bash
|
| 215 |
export SPIDER_ROOT="$PWD/data/spider"
|
|
|
|
| 219 |
|
| 220 |
PYTHONPATH=$PWD \
|
| 221 |
python benchmarks/plot_results.py
|
| 222 |
+
```
|
| 223 |
|
| 224 |
+
Artifacts saved under:
|
| 225 |
|
| 226 |
```
|
| 227 |
+
benchmarks/results_pro/<timestamp>/
|
| 228 |
summary.json
|
| 229 |
eval.jsonl
|
| 230 |
metrics_overview.png
|
|
|
|
| 233 |
errors_overview.png
|
| 234 |
```
|
| 235 |
|
|
|
|
|
|
|
| 236 |
---
|
| 237 |
|
| 238 |
+
# **7) API Usage**
|
| 239 |
|
| 240 |
## 🔍 NL → SQL
|
| 241 |
|
|
|
|
| 249 |
}'
|
| 250 |
```
|
| 251 |
|
| 252 |
+
### ✔ Sample Response (accurate)
|
| 253 |
|
| 254 |
```json
|
| 255 |
{
|
| 256 |
"ambiguous": false,
|
| 257 |
+
"sql": "SELECT ...",
|
| 258 |
+
"rationale": "Explanation of why this SQL was generated.",
|
| 259 |
+
"result": {
|
| 260 |
+
"rows": 5,
|
| 261 |
+
"columns": ["CustomerId", "Total"],
|
| 262 |
+
"rows_data": [
|
| 263 |
+
[1, 39.6],
|
| 264 |
+
[2, 38.7],
|
| 265 |
+
[3, 35.4]
|
| 266 |
+
]
|
| 267 |
+
},
|
| 268 |
"traces": [
|
| 269 |
{"stage": "detector", "duration_ms": 1},
|
| 270 |
+
{"stage": "planner", "duration_ms": 8943},
|
| 271 |
+
{"stage": "generator","duration_ms": 1722},
|
| 272 |
+
{"stage": "safety", "duration_ms": 2},
|
| 273 |
{"stage": "executor", "duration_ms": 1},
|
| 274 |
{"stage": "verifier", "duration_ms": 1},
|
| 275 |
+
{"stage": "repair", "duration_ms": 522}
|
| 276 |
]
|
| 277 |
}
|
| 278 |
```
|
| 279 |
|
| 280 |
---
|
| 281 |
|
| 282 |
+
## 📁 Upload a SQLite DB
|
| 283 |
|
| 284 |
```bash
|
| 285 |
curl -X POST "http://localhost:8000/api/v1/nl2sql/upload_db" \
|
|
|
|
| 287 |
-F "file=@/path/to/db.sqlite"
|
| 288 |
```
|
| 289 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 290 |
---
|
| 291 |
|
| 292 |
## 📑 Schema Preview
|
|
|
|
| 298 |
|
| 299 |
---
|
| 300 |
|
| 301 |
+
# **8) Environment Variables**
|
| 302 |
|
| 303 |
+
| Variable | Purpose |
|
| 304 |
+
| ---------------------- | ---------------------------------------- |
|
| 305 |
+
| `API_KEYS` | Comma-separated list of backend API keys |
|
| 306 |
+
| `API_KEY` | Used by Gradio UI to call the backend |
|
| 307 |
+
| `DEV_MODE` | Enables strict ambiguity detection |
|
| 308 |
+
| `NL2SQL_CACHE_TTL_SEC` | Cache TTL |
|
| 309 |
+
| `NL2SQL_CACHE_MAX` | Max cache entries |
|
| 310 |
+
| `SPIDER_ROOT` | Path to Spider dataset |
|
| 311 |
+
| `USE_MOCK` | Skip execution (for testing) |
|
| 312 |
|
| 313 |
+
> Gradio uses `API_KEY` → backend expects it as `X-API-Key`.
|
| 314 |
+
> Backend accepts multiple keys via `API_KEYS`.
|
|
|
|
| 315 |
|
| 316 |
+
---
|
| 317 |
|
| 318 |
+
# **9) Future Work**
|
| 319 |
|
| 320 |
+
### 1) Streaming SQL Generation (SSE)
|
| 321 |
|
| 322 |
### 2) Redis Distributed Cache
|
| 323 |
|
|
|
|
|
|
|
| 324 |
### 3) Multi-Model Planner/Generator
|
| 325 |
|
|
|
|
|
|
|
| 326 |
### 4) A/B Testing Framework
|
| 327 |
|
|
|
|
|
|
|
| 328 |
### 5) Schema Embeddings
|
| 329 |
|
|
|
|
|
|
|
| 330 |
### 6) Nightly CI Benchmarks
|
| 331 |
|
| 332 |
+
### 7) Advanced Repair (diff-based)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 333 |
|
| 334 |
+
### 8) Helm / Compose Deployment Template
|
| 335 |
|
| 336 |
---
|
| 337 |
|
| 338 |
+
# **10) License**
|
| 339 |
|
| 340 |
MIT License.
|
app/routers/nl2sql.py
CHANGED
|
@@ -384,12 +384,24 @@ def nl2sql_handler(
|
|
| 384 |
|
| 385 |
# Success path → 200 (coerce/standardize traces for API)
|
| 386 |
traces = [_round_trace(t) for t in (result.traces or [])]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 387 |
payload = NL2SQLResponse(
|
| 388 |
ambiguous=False,
|
| 389 |
sql=result.sql,
|
| 390 |
rationale=result.rationale,
|
| 391 |
traces=traces,
|
|
|
|
| 392 |
)
|
|
|
|
| 393 |
# store in cache
|
| 394 |
_CACHE[ck] = (time.time(), cast(Dict[str, Any], payload.model_dump()))
|
| 395 |
return payload
|
|
|
|
| 384 |
|
| 385 |
# Success path → 200 (coerce/standardize traces for API)
|
| 386 |
traces = [_round_trace(t) for t in (result.traces or [])]
|
| 387 |
+
|
| 388 |
+
# Normalize execution result (if executor attached one)
|
| 389 |
+
response_result: Dict[str, Any] = {}
|
| 390 |
+
raw_result = getattr(result, "result", None)
|
| 391 |
+
if raw_result is not None:
|
| 392 |
+
if isinstance(raw_result, dict):
|
| 393 |
+
response_result = raw_result
|
| 394 |
+
else:
|
| 395 |
+
response_result = cast(Dict[str, Any], _to_dict(raw_result))
|
| 396 |
+
|
| 397 |
payload = NL2SQLResponse(
|
| 398 |
ambiguous=False,
|
| 399 |
sql=result.sql,
|
| 400 |
rationale=result.rationale,
|
| 401 |
traces=traces,
|
| 402 |
+
result=response_result,
|
| 403 |
)
|
| 404 |
+
|
| 405 |
# store in cache
|
| 406 |
_CACHE[ck] = (time.time(), cast(Dict[str, Any], payload.model_dump()))
|
| 407 |
return payload
|
app/schemas.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
-
from pydantic import BaseModel
|
| 2 |
-
from typing import List, Optional, Any, Dict
|
| 3 |
|
| 4 |
|
| 5 |
class NL2SQLRequest(BaseModel):
|
|
@@ -21,10 +21,11 @@ class TraceModel(BaseModel):
|
|
| 21 |
|
| 22 |
|
| 23 |
class NL2SQLResponse(BaseModel):
|
| 24 |
-
ambiguous: bool
|
| 25 |
sql: Optional[str] = None
|
| 26 |
rationale: Optional[str] = None
|
| 27 |
-
traces:
|
|
|
|
| 28 |
|
| 29 |
|
| 30 |
class ClarifyResponse(BaseModel):
|
|
|
|
| 1 |
+
from pydantic import BaseModel
|
| 2 |
+
from typing import List, Optional, Any, Dict
|
| 3 |
|
| 4 |
|
| 5 |
class NL2SQLRequest(BaseModel):
|
|
|
|
| 21 |
|
| 22 |
|
| 23 |
class NL2SQLResponse(BaseModel):
|
| 24 |
+
ambiguous: bool
|
| 25 |
sql: Optional[str] = None
|
| 26 |
rationale: Optional[str] = None
|
| 27 |
+
traces: List[Dict[str, Any]] = []
|
| 28 |
+
result: Dict[str, Any] = {}
|
| 29 |
|
| 30 |
|
| 31 |
class ClarifyResponse(BaseModel):
|