# 10div10/sql-copilot architecture

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

- Interactive diagram: https://gitdiagram.com/10div10/sql-copilot
- Repository: https://github.com/10div10/sql-copilot

## Overview

SQL Copilot turns a user’s question into validated, read-only SQL and a plain-language answer. Users first upload a CSV or connect a database; the API creates a session and indexes its schema. For queries, it supplies either the full schema or retrieved table context to SQL generation, validates the result, executes it, optionally retries after an execution error, and explains the returned rows. The graph reflects the README and sampled backend source; frontend implementation details were not sampled, so its documented API relationship is cited to the README.

Main entry points: [`backend/main.py`](https://github.com/10div10/sql-copilot/blob/main/backend/main.py) (FastAPI endpoints), [`backend/db.py`](https://github.com/10div10/sql-copilot/blob/main/backend/db.py) (Engine setup), [`backend/schema_store.py`](https://github.com/10div10/sql-copilot/blob/main/backend/schema_store.py) (Schema indexing), [`frontend/index.html`](https://github.com/10div10/sql-copilot/blob/main/frontend/index.html) (Browser UI)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_interface["User interface"]
  node_frontend["Browser UI<br/>[index.html]"]
  node_api["FastAPI endpoints<br/>[main.py]"]
end

subgraph group_sessions["Data sessions"]
  node_csv_loader["CSV loader<br/>[db.py]"]
  node_engine_factory["Engine setup<br/>[db.py]"]
  node_database[("Session database")]
end

subgraph group_query["Query pipeline"]
  node_prompt_context["Schema context<br/>[main.py]"]
  node_sql_guard["SQL validation<br/>[sql_guard.py]"]
  node_query_execution["Query execution<br/>[main.py]"]
end

subgraph group_intelligence["Schema and language"]
  node_schema_index["Schema indexing<br/>[schema_store.py]"]
  node_schema_store[("Chroma schema index<br/>[schema_store.py]")]
  node_retrieval["Table retrieval<br/>[schema_store.py]"]
  node_sql_generation["SQL generation<br/>[llm.py]"]
  node_sql_correction["SQL correction<br/>[llm.py]"]
  node_result_explanation["Result explanation<br/>[llm.py]"]
end

node_user(("User"))
node_groq["Groq LLM"]
node_db_service[("Connected database")]

node_user -->|"asks or uploads"| node_frontend
node_frontend -->|"calls API"| node_api
node_api -->|"returns results"| node_frontend
node_api -->|"loads CSV"| node_csv_loader
node_api -->|"creates engine"| node_engine_factory
node_csv_loader -->|"writes dataset"| node_database
node_engine_factory -->|"opens session DB"| node_database
node_engine_factory -->|"connects"| node_db_service
node_api -->|"builds schema index"| node_schema_index
node_schema_index -->|"introspects and samples"| node_database
node_schema_index -->|"stores table docs"| node_schema_store
node_api -->|"retrieves tables"| node_retrieval
node_retrieval -->|"queries index"| node_schema_store
node_api -->|"builds context"| node_prompt_context
node_retrieval -->|"supplies relevant docs"| node_prompt_context
node_prompt_context -->|"provides schema"| node_sql_generation
node_api -->|"requests SQL"| node_sql_generation
node_sql_generation -->|"generates SQL"| node_groq
node_api -->|"validates SQL"| node_sql_guard
node_api -->|"executes safe SQL"| node_query_execution
node_query_execution -->|"reads rows"| node_database
node_query_execution -->|"requests retry SQL"| node_sql_correction
node_sql_correction -->|"corrects SQL"| node_groq
node_api -->|"requests answer"| node_result_explanation
node_result_explanation -->|"explains results"| node_groq

click node_frontend "https://github.com/10div10/sql-copilot/blob/main/frontend/index.html"
click node_api "https://github.com/10div10/sql-copilot/blob/main/backend/main.py"
click node_csv_loader "https://github.com/10div10/sql-copilot/blob/main/backend/db.py"
click node_engine_factory "https://github.com/10div10/sql-copilot/blob/main/backend/db.py"
click node_schema_index "https://github.com/10div10/sql-copilot/blob/main/backend/schema_store.py"
click node_schema_store "https://github.com/10div10/sql-copilot/blob/main/backend/schema_store.py"
click node_retrieval "https://github.com/10div10/sql-copilot/blob/main/backend/schema_store.py"
click node_prompt_context "https://github.com/10div10/sql-copilot/blob/main/backend/main.py"
click node_sql_generation "https://github.com/10div10/sql-copilot/blob/main/backend/llm.py"
click node_sql_guard "https://github.com/10div10/sql-copilot/blob/main/backend/sql_guard.py"
click node_query_execution "https://github.com/10div10/sql-copilot/blob/main/backend/main.py"
click node_sql_correction "https://github.com/10div10/sql-copilot/blob/main/backend/llm.py"
click node_result_explanation "https://github.com/10div10/sql-copilot/blob/main/backend/llm.py"

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_frontend,node_api,node_user toneBlue
class node_csv_loader,node_engine_factory,node_database,node_db_service toneAmber
class node_prompt_context,node_sql_guard,node_query_execution toneMint
class node_schema_index,node_schema_store,node_retrieval,node_sql_generation,node_sql_correction,node_result_explanation toneRose
class node_groq toneIndigo
```

## Components

### User interface

- [Browser UI](https://github.com/10div10/sql-copilot/blob/main/frontend/index.html): `frontend/index.html`
- [FastAPI endpoints](https://github.com/10div10/sql-copilot/blob/main/backend/main.py): `backend/main.py`

### Data sessions

- [CSV loader](https://github.com/10div10/sql-copilot/blob/main/backend/db.py): `backend/db.py`
- [Engine setup](https://github.com/10div10/sql-copilot/blob/main/backend/db.py): `backend/db.py`
- **Session database**

### Query pipeline

- [Schema context](https://github.com/10div10/sql-copilot/blob/main/backend/main.py): `backend/main.py`
- [SQL validation](https://github.com/10div10/sql-copilot/blob/main/backend/sql_guard.py): `backend/sql_guard.py`
- [Query execution](https://github.com/10div10/sql-copilot/blob/main/backend/main.py): `backend/main.py`

### Schema and language

- [Schema indexing](https://github.com/10div10/sql-copilot/blob/main/backend/schema_store.py): `backend/schema_store.py`
- [Chroma schema index](https://github.com/10div10/sql-copilot/blob/main/backend/schema_store.py): `backend/schema_store.py`
- [Table retrieval](https://github.com/10div10/sql-copilot/blob/main/backend/schema_store.py): `backend/schema_store.py`
- [SQL generation](https://github.com/10div10/sql-copilot/blob/main/backend/llm.py): `backend/llm.py`
- [SQL correction](https://github.com/10div10/sql-copilot/blob/main/backend/llm.py): `backend/llm.py`
- [Result explanation](https://github.com/10div10/sql-copilot/blob/main/backend/llm.py): `backend/llm.py`

### Other components

- **User**
- **Groq LLM**
- **Connected database**

## Connections

- User → Browser UI: asks or uploads (evidence: `README.md`)
- Browser UI → FastAPI endpoints: calls API (evidence: `README.md`)
- FastAPI endpoints → Browser UI: returns results (evidence: `README.md`)
- FastAPI endpoints → CSV loader: loads CSV (evidence: `backend/main.py`)
- FastAPI endpoints → Engine setup: creates engine (evidence: `backend/main.py`)
- CSV loader → Session database: writes dataset (evidence: `backend/db.py`)
- Engine setup → Session database: opens session DB (evidence: `backend/db.py`)
- Engine setup → Connected database: connects (evidence: `backend/db.py`)
- FastAPI endpoints → Schema indexing: builds schema index (evidence: `backend/main.py`)
- Schema indexing → Session database: introspects and samples (evidence: `backend/schema_store.py`)
- Schema indexing → Chroma schema index: stores table docs (evidence: `backend/schema_store.py`)
- FastAPI endpoints → Table retrieval: retrieves tables (evidence: `backend/main.py`)
- Table retrieval → Chroma schema index: queries index (evidence: `backend/schema_store.py`)
- FastAPI endpoints → Schema context: builds context (evidence: `backend/main.py`)
- Table retrieval → Schema context: supplies relevant docs (evidence: `backend/main.py`)
- Schema context → SQL generation: provides schema (evidence: `backend/main.py`)
- FastAPI endpoints → SQL generation: requests SQL (evidence: `backend/main.py`)
- SQL generation → Groq LLM: generates SQL (evidence: `backend/llm.py`)
- FastAPI endpoints → SQL validation: validates SQL (evidence: `backend/main.py`)
- FastAPI endpoints → Query execution: executes safe SQL (evidence: `backend/main.py`)
- Query execution → Session database: reads rows (evidence: `backend/main.py`)
- Query execution → SQL correction: requests retry SQL (evidence: `backend/main.py`)
- SQL correction → Groq LLM: corrects SQL (evidence: `backend/llm.py`)
- FastAPI endpoints → Result explanation: requests answer (evidence: `backend/main.py`)
- Result explanation → Groq LLM: explains results (evidence: `backend/llm.py`)
