# manojkesani/nl_sql architecture

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

- Interactive diagram: https://gitdiagram.com/manojkesani/nl_sql
- Repository: https://github.com/manojkesani/nl_sql

## Overview

This is a natural-language-to-SQL application for registered SQLite databases. A user submits a question through the web UI or API; the agent selects a database, builds a model-backed agent, executes read-only SQL, and returns a structured answer with usage and prompt metadata. Supporting product branches prepare schema profiles and prompts, generate and review evaluation data, and expose a database tool over MCP. The graph reflects the newer `nlsql` package as well as README-documented UI and service capabilities; unsampled links are omitted or marked unverified.

Main entry points: [`nlsql/ingest/golden.py`](https://github.com/manojkesani/nl_sql/blob/master/nlsql/ingest/golden.py) (Golden data generation), [`nlsql/agent/build.py`](https://github.com/manojkesani/nl_sql/blob/master/nlsql/agent/build.py) (SQL agent), [`nlsql/agent/service.py`](https://github.com/manojkesani/nl_sql/blob/master/nlsql/agent/service.py) (Agent service), [`nlsql/ingest/enrich.py`](https://github.com/manojkesani/nl_sql/blob/master/nlsql/ingest/enrich.py) (Schema enrichment)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_interaction["Question answering"]
  node_webui["Web UI<br/>[index.html]"]
  node_api["HTTP API<br/>[api.py]"]
  node_service["Agent service<br/>[service.py]"]
  node_router["Database router<br/>[router.py]"]
  node_agent["SQL agent<br/>[build.py]"]
  node_response["Structured response<br/>[schema.py]"]
end

subgraph group_database["Database access"]
  node_registry[("Database registry<br/>[registry.py]")]
  node_executor["Read-only SQL executor<br/>[executor.py]"]
  node_sqlite[("SQLite databases<br/>[longlist.db]")]
  node_mcp["MCP database server<br/>[db_server.py]"]
end

subgraph group_preparation["Database preparation"]
  node_introspection["Schema profiler<br/>[introspect.py]"]
  node_profile["Profile management<br/>[profile.py]"]
  node_enrichment["Schema enrichment<br/>[enrich.py]"]
  node_golden["Golden data generation<br/>[golden.py]"]
  node_promptgen["Prompt generation<br/>[promptgen.py]"]
  node_pipeline["Ingest pipeline<br/>[pipeline.py]"]
  node_review["Golden data review<br/>[review.py]"]
end

subgraph group_models["Models and tracing"]
  node_model_factory["Model factory<br/>[factory.py]"]
  node_providers["Model providers<br/>[providers.py]"]
  node_structured["Structured LLM calls<br/>[structured.py]"]
  node_usage["Usage tracking<br/>[usage.py]"]
  node_phoenix["Phoenix tracing<br/>[observability.py]"]
end

subgraph group_quality["Evaluation"]
  node_eval_runner["Evaluation runner<br/>[runner.py]"]
  node_metrics["Result metrics<br/>[metrics.py]"]
end

node_user(("Question asker"))
node_trace_service(("Phoenix service"))

node_user -->|"asks"| node_webui
node_webui -->|"submits question"| node_api
node_user -->|"calls API"| node_api
node_api -->|"calls"| node_service
node_service -->|"routes question"| node_router
node_service -->|"loads config"| node_registry
node_service -->|"runs agent"| node_agent
node_service -->|"tracks usage"| node_usage
node_service -->|"tags traces"| node_phoenix
node_router -->|"lists databases"| node_registry
node_router -->|"builds model"| node_model_factory
node_router -->|"requests route"| node_structured
node_agent -->|"resolves database"| node_registry
node_agent -->|"loads prompt"| node_promptgen
node_agent -->|"builds model"| node_model_factory
node_agent -->|"runs SQL tool"| node_executor
node_agent -->|"formats answer"| node_response
node_executor -->|"queries read-only"| node_sqlite
node_mcp -->|"runs queries"| node_executor
node_profile -->|"resolves database"| node_registry
node_profile -->|"profiles schema"| node_introspection
node_introspection -->|"opens read-only"| node_executor
node_pipeline -->|"runs profiling"| node_profile
node_pipeline -->|"runs enrichment"| node_enrichment
node_pipeline -->|"generates goldens"| node_golden
node_pipeline -->|"generates prompt"| node_promptgen
node_enrichment -->|"reads schema card"| node_profile
node_enrichment -->|"builds model"| node_model_factory
node_enrichment -->|"creates documentation"| node_structured
node_golden -->|"validates SQL"| node_executor
node_golden -->|"compares results"| node_metrics
node_golden -->|"reads documentation"| node_enrichment
node_golden -->|"reads schema card"| node_profile
node_golden -->|"builds model"| node_model_factory
node_promptgen -->|"uses profile"| node_profile
node_promptgen -->|"uses enrichment"| node_enrichment
node_promptgen -->|"uses examples"| node_golden
node_review -->|"reviews records"| node_golden
node_model_factory -->|"selects provider"| node_providers
node_phoenix -->|"exports traces"| node_trace_service
node_eval_runner -->|"loads database"| node_registry
node_eval_runner -->|"builds agent"| node_agent
node_eval_runner -->|"runs evaluation SQL"| node_executor
node_eval_runner -->|"loads test cases"| node_golden
node_eval_runner -->|"scores results"| node_metrics

click node_webui "https://github.com/manojkesani/nl_sql/blob/master/frontend/index.html"
click node_api "https://github.com/manojkesani/nl_sql/blob/master/nlsql/api.py"
click node_service "https://github.com/manojkesani/nl_sql/blob/master/nlsql/agent/service.py"
click node_router "https://github.com/manojkesani/nl_sql/blob/master/nlsql/agent/router.py"
click node_agent "https://github.com/manojkesani/nl_sql/blob/master/nlsql/agent/build.py"
click node_response "https://github.com/manojkesani/nl_sql/blob/master/nlsql/agent/schema.py"
click node_registry "https://github.com/manojkesani/nl_sql/blob/master/nlsql/registry.py"
click node_executor "https://github.com/manojkesani/nl_sql/blob/master/nlsql/db/executor.py"
click node_sqlite "https://github.com/manojkesani/nl_sql/blob/master/longlist.db"
click node_mcp "https://github.com/manojkesani/nl_sql/blob/master/db_server.py"
click node_introspection "https://github.com/manojkesani/nl_sql/blob/master/nlsql/db/introspect.py"
click node_profile "https://github.com/manojkesani/nl_sql/blob/master/nlsql/ingest/profile.py"
click node_enrichment "https://github.com/manojkesani/nl_sql/blob/master/nlsql/ingest/enrich.py"
click node_golden "https://github.com/manojkesani/nl_sql/blob/master/nlsql/ingest/golden.py"
click node_promptgen "https://github.com/manojkesani/nl_sql/blob/master/nlsql/ingest/promptgen.py"
click node_pipeline "https://github.com/manojkesani/nl_sql/blob/master/nlsql/ingest/pipeline.py"
click node_review "https://github.com/manojkesani/nl_sql/blob/master/nlsql/ingest/review.py"
click node_model_factory "https://github.com/manojkesani/nl_sql/blob/master/nlsql/llm/factory.py"
click node_providers "https://github.com/manojkesani/nl_sql/blob/master/nlsql/llm/providers.py"
click node_structured "https://github.com/manojkesani/nl_sql/blob/master/nlsql/llm/structured.py"
click node_usage "https://github.com/manojkesani/nl_sql/blob/master/nlsql/llm/usage.py"
click node_phoenix "https://github.com/manojkesani/nl_sql/blob/master/nlsql/observability.py"
click node_eval_runner "https://github.com/manojkesani/nl_sql/blob/master/nlsql/eval/runner.py"
click node_metrics "https://github.com/manojkesani/nl_sql/blob/master/nlsql/eval/metrics.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_webui,node_api,node_service,node_router,node_agent,node_response toneBlue
class node_registry,node_executor,node_sqlite,node_mcp toneAmber
class node_introspection,node_profile,node_enrichment,node_golden,node_promptgen,node_pipeline,node_review toneMint
class node_model_factory,node_providers,node_structured,node_usage,node_phoenix toneRose
class node_eval_runner,node_metrics,node_user,node_trace_service toneIndigo
```

## Components

### Question answering

- [Web UI](https://github.com/manojkesani/nl_sql/blob/master/frontend/index.html): `frontend/index.html`
- [HTTP API](https://github.com/manojkesani/nl_sql/blob/master/nlsql/api.py): `nlsql/api.py`
- [Agent service](https://github.com/manojkesani/nl_sql/blob/master/nlsql/agent/service.py): `nlsql/agent/service.py`
- [Database router](https://github.com/manojkesani/nl_sql/blob/master/nlsql/agent/router.py): `nlsql/agent/router.py`
- [SQL agent](https://github.com/manojkesani/nl_sql/blob/master/nlsql/agent/build.py): `nlsql/agent/build.py`
- [Structured response](https://github.com/manojkesani/nl_sql/blob/master/nlsql/agent/schema.py): `nlsql/agent/schema.py`

### Database access

- [Database registry](https://github.com/manojkesani/nl_sql/blob/master/nlsql/registry.py): `nlsql/registry.py`
- [Read-only SQL executor](https://github.com/manojkesani/nl_sql/blob/master/nlsql/db/executor.py): `nlsql/db/executor.py`
- [SQLite databases](https://github.com/manojkesani/nl_sql/blob/master/longlist.db): `longlist.db`
- [MCP database server](https://github.com/manojkesani/nl_sql/blob/master/db_server.py): `db_server.py`

### Database preparation

- [Schema profiler](https://github.com/manojkesani/nl_sql/blob/master/nlsql/db/introspect.py): `nlsql/db/introspect.py`
- [Profile management](https://github.com/manojkesani/nl_sql/blob/master/nlsql/ingest/profile.py): `nlsql/ingest/profile.py`
- [Schema enrichment](https://github.com/manojkesani/nl_sql/blob/master/nlsql/ingest/enrich.py): `nlsql/ingest/enrich.py`
- [Golden data generation](https://github.com/manojkesani/nl_sql/blob/master/nlsql/ingest/golden.py): `nlsql/ingest/golden.py`
- [Prompt generation](https://github.com/manojkesani/nl_sql/blob/master/nlsql/ingest/promptgen.py): `nlsql/ingest/promptgen.py`
- [Ingest pipeline](https://github.com/manojkesani/nl_sql/blob/master/nlsql/ingest/pipeline.py): `nlsql/ingest/pipeline.py`
- [Golden data review](https://github.com/manojkesani/nl_sql/blob/master/nlsql/ingest/review.py): `nlsql/ingest/review.py`

### Models and tracing

- [Model factory](https://github.com/manojkesani/nl_sql/blob/master/nlsql/llm/factory.py): `nlsql/llm/factory.py`
- [Model providers](https://github.com/manojkesani/nl_sql/blob/master/nlsql/llm/providers.py): `nlsql/llm/providers.py`
- [Structured LLM calls](https://github.com/manojkesani/nl_sql/blob/master/nlsql/llm/structured.py): `nlsql/llm/structured.py`
- [Usage tracking](https://github.com/manojkesani/nl_sql/blob/master/nlsql/llm/usage.py): `nlsql/llm/usage.py`
- [Phoenix tracing](https://github.com/manojkesani/nl_sql/blob/master/nlsql/observability.py): `nlsql/observability.py`

### Evaluation

- [Evaluation runner](https://github.com/manojkesani/nl_sql/blob/master/nlsql/eval/runner.py): `nlsql/eval/runner.py`
- [Result metrics](https://github.com/manojkesani/nl_sql/blob/master/nlsql/eval/metrics.py): `nlsql/eval/metrics.py`

### Other components

- **Question asker**
- **Phoenix service**

## Connections

- Question asker → Web UI: asks (evidence: `README.md`)
- Web UI → HTTP API: submits question (evidence: `README.md`)
- Question asker → HTTP API: calls API (evidence: `README.md`)
- HTTP API → Agent service: calls (evidence: `nlsql/api.py`)
- Agent service → Database router: routes question (evidence: `nlsql/agent/service.py`)
- Agent service → Database registry: loads config (evidence: `nlsql/agent/service.py`)
- Agent service → SQL agent: runs agent (evidence: `nlsql/agent/service.py`)
- Agent service → Usage tracking: tracks usage (evidence: `nlsql/agent/service.py`)
- Agent service → Phoenix tracing: tags traces (evidence: `nlsql/agent/service.py`)
- Database router → Database registry: lists databases (evidence: `nlsql/agent/router.py`)
- Database router → Model factory: builds model (evidence: `nlsql/agent/router.py`)
- Database router → Structured LLM calls: requests route (evidence: `nlsql/agent/router.py`)
- SQL agent → Database registry: resolves database (evidence: `nlsql/agent/build.py`)
- SQL agent → Prompt generation: loads prompt (evidence: `nlsql/agent/build.py`)
- SQL agent → Model factory: builds model (evidence: `nlsql/agent/build.py`)
- SQL agent → Read-only SQL executor: runs SQL tool (evidence: `nlsql/agent/build.py`)
- SQL agent → Structured response: formats answer (evidence: `nlsql/agent/build.py`)
- Read-only SQL executor → SQLite databases: queries read-only (evidence: `nlsql/db/executor.py`)
- MCP database server → Read-only SQL executor: runs queries (evidence: `db_server.py`)
- Profile management → Database registry: resolves database (evidence: `nlsql/ingest/profile.py`)
- Profile management → Schema profiler: profiles schema (evidence: `nlsql/ingest/profile.py`)
- Schema profiler → Read-only SQL executor: opens read-only (evidence: `nlsql/db/introspect.py`)
- Ingest pipeline → Profile management: runs profiling (evidence: `nlsql/ingest/pipeline.py`)
- Ingest pipeline → Schema enrichment: runs enrichment (evidence: `nlsql/ingest/pipeline.py`)
- Ingest pipeline → Golden data generation: generates goldens (evidence: `nlsql/ingest/pipeline.py`)
- Ingest pipeline → Prompt generation: generates prompt (evidence: `nlsql/ingest/pipeline.py`)
- Schema enrichment → Profile management: reads schema card (evidence: `nlsql/ingest/enrich.py`)
- Schema enrichment → Model factory: builds model (evidence: `nlsql/ingest/enrich.py`)
- Schema enrichment → Structured LLM calls: creates documentation (evidence: `nlsql/ingest/enrich.py`)
- Golden data generation → Read-only SQL executor: validates SQL (evidence: `nlsql/ingest/golden.py`)
- Golden data generation → Result metrics: compares results (evidence: `nlsql/ingest/golden.py`)
- Golden data generation → Schema enrichment: reads documentation (evidence: `nlsql/ingest/golden.py`)
- Golden data generation → Profile management: reads schema card (evidence: `nlsql/ingest/golden.py`)
- Golden data generation → Model factory: builds model (evidence: `nlsql/ingest/golden.py`)
- Prompt generation → Profile management: uses profile (evidence: `nlsql/ingest/promptgen.py`)
- Prompt generation → Schema enrichment: uses enrichment (evidence: `nlsql/ingest/promptgen.py`)
- Prompt generation → Golden data generation: uses examples (evidence: `nlsql/ingest/promptgen.py`)
- Golden data review → Golden data generation: reviews records (evidence: `nlsql/ingest/review.py`)
- Model factory → Model providers: selects provider (evidence: `nlsql/llm/factory.py`)
- Phoenix tracing → Phoenix service: exports traces (evidence: `nlsql/observability.py`)
- Evaluation runner → Database registry: loads database (evidence: `nlsql/eval/runner.py`)
- Evaluation runner → SQL agent: builds agent (evidence: `nlsql/eval/runner.py`)
- Evaluation runner → Read-only SQL executor: runs evaluation SQL (evidence: `nlsql/eval/runner.py`)
- Evaluation runner → Golden data generation: loads test cases (evidence: `nlsql/eval/runner.py`)
- Evaluation runner → Result metrics: scores results (evidence: `nlsql/eval/runner.py`)
