Transform natural language questions into SQL queries with the power of AI. No SQL expertise required.
The AI-Powered SQL Agent is an intelligent system that bridges the gap between natural language and database queries. Using advanced LLM capabilities and LangChain's Expression Language (LCEL), this agent enables anyone to query databases using everyday languageβno SQL knowledge needed.
- π£οΈ Natural Language Processing: Ask questions in plain English
- π Intelligent Query Generation: Automatically converts NLQ to SQL
- π‘οΈ Error Recovery: Handles and corrects SQL errors automatically
- π Multi-Query Support: Executes complex queries across multiple tables
- β‘ Token Efficient: Optimized schema retrieval for better performance
- π LCEL Architecture: Built on LangChain's modern expression language
This project demonstrates advanced LCEL capabilities with LangChain Community Tools:
LCEL Features:
- Runnable Interface: Core abstraction for all composable components
- Pipe Operator (|): Chain operations together seamlessly
- RunnableLambda: Wrap custom functions as runnables
- AgentExecutor: Execute agents with tool access
- create_react_agent: REACT pattern (Reasoning + Acting)
- invoke(): Execute the complete chain
- stream(): Real-time response streaming
LangChain Community Tools:
- QuerySQLDatabaseTool: Execute SQL queries on database
- InfoSQLDatabaseTool: Get table schema and structure information
- ListSQLDatabaseTool: List all available tables in database
- Custom Tools: Validation and error recovery tools
- Python 3.11 or higher
- MySQL Server
- IBM Watson API credentials (for LLM)
- Clone the repository
git clone https://github.com/altugyerli/Ai-powerred-sql-agent.git
cd Ai-powerred-sql-agent- Create virtual environment
python3.11 -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies
pip install -r requirements.txtCreate a .env file in the project root:
# IBM Watson Credentials
IBM_API_KEY=your_api_key_here
IBM_PROJECT_ID=your_project_id
# MySQL Configuration
MYSQL_USER=root
MYSQL_PASSWORD=your_password
MYSQL_HOST=localhost
MYSQL_PORT=3306
MYSQL_DATABASE=chinookfrom sql_agent import SQLAgent
# Initialize the agent (builds LCEL + Tools internally)
agent = SQLAgent()
# The agent automatically has access to:
# - QuerySQLDatabaseTool: Execute SQL queries
# - InfoSQLDatabaseTool: Get table schema
# - ListSQLDatabaseTool: List tables
# - validate_sql_query: Validate queries
# - recover_from_error: Error recovery
# Execute query using LCEL agent
result = agent.query("How many albums are in the database?")
print(result)
# Output: {
# 'question': 'How many albums are in the database?',
# 'answer': 'There are 347 albums in the database.',
# 'status': 'success'
# }from sql_agent import SQLAgent
agent = SQLAgent()
# Method 1: invoke() - Synchronous execution
result = agent.query("How many albums?")
print(result)
# Method 2: stream() - Real-time streaming with tools
for chunk in agent.agent_executor.stream({"input": "How many albums?"}):
print(chunk, end="", flush=True)
# Method 3: batch() - Process multiple queries
questions = [
{"input": "How many albums?"},
{"input": "How many artists?"},
{"input": "How many customers?"}
]
results = agent.agent_executor.batch(questions)
for result in results:
print(result)# Complex multi-table queries
agent.query("Show me the top 5 artists by total sales")
# Aggregations and filtering
agent.query("Which customers spent more than $50?")
# Table descriptions
agent.query("Describe the structure of the PlaylistTrack table")from sql_agent import SQLAgent
agent = SQLAgent()
agent.run_interactive() # Uses LCEL + Tools internallyInteractive Session Output:
======================================================================
π€ AI-Powered SQL Agent (LCEL + Tools)
======================================================================
Built with:
β’ LangChain Expression Language (LCEL)
β’ LangChain Community Tools (SQL Database Tools)
β’ REACT Agent Pattern (Reasoning + Acting)
β’ IBM Granite 3.2 8B Instruct LLM
Available Tools:
β’ query_sql_database: Execute SQL queries
β’ info_sql_database: Get table schema
β’ list_sql_database: List tables
β’ validate_sql_query: Validate queries
β’ recover_from_error: Error recovery
Type 'exit' to quit
======================================================================
π Ask a question: How many customers are there?
π Processing your question...
β
Status: success
π Question: How many customers are there?
π¬ Answer:
There are 59 customers in the database.
This project leverages LangChain Expression Language (LCEL) with LangChain Community Tools:
- Runnable Interface: All components implement the Runnable abstraction
- Tool Integration: Use LangChain's built-in SQL database tools
- REACT Agent: Reasoning + Acting pattern for intelligent decision making
- Type Safety: Full type hints throughout
- Streaming Support: Real-time response streaming with
.stream() - Error Handling: Comprehensive error recovery mechanisms
User Query (Natural Language)
β
[LLM with Tool Access]
βββ QuerySQLDatabaseTool (Execute SQL)
βββ InfoSQLDatabaseTool (Get Schema)
βββ ListSQLDatabaseTool (List Tables)
βββ validate_sql_query (Custom Tool)
βββ recover_from_error (Custom Tool)
β
[REACT Agent Loop]
β’ Reason about the question
β’ Select appropriate tool
β’ Execute tool
β’ Observe results
β’ Repeat until answer found
β
Formatted Response
# Create tools from LangChain community
tools = [
QuerySQLDatabaseTool(db=db), # Execute queries
InfoSQLDatabaseTool(db=db), # Get table info
ListSQLDatabaseTool(db=db), # List tables
validate_sql_query, # Custom validation
recover_from_error, # Custom recovery
]
# Create REACT agent with tools
agent = create_react_agent(llm, tools, prompt)
# Execute with AgentExecutor
executor = AgentExecutor(agent=agent, tools=tools)
result = executor.invoke({"input": question})All components implement LangChain's Runnable interface:
class Runnable:
def invoke(input) β output # Execute synchronously
def stream(input) β Iterator # Stream results
def batch(inputs) β List # Process multiple inputsWhen you ask a question, the agent follows the REACT pattern:
- Reason: LLM analyzes the question and decides what to do
- Act: LLM selects and calls appropriate tools
- Observe: Tools return results
- Repeat: Loop continues until answer is found
1. REASON
LLM: "I need to count albums. First, let me list tables."
2. ACT
Tool: ListSQLDatabaseTool
Result: Tables include Album, Artist, Track, etc.
3. OBSERVE
LLM: "Found Album table. Now let me get its schema."
4. ACT
Tool: InfoSQLDatabaseTool
Result: Album table has AlbumId, Title, ArtistId columns
5. OBSERVE
LLM: "Now I'll count the albums."
6. ACT
Tool: QuerySQLDatabaseTool
SQL: SELECT COUNT(*) FROM Album
Result: 347
7. ANSWER
"There are 347 albums in the database."
- QuerySQLDatabaseTool: Execute SQL queries
- InfoSQLDatabaseTool: Get table schema and structure
- ListSQLDatabaseTool: List all available tables
- validate_sql_query: Check query safety
- recover_from_error: Suggest fixes for errors
.
βββ sql_agent.py # Main agent implementation
βββ llm_agent.py # LLM configuration
βββ requirements.txt # Python dependencies
βββ .env.example # Environment template
βββ README.md # This file
- Model: IBM Granite 3.2 8B Instruct
- Max Tokens: 1024
- Temperature: 0.2 (deterministic)
- Top-P: 0.95
- Repetition Penalty: 1.2
Currently optimized for MySQL. Easily extensible to:
- PostgreSQL
- SQLite
- Oracle
- SQL Server
Run the test suite:
python -m pytest tests/Test a specific query:
python sql_agent.py| Query | Purpose |
|---|---|
| "How many albums are there?" | Count aggregation |
| "List top 10 artists" | Sorting & limiting |
| "Describe the Customer table" | Schema inspection |
| "Show sales by genre" | Grouping & aggregation |
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit changes (
git commit -m 'Add amazing feature') - Push to branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- ARCHITECTURE.md - Detailed system architecture and LCEL + Tools design
- LCEL_GUIDE.md - Complete guide to LCEL concepts and features
- EXAMPLES.md - Comprehensive usage examples
- FAQ.md - Frequently asked questions
- LangChain Documentation
- LCEL Documentation
- LangChain Tools
- Agents Documentation
- IBM Watson Documentation
- LangChain - For LCEL framework and community tools
- IBM Watson - For LLM capabilities
- MySQL - Database engine
- Chinook Database - Sample database
For issues, questions, or suggestions:
- Open an Issue
- Start a Discussion
Made with β€οΈ by AltuΔ Yerli
Powered by LangChain LCEL + Community Tools