A modern, AI-powered chat interface that allows users to query databases using natural language. Built with Streamlit and LangChain, this application transforms plain English questions into SQL queries and displays results in an intuitive chat format.
- π£οΈ Natural Language Queries - Ask questions in plain English
- π¬ Chat Interface - Clean, modern chat UI similar to ChatGPT
- π Real-time Results - Instant SQL query execution and results
- π SQL Query Display - Shows the generated SQL queries with syntax highlighting
- π Sample Questions - Pre-built examples to get started quickly
- π Query Statistics - Track your usage with built-in metrics
- π Fast & Responsive - Optimized for quick interactions
- Frontend: Streamlit
- AI/ML: LangChain, Groq
- Database: MySQL
- Monitoring: LangSmith (for tracing and debugging)
- Language: Python 3.8+
- Python 3.8 or higher
- pip package manager
-
Clone the repository
git clone <repository-url> cd sql-query-assistant
-
Create virtual environment
python -m venv env source env/bin/activate # On Windows: env\Scripts\activate
-
Install dependencies
pip install -r requirements.txt
-
Configure your database
- Update the
config.pyfile with your MySQL database connection details:
db_config = { "host": "localhost", # or "127.0.0.1" or your database URL "user": "your_username", # your MySQL username "password": "your_password", # your MySQL password "database": "your_db_name", # your database name "port": 3306 # default MySQL port }
- Update the
-
Set up environment variables
# Create .env file and add your API keys GROQ_API_KEY=your_groq_api_key_here LANGCHAIN_TRACING_V2=true LANGCHAIN_API_KEY=your_langsmith_api_key_here
Option 1: Run the complete application
python main.pyOption 2: Run only the Streamlit interface
streamlit run client.pyThe application will open in your default browser at http://localhost:8501
- Start a Conversation: Type your question in natural language
- View Results: See both the answer and the SQL query that was generated
- Try Samples: Use pre-built sample questions from the sidebar
- Track Usage: Monitor your query count and chat history
The following examples are designed for a library database system:
"Show me all books in the database"
"What's the average price of books by category?"
"Find the top 5 most expensive books"
"Count how many books were published after 2020"
"List all authors who have written more than 3 books"
"Show me books by a specific author"
"Find books in the 'Fiction' category"
"What are the cheapest books available?"
"Show me recently added books to the library"
"List books that are currently available for borrowing"
Note: These queries are customized for a library database schema with tables for books, authors, categories, etc.
sql-query-assistant/
βββ main.py # Main application entry point
βββ client.py # Streamlit interface
βββ chain.py # LangChain SQL chain configuration
βββ config.py # Database configuration
|ββ prompt_template.py # prompt templates
βββ requirements.txt # Python dependencies
βββ .env # Environment variables (not in repo)
βββ .gitignore # Git ignore rules
βββ README.md # Project documentation
Edit config.py to configure your MySQL database connection:
db_config = {
"host": "localhost", # or "127.0.0.1" or your database URL
"user": "your_username", # your MySQL username
"password": "your_password", # your MySQL password
"database": "your_db_name", # your database name
"port": 3306 # default MySQL port
}This project uses LangSmith for monitoring and debugging AI chains:
- Tracing: Monitor LLM calls and chain execution
- Debugging: Track performance and identify issues
- Analytics: View usage patterns and metrics
Set up in your .env file:
LANGCHAIN_TRACING_V2=true
LANGCHAIN_API_KEY=your_langsmith_api_key_hereThe application uses customizable prompt templates for:
- Query Generation: Converting natural language to SQL
- Response Formatting: Formatting results with markdown
Update config.py for different database setups:
# Example for different environments
db_config = {
"host": "your-production-host.com", # Change for production
"user": "prod_user", # Production username
"password": "secure_password", # Secure production password
"database": "production_db", # Production database name
"port": 3306
}The application uses minimal CSS for clean aesthetics. Modify the CSS in client.py to customize the appearance.
- Converts English questions to SQL queries
- Handles complex queries with joins, aggregations, and filtering
- Supports various SQL operations (SELECT, COUNT, SUM, AVG, etc.)
- Real-time conversation flow
- Message timestamps
- Clean, readable formatting
- Syntax-highlighted SQL queries
- Graceful error messages for invalid queries
- Connection issue detection
- User-friendly error explanations
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the 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.
- LangChain for the SQL chain functionality
- Streamlit for the beautiful web interface
- Groq for fast AI inference
- LangSmith for AI monitoring and debugging
- MySQL for reliable database operations
- OpenAI for inspiration from ChatGPT's interface
Database Connection Errors
Check your db_config in config.py file:
- Verify MySQL host, username, and password
- Ensure database exists and is accessible
- Check if MySQL service is running
API Key Issues
Ensure GROQ_API_KEY is set in .env file
Add LANGCHAIN_API_KEY for LangSmith tracing
Check API key validity and quota
Import Errors
Activate virtual environment: source env/bin/activate
Install requirements: pip install -r requirements.txt
Check Python version (3.8+ required)
LangSmith Tracing Issues
Verify LANGCHAIN_TRACING_V2=true in .env
Check LANGCHAIN_API_KEY is valid
Visit LangSmith dashboard to confirm setup
Built with β€οΈ using Python, Streamlit, and LangChain