SQL Agent is an intelligent, AI-powered tool designed to bridge the gap between human language and relational databases. It allows users to query databases (Postgres, SQL Server) using plain English, automatically translating questions into valid SQL, executing them, and providing natural language insights along with dynamic visualizations.
- Text-to-SQL Conversion: Converts complex natural language questions into optimized SQL queries.
- Multi-LLM Integration: Supports multiple providers including Groq (Llama-3), OpenAI (GPT-4o), Google Gemini, Anthropic, and DeepSeek.
- Self-Correcting SQL: Automatically detects and repairs common SQL syntax errors or schema mismatches via an internal "SQL Repair" loop.
- Dynamic Visualization: Suggests and renders appropriate charts (Bar, Line, Pie, Area) using Chart.js.
- Enterprise-Grade Security (RBAC): Implements Role-Based Access Control to filter database schemas, ensuring users only see data they are authorized to access.
- SQL Injection Prevention: Uses AST (Abstract Syntax Tree) parsing via
sqlglotto block destructive commands (DROP,DELETE,UPDATE) and preventSELECT *. - Semantic Search: Uses FAISS and
sentence-transformersfor intelligent schema discovery in large databases.
- Backend: FastAPI (Python)
- Frontend: HTML5, CSS3, Vanilla JavaScript (Refined UI)
- Database Support: PostgreSQL (
psycopg2), SQL Server (pyodbc) - LLM Processing: Groq, OpenAI, Google AI, Anthropic APIs
- Core Libraries:
sqlglot: SQL Parsing and Dialect Translationpandas: Data manipulation and reportingFAISS&sentence-transformers: Vector search for schema metadatafpdf2&xlsxwriter: PDF and Excel report generation
graph TD
User((User)) -->|Question| UI[Frontend UI]
UI -->|API Request| FastAPI Backend
subgraph "Backend Processing"
FastAPI[FastAPI Backend] --> RBAC[RBAC Schema Filter]
RBAC --> Intent[Intent Analysis]
Intent --> Prompt[Prompt Builder]
Prompt --> LLM[LLM Generator]
LLM -->|SQL Query| Validator[SQLGlot Validator]
Validator -->|Safe Query| Exec[Query Executor]
end
Exec -->|SQL| DB[(Postgres/SQL Server)]
DB -->|Results| Exec
Exec -->|Data| UI
UI -->|Charts/Summary| User
- User Query: The user submits a question in the UI.
- Schema Introspection: The backend retrieves the database schema (filtered by RBAC).
- Intent Analysis: A small LLM call identifies the type of analysis (Trend, Comparison, etc.) to suggest visualizations.
- Prompt Construction: A structured prompt is built, including schema, history, and results from semantic search.
- SQL Generation: The LLM generates the SQL query.
- Validation & Optimization:
sqlglotvalidates the query against security rules. - Execution: The query is executed on the target database via Pandas.
- Insights & Visuals: The system generates a natural language summary and suggests charts.
- Python 3.9+
- A valid API Key for Groq or OpenAI.
# Clone the repository
git clone <your-repo-link>
cd SQL_Agent
# Create a virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txtCreate a .env file in the root directory:
GROQ_API_KEY=your_groq_key
OPENAI_API_KEY=your_openai_key
# Optional
GEMINI_API_KEY=your_gemini_key
ANTHROPIC_API_KEY=your_anthropic_keypython main.pyThen, open frontend/landing.html in your web browser.
SQL Agent is designed with a "Security-First" approach:
- Restricted Operations: Only
SELECTstatements are permitted. - AST Validation: Every query is parsed before execution. If a non-select or forbidden pattern (like
SELECT *) is detected, the system rejects it or asks for repair. - RBAC Schema Masking: The LLM only receives metadata for tables and columns authorized for the current user's role.
- Agentic Multi-Step Reasoning: Supporting multi-turn data analysis.
- Advanced Caching: Redis-based result caching for performance.
- Deeper Integration: Direct Slack/Teams integration for query alerts.