Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SQL Agent Project

Overview

The SQL Agent is a Text-to-SQL application enabling users to ask questions in natural language and receive answers from tabular data using automatically generated SQL queries. Designed for non-technical users, it emphasizes error handling, performance analysis, and user experience.


Key Features

  • 🗣️ Natural Language to SQL conversion (LLM-powered)
  • 🧠 Smart Schema Validation with suggestions for column mismatches
  • 📊 Performance Metrics & Matrix Analysis (execution time, rows, size, complexity, memory)
  • ✏️ Query Editing & Re-execution with before/after performance comparison
  • 🖥️ User-Friendly Streamlit UI (5 tabs: Answer, SQL Query, Raw Results, Performance, Optimization)
  • 📚 Comprehensive documentation and troubleshooting

Technical Stack

  • Python (core language)
  • Streamlit (UI framework)
  • NumPy (matrix operations)
  • pandas (data handling)
  • SQLite (database)
  • difflib (string similarity)
  • OpenAI API (LLM for SQL generation)

Installation

  1. Clone the repository:
    git clone https://github.com/krushna27/Natural-Language-to-SQL-conversion-AI-Agent.git
    cd nlp-to-sql-query-system
  2. Create a virtual environment:
    python -m venv venv
    # On Windows:
    venv\Scripts\activate
    # On Unix/Mac:
    source venv/bin/activate
  3. Install dependencies:
    pip install -r requirements.txt
  4. Set up environment variables (if using OpenAI API):
    cp .env.example .env
    # Edit .env and add your OpenAI API key

Usage

Run the Streamlit application:

streamlit run src/app.py

Then:

  1. Upload your CSV or Excel file
  2. Ask questions in natural language
  3. View the AI-generated SQL, results, and performance metrics
  4. Edit and re-execute queries for optimization

Example Queries

  • "What are the top 5 records?"
  • "Show me the total count by category"
  • "What is the average value?"
  • "List all unique items"
  • "Show transactions above $1000 grouped by payment method"

UI Tabs & User Experience

Tab Purpose
💬 Answer AI's natural language response
🔧 SQL Query SQL code + editing + re-execution
📊 Raw Results Table view + CSV download
⚡ Performance Metrics, execution time, complexity
🎯 Optimization Auto recommendations to speed up queries

Architecture & Data Flow

  • Modular design: schema validation, performance metrics, database loading, UI components
  • Session state: tracks query history, metrics, and user edits
  • Error handling: actionable error messages and suggestions

System Flow

  1. User uploads data and asks a question in natural language
  2. LLM generates an SQL query
  3. Schema validator checks for column mismatches and suggests corrections if needed
  4. Query executes, and performance metrics are collected
  5. Results, metrics, and recommendations are displayed in the UI
  6. User can edit the SQL and re-execute to optimize performance

Requirements

  • Python 3.8+
  • OpenAI API key (for LLM features)

About

The SQL Agent is a Text-to-SQL application enabling users to ask questions in natural language and receive answers from tabular data using automatically generated SQL queries. Designed for non-technical users, it emphasizes error handling, performance analysis, and user experience.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages