Skip to content

Latest commit

Β 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ” SQL Query Assistant

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.

✨ Features

  • πŸ—£οΈ 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

πŸ› οΈ Tech Stack

  • Frontend: Streamlit
  • AI/ML: LangChain, Groq
  • Database: MySQL
  • Monitoring: LangSmith (for tracing and debugging)
  • Language: Python 3.8+

πŸ“¦ Installation

Prerequisites

  • Python 3.8 or higher
  • pip package manager

Setup

  1. Clone the repository

    git clone <repository-url>
    cd sql-query-assistant
  2. Create virtual environment

    python -m venv env
    source env/bin/activate  # On Windows: env\Scripts\activate
  3. Install dependencies

    pip install -r requirements.txt
  4. Configure your database

    • Update the config.py file 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
    }
  5. 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

πŸš€ Usage

Running the Application

Option 1: Run the complete application

python main.py

Option 2: Run only the Streamlit interface

streamlit run client.py

The application will open in your default browser at http://localhost:8501

How to Use

  1. Start a Conversation: Type your question in natural language
  2. View Results: See both the answer and the SQL query that was generated
  3. Try Samples: Use pre-built sample questions from the sidebar
  4. Track Usage: Monitor your query count and chat history

Example Queries

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.

πŸ“ Project Structure

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

βš™οΈ Configuration

Database Setup

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
}

LangSmith Integration

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_here

Prompt Templates

The application uses customizable prompt templates for:

  • Query Generation: Converting natural language to SQL
  • Response Formatting: Formatting results with markdown

Database Configuration

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
}

Styling

The application uses minimal CSS for clean aesthetics. Modify the CSS in client.py to customize the appearance.

πŸ“Š Features in Detail

Natural Language Processing

  • Converts English questions to SQL queries
  • Handles complex queries with joins, aggregations, and filtering
  • Supports various SQL operations (SELECT, COUNT, SUM, AVG, etc.)

Chat Interface

  • Real-time conversation flow
  • Message timestamps
  • Clean, readable formatting
  • Syntax-highlighted SQL queries

Error Handling

  • Graceful error messages for invalid queries
  • Connection issue detection
  • User-friendly error explanations

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ™ Acknowledgments

  • 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

πŸ› Troubleshooting

Common Issues

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

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages