Skip to content

About

Natural language → SQL with LangChain. Auto charts and AI summaries of the results in a Streamlit app.

Resources

Stars

2 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

🗃️ Talk to Data — SQL Data Analyst Assistant

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.


Features

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 …)

Demo

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?

Project Structure

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)

Database schema

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

Setup

1 · Prerequisites

2 · Clone & install

git clone <repo-url>
cd sql-analyst
python -m venv .venv && source .venv/bin/activate
pip install -r requirements.txt

3 · Configure environment

cp .env.example .env
# Edit .env and set your key:
# DEEPSEEK_API_KEY=sk-...

Or paste the key directly in the sidebar at runtime.

4 · Seed the database

python data/seed.py

Expected output:

✅ Database seeded → data/store.db
   products:  20
   regions:   5
   customers: 80
   sales:     3,000

5 · Run

streamlit run app.py

Open http://localhost:8501.


Configuration

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

How it works

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

Extending

  • Different database: change DB_URI in analyst/config.py to any SQLAlchemy-compatible connection string (PostgreSQL, MySQL, etc.)
  • Different LLM: swap ChatOpenAI for any LangChain-compatible chat model
  • Custom charts: edit analyst/visualizer.py — the heuristic is straightforward to extend

Environment file example

# .env.example
DEEPSEEK_API_KEY=sk-your-key-here

About

Natural language → SQL with LangChain. Auto charts and AI summaries of the results in a Streamlit app.

Resources

Stars

2 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages