# ra-manuth/ai-agent-text_to_sql architecture

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

- Interactive diagram: https://gitdiagram.com/ra-manuth/ai-agent-text_to_sql
- Repository: https://github.com/ra-manuth/ai-agent-text_to_sql

## Overview

This repository is a command-line natural-language-to-SQL assistant with SQL Server and SQLite implementations. In the SQL Server workflow, the user’s question and detected schema go to Gemini; returned SQL is cleaned, checked, executed, and displayed, with schema refresh after qualifying DDL. The sampled `create_db.py` contains a separate SQLite assistant workflow and uses `sales.db`; despite the README describing `sqlite_assistant.py`, that file is absent from the provided tree. The graph reflects the sampled code and documented capabilities; no unsampled SQLite implementation is assumed.

Main entry points: [`sql_assistant.py`](https://github.com/ra-manuth/ai-agent-text_to_sql/blob/main/sql_assistant.py) (SQL Server CLI), [`create_db.py`](https://github.com/ra-manuth/ai-agent-text_to_sql/blob/main/create_db.py) (Question handler), [`sales.db`](https://github.com/ra-manuth/ai-agent-text_to_sql/blob/main/sales.db) (Sales database)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_sqlserver["SQL Server assistant"]
  node_sql_cli["SQL Server CLI<br/>[sql_assistant.py]"]
  node_schema["Schema detection<br/>[sql_assistant.py]"]
  node_sql_generation["SQL generation<br/>[sql_assistant.py]"]
  node_sql_cleaning["SQL cleaning<br/>[sql_assistant.py]"]
  node_sql_safety["Safety checks<br/>[sql_assistant.py]"]
  node_sql_execution["SQL execution<br/>[sql_assistant.py]"]
  node_sql_database[("SQL Server")]
end

subgraph group_sqlite["SQLite assistant"]
  node_sqlite_cli["SQLite CLI<br/>[create_db.py]"]
  node_sqlite_orchestrator["Question handler<br/>[create_db.py]"]
  node_sqlite_generation["SQL generation<br/>[create_db.py]"]
  node_sqlite_cleaning["SQL cleaning<br/>[create_db.py]"]
  node_sqlite_execution["SQL execution<br/>[create_db.py]"]
  node_sales_db[("Sales database<br/>[sales.db]")]
end

subgraph group_integrations["External services"]
  node_gemini["Gemini API"]
end

node_user(("User"))

node_user -->|"asks question"| node_sql_cli
node_sql_cli -->|"loads schema"| node_schema
node_sql_cli -->|"sends question and schema"| node_sql_generation
node_sql_generation -->|"requests SQL"| node_gemini
node_sql_cli -->|"cleans response"| node_sql_cleaning
node_sql_cli -->|"checks SQL"| node_sql_safety
node_sql_cli -->|"runs SQL"| node_sql_execution
node_sql_execution -->|"queries and updates"| node_sql_database
node_sql_cli -->|"prints results"| node_user
node_user -->|"asks question"| node_sqlite_cli
node_sqlite_cli -->|"handles question"| node_sqlite_orchestrator
node_sqlite_orchestrator -->|"requests SQL"| node_sqlite_generation
node_sqlite_generation -->|"requests SQL"| node_gemini
node_sqlite_orchestrator -->|"cleans response"| node_sqlite_cleaning
node_sqlite_orchestrator -->|"runs SQL"| node_sqlite_execution
node_sqlite_execution -->|"queries and updates"| node_sales_db
node_sqlite_cli -->|"prints results"| node_user

click node_sql_cli "https://github.com/ra-manuth/ai-agent-text_to_sql/blob/main/sql_assistant.py"
click node_schema "https://github.com/ra-manuth/ai-agent-text_to_sql/blob/main/sql_assistant.py"
click node_sql_generation "https://github.com/ra-manuth/ai-agent-text_to_sql/blob/main/sql_assistant.py"
click node_sql_cleaning "https://github.com/ra-manuth/ai-agent-text_to_sql/blob/main/sql_assistant.py"
click node_sql_safety "https://github.com/ra-manuth/ai-agent-text_to_sql/blob/main/sql_assistant.py"
click node_sql_execution "https://github.com/ra-manuth/ai-agent-text_to_sql/blob/main/sql_assistant.py"
click node_sqlite_cli "https://github.com/ra-manuth/ai-agent-text_to_sql/blob/main/create_db.py"
click node_sqlite_orchestrator "https://github.com/ra-manuth/ai-agent-text_to_sql/blob/main/create_db.py"
click node_sqlite_generation "https://github.com/ra-manuth/ai-agent-text_to_sql/blob/main/create_db.py"
click node_sqlite_cleaning "https://github.com/ra-manuth/ai-agent-text_to_sql/blob/main/create_db.py"
click node_sqlite_execution "https://github.com/ra-manuth/ai-agent-text_to_sql/blob/main/create_db.py"
click node_sales_db "https://github.com/ra-manuth/ai-agent-text_to_sql/blob/main/sales.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_sql_cli,node_schema,node_sql_generation,node_sql_cleaning,node_sql_safety,node_sql_execution,node_sql_database,node_user toneBlue
class node_sqlite_cli,node_sqlite_orchestrator,node_sqlite_generation,node_sqlite_cleaning,node_sqlite_execution,node_sales_db toneAmber
class node_gemini toneMint
```

## Components

### SQL Server assistant

- [SQL Server CLI](https://github.com/ra-manuth/ai-agent-text_to_sql/blob/main/sql_assistant.py): `sql_assistant.py`
- [Schema detection](https://github.com/ra-manuth/ai-agent-text_to_sql/blob/main/sql_assistant.py): `sql_assistant.py`
- [SQL generation](https://github.com/ra-manuth/ai-agent-text_to_sql/blob/main/sql_assistant.py): `sql_assistant.py`
- [SQL cleaning](https://github.com/ra-manuth/ai-agent-text_to_sql/blob/main/sql_assistant.py): `sql_assistant.py`
- [Safety checks](https://github.com/ra-manuth/ai-agent-text_to_sql/blob/main/sql_assistant.py): `sql_assistant.py`
- [SQL execution](https://github.com/ra-manuth/ai-agent-text_to_sql/blob/main/sql_assistant.py): `sql_assistant.py`
- **SQL Server**

### SQLite assistant

- [SQLite CLI](https://github.com/ra-manuth/ai-agent-text_to_sql/blob/main/create_db.py): `create_db.py`
- [Question handler](https://github.com/ra-manuth/ai-agent-text_to_sql/blob/main/create_db.py): `create_db.py`
- [SQL generation](https://github.com/ra-manuth/ai-agent-text_to_sql/blob/main/create_db.py): `create_db.py`
- [SQL cleaning](https://github.com/ra-manuth/ai-agent-text_to_sql/blob/main/create_db.py): `create_db.py`
- [SQL execution](https://github.com/ra-manuth/ai-agent-text_to_sql/blob/main/create_db.py): `create_db.py`
- [Sales database](https://github.com/ra-manuth/ai-agent-text_to_sql/blob/main/sales.db): `sales.db`

### External services

- **Gemini API**

### Other components

- **User**

## Connections

- User → SQL Server CLI: asks question (evidence: `sql_assistant.py`)
- SQL Server CLI → Schema detection: loads schema (evidence: `sql_assistant.py`)
- SQL Server CLI → SQL generation: sends question and schema (evidence: `sql_assistant.py`)
- SQL generation → Gemini API: requests SQL (evidence: `sql_assistant.py`)
- SQL Server CLI → SQL cleaning: cleans response (evidence: `sql_assistant.py`)
- SQL Server CLI → Safety checks: checks SQL (evidence: `sql_assistant.py`)
- SQL Server CLI → SQL execution: runs SQL (evidence: `sql_assistant.py`)
- SQL execution → SQL Server: queries and updates (evidence: `sql_assistant.py`)
- SQL Server CLI → User: prints results (evidence: `sql_assistant.py`)
- User → SQLite CLI: asks question (evidence: `create_db.py`)
- SQLite CLI → Question handler: handles question (evidence: `create_db.py`)
- Question handler → SQL generation: requests SQL (evidence: `create_db.py`)
- SQL generation → Gemini API: requests SQL (evidence: `create_db.py`)
- Question handler → SQL cleaning: cleans response (evidence: `create_db.py`)
- Question handler → SQL execution: runs SQL (evidence: `create_db.py`)
- SQL execution → Sales database: queries and updates (evidence: `create_db.py`)
- SQLite CLI → User: prints results (evidence: `create_db.py`)
