Skip to content

The-Harsh-Vardhan/ReasonSQL

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

85 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

ReasonSQL - Multi-Agent NL→SQL System

Python 3.10+ CI Next.js FastAPI LLM: Gemini Live Demo Render License: MIT

Why simple "prompt β†’ SQL" fails, and how 12 specialized agents fix it.

πŸ”— Live Demo β†’


⚑ TL;DR

❌ Naive Approach βœ… ReasonSQL
Schema + Question β†’ LLM β†’ SQL 12 Specialized Agents in Pipeline
Hallucinates table names Explores schema BEFORE generating
Assumes meaning of "recent", "best" Asks clarifying questions
Returns errors, not answers Self-corrects on failures
No safety (SELECT * on 1M rows) Safety-validated, enforces LIMIT
Black box Full reasoning trace visible

Result: ~50% accuracy β†’ 85%+ on complex queries


✨ Features

Core Intelligence

  • 12 Specialized AI Agents β€” Intent analysis, schema exploration, SQL generation, safety validation, self-correction, response synthesis
  • Batch-Optimized Pipeline β€” Only 4-6 LLM calls per query (vs. 12+ with naive approaches)
  • Self-Correction β€” Automatically retries and fixes errors
  • Safety Validation β€” Blocks DROP/DELETE/UPDATE, enforces SELECT-only with LIMIT

Frontend (Next.js)

  • SQL Syntax Highlighting β€” Color-coded keywords, strings, numbers, functions
  • Copy Buttons β€” One-click copy for answers and generated SQL
  • CSV Export β€” Download query results as CSV
  • Shareable Links β€” Share queries via URL (?q=your+query)
  • Keyboard Shortcuts β€” Ctrl+Enter to submit queries
  • Saved Queries / Bookmarks β€” Star queries for quick re-use
  • Query Suggestions β€” Preset query pills for quick exploration
  • Live Execution Timer β€” Real-time countdown while processing
  • Toast Notifications β€” Slide-in feedback for all actions
  • Schema Explorer β€” Browse database tables and columns in the sidebar
  • System Status β€” Live connection indicators for API and database
  • Agent Pipeline Visualization β€” See which agents ran in sequence
  • Architecture Section β€” Expandable "How it works" with agent descriptions
  • Responsive Design β€” Collapsible sidebar with hamburger menu on mobile
  • Analytics Dashboard β€” Query stats, success rate chart, top queries (/dashboard)
  • PWA Support β€” Installable on mobile/desktop
  • OpenGraph Social Preview β€” Branded card when sharing on LinkedIn/Twitter

Backend (FastAPI)

  • Async Pipeline β€” Fully asynchronous with asyncpg for non-blocking database calls
  • Redis Caching β€” LLM response caching with automatic in-memory fallback
  • Multi-turn Context β€” Follow-up questions with pronoun resolution ("show me their albums")
  • Vector Search (RAG) β€” Semantic schema selection for large databases (15+ tables)
  • CSV Upload β€” POST /upload endpoint to ingest CSV files on-the-fly
  • PostgreSQL + SQLite β€” Supabase PostgreSQL in production, SQLite for local dev
  • CI/CD Pipeline β€” GitHub Actions for automated testing on push/PR
  • Quota Management β€” Gemini API key rotation and rate limiting

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    Next.js Frontend                         β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”‚
β”‚  β”‚  Query    β”‚ β”‚  Schema  β”‚ β”‚ Dashboardβ”‚ β”‚  System  β”‚      β”‚
β”‚  β”‚  Input    β”‚ β”‚ Explorer β”‚ β”‚ /dashboardβ”‚ β”‚  Status  β”‚      β”‚
β”‚  β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β”‚
β”‚       β”‚                                                     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
        β”‚  POST /query
        β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    FastAPI Backend                          β”‚
β”‚                                                            β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚ Intent  β”‚β†’ β”‚ Schema  β”‚β†’ β”‚   SQL    β”‚β†’ β”‚  Safety    β”‚  β”‚
β”‚  β”‚Analyzer β”‚  β”‚Explorer β”‚  β”‚Generator β”‚  β”‚ Validator  β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜  β”‚
β”‚                                                  β”‚         β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚Response │← β”‚  Self   │← β”‚  Query   │← β”‚    FK      β”‚  β”‚
β”‚  β”‚ Synth   β”‚  β”‚Correctorβ”‚  β”‚ Executor β”‚  β”‚ Validator  β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β”‚                                                            β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                          β”‚
                          β–Ό
              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
              β”‚  PostgreSQL (Supabase) β”‚
              β”‚  or SQLite (local)     β”‚
              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸš€ Quick Start

Local Development

# 1. Clone & install backend
git clone https://github.com/The-Harsh-Vardhan/ReasonSQL.git
cd ReasonSQL
pip install -r requirements.txt

# 2. Configure environment
cp .env.example .env
# Add your GEMINI_API_KEY to .env

# 3. Start backend
python -m uvicorn backend.api.main:app --port 8000

# 4. Start frontend (new terminal)
cd frontend-next
npm install && npm run dev
# Visit http://localhost:3000

Environment Variables

Variable Required Description
GEMINI_API_KEY βœ… Google Gemini API key
DATABASE_URL For PostgreSQL Supabase connection string
DATABASE_PATH For SQLite Path to .db file (default: data/chinook.db)
NEXT_PUBLIC_API_URL For deploy Backend API URL

πŸ“ Project Structure

ReasonSQL/
β”œβ”€β”€ backend/                    # Core modular API (FastAPI)
β”‚   β”œβ”€β”€ api/                    # Routers, deps, schemas
β”‚   β”œβ”€β”€ agents/                 # 12 specialized agent definitions
β”‚   β”œβ”€β”€ orchestrator/           # Multi-agent logic
β”‚   └── adapters/               # DB Adapters (SQLite/Postgres)
β”œβ”€β”€ frontend-next/              # Next.js 16 Dashboard
β”œβ”€β”€ tests/                      # API and Integration tests
β”œβ”€β”€ data/                       # Sample datasets (Chinook)
β”œβ”€β”€ configs/                    # Environment & provider configs
β”œβ”€β”€ docs/                       # Hosting & usage guides

πŸ“– How to Use

  1. Ask a Question: Type any natural language question about the database (e.g., "Who are the top 10 customers by spend?").
  2. Observe the Reasoning: Click the "Reasoning" tab to see how the 12 agents analyzed your intent, explored the schema, and validated the SQL.
  3. Review Results: View the generated SQL and the live data preview in the "Result" tab.
  4. Export & Share: Download results as CSV or share the specific query URL with your team.

πŸš€ Deployment

Platform Component Free Tier Status
Vercel Next.js Frontend βœ… Free Live β†’
Render FastAPI Backend βœ… 750 hrs/mo Active
Supabase PostgreSQL DB βœ… 500MB Connected

See HOSTING.md for detailed instructions.


πŸ“– Documentation


πŸ› οΈ Tech Stack

Layer Technology
Frontend Next.js 16, React, Tailwind CSS
Backend Python, FastAPI, Pydantic
LLM Google Gemini (with key rotation)
Database PostgreSQL (Supabase) / SQLite
Caching Redis (with in-memory fallback)
Embeddings sentence-transformers (all-MiniLM-L6-v2)
Hosting Vercel (frontend) + Render (backend)
CI/CD GitHub Actions
Analytics Vercel Analytics

πŸ“œ License

MIT License β€” see LICENSE for details.


🀝 Contributing

Contributions are welcome! Please read CONTRIBUTING.md for guidelines.


Built with ReasonSQL
12 Agents β€’ 4 LLM Calls β€’ Full Transparency

Live Demo β€’ Source Code

About

A sophisticated system that converts natural language questions into SQL queries through a 12-agent pipeline with schema reasoning, self-correction, safety validation, and explainable AI.

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors