# vaishnavi-newalkar/query_potter_sql_to_nl architecture

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

- Interactive diagram: https://gitdiagram.com/vaishnavi-newalkar/query_potter_sql_to_nl
- Repository: https://github.com/vaishnavi-newalkar/query_potter_sql_to_nl

## Overview

#### Purpose and Entry Points
This is a Python natural-language-to-SQL assistant for SQLite databases. It accepts conversational questions, derives safe read-only SQL with an LLM, executes it, and returns interpreted answers with reasoning and recovery details.

- FastAPI service entry: `api.py`
- Streamlit chat client: `ui.py`
- Alternate/local application launcher: `app.py`
- Bundled target database: `data/Chinook_Sqlite.sqlite`

#### API, UI, and Session Boundary
The FastAPI backend exposes database upload, query processing, and schema access; the Streamlit UI calls that backend and presents chat history plus pipeline reasoning. Session state preserves multi-turn context and supports clarification follow-ups rather than treating each question independently.

- Service boundary: `api.py`
- Client boundary: `ui.py`
- Conversation/session state: `session/session_manager.py`, `session/memory.py`

#### Database Intake and Schema Model
Uploaded or configured SQLite databases are validated before use. Schema extraction and caching provide tables, columns, and relationships to downstream stages. A foreign-key graph represents joinable relationships, while schema refinement narrows the LLM context to relevant tables rather than passing the entire database schema.

- Validation/execution boundary: `db/validator.py`, `db/executor.py`
- Schema discovery/cache: `db/schema_extractor.py`, `db/schema_cache.py`
- Relationship and relevance model: `schema/fk_graph.py`, `schema/refiner.py`

#### Intent Routing and Query Planning
Natural-language processing first identifies schema-oriented meta-queries and ambiguity. Meta requests can be answered directly from extracted schema. Ambiguous requests produce clarification prompts; clarification intent is merged with prior context. Otherwise, the planner identifies filters, joins, aggregations, exclusions, and query complexity to guide SQL generation.

- Meta and ambiguity routing: `nlp/meta_handler.py`, `nlp/ambiguity_detector.py`
- Context and clarification merge: `nlp/context_builder.py`, `nlp/intent_merger.py`
- Query plan construction: `nlp/planner.py`, `nlp/classifier.py`

#### LLM SQL Generation and Recovery
The system uses Groq-hosted LLM inference, configured through environment settings, to produce SQL and reasoning from the query plan plus refined schema. Prompt templates encode complexity-aware generation rules. If execution fails, a self-correction stage analyzes the database error and retries generation, with the README specifying up to two retries.

- External LLM integration: `llm/client.py`
- SQL/reasoning generation: `llm/sql_generator.py`, `llm/prompt_templates.py`
- Error-driven regeneration: `llm/self_correction.py`
- Runtime configuration: `config/settings.py`

#### Safety and Execution Boundary
Generated SQL must pass layered validation before reaching SQLite. The validator enforces SELECT-only/read-only behavior and blocks unsafe patterns; execution occurs through the database executor rather than directly from API handlers. This is the critical trust boundary between probabilistic LLM output and local database access.

- SQL policy enforcement: `validation/sql_validator.py`
- Database-specific validation and execution: `db/validator.py`, `db/executor.py`

#### Result Interpretation
Successful row sets, empty results, errors, generated SQL, and reasoning traces are transformed into user-facing responses. General conversational responses are handled separately from database-result interpretation, allowing schema/meta and non-query interactions to remain conversational.

- Structured result interpretation: `response/interpreter.py`, `response/answer_generator.py`
- General chat response path: `response/general_chat.py`

Main entry points: [`api.py`](https://github.com/vaishnavi-newalkar/query_potter_sql_to_nl/blob/main/api.py) (FastAPI service), [`ui.py`](https://github.com/vaishnavi-newalkar/query_potter_sql_to_nl/blob/main/ui.py) (Streamlit chat client), [`validation/sql_validator.py`](https://github.com/vaishnavi-newalkar/query_potter_sql_to_nl/blob/main/validation/sql_validator.py) (Read-only SQL policy), [`llm/self_correction.py`](https://github.com/vaishnavi-newalkar/query_potter_sql_to_nl/blob/main/llm/self_correction.py) (Error-driven correction)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Entry Points"]
  node_ui["Streamlit chat client<br/>[ui.py]"]
  node_api["FastAPI service<br/>API boundary<br/>[api.py]"]
  node_app["Local launcher<br/>[app.py]"]
  node_session["Conversation state<br/>session management"]
end

subgraph group_schema["Database &amp; Schema"]
  node_database[("SQLite database<br/>target database")]
  node_db_intake["DB validation &amp; execution<br/>database boundary"]
  node_schema_catalog["Schema discovery &amp; cache<br/>schema model"]
  node_schema_relevance["FK graph &amp; refinement<br/>schema relevance"]
end

subgraph group_nlp["Intent &amp; Planning"]
  node_routing{{"Meta &amp; ambiguity routing<br/>intent routing"}}
  node_context["Context &amp; intent merge<br/>conversation context"]
  node_planner["Query planner<br/>query planning"]
end

subgraph group_llm["LLM Generation"]
  node_settings["Runtime settings<br/>configuration<br/>[settings.py]"]
  node_llm_client{{"Groq LLM client<br/>external inference<br/>[client.py]"}}
  node_sql_generation["SQL &amp; reasoning generation<br/>LLM generation"]
  node_sql_policy{{"Read-only SQL policy<br/>security boundary<br/>[sql_validator.py]"}}
  node_self_correction["Error-driven correction<br/>retry recovery<br/>[self_correction.py]"]
end

subgraph group_response["Response"]
  node_result_response["Result interpretation<br/>response assembly"]
  node_general_chat["General chat response<br/>conversational response<br/>[general_chat.py]"]
end

node_app -->|"launches service"| node_api
node_ui -->|"API requests"| node_api
node_api -->|"loads and saves context"| node_session
node_api -->|"upload and query boundary"| node_db_intake
node_db_intake -->|"validated access"| node_database
node_api -->|"schema requests"| node_schema_catalog
node_schema_catalog -->|"extracts schema"| node_database
node_schema_catalog -->|"catalog and relationships"| node_schema_relevance
node_api -->|"natural-language request"| node_routing
node_session -->|"prior turns"| node_context
node_routing -.->|"clarification path"| node_context
node_routing -->|"meta or conversational request"| node_general_chat
node_context -->|"resolved intent"| node_planner
node_planner -->|"relevance requirements"| node_schema_relevance
node_planner -->|"query plan"| node_sql_generation
node_schema_relevance -->|"refined schema"| node_sql_generation
node_settings -->|"configuration"| node_llm_client
node_sql_generation -->|"generation request"| node_llm_client
node_sql_generation -->|"generated SQL"| node_sql_policy
node_sql_policy -->|"approved read-only SQL"| node_db_intake
node_db_intake -.->|"execution error"| node_self_correction
node_self_correction -.->|"corrected regeneration"| node_sql_generation
node_db_intake -->|"query result or error"| node_result_response
node_result_response -->|"structured answer"| node_api
node_general_chat -->|"conversational answer"| node_api
node_api -->|"answer and reasoning"| node_ui

click node_ui "https://github.com/vaishnavi-newalkar/query_potter_sql_to_nl/blob/main/ui.py"
click node_api "https://github.com/vaishnavi-newalkar/query_potter_sql_to_nl/blob/main/api.py"
click node_app "https://github.com/vaishnavi-newalkar/query_potter_sql_to_nl/blob/main/app.py"
click node_database "https://github.com/vaishnavi-newalkar/query_potter_sql_to_nl/blob/main/data/Chinook_Sqlite.sqlite"
click node_settings "https://github.com/vaishnavi-newalkar/query_potter_sql_to_nl/blob/main/config/settings.py"
click node_llm_client "https://github.com/vaishnavi-newalkar/query_potter_sql_to_nl/blob/main/llm/client.py"
click node_sql_policy "https://github.com/vaishnavi-newalkar/query_potter_sql_to_nl/blob/main/validation/sql_validator.py"
click node_self_correction "https://github.com/vaishnavi-newalkar/query_potter_sql_to_nl/blob/main/llm/self_correction.py"
click node_general_chat "https://github.com/vaishnavi-newalkar/query_potter_sql_to_nl/blob/main/response/general_chat.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_ui,node_api,node_app,node_session toneBlue
class node_database,node_db_intake,node_schema_catalog,node_schema_relevance toneAmber
class node_routing,node_context,node_planner toneMint
class node_settings,node_llm_client,node_sql_generation,node_sql_policy,node_self_correction toneRose
class node_result_response,node_general_chat toneIndigo
```

## Components

### Entry Points

- [Streamlit chat client](https://github.com/vaishnavi-newalkar/query_potter_sql_to_nl/blob/main/ui.py): client · Presents chat history and pipeline reasoning. · `ui.py`
- [FastAPI service](https://github.com/vaishnavi-newalkar/query_potter_sql_to_nl/blob/main/api.py): API boundary · Handles upload, schema, and query requests. · `api.py`
- [Local launcher](https://github.com/vaishnavi-newalkar/query_potter_sql_to_nl/blob/main/app.py): launcher · `app.py`
- **Conversation state**: session management · Preserves multi-turn context and follow-ups.

### Database & Schema

- [SQLite database](https://github.com/vaishnavi-newalkar/query_potter_sql_to_nl/blob/main/data/Chinook_Sqlite.sqlite): target database · `data/Chinook_Sqlite.sqlite`
- **DB validation & execution**: database boundary · Validates database access and executes approved SQL.
- **Schema discovery & cache**: schema model · Extracts and caches tables, columns, and relationships.
- **FK graph & refinement**: schema relevance · Finds join paths and narrows schema context.

### Intent & Planning

- **Meta & ambiguity routing**: intent routing · Answers schema requests directly or requests clarification.
- **Context & intent merge**: conversation context · Combines clarification turns with prior intent.
- **Query planner**: query planning · Identifies joins, filters, aggregations, and complexity.

### LLM Generation

- [Runtime settings](https://github.com/vaishnavi-newalkar/query_potter_sql_to_nl/blob/main/config/settings.py): configuration · Configures Groq-hosted inference. · `config/settings.py`
- [Groq LLM client](https://github.com/vaishnavi-newalkar/query_potter_sql_to_nl/blob/main/llm/client.py): external inference · `llm/client.py`
- **SQL & reasoning generation**: LLM generation · Uses complexity-aware prompts and refined schema.
- [Read-only SQL policy](https://github.com/vaishnavi-newalkar/query_potter_sql_to_nl/blob/main/validation/sql_validator.py): security boundary · Enforces SELECT-only SQL and blocks unsafe patterns. · `validation/sql_validator.py`
- [Error-driven correction](https://github.com/vaishnavi-newalkar/query_potter_sql_to_nl/blob/main/llm/self_correction.py): retry recovery · Regenerates SQL from execution errors, up to two retries. · `llm/self_correction.py`

### Response

- **Result interpretation**: response assembly · Formats rows, empty results, errors, SQL, and reasoning.
- [General chat response](https://github.com/vaishnavi-newalkar/query_potter_sql_to_nl/blob/main/response/general_chat.py): conversational response · `response/general_chat.py`

## Connections

- Local launcher → FastAPI service: launches service
- Streamlit chat client → FastAPI service: API requests
- FastAPI service → Conversation state: loads and saves context
- FastAPI service → DB validation & execution: upload and query boundary
- DB validation & execution → SQLite database: validated access
- FastAPI service → Schema discovery & cache: schema requests
- Schema discovery & cache → SQLite database: extracts schema
- Schema discovery & cache → FK graph & refinement: catalog and relationships
- FastAPI service → Meta & ambiguity routing: natural-language request
- Conversation state → Context & intent merge: prior turns
- Meta & ambiguity routing → Context & intent merge: clarification path
- Meta & ambiguity routing → General chat response: meta or conversational request
- Context & intent merge → Query planner: resolved intent
- Query planner → FK graph & refinement: relevance requirements
- Query planner → SQL & reasoning generation: query plan
- FK graph & refinement → SQL & reasoning generation: refined schema
- Runtime settings → Groq LLM client: configuration
- SQL & reasoning generation → Groq LLM client: generation request
- SQL & reasoning generation → Read-only SQL policy: generated SQL
- Read-only SQL policy → DB validation & execution: approved read-only SQL
- DB validation & execution → Error-driven correction: execution error
- Error-driven correction → SQL & reasoning generation: corrected regeneration
- DB validation & execution → Result interpretation: query result or error
- Result interpretation → FastAPI service: structured answer
- General chat response → FastAPI service: conversational answer
- FastAPI service → Streamlit chat client: answer and reasoning
