Team Ai
Apppublic

joelgilbert/NL2SQL

sourceHugging Facemitupdated 11mo agoView on Hugging Face
0likes
App README

๐Ÿค– 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 inference
  • โ€”CLOUDFLARE_ACCOUNT_ID - Cloudflare account
  • โ€”CLOUDFLARE_AUTH_TOKEN - Cloudflare authentication
  • โ€”UPSTASH_VECTOR_URL - Upstash Vector database URL
  • โ€”UPSTASH_VECTOR_TOKEN - Upstash authentication token
  • โ€”DBA_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.