# abhinavvikram333/nl_to_sql_rag architecture

> Architecture diagram of the GitHub repository abhinavvikram333/nl_to_sql_rag, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/abhinavvikram333/nl_to_sql_rag
- Repository: https://github.com/abhinavvikram333/nl_to_sql_rag

## Overview

#### Purpose and Entry Point
This repository is a natural-language-to-SQL prototype for an expense-oriented dimensional data warehouse. The executable workflow is concentrated in the notebook `NL_TO_SQL_PROJECT_API/FINAL_CODE.ipynb`; no standalone application server or README-defined CLI/API entry point is present.

#### Warehouse Schema
The SQL schema defines a star-style model used as the target for generated queries:
- Fact table: `NL_TO_SQL_PROJECT_API/SCHEMA/FACT_EXP.sql`
- Dimensions: `NL_TO_SQL_PROJECT_API/SCHEMA/DIM_DATE.sql`, `NL_TO_SQL_PROJECT_API/SCHEMA/DIM_EMPLOYEE.sql`, `NL_TO_SQL_PROJECT_API/SCHEMA/DIM_CATEGORY.sql`
- Database/table setup and seed-oriented creation script: `NL_TO_SQL_PROJECT_API/SCHEMA/TABLE DATA BASE CREATION.sql`

Natural-language requests should be translated against this schema, with joins and aggregations centered on expense facts and date, employee, and category attributes.

#### RAG Knowledge Layer
Retrieval-augmented generation grounds SQL creation in domain documentation rather than relying only on model knowledge:
- Schema semantics: `NL_TO_SQL_PROJECT_API/rag_data/SCHEMA_DESCRIPTION.txt`
- Business constraints and interpretation rules: `NL_TO_SQL_PROJECT_API/rag_data/BUSINESS RULES.txt`
- SQL patterns/examples: `NL_TO_SQL_PROJECT_API/rag_data/SQL_EXAMPLES.txt`

These documents form the authoritative prompt context for interpreting user intent, selecting tables/columns, and applying business logic.

#### Vector Retrieval Storage
A persisted Chroma-style vector database stores embeddings of the RAG corpus:
- Metadata/database: `NL_TO_SQL_PROJECT_API/vector_db/chroma.sqlite3`
- Vector index segment: `NL_TO_SQL_PROJECT_API/vector_db/3745870e-ab02-4fd5-bb7c-337d74122f92/data_level0.bin`
- Graph/index links: `NL_TO_SQL_PROJECT_API/vector_db/3745870e-ab02-4fd5-bb7c-337d74122f92/link_lists.bin`

The notebook likely opens this local store, retrieves relevant schema/rule/example chunks for an incoming question, and incorporates them into the SQL-generation context. The committed binary index is an operational dependency unless the notebook includes re-ingestion/re-embedding logic.

#### Primary Control Flow
1. Establish access to the expense warehouse schema and local vector store.
2. Receive or define a natural-language analytical question in `NL_TO_SQL_PROJECT_API/FINAL_CODE.ipynb`.
3. Embed/query the question against the persisted vector database.
4. Combine retrieved documentation with the question in an LLM prompt.
5. Generate SQL targeting the fact/dimension model.
6. Potentially execute, inspect, or refine the SQL in the notebook workflow.

The key boundary is between untrusted/generated SQL and the target database: generated statements should be validated and restricted before execution, especially if this prototype is promoted into an API.

#### Runtime and External Dependencies
Python notebook dependencies are declared in `NL_TO_SQL_PROJECT_API/requirements.txt`. Material runtime services are an LLM/embedding provider (not identifiable from the tree), a local Chroma-compatible vector database, and the relational database hosting the scripts in `NL_TO_SQL_PROJECT_API/SCHEMA/`. Credentials, deployment configuration, and a production request-serving layer are not represented in the repository tree.

Main entry points: [`NL_TO_SQL_PROJECT_API/FINAL_CODE.ipynb`](https://github.com/abhinavvikram333/nl_to_sql_rag/blob/main/NL_TO_SQL_PROJECT_API/FINAL_CODE.ipynb) (NL-to-SQL workflow), [`NL_TO_SQL_PROJECT_API/vector_db/chroma.sqlite3`](https://github.com/abhinavvikram333/nl_to_sql_rag/blob/main/NL_TO_SQL_PROJECT_API/vector_db/chroma.sqlite3) (Persisted vector store), [`NL_TO_SQL_PROJECT_API/SCHEMA/FACT_EXP.sql`](https://github.com/abhinavvikram333/nl_to_sql_rag/blob/main/NL_TO_SQL_PROJECT_API/SCHEMA/FACT_EXP.sql) (Expense fact), [`NL_TO_SQL_PROJECT_API/rag_data/SCHEMA_DESCRIPTION.txt`](https://github.com/abhinavvikram333/nl_to_sql_rag/blob/main/NL_TO_SQL_PROJECT_API/rag_data/SCHEMA_DESCRIPTION.txt) (Schema semantics)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_notebook["Notebook workflow"]
  node_workflow["NL-to-SQL workflow<br/>Python notebook<br/>[FINAL_CODE.ipynb]"]
  node_question["Analytical question<br/>natural-language input"]
  node_retrieval["Context retrieval<br/>RAG stage<br/>[FINAL_CODE.ipynb]"]
  node_sql_generation["SQL generation<br/>LLM prompt stage<br/>[FINAL_CODE.ipynb]"]
  node_execution["SQL inspection or execution<br/>notebook stage<br/>[FINAL_CODE.ipynb]"]
end

subgraph group_knowledge["RAG knowledge"]
  node_schema_docs["Schema semantics<br/>RAG document"]
  node_business_rules["Business rules<br/>RAG document<br/>[BUSINESS RULES.txt]"]
  node_sql_examples["SQL examples<br/>RAG document<br/>[SQL_EXAMPLES.txt]"]
  node_vector_store[("Persisted vector store<br/>Chroma-compatible store<br/>[chroma.sqlite3]")]
  node_vector_index["Vector index segment<br/>binary index<br/>[data_level0.bin]"]
end

subgraph group_warehouse["Expense warehouse"]
  node_expense_fact[("Expense fact<br/>fact table<br/>[FACT_EXP.sql]")]
  node_date_dimension["Date dimension<br/>dimension table<br/>[DIM_DATE.sql]"]
  node_employee_dimension["Employee dimension<br/>dimension table<br/>[DIM_EMPLOYEE.sql]"]
  node_category_dimension["Category dimension<br/>dimension table<br/>[DIM_CATEGORY.sql]"]
  node_warehouse_setup["Warehouse setup<br/>database creation script"]
  node_relational_db[("Expense warehouse database<br/>relational database")]
end

subgraph group_runtime["Runtime dependencies"]
  node_llm_embedding_provider{{"LLM and embedding provider<br/>external AI service"}}
  node_python_dependencies["Notebook dependencies<br/>Python requirements<br/>[requirements.txt]"]
end

node_question -->|"defined in"| node_workflow
node_workflow -->|"orchestrates"| node_retrieval
node_workflow -->|"orchestrates"| node_sql_generation
node_workflow -->|"orchestrates"| node_execution
node_retrieval -->|"embeds/query"| node_llm_embedding_provider
node_retrieval -->|"searches"| node_vector_store
node_vector_store -->|"uses"| node_vector_index
node_schema_docs -.->|"indexed as context"| node_vector_store
node_business_rules -.->|"indexed as context"| node_vector_store
node_sql_examples -.->|"indexed as context"| node_vector_store
node_retrieval -->|"retrieved context"| node_sql_generation
node_sql_generation -->|"generates with"| node_llm_embedding_provider
node_sql_generation -->|"generated SQL"| node_execution
node_execution -->|"untrusted SQL boundary"| node_relational_db
node_expense_fact -.->|"defined in"| node_relational_db
node_date_dimension -->|"joins by date key"| node_expense_fact
node_employee_dimension -->|"joins by employee key"| node_expense_fact
node_category_dimension -->|"joins by category key"| node_expense_fact
node_warehouse_setup -->|"creates and seeds"| node_relational_db
node_python_dependencies -.->|"supports runtime"| node_workflow

click node_workflow "https://github.com/abhinavvikram333/nl_to_sql_rag/blob/main/NL_TO_SQL_PROJECT_API/FINAL_CODE.ipynb"
click node_retrieval "https://github.com/abhinavvikram333/nl_to_sql_rag/blob/main/NL_TO_SQL_PROJECT_API/FINAL_CODE.ipynb"
click node_sql_generation "https://github.com/abhinavvikram333/nl_to_sql_rag/blob/main/NL_TO_SQL_PROJECT_API/FINAL_CODE.ipynb"
click node_execution "https://github.com/abhinavvikram333/nl_to_sql_rag/blob/main/NL_TO_SQL_PROJECT_API/FINAL_CODE.ipynb"
click node_schema_docs "https://github.com/abhinavvikram333/nl_to_sql_rag/blob/main/NL_TO_SQL_PROJECT_API/rag_data/SCHEMA_DESCRIPTION.txt"
click node_business_rules "https://github.com/abhinavvikram333/nl_to_sql_rag/blob/main/NL_TO_SQL_PROJECT_API/rag_data/BUSINESS%20RULES.txt"
click node_sql_examples "https://github.com/abhinavvikram333/nl_to_sql_rag/blob/main/NL_TO_SQL_PROJECT_API/rag_data/SQL_EXAMPLES.txt"
click node_vector_store "https://github.com/abhinavvikram333/nl_to_sql_rag/blob/main/NL_TO_SQL_PROJECT_API/vector_db/chroma.sqlite3"
click node_vector_index "https://github.com/abhinavvikram333/nl_to_sql_rag/blob/main/NL_TO_SQL_PROJECT_API/vector_db/3745870e-ab02-4fd5-bb7c-337d74122f92/data_level0.bin"
click node_expense_fact "https://github.com/abhinavvikram333/nl_to_sql_rag/blob/main/NL_TO_SQL_PROJECT_API/SCHEMA/FACT_EXP.sql"
click node_date_dimension "https://github.com/abhinavvikram333/nl_to_sql_rag/blob/main/NL_TO_SQL_PROJECT_API/SCHEMA/DIM_DATE.sql"
click node_employee_dimension "https://github.com/abhinavvikram333/nl_to_sql_rag/blob/main/NL_TO_SQL_PROJECT_API/SCHEMA/DIM_EMPLOYEE.sql"
click node_category_dimension "https://github.com/abhinavvikram333/nl_to_sql_rag/blob/main/NL_TO_SQL_PROJECT_API/SCHEMA/DIM_CATEGORY.sql"
click node_warehouse_setup "https://github.com/abhinavvikram333/nl_to_sql_rag/blob/main/NL_TO_SQL_PROJECT_API/SCHEMA/TABLE%20DATA%20BASE%20CREATION.sql"
click node_python_dependencies "https://github.com/abhinavvikram333/nl_to_sql_rag/blob/main/NL_TO_SQL_PROJECT_API/requirements.txt"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_workflow,node_question,node_retrieval,node_sql_generation,node_execution toneBlue
class node_schema_docs,node_business_rules,node_sql_examples,node_vector_store,node_vector_index toneAmber
class node_expense_fact,node_date_dimension,node_employee_dimension,node_category_dimension,node_warehouse_setup,node_relational_db toneMint
class node_llm_embedding_provider,node_python_dependencies toneRose
```

## Components

### Notebook workflow

- [NL-to-SQL workflow](https://github.com/abhinavvikram333/nl_to_sql_rag/blob/main/NL_TO_SQL_PROJECT_API/FINAL_CODE.ipynb): Python notebook · Primary executable entry point. · `NL_TO_SQL_PROJECT_API/FINAL_CODE.ipynb`
- **Analytical question**: natural-language input
- [Context retrieval](https://github.com/abhinavvikram333/nl_to_sql_rag/blob/main/NL_TO_SQL_PROJECT_API/FINAL_CODE.ipynb): RAG stage · Embeds the question and retrieves grounding context. · `NL_TO_SQL_PROJECT_API/FINAL_CODE.ipynb`
- [SQL generation](https://github.com/abhinavvikram333/nl_to_sql_rag/blob/main/NL_TO_SQL_PROJECT_API/FINAL_CODE.ipynb): LLM prompt stage · Combines question and retrieved context into warehouse SQL. · `NL_TO_SQL_PROJECT_API/FINAL_CODE.ipynb`
- [SQL inspection or execution](https://github.com/abhinavvikram333/nl_to_sql_rag/blob/main/NL_TO_SQL_PROJECT_API/FINAL_CODE.ipynb): notebook stage · Generated SQL may be reviewed, run, or refined. · `NL_TO_SQL_PROJECT_API/FINAL_CODE.ipynb`

### RAG knowledge

- [Schema semantics](https://github.com/abhinavvikram333/nl_to_sql_rag/blob/main/NL_TO_SQL_PROJECT_API/rag_data/SCHEMA_DESCRIPTION.txt): RAG document · `NL_TO_SQL_PROJECT_API/rag_data/SCHEMA_DESCRIPTION.txt`
- [Business rules](https://github.com/abhinavvikram333/nl_to_sql_rag/blob/main/NL_TO_SQL_PROJECT_API/rag_data/BUSINESS%20RULES.txt): RAG document · `NL_TO_SQL_PROJECT_API/rag_data/BUSINESS RULES.txt`
- [SQL examples](https://github.com/abhinavvikram333/nl_to_sql_rag/blob/main/NL_TO_SQL_PROJECT_API/rag_data/SQL_EXAMPLES.txt): RAG document · `NL_TO_SQL_PROJECT_API/rag_data/SQL_EXAMPLES.txt`
- [Persisted vector store](https://github.com/abhinavvikram333/nl_to_sql_rag/blob/main/NL_TO_SQL_PROJECT_API/vector_db/chroma.sqlite3): Chroma-compatible store · Local embeddings and retrieval index for the RAG corpus. · `NL_TO_SQL_PROJECT_API/vector_db/chroma.sqlite3`
- [Vector index segment](https://github.com/abhinavvikram333/nl_to_sql_rag/blob/main/NL_TO_SQL_PROJECT_API/vector_db/3745870e-ab02-4fd5-bb7c-337d74122f92/data_level0.bin): binary index · `NL_TO_SQL_PROJECT_API/vector_db/3745870e-ab02-4fd5-bb7c-337d74122f92/data_level0.bin`

### Expense warehouse

- [Expense fact](https://github.com/abhinavvikram333/nl_to_sql_rag/blob/main/NL_TO_SQL_PROJECT_API/SCHEMA/FACT_EXP.sql): fact table · Central expense measures and dimensional keys. · `NL_TO_SQL_PROJECT_API/SCHEMA/FACT_EXP.sql`
- [Date dimension](https://github.com/abhinavvikram333/nl_to_sql_rag/blob/main/NL_TO_SQL_PROJECT_API/SCHEMA/DIM_DATE.sql): dimension table · `NL_TO_SQL_PROJECT_API/SCHEMA/DIM_DATE.sql`
- [Employee dimension](https://github.com/abhinavvikram333/nl_to_sql_rag/blob/main/NL_TO_SQL_PROJECT_API/SCHEMA/DIM_EMPLOYEE.sql): dimension table · `NL_TO_SQL_PROJECT_API/SCHEMA/DIM_EMPLOYEE.sql`
- [Category dimension](https://github.com/abhinavvikram333/nl_to_sql_rag/blob/main/NL_TO_SQL_PROJECT_API/SCHEMA/DIM_CATEGORY.sql): dimension table · `NL_TO_SQL_PROJECT_API/SCHEMA/DIM_CATEGORY.sql`
- [Warehouse setup](https://github.com/abhinavvikram333/nl_to_sql_rag/blob/main/NL_TO_SQL_PROJECT_API/SCHEMA/TABLE%20DATA%20BASE%20CREATION.sql): database creation script · Creates and seeds the warehouse tables. · `NL_TO_SQL_PROJECT_API/SCHEMA/TABLE DATA BASE CREATION.sql`
- **Expense warehouse database**: relational database · Target database for generated SQL.

### Runtime dependencies

- **LLM and embedding provider**: external AI service
- [Notebook dependencies](https://github.com/abhinavvikram333/nl_to_sql_rag/blob/main/NL_TO_SQL_PROJECT_API/requirements.txt): Python requirements · `NL_TO_SQL_PROJECT_API/requirements.txt`

## Connections

- Analytical question → NL-to-SQL workflow: defined in
- NL-to-SQL workflow → Context retrieval: orchestrates
- NL-to-SQL workflow → SQL generation: orchestrates
- NL-to-SQL workflow → SQL inspection or execution: orchestrates
- Context retrieval → LLM and embedding provider: embeds/query
- Context retrieval → Persisted vector store: searches
- Persisted vector store → Vector index segment: uses
- Schema semantics → Persisted vector store: indexed as context
- Business rules → Persisted vector store: indexed as context
- SQL examples → Persisted vector store: indexed as context
- Context retrieval → SQL generation: retrieved context
- SQL generation → LLM and embedding provider: generates with
- SQL generation → SQL inspection or execution: generated SQL
- SQL inspection or execution → Expense warehouse database: untrusted SQL boundary · Validate and restrict generated statements before execution.
- Expense fact → Expense warehouse database: defined in
- Date dimension → Expense fact: joins by date key
- Employee dimension → Expense fact: joins by employee key
- Category dimension → Expense fact: joins by category key
- Warehouse setup → Expense warehouse database: creates and seeds
- Notebook dependencies → NL-to-SQL workflow: supports runtime
