# novelxv/langchain-sql-qa architecture

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

- Interactive diagram: https://gitdiagram.com/novelxv/langchain-sql-qa
- Repository: https://github.com/novelxv/langchain-sql-qa

## Overview

This is a command-line natural-language SQL Q&A application over a PostgreSQL database. A user submits a question; the LangGraph workflow generates SQL, executes it, and uses the result to produce an answer for the CLI. The repository also includes a Chinook database reset/import utility and supports local Ollama with OpenAI fallback. Edges reflect calls and setup behavior visible in the excerpts; database provisioning is shown separately from the runtime query workflow.

Main entry points: [`src/chain.py`](https://github.com/novelxv/langchain-sql-qa/blob/main/src/chain.py) (SQL generation), [`src/llm_config.py`](https://github.com/novelxv/langchain-sql-qa/blob/main/src/llm_config.py) (LLM selection), [`src/main.py`](https://github.com/novelxv/langchain-sql-qa/blob/main/src/main.py) (Q&A CLI), [`scripts/clean_database.py`](https://github.com/novelxv/langchain-sql-qa/blob/main/scripts/clean_database.py) (Chinook setup)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_interaction["User interaction"]
  node_cli["Q&amp;A CLI<br/>[main.py]"]
end

subgraph group_workflow["Query workflow"]
  node_graph["LangGraph workflow<br/>[chain.py]"]
  node_sqlgen["SQL generation<br/>[chain.py]"]
  node_execution["SQL execution<br/>[chain.py]"]
  node_answer["Answer generation<br/>[chain.py]"]
end

subgraph group_integrations["Runtime integrations"]
  node_llm_config["LLM selection<br/>[llm_config.py]"]
  node_ollama["Ollama"]
  node_openai["OpenAI"]
  node_db_connection["DB connection<br/>[database.py]"]
  node_postgres[("PostgreSQL")]
end

subgraph group_setup["Database setup"]
  node_db_setup["Chinook setup<br/>[clean_database.py]"]
  node_chinook_sql["Chinook SQL data"]
end

node_user(("User"))

node_user -->|"enters question"| node_cli
node_cli -->|"streams question"| node_graph
node_graph -->|"starts sequence"| node_sqlgen
node_sqlgen -->|"passes query"| node_execution
node_execution -->|"passes result"| node_answer
node_answer -->|"returns answer"| node_cli
node_sqlgen -->|"uses prompts"| node_llm_config
node_sqlgen -->|"invokes model"| node_ollama
node_answer -->|"invokes model"| node_ollama
node_answer -->|"invokes model"| node_openai
node_llm_config -->|"probes models"| node_ollama
node_llm_config -->|"fallback probe"| node_openai
node_graph -->|"imports database"| node_db_connection
node_db_connection -->|"connects to"| node_postgres
node_sqlgen -->|"reads table info"| node_postgres
node_execution -->|"runs SQL"| node_postgres
node_db_setup -->|"recreates and loads"| node_postgres
node_db_setup -->|"imports when present"| node_chinook_sql

click node_cli "https://github.com/novelxv/langchain-sql-qa/blob/main/src/main.py"
click node_graph "https://github.com/novelxv/langchain-sql-qa/blob/main/src/chain.py"
click node_sqlgen "https://github.com/novelxv/langchain-sql-qa/blob/main/src/chain.py"
click node_execution "https://github.com/novelxv/langchain-sql-qa/blob/main/src/chain.py"
click node_answer "https://github.com/novelxv/langchain-sql-qa/blob/main/src/chain.py"
click node_llm_config "https://github.com/novelxv/langchain-sql-qa/blob/main/src/llm_config.py"
click node_db_connection "https://github.com/novelxv/langchain-sql-qa/blob/main/src/database.py"
click node_db_setup "https://github.com/novelxv/langchain-sql-qa/blob/main/scripts/clean_database.py"
click node_chinook_sql "https://github.com/novelxv/langchain-sql-qa/blob/main/data/Chinook_PostgreSql.sql"

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_cli,node_user toneBlue
class node_graph,node_sqlgen,node_execution,node_answer toneAmber
class node_llm_config,node_ollama,node_openai,node_db_connection,node_postgres toneMint
class node_db_setup,node_chinook_sql toneRose
```

## Components

### User interaction

- [Q&A CLI](https://github.com/novelxv/langchain-sql-qa/blob/main/src/main.py): `src/main.py`

### Query workflow

- [LangGraph workflow](https://github.com/novelxv/langchain-sql-qa/blob/main/src/chain.py): `src/chain.py`
- [SQL generation](https://github.com/novelxv/langchain-sql-qa/blob/main/src/chain.py): `src/chain.py`
- [SQL execution](https://github.com/novelxv/langchain-sql-qa/blob/main/src/chain.py): `src/chain.py`
- [Answer generation](https://github.com/novelxv/langchain-sql-qa/blob/main/src/chain.py): `src/chain.py`

### Runtime integrations

- [LLM selection](https://github.com/novelxv/langchain-sql-qa/blob/main/src/llm_config.py): `src/llm_config.py`
- **Ollama**
- **OpenAI**
- [DB connection](https://github.com/novelxv/langchain-sql-qa/blob/main/src/database.py): `src/database.py`
- **PostgreSQL**

### Database setup

- [Chinook setup](https://github.com/novelxv/langchain-sql-qa/blob/main/scripts/clean_database.py): `scripts/clean_database.py`
- [Chinook SQL data](https://github.com/novelxv/langchain-sql-qa/blob/main/data/Chinook_PostgreSql.sql): `data/Chinook_PostgreSql.sql`

### Other components

- **User**

## Connections

- User → Q&A CLI: enters question (evidence: `src/main.py`)
- Q&A CLI → LangGraph workflow: streams question (evidence: `src/main.py`)
- LangGraph workflow → SQL generation: starts sequence (evidence: `src/chain.py`)
- SQL generation → SQL execution: passes query (evidence: `src/chain.py`)
- SQL execution → Answer generation: passes result (evidence: `src/chain.py`)
- Answer generation → Q&A CLI: returns answer (evidence: `src/main.py`)
- SQL generation → LLM selection: uses prompts (evidence: `src/chain.py`)
- SQL generation → Ollama: invokes model (evidence: `src/chain.py`)
- Answer generation → Ollama: invokes model (evidence: `src/chain.py`)
- Answer generation → OpenAI: invokes model (evidence: `src/chain.py`)
- LLM selection → Ollama: probes models (evidence: `src/llm_config.py`)
- LLM selection → OpenAI: fallback probe (evidence: `src/llm_config.py`)
- LangGraph workflow → DB connection: imports database (evidence: `src/chain.py`)
- DB connection → PostgreSQL: connects to (evidence: `src/database.py`)
- SQL generation → PostgreSQL: reads table info (evidence: `src/chain.py`)
- SQL execution → PostgreSQL: runs SQL (evidence: `src/chain.py`)
- Chinook setup → PostgreSQL: recreates and loads (evidence: `scripts/clean_database.py`)
- Chinook setup → Chinook SQL data: imports when present (evidence: `scripts/clean_database.py`)
