joelgilbert/NL2SQL
0
๐ค Natural Language to SQL Query System
Transform your questions into SQL queries using AI! This application uses a multi-agent architecture to understand natural language questions, generate SQL queries, execute them safely, and explain the results in plain English.
โจ Features
- ๐ง Multi-Agent AI System
- Gatekeeper: Validates and classifies user intent
- SQL Generator: Creates optimized queries using Cloudflare Workers AI
- Explainer: Provides business-friendly result summaries
- ๐ Enterprise-Grade Security
- Read-only mode by default (SELECT queries only)
- DBA mode with password protection for write operations
- SQL injection prevention
- Comprehensive audit logging
- ๐ Smart Query Generation
- Automatic error correction (up to 3 retries)
- Semantic schema search using vector embeddings
- Query optimization and validation
- ๐ Interactive Results
- Clean table visualization
- Natural language explanations
- Query execution metrics
- Session statistics
๐ฏ How to Use
Ask Questions in Plain English
Examples:
- "How many customers do we have?"
- "Show me total revenue by product category"
- "Who are our top 5 customers by spending?"
- "List orders from last week"
- "What's the average order value?"
Two Modes
๐ข Read-Only Mode (Default)
- Safe for business intelligence queries
- SELECT queries only
- No data modification
๐ด DBA Mode (Password Protected)
- Full database access
- INSERT, UPDATE, DELETE operations
- Requires approval for destructive queries
๐๏ธ Architecture
User Question
โ
Agent 1: Gatekeeper (Groq/Llama-3.1)
- Validates intent
- Filters irrelevant questions
โ
Vector Search (Upstash Vector)
- Finds relevant table schemas
- Retrieves similar queries
โ
Agent 2: SQL Generator (Cloudflare/SQLCoder-7B-2)
- Generates SQL query
- Auto-corrects errors
- Validates syntax
โ
Security Validator
- Checks for SQL injection
- Verifies permissions
- Validates table access
โ
Query Executor (Neon PostgreSQL)
- Executes query safely
- Returns results
โ
Agent 3: Explainer (Groq/Llama-3.1)
- Analyzes results
- Generates insights
- Creates human-friendly explanation๐ ๏ธ Technology Stack
- Frontend: Streamlit
- Database: Neon Serverless PostgreSQL
- Vector DB: Upstash Vector (with BAAI/bge-m3 embeddings)
- LLMs:
- Groq API (Llama-3.1-70b-versatile)
- Cloudflare Workers AI (SQLCoder-7B-2)
- Security: Custom query validation & audit logging
๐ Sample Database
The demo uses an e-commerce database with:
- 6 tables: regions, customers, categories, products, orders, order_items
- 200+ rows of realistic sample data
- Full relationships: foreign keys and indexes
๐ Security Features
Input Validation
- SQL injection pattern detection
- Multi-statement prevention
- Destructive operation blocking
Access Control
- Two-tier permission model
- Password-based DBA access
- Session timeout (1 hour)
- Timing-attack resistant authentication
Audit Trail
- All query attempts logged
- Execution results tracked
- Performance metrics recorded
๐ก Use Cases
- Business Intelligence: Non-technical users can query data
- Data Exploration: Quick insights without writing SQL
- Learning SQL: See how natural language maps to SQL
- Data Analysis: Fast ad-hoc queries and reporting
๐ Example Queries
Easy
- "Count total orders"
- "List all product categories"
- "Show active customers"
Medium
- "Total sales by region last month"
- "Average order value per customer"
- "Top 10 best-selling products"
Advanced
- "Monthly revenue trend for last 6 months"
- "Customer lifetime value analysis"
- "Products with low stock (< 50 units)"
โ๏ธ Configuration
This app requires the following environment variables (set in Space Settings โ Repository secrets):
NEON_READONLY_CONNECTION_STRING- Database connection (read-only)NEON_DBA_CONNECTION_STRING- Database connection (full access)GROQ_API_KEY- Groq API for LLM inferenceCLOUDFLARE_ACCOUNT_ID- Cloudflare accountCLOUDFLARE_AUTH_TOKEN- Cloudflare authenticationUPSTASH_VECTOR_URL- Upstash Vector database URLUPSTASH_VECTOR_TOKEN- Upstash authentication tokenDBA_PASSWORD- Password for DBA mode
๐ License
MIT License - Feel free to use and modify!
๐ค Contributing
Built with โค๏ธ using open-source technologies. Contributions welcome!
๐ Links
- Documentation: Full README
- Deployment Guide: DEPLOYMENT_CHECKLIST.md
- Quick Start: QUICKSTART.md
Note: This is a demo application. For production use, ensure proper security review, API rate limiting, and cost monitoring.
