k1golestan/text-to-sql-agent
0
๐๏ธ Multi-Turn Text-to-SQL Agent with Clarification
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.
๐ฏ What Makes This Different
Traditional text-to-SQL systems blindly generate a query from your question. This agent follows a 3-step decision process inspired by recent research:
- Classify โ Is the question answerable, ambiguous, or unanswerable?
- Clarify โ If ambiguous, ask the user targeted questions before generating SQL
- Execute & Verify โ Generate SQL, run it, self-correct if errors occur
๐งช Try These Examples
๐๏ธ Architecture
User Question
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโ
โ Intent Classifier โ โ Answerable / Ambiguous / Unanswerable
โโโโโโโโโโโฌโโโโโโโโโโโโ
โ
โโโโโโโผโโโโโโ
โผ โผ โผ
Clear Ambig N/A
โ โ โ
โผ โผ โผ
SQL Ask Explain
Gen Clarify Why
โ โ
โผ โผ
Execute User
DB Reply
โ โ
โผ โโโโ (next turn)
Results๐ Demo Database
The Space comes with a pre-loaded company database (6 tables, ~60 rows):
- departments โ Engineering, Sales, Marketing, HR, Finance
- employees โ 12 employees with salary, hire date, department, manager
- customers โ 8 B2B customers with tiers (standard/premium/enterprise)
- products โ 8 products (Hardware/Software) with price, cost, stock
- orders โ 12 orders with status (completed/shipped/pending/cancelled)
- order_items โ 17 line items with quantity, price, discount
๐ Research Foundation
This agent's design draws from:
๐ง Technical Stack
- Agent: smolagents
CodeAgentwith ReAct loop - LLM: Qwen/Qwen2.5-Coder-32B-Instruct via HF Inference API
- Database: SQLite (in-memory demo)
- UI: Gradio chat interface with multi-turn support
๐ Run Locally
pip install smolagents[gradio] sqlalchemy
export HF_TOKEN=your_token_here
python app.pyLicense
MIT
