Spaces:
Sleeping
Sleeping
Add comprehensive README
Browse files
README.md
CHANGED
|
@@ -1,12 +1,106 @@
|
|
| 1 |
---
|
| 2 |
-
title: Text
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
colorTo: indigo
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 6.13.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
---
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Multi-Turn Text-to-SQL Agent
|
| 3 |
+
emoji: ποΈ
|
| 4 |
+
colorFrom: blue
|
| 5 |
colorTo: indigo
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 6.13.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
+
license: mit
|
| 11 |
+
tags:
|
| 12 |
+
- text-to-sql
|
| 13 |
+
- agent
|
| 14 |
+
- smolagents
|
| 15 |
+
- multi-turn
|
| 16 |
+
- clarification
|
| 17 |
---
|
| 18 |
|
| 19 |
+
# ποΈ Multi-Turn Text-to-SQL Agent with Clarification
|
| 20 |
+
|
| 21 |
+
An intelligent SQL assistant that doesn't just generate SQL β it **thinks before querying**. When your question is ambiguous, it asks for clarification first. When data doesn't exist, it tells you why and suggests alternatives.
|
| 22 |
+
|
| 23 |
+
## π― What Makes This Different
|
| 24 |
+
|
| 25 |
+
Traditional text-to-SQL systems blindly generate a query from your question. This agent follows a **3-step decision process** inspired by recent research:
|
| 26 |
+
|
| 27 |
+
1. **Classify** β Is the question answerable, ambiguous, or unanswerable?
|
| 28 |
+
2. **Clarify** β If ambiguous, ask the user targeted questions before generating SQL
|
| 29 |
+
3. **Execute & Verify** β Generate SQL, run it, self-correct if errors occur
|
| 30 |
+
|
| 31 |
+
## π§ͺ Try These Examples
|
| 32 |
+
|
| 33 |
+
| Query | Expected Behavior |
|
| 34 |
+
|-------|------------------|
|
| 35 |
+
| `"Show me the top employees"` | π€ **Asks clarification** β Top by salary? Orders handled? Tenure? |
|
| 36 |
+
| `"Which customer spent the most?"` | β
**Answers directly** with SQL JOIN across orders/customers |
|
| 37 |
+
| `"What's the customer satisfaction score?"` | β **Explains** the data doesn't exist, suggests alternatives |
|
| 38 |
+
| `"By salary, in Engineering"` (after ambiguous Q) | β
**Remembers context** and answers the clarified question |
|
| 39 |
+
|
| 40 |
+
## ποΈ Architecture
|
| 41 |
+
|
| 42 |
+
```
|
| 43 |
+
User Question
|
| 44 |
+
β
|
| 45 |
+
βΌ
|
| 46 |
+
βββββββββββββββββββββββ
|
| 47 |
+
β Intent Classifier β β Answerable / Ambiguous / Unanswerable
|
| 48 |
+
βββββββββββ¬ββββββββββββ
|
| 49 |
+
β
|
| 50 |
+
βββββββΌββββββ
|
| 51 |
+
βΌ βΌ βΌ
|
| 52 |
+
Clear Ambig N/A
|
| 53 |
+
β β β
|
| 54 |
+
βΌ βΌ βΌ
|
| 55 |
+
SQL Ask Explain
|
| 56 |
+
Gen Clarify Why
|
| 57 |
+
β β
|
| 58 |
+
βΌ βΌ
|
| 59 |
+
Execute User
|
| 60 |
+
DB Reply
|
| 61 |
+
β β
|
| 62 |
+
βΌ ββββ (next turn)
|
| 63 |
+
Results
|
| 64 |
+
```
|
| 65 |
+
|
| 66 |
+
## π Demo Database
|
| 67 |
+
|
| 68 |
+
The Space comes with a pre-loaded **company database** (6 tables, ~60 rows):
|
| 69 |
+
|
| 70 |
+
- **departments** β Engineering, Sales, Marketing, HR, Finance
|
| 71 |
+
- **employees** β 12 employees with salary, hire date, department, manager
|
| 72 |
+
- **customers** β 8 B2B customers with tiers (standard/premium/enterprise)
|
| 73 |
+
- **products** β 8 products (Hardware/Software) with price, cost, stock
|
| 74 |
+
- **orders** β 12 orders with status (completed/shipped/pending/cancelled)
|
| 75 |
+
- **order_items** β 17 line items with quantity, price, discount
|
| 76 |
+
|
| 77 |
+
## π Research Foundation
|
| 78 |
+
|
| 79 |
+
This agent's design draws from:
|
| 80 |
+
|
| 81 |
+
| Paper | Key Contribution |
|
| 82 |
+
|-------|-----------------|
|
| 83 |
+
| [MMSQL](https://arxiv.org/abs/2412.17867) | 4-type question classification (answerable/ambiguous/unanswerable/improper) |
|
| 84 |
+
| [PRACTIQ](https://arxiv.org/abs/2410.11076) | Multi-turn clarification dialogue patterns for SQL |
|
| 85 |
+
| [SQLFixAgent](https://arxiv.org/abs/2406.13408) | Self-correcting SQL via ReAct reasoning |
|
| 86 |
+
| [MTSQL-R1](https://arxiv.org/abs/2510.12831) | Agentic multi-turn SQL with memory verification |
|
| 87 |
+
| [Disambiguate-then-Parse](https://arxiv.org/abs/2502.18448) | Interpretation generation for ambiguous queries |
|
| 88 |
+
|
| 89 |
+
## π§ Technical Stack
|
| 90 |
+
|
| 91 |
+
- **Agent**: [smolagents](https://huggingface.co/docs/smolagents) `CodeAgent` with ReAct loop
|
| 92 |
+
- **LLM**: Qwen/Qwen2.5-Coder-32B-Instruct via HF Inference API
|
| 93 |
+
- **Database**: SQLite (in-memory demo)
|
| 94 |
+
- **UI**: Gradio chat interface with multi-turn support
|
| 95 |
+
|
| 96 |
+
## π Run Locally
|
| 97 |
+
|
| 98 |
+
```bash
|
| 99 |
+
pip install smolagents[gradio] sqlalchemy
|
| 100 |
+
export HF_TOKEN=your_token_here
|
| 101 |
+
python app.py
|
| 102 |
+
```
|
| 103 |
+
|
| 104 |
+
## License
|
| 105 |
+
|
| 106 |
+
MIT
|