# hsmshubham007-code/text_to_sql architecture

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

- Interactive diagram: https://gitdiagram.com/hsmshubham007-code/text_to_sql
- Repository: https://github.com/hsmshubham007-code/text_to_sql

## Overview

This is a Streamlit natural-language interface for querying the Chinook music-store SQLite database through Groq. The principal flow is question submission, unsupported-topic refusal or SQL generation, schema context retrieval, SQL validation and bounded repair, read-only execution, and result display. The four documented database tools expose table listing, table inspection, schema overview, and SQL execution. The graph shows the tool registry and its database/safety layers separately; evaluation scripts and tests are excluded. Relationships are based on the README and sampled source; unsampled behavior is not inferred.

Main entry points: [`src/database_tools.py`](https://github.com/hsmshubham007-code/text_to_sql/blob/master/src/database_tools.py) (Database access), [`src/sql_generator.py`](https://github.com/hsmshubham007-code/text_to_sql/blob/master/src/sql_generator.py) (SQL generation), [`src/agent_tools.py`](https://github.com/hsmshubham007-code/text_to_sql/blob/master/src/agent_tools.py) (Tool registry), [`src/agent.py`](https://github.com/hsmshubham007-code/text_to_sql/blob/master/src/agent.py) (Agent response)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_interface["User interface"]
  node_streamlit["Question UI<br/>[app.py]"]
end

subgraph group_agent["Agent and generation"]
  node_agent_entry["Agent response<br/>[agent.py]"]
  node_sql_pipeline["SQL generation<br/>[sql_generator.py]"]
  node_prompt["Generation prompt<br/>[sql_generation.txt]"]
end

subgraph group_tools["Database tools"]
  node_tool_registry["Tool registry<br/>[agent_tools.py]"]
  node_table_list["Table listing<br/>[agent_tools.py]"]
  node_table_inspect["Table inspection<br/>[agent_tools.py]"]
  node_schema_overview["Schema overview<br/>[agent_tools.py]"]
  node_query_tool["SQL execution tool<br/>[agent_tools.py]"]
end

subgraph group_storage["Read-only storage"]
  node_db_access["Database access<br/>[database_tools.py]"]
  node_sql_safety["SQL safety<br/>[sql_safety.py]"]
  node_chinook[("Chinook SQLite<br/>[chinook.db]")]
end

node_user(("Business user"))
node_groq{{"Groq model"}}

node_user -->|"asks question"| node_streamlit
node_streamlit -->|"submits question"| node_agent_entry
node_agent_entry -->|"delegates query"| node_sql_pipeline
node_sql_pipeline -->|"requests SQL"| node_groq
node_sql_pipeline -->|"loads prompt"| node_prompt
node_sql_pipeline -->|"retrieves schema"| node_schema_overview
node_sql_pipeline -->|"executes query"| node_query_tool
node_tool_registry -->|"registers tool"| node_table_list
node_tool_registry -->|"registers tool"| node_table_inspect
node_tool_registry -->|"registers tool"| node_schema_overview
node_tool_registry -->|"registers tool"| node_query_tool
node_table_list -->|"lists tables"| node_db_access
node_table_inspect -->|"reads schema"| node_db_access
node_schema_overview -->|"collects schemas"| node_db_access
node_query_tool -->|"runs SQL"| node_db_access
node_db_access -->|"delegates execution"| node_sql_safety
node_db_access -->|"reads metadata"| node_chinook
node_sql_safety -->|"executes read-only"| node_chinook
node_agent_entry -->|"returns results"| node_streamlit

click node_streamlit "https://github.com/hsmshubham007-code/text_to_sql/blob/master/app.py"
click node_agent_entry "https://github.com/hsmshubham007-code/text_to_sql/blob/master/src/agent.py"
click node_sql_pipeline "https://github.com/hsmshubham007-code/text_to_sql/blob/master/src/sql_generator.py"
click node_prompt "https://github.com/hsmshubham007-code/text_to_sql/blob/master/prompts/sql_generation.txt"
click node_tool_registry "https://github.com/hsmshubham007-code/text_to_sql/blob/master/src/agent_tools.py"
click node_table_list "https://github.com/hsmshubham007-code/text_to_sql/blob/master/src/agent_tools.py"
click node_table_inspect "https://github.com/hsmshubham007-code/text_to_sql/blob/master/src/agent_tools.py"
click node_schema_overview "https://github.com/hsmshubham007-code/text_to_sql/blob/master/src/agent_tools.py"
click node_query_tool "https://github.com/hsmshubham007-code/text_to_sql/blob/master/src/agent_tools.py"
click node_db_access "https://github.com/hsmshubham007-code/text_to_sql/blob/master/src/database_tools.py"
click node_sql_safety "https://github.com/hsmshubham007-code/text_to_sql/blob/master/src/sql_safety.py"
click node_chinook "https://github.com/hsmshubham007-code/text_to_sql/blob/master/data/chinook.db"

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_streamlit,node_user toneBlue
class node_agent_entry,node_sql_pipeline,node_prompt toneAmber
class node_tool_registry,node_table_list,node_table_inspect,node_schema_overview,node_query_tool toneMint
class node_db_access,node_sql_safety,node_chinook toneRose
class node_groq toneIndigo
```

## Components

### User interface

- [Question UI](https://github.com/hsmshubham007-code/text_to_sql/blob/master/app.py): `app.py`

### Agent and generation

- [Agent response](https://github.com/hsmshubham007-code/text_to_sql/blob/master/src/agent.py): `src/agent.py`
- [SQL generation](https://github.com/hsmshubham007-code/text_to_sql/blob/master/src/sql_generator.py): `src/sql_generator.py`
- [Generation prompt](https://github.com/hsmshubham007-code/text_to_sql/blob/master/prompts/sql_generation.txt): `prompts/sql_generation.txt`

### Database tools

- [Tool registry](https://github.com/hsmshubham007-code/text_to_sql/blob/master/src/agent_tools.py): `src/agent_tools.py`
- [Table listing](https://github.com/hsmshubham007-code/text_to_sql/blob/master/src/agent_tools.py): `src/agent_tools.py`
- [Table inspection](https://github.com/hsmshubham007-code/text_to_sql/blob/master/src/agent_tools.py): `src/agent_tools.py`
- [Schema overview](https://github.com/hsmshubham007-code/text_to_sql/blob/master/src/agent_tools.py): `src/agent_tools.py`
- [SQL execution tool](https://github.com/hsmshubham007-code/text_to_sql/blob/master/src/agent_tools.py): `src/agent_tools.py`

### Read-only storage

- [Database access](https://github.com/hsmshubham007-code/text_to_sql/blob/master/src/database_tools.py): `src/database_tools.py`
- [SQL safety](https://github.com/hsmshubham007-code/text_to_sql/blob/master/src/sql_safety.py): `src/sql_safety.py`
- [Chinook SQLite](https://github.com/hsmshubham007-code/text_to_sql/blob/master/data/chinook.db): `data/chinook.db`

### Other components

- **Business user**
- **Groq model**

## Connections

- Business user → Question UI: asks question (evidence: `README.md`)
- Question UI → Agent response: submits question (evidence: `app.py`)
- Agent response → SQL generation: delegates query (evidence: `src/agent.py`)
- SQL generation → Groq model: requests SQL (evidence: `src/sql_generator.py`)
- SQL generation → Generation prompt: loads prompt (evidence: `src/sql_generator.py`)
- SQL generation → Schema overview: retrieves schema (evidence: `src/sql_generator.py`)
- SQL generation → SQL execution tool: executes query (evidence: `src/sql_generator.py`)
- Tool registry → Table listing: registers tool (evidence: `src/agent_tools.py`)
- Tool registry → Table inspection: registers tool (evidence: `src/agent_tools.py`)
- Tool registry → Schema overview: registers tool (evidence: `src/agent_tools.py`)
- Tool registry → SQL execution tool: registers tool (evidence: `src/agent_tools.py`)
- Table listing → Database access: lists tables (evidence: `src/agent_tools.py`)
- Table inspection → Database access: reads schema (evidence: `src/agent_tools.py`)
- Schema overview → Database access: collects schemas (evidence: `src/agent_tools.py`)
- SQL execution tool → Database access: runs SQL (evidence: `src/agent_tools.py`)
- Database access → SQL safety: delegates execution (evidence: `src/database_tools.py`)
- Database access → Chinook SQLite: reads metadata (evidence: `src/database_tools.py`)
- SQL safety → Chinook SQLite: executes read-only (evidence: `src/sql_safety.py`)
- Agent response → Question UI: returns results (evidence: `app.py`)
