Convert natural-language questions into SQL queries, visualise the results as charts, and receive AI-generated bullet-point summaries — all from a clean Streamlit interface.
| Feature | Details |
|---|---|
| NL → SQL | LangChain create_sql_query_chain + DeepSeek LLM |
| Auto-visualisation | Heuristic chart selection (line / bar / pie / scatter) via Plotly |
| Insight summaries | 2–4 bullet-point findings written by the LLM |
| Query history | Tabbed panel — compare multiple queries side-by-side |
| CSV download | Export any result set with one click |
| Safe execution | Blocks all mutating SQL (INSERT / UPDATE / DELETE / DROP …) |
Record a quick screen capture and drop the GIF here
[demo.gif]
Example interactions:
▶ What are the top 5 products by total revenue?
▶ Show monthly revenue trend for 2024
▶ Which region had the highest average order value?
▶ What is the revenue breakdown by product category?
▶ Who are the top 10 customers by total spend?
sql-analyst/
├── app.py # Streamlit entry point
├── requirements.txt
├── .env # DEEPSEEK_API_KEY (not committed)
├── analyst/
│ ├── __init__.py
│ ├── config.py # Paths, model defaults
│ ├── database.py # SQLDatabase wrapper + schema helpers
│ ├── chain.py # NL → SQL chain + safe execution
│ ├── visualizer.py # Auto-chart selection (Plotly)
│ └── summarizer.py # LLM insight summary
└── data/
├── seed.py # Creates & populates store.db
└── store.db # SQLite database (generated)
| Table | Rows | Key columns |
|---|---|---|
products |
20 | id, name, category, unit_price |
regions |
5 | id, name, country |
customers |
80 | id, name, city, country, segment |
sales |
3 000 | order_date, product_id, customer_id, region_id, quantity, revenue |
- Python 3.11+
- A DeepSeek API key
git clone <repo-url>
cd sql-analyst
python -m venv .venv && source .venv/bin/activate
pip install -r requirements.txtcp .env.example .env
# Edit .env and set your key:
# DEEPSEEK_API_KEY=sk-...Or paste the key directly in the sidebar at runtime.
python data/seed.pyExpected output:
✅ Database seeded → data/store.db
products: 20
regions: 5
customers: 80
sales: 3,000
streamlit run app.pyOpen http://localhost:8501.
All tuneable constants live in analyst/config.py:
| Constant | Default | Purpose |
|---|---|---|
DEFAULT_LLM_MODEL |
deepseek-chat |
LLM used for SQL generation and summaries |
DEFAULT_TEMPERATURE |
0.0 |
Lower = more deterministic SQL |
MAX_ROWS_DISPLAY |
500 |
Rows shown in the data table |
MAX_ROWS_CHART |
200 |
Max rows passed to the chart renderer |
User question
│
▼
LangChain create_sql_query_chain
│ (schema injected into prompt)
▼
DeepSeek LLM ──► raw SQL string
│
├─ Safety check (block mutating statements)
│
▼
SQLAlchemy + SQLite ──► pandas DataFrame
│
├─► Plotly ──► auto-selected chart
│
└─► DeepSeek LLM ──► bullet-point insights
- Different database: change
DB_URIinanalyst/config.pyto any SQLAlchemy-compatible connection string (PostgreSQL, MySQL, etc.) - Different LLM: swap
ChatOpenAIfor any LangChain-compatible chat model - Custom charts: edit
analyst/visualizer.py— the heuristic is straightforward to extend
# .env.example
DEEPSEEK_API_KEY=sk-your-key-here