A production-grade AI agent that converts natural language questions into SQL queries and returns results — powered by Mistral 7B running fully offline.
"Show me top 5 customers by revenue"
SELECT c.name, SUM(o.total_amount) as total_revenue
FROM customers c
JOIN orders o ON c.customer_id = o.customer_id
GROUP BY c.customer_id
ORDER BY total_revenue DESC
LIMIT 5;Returns a clean table with results instantly.
Most people in a company cannot write SQL. This tool lets anyone — managers, analysts, HR — query a database using plain English, without any SQL knowledge.
- Type a question in plain English
- The agent understands your intent
- Generates accurate SQL using a local LLM
- Validates it through a 4-level safety pipeline
- Executes it and returns results in a clean table
User Question
↓
NLP Processor → detects intent, tables, aggregations
↓
SQL Generator → Mistral 7B via Ollama (runs locally)
↓
Validator → 4-level safety check (blacklist, syntax, SELECT-only, LLM critique)
↓
Executor → SQLite database
↓
Response Formatter → JSON + table + summary
↓
Streamlit UI
| Layer | Technology |
|---|---|
| LLM | Mistral 7B via Ollama |
| Backend | FastAPI |
| Database | SQLite + SQLAlchemy |
| SQL Validation | SQLGlot |
| Frontend | Streamlit |
| Language | Python 3.11 |
- Fully offline — no data sent to external APIs
- 4-level SQL safety validation pipeline
- Auto retry loop with LLM self-correction
- ReAct-style agent orchestration
- Production-grade logging and error handling
- Modular, clean codebase following real-world structure
text2sql-agent/
├── app/
│ ├── agent/ # NLP processor, SQL generator, orchestrator
│ ├── tools/ # Validator, executor
│ ├── llm/ # Mistral client
│ ├── db/ # Models, connection, schema loader
│ ├── api/ # FastAPI routes
│ └── formatter/ # Response formatter
├── data/ # Database + seed script
├── ui/ # Streamlit frontend
├── tests/ # Unit tests
└── eval/ # Benchmark evaluation
- Python 3.11
- Ollama installed
git clone https://github.com/YOUR_USERNAME/text2sql-agent.git
cd text2sql-agentpy -3.11 -m venv venv
venv\Scripts\activate.bat # Windows
source venv/bin/activate # Mac/Linuxpip install -r requirements.txtcopy .env.example .envpython data/seed.pyollama pull mistralTerminal 1:
ollama serveTerminal 2:
uvicorn app.main:app --reload --port 8000Terminal 3:
streamlit run ui/streamlit_app.pyOpen http://localhost:8501 in your browser.
Show me top 5 customers by revenue
What is the total revenue from delivered orders
List all products with price less than 1000
Which city has the most customers
How many orders were placed this year
Show me all Electronics products
What is the average order value
List customers from Maharashtra
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/v1/health | Check API and LLM status |
| POST | /api/v1/query | Submit a natural language question |
| GET | /api/v1/schema | View the database schema |
Companies in banking, healthcare, and government cannot send data to external APIs due to privacy regulations (GDPR, India's DPDP Act). This project runs entirely on-premise — no data ever leaves the machine.
- Fine-tune Mistral on Spider dataset for higher accuracy
- Add PostgreSQL support
- Add user authentication
- Deploy on Railway / Render
- Support multi-turn conversation memory