CoolFace
Apppublic

k1golestan/text-to-sql-agent

sourceHugging Facemitupdated 5mo agoView on Hugging Face
0likes
App README

๐Ÿ—ƒ๏ธ 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:

  1. 1.Classify โ€” Is the question answerable, ambiguous, or unanswerable?
  2. 2.Clarify โ€” If ambiguous, ask the user targeted questions before generating SQL
  3. 3.Execute & Verify โ€” Generate SQL, run it, self-correct if errors occur

๐Ÿงช Try These Examples

QueryExpected Behavior
"Show me the top employees"๐Ÿค” Asks clarification โ€” Top by salary? Orders handled? Tenure?
"Which customer spent the most?"โœ… Answers directly with SQL JOIN across orders/customers
"What's the customer satisfaction score?"โŒ Explains the data doesn't exist, suggests alternatives
"By salary, in Engineering" (after ambiguous Q)โœ… Remembers context and answers the clarified question

๐Ÿ—๏ธ 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:

PaperKey Contribution
MMSQL4-type question classification (answerable/ambiguous/unanswerable/improper)
PRACTIQMulti-turn clarification dialogue patterns for SQL
SQLFixAgentSelf-correcting SQL via ReAct reasoning
MTSQL-R1Agentic multi-turn SQL with memory verification
Disambiguate-then-ParseInterpretation generation for ambiguous queries

๐Ÿ”ง Technical Stack

  • โ€”Agent: smolagents CodeAgent with 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

bash
pip install smolagents[gradio] sqlalchemy
export HF_TOKEN=your_token_here
python app.py

License

MIT