How tclee/sql-rag works
Project Type and Purpose
• Type: A proof-of-concept “Retrieval-Augmented Generation” (RAG) application written as a Jupyter notebook.
• Purpose:
– Convert a user’s natural-language question into a SQL query
– Execute the SQL query against a sample SQLite database
– Synthesize an answer from the query results using a large language model (LLM)
File Structure
Top-level directories:
• db/ – contains Chinook.db (SQLite) + SQL script
• examples/ – few-shot SQL examples (JSON & notebook)
• prompts/ – prompt templates for LLM
• img/ – architecture illustration
Key files:
• sql-rag.ipynb – main notebook orchestrating the pipeline
• environment.yml – conda environment spec
• .env (user-created) – LLM/API keys
Extract Architectural Insights from the README
• Two parallel implementations:
a. LangChain “chain” approach
b. LangGraph “agent” approach
• Workflow stages (Figure 1 in img/sql_usecase.png):
1. Question → LLM → SQL
2. Execute SQL via tool
3. LLM synthesizes answer from results
1. User Interface
– Jupyter Lab notebook front-end
– Python cells capturing user question
2. Prompt & Tooling Layer
– Prompt templates (prompts/*.txt)
– Few-shot examples (examples/examples.json)
3. LLM Orchestration
– LangChain pipeline (Chain)
– LangGraph agent
– Google Gemini API client
4. SQL Execution Component
– SQLite engine (Chinook.db)
– SQL tool wrapper (via LangChain Tools/Agent Tools)
5. Data Storage
– db/Chinook.db (relational schema)
– SQL script for schema recreation
6. Optional Observability
– LangSmith tracing (via LANGSMITH_API_KEY)
7. Development Environment
– Conda environment (environment.yml)
– python-dotenv (.env)
Relationships and Data-Flow
• 1 → 2: User types question in notebook → Notebook loads prompt template + examples
• 2 → 3: Notebook calls LLM with prompt → LLM returns a SQL query
• 3 → 4: SQL query passed to SQL tool → SQLite executes query → returns result set
• 4 → 3: Result set fed back into LLM with a follow-up prompt → LLM synthesizes natural-language answer
• 3 → 1: Final answer rendered back in notebook
Architectural Patterns and Design Principles
• Pipeline pattern: sequential stages (LLM→Tool→LLM)
• Agent vs. Chain:
– Chain: fixed prompts + steps
– Agent: dynamic tool selection via “agent”
• Separation of concerns:
– Prompt management isolated in prompts/
– Example management in examples/
– Data layer isolated in db/