DEKU02/nl2sql
0
๐ง Multi-Domain NLโSQL Copilot
A production-quality Natural Language โ SQL system that converts plain English questions into executable SQL across three real-world databases โ using schema-aware RAG, multiple LLM backends, and strict SQL guardrails.
Live Demo
Pick a database, enter your API key in the sidebar, ask a question, and click Run SQL.
Databases:
- ๐๏ธ NYC 311 โ 300k+ government service requests (complaints, agencies, boroughs)
- ๐ Olist E-Commerce โ 100k+ Brazilian e-commerce orders (sellers, products, reviews)
- ๐ฅ Synthea Healthcare โ 10k synthetic patients (encounters, conditions, medications)
Example questions:
- "What are the top 5 most common complaint types across all boroughs?"
- "Which product category generates the most total revenue?"
- "How many patients have diabetes as a condition?"
How It Works
- Schema RAG โ your question is embedded and matched against pre-indexed schema chunks (ChromaDB + sentence-transformers)
- Prompt construction โ top-K schema chunks + domain glossary + few-shot examples โ structured prompt
- LLM generation โ generates SQL (SELECT-only, guardrails enforced)
- Safe execution โ SQL is validated (no DDL/DML), LIMIT is injected, then run against a bundled SQLite database
- Results โ table view + CSV download
Benchmark Results
Supported LLM Backends
Enabling the Olist Database
Olist data comes from Kaggle and cannot be downloaded during Docker build (secrets aren't available at that stage). To enable it:
- Get a free Kaggle account at kaggle.com
- Go to Settings โ API โ Create New Token to get your credentials
- In your Space settings, add two Secrets:
KAGGLE_USERNAMEโ your Kaggle usernameKAGGLE_KEYโ your API key- Restart the Space โ Olist will build automatically on first boot (~2 min)
Without these secrets, the Space runs with NYC 311 and Synthea only (both auto-download).
Tech Stack
- RAG: ChromaDB + sentence-transformers (
all-MiniLM-L6-v2) - LLM: Groq / OpenAI / HuggingFace / Ollama
- DB: SQLite (Spaces) / PostgreSQL (local Docker)
- Guardrails: sqlglot AST parsing โ SELECT/WITH only, LIMIT enforcement
- UI: Streamlit
- Tests: 77 pytest unit tests
