CoolFace
Apppublic

Cubert23/sql-query-buddy

sourceHugging Faceupdated 10mo agoView on Hugging Face
0likes
App README

๐Ÿค– 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

ComponentTechnology
AI FrameworkLangChain
LLMOpenAI GPT-3.5
Vector DatabaseFAISS
EmbeddingsOpenAI Embeddings
DatabaseSQLite
FrontendGradio
LanguagePython 3.13

๐Ÿ“ฆ Installation

1. Clone the repository

bash
git clone https://github.com/yourusername/sql-query-buddy.git
cd sql-query-buddy

2. Create virtual environment

bash
python -m venv .venv
source .venv/bin/activate  # Mac/Linux
# OR
.venv\Scripts\activate     # Windows

3. Install dependencies

bash
pip install langchain langchain-openai langchain-community python-dotenv sqlalchemy faiss-cpu gradio

4. Set up environment variables

Create a .env file in the project root:

OPENAI_API_KEY=your_openai_api_key_here

5. Initialize the database

bash
cd database
python setup_database.py
cd ..

๐Ÿš€ Usage

Run the application

bash
python app.py

This 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

  1. 1.User asks a question in natural language
  1. 1.Vector search finds relevant table schemas from FAISS database
  1. 1.LLM generates SQL using the retrieved schema context
  1. 1.Query executes against the SQLite database
  1. 1.AI analyzes results and provides insights
  1. 1.Conversation saved to memory for follow-up questions

๐Ÿ”‘ Key Components

sql_agent.py

  • โ€”create_schema_documents() - Creates searchable documents for each table
  • โ€”create_vector_store() - Embeds schemas in FAISS vector database
  • โ€”get_relevant_schemas() - Retrieves relevant tables based on question
  • โ€”ask_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