# tclee/sql-rag architecture

> Architecture diagram of the GitHub repository tclee/sql-rag, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-09-02). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tclee/sql-rag
- Repository: https://github.com/tclee/sql-rag

## Overview

##### 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/

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph "User Interface"
        UI["Jupyter Notebook UI"]:::user
    end
    UI -->|"NL Question"| Env["Environment (.env & environment.yml)"]:::env
    UI -->|"NL Question"| PromptStep["Load Prompt + Examples"]:::prompt

    subgraph "Prompt & Tooling Layer"
        Prompts["Prompt Templates"]:::prompt
        ExamplesJson["Few-shot Examples"]:::prompt
        ExemplConv["Example Conversion Notebook"]:::prompt
        RawSQL["Raw SQL Examples"]:::prompt
    end
    PromptStep --> Prompts
    PromptStep --> ExamplesJson
    PromptStep --> ExemplConv
    PromptStep --> RawSQL
    Prompts -->|"Prompt + Examples"| LangChain
    ExamplesJson -->|"Prompt + Examples"| LangChain
    ExemplConv -->|"Prompt + Examples"| LangChain
    RawSQL -->|"Prompt + Examples"| LangChain
    Prompts -->|"Prompt + Examples"| LangGraph
    ExamplesJson -->|"Prompt + Examples"| LangGraph
    ExemplConv -->|"Prompt + Examples"| LangGraph
    RawSQL -->|"Prompt + Examples"| LangGraph

    subgraph "Observability (LangSmith)"[dashed]
        subgraph "LLM Orchestration"
            LangChain["LangChain Pipeline"]:::orchestration
            LangGraph["LangGraph Agent"]:::orchestration
            Gemini["Google Gemini API"]:::orchestration
        end
    end

    LangChain -->|"SQL Query"| SQLTool["SQL Tool"]:::tool
    LangGraph -->|"SQL Query"| SQLTool
    SQLTool -->|"Execute SQL"| DB["SQLite DB"]:::storage
    DB -->|"Query Results"| LangChain
    DB -->|"Query Results"| LangGraph
    LangChain -->|"Final Answer"| UI
    LangGraph -->|"Final Answer"| UI

    subgraph "Data Storage"
        DBScript["DB Schema Script"]:::storage
    end
    DBScript

    subgraph "Development Environment"
        Documentation["Documentation (README)"]:::docs
    end

    classDef user fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef prompt fill:#90EE90,stroke:#333,stroke-width:1px
    classDef orchestration fill:#32CD32,stroke:#333,stroke-width:1px
    classDef tool fill:#FFA500,stroke:#333,stroke-width:1px
    classDef storage fill:#D3D3D3,stroke:#333,stroke-width:1px
    classDef env fill:#F5F5F5,stroke:#333,stroke-width:1px
    classDef docs fill:#E0E0E0,stroke:#333,stroke-width:1px

    click UI "https://github.com/tclee/sql-rag/blob/main/sql-rag.ipynb"
    click Prompts "https://github.com/tclee/sql-rag/blob/main/prompts/system-prompt-template.txt"
    click ExamplesJson "https://github.com/tclee/sql-rag/blob/main/examples/examples.json"
    click ExemplConv "https://github.com/tclee/sql-rag/blob/main/examples/examples-to-json.ipynb"
    click RawSQL "https://github.com/tclee/sql-rag/blob/main/examples/examples.sql"
    click DB "https://github.com/tclee/sql-rag/blob/main/db/Chinook.db"
    click DBScript "https://github.com/tclee/sql-rag/blob/main/db/Chinook_Sqlite_AutoIncrementPKs.sql"
    click Env "https://github.com/tclee/sql-rag/blob/main/environment.yml"
    click Documentation "https://github.com/tclee/sql-rag/blob/main/README.md"
```
