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.
- 🗣️ 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
- Python (core language)
- Streamlit (UI framework)
- NumPy (matrix operations)
- pandas (data handling)
- SQLite (database)
- difflib (string similarity)
- OpenAI API (LLM for SQL generation)
- Clone the repository:
git clone https://github.com/krushna27/Natural-Language-to-SQL-conversion-AI-Agent.git cd nlp-to-sql-query-system - Create a virtual environment:
python -m venv venv # On Windows: venv\Scripts\activate # On Unix/Mac: source venv/bin/activate
- Install dependencies:
pip install -r requirements.txt
- Set up environment variables (if using OpenAI API):
cp .env.example .env # Edit .env and add your OpenAI API key
Run the Streamlit application:
streamlit run src/app.pyThen:
- Upload your CSV or Excel file
- Ask questions in natural language
- View the AI-generated SQL, results, and performance metrics
- Edit and re-execute queries for optimization
- "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"
| 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 |
- 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
- User uploads data and asks a question in natural language
- LLM generates an SQL query
- Schema validator checks for column mismatches and suggests corrections if needed
- Query executes, and performance metrics are collected
- Results, metrics, and recommendations are displayed in the UI
- User can edit the SQL and re-execute to optimize performance
- Python 3.8+
- OpenAI API key (for LLM features)