Cubert23/sql-query-buddy
๐ค SQL Query Buddy
A conversational AI agent that converts natural language questions into SQL queries, executes them, and provides AI-driven insights.
Built with LangChain, FAISS VectorDB, and Gradio for the Codecademy GenAI & Agents Bootcamp Contest.
๐ฏ Features
- Natural Language to SQL - Ask questions in plain English, get accurate SQL queries
- RAG-Powered - Uses vector database to semantically search table schemas
- SQL Explanations - Beginner-friendly explanations of what each query does
- AI Insights - Analyzes query results and provides business insights
- Conversation Memory - Remembers context for follow-up questions
- Clean Chat Interface - Interactive Gradio UI for easy interaction
๐ ๏ธ Tech Stack
๐ฆ Installation
1. Clone the repository
git clone https://github.com/yourusername/sql-query-buddy.git
cd sql-query-buddy2. Create virtual environment
python -m venv .venv
source .venv/bin/activate # Mac/Linux
# OR
.venv\Scripts\activate # Windows3. Install dependencies
pip install langchain langchain-openai langchain-community python-dotenv sqlalchemy faiss-cpu gradio4. Set up environment variables
Create a .env file in the project root:
OPENAI_API_KEY=your_openai_api_key_here5. Initialize the database
cd database
python setup_database.py
cd ..๐ Usage
Run the application
python app.pyThis will launch the Gradio interface in your browser.
Example Questions
- "Show me all customers from California"
- "What are the top 3 customers by total spending?"
- "Which product category generates the most revenue?"
- "Show total sales per region for 2025"
- "How many unique products were sold in January?"
- "Show the trend of monthly revenue over time"
- "Find the average order value for returning customers"
๐๏ธ Database Schema
The project uses a retail commerce database with 4 tables:
customers - Customer information (name, email, region, signup date)
products - Product catalog (name, category, price)
orders - Order records (customer, date, total amount)
order_items - Line items (links orders to products with quantity)
๐ง How It Works
- User asks a question in natural language
- Vector search finds relevant table schemas from FAISS database
- LLM generates SQL using the retrieved schema context
- Query executes against the SQLite database
- AI analyzes results and provides insights
- Conversation saved to memory for follow-up questions
๐ Key Components
sql_agent.py
create_schema_documents()- Creates searchable documents for each tablecreate_vector_store()- Embeds schemas in FAISS vector databaseget_relevant_schemas()- Retrieves relevant tables based on questionask_question()- Main RAG pipeline: retrieve โ generate โ execute โ insight
app.py
- Gradio chat interface
- Integrates with sql_agent functions
- Displays SQL, results, and insights
๐ค Author
Rubin - Software Engineer Codecademy GenAI & Agents Bootcamp Participant
๐ท๏ธ Tags
#CodecademyGenAIBootcamp #RAG #LangChain #VectorDB #SQL #AI #Gradio
