# aspisov/database-agent architecture

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

- Interactive diagram: https://gitdiagram.com/aspisov/database-agent
- Repository: https://github.com/aspisov/database-agent

## Overview

1. This repository is a Python-based conversational agent for PostgreSQL. Its architecture centers on a natural-language front end, an LLM-backed routing layer, specialized agents for different query types, and a database connector that executes SQL against PostgreSQL.

2. The main request path starts in the Streamlit UI under `app/ui`. The UI collects a user message, preserves conversation state, and displays either plain chat responses, tabular query results, or visualizations. `streamlit_wrapper.py` likely isolates Streamlit-specific concerns from the core app logic.

3. `app/agents/router.py` is the orchestration boundary. It classifies the incoming question and chooses one of the specialized handlers: `chat`, `text2sql`, or `visualization`. This router is the key architectural decision point and the main place where conversation context influences behavior.

4. `app/agents/text2sql.py` is the database query subsystem. It translates a natural-language request into SQL, validates or verifies the query using dedicated prompts, and then relies on the database connector to run it. This is the core read/query path for analytical questions.

5. `app/agents/visualization.py` sits on top of query results and turns them into charts. It depends on structured data coming back from SQL execution, then uses helper utilities in `app/utils/dataframe_utils.py` to shape results into something chartable.

6. `app/agents/chat.py` handles non-SQL conversational requests. It is the fallback path for general database questions, clarification, and usage guidance, and it shares the same context model as the other agents so the conversation remains coherent.

7. `app/database/connector.py` is the main infrastructure boundary to PostgreSQL. It owns connection management, schema inspection, query execution, and result retrieval. The README also indicates timeout handling and formatting responsibilities here, so this is the layer where DB safety and execution control are concentrated.

8. Shared state and outputs are modeled in `app/models/context.py` and `app/models/response.py`. These models provide a common contract between the router, agents, and UI, which keeps the system from passing raw ad hoc dictionaries around.

9. Prompt construction is centralized in `app/prompts/prompt_manager.py` with Jinja templates in `app/prompts/templates`. The prompt set includes router classification prompts, text-to-SQL generation and verification prompts, and chat/system prompts. This makes the LLM behavior a first-class configurable subsystem rather than scattered inline strings.

10. `app/utils/llm_factory.py` abstracts model/provider creation, and `app/config/settings.py` centralizes runtime configuration such as database credentials and LLM model names. From an architecture standpoint, these two modules define the external dependency boundaries: one toward the LLM provider and one toward PostgreSQL.

11. The `docker/` directory contains local infrastructure for a reproducible PostgreSQL environment. `schema.sql` and `data.sql` indicate the repo can bootstrap a seeded database for demos or testing, which is important for understanding how the agent is validated outside a production database.

12. Overall flow: user message in Streamlit → router classifies intent with LLM prompts → specialized agent generates SQL, chat text, or visualization instructions → database connector executes SQL when needed → results are normalized through models/utils → UI renders the final response. The architecture is therefore a thin UI over an LLM-orchestrated multi-agent backend with PostgreSQL as the authoritative data source.

Main entry points: [`app/agents/router.py`](https://github.com/aspisov/database-agent/blob/main/app/agents/router.py) (Router), [`app/agents/chat.py`](https://github.com/aspisov/database-agent/blob/main/app/agents/chat.py) (Chat agent), [`app/agents/text2sql.py`](https://github.com/aspisov/database-agent/blob/main/app/agents/text2sql.py) (Text-to-SQL), [`app/agents/visualization.py`](https://github.com/aspisov/database-agent/blob/main/app/agents/visualization.py) (Visualization)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_ui["UI"]
  node_app_main(("App entry<br/>python entrypoint<br/>[main.py]"))
  node_streamlit_app["Chat UI<br/>streamlit app<br/>[streamlit_app.py]"]
  node_streamlit_wrapper["UI wrapper<br/>ui adapter"]
end

subgraph group_agents["Agents"]
  node_router{{"Router<br/>[router.py]"}}
  node_chat_agent["Chat agent<br/>conversational handler<br/>[chat.py]"]
  node_text2sql_agent["Text-to-SQL<br/>query agent<br/>[text2sql.py]"]
  node_viz_agent["Visualization<br/>chart agent<br/>[visualization.py]"]
  node_agent_base["Agent base<br/>[base.py]"]
end

subgraph group_core["Core models"]
  node_context_model["Context<br/>conversation state<br/>[context.py]"]
  node_response_model["Response<br/>output model<br/>[response.py]"]
end

subgraph group_infra["Infrastructure"]
  node_db_connector[("PostgreSQL<br/>db connector<br/>[connector.py]")]
  node_prompt_manager["Prompts<br/>prompt orchestration<br/>[prompt_manager.py]"]
  node_prompt_templates["Templates"]
  node_llm_factory["LLM factory<br/>model factory<br/>[llm_factory.py]"]
  node_settings["Settings<br/>runtime config<br/>[settings.py]"]
  node_dataframe_utils["Data utils<br/>data shaping<br/>[dataframe_utils.py]"]
end

subgraph group_deploy["Local deploy"]
  node_compose["Docker compose<br/>local infra<br/>[docker-compose.yml]"]
  node_seed_sql["DB seed<br/>schema and data<br/>[schema.sql]"]
end

node_app_main -->|"launches"| node_streamlit_app
node_streamlit_app -->|"uses"| node_streamlit_wrapper
node_streamlit_app -->|"submits message"| node_router
node_streamlit_app -->|"stores state"| node_context_model
node_router -->|"classifies with"| node_prompt_manager
node_router -->|"gets model"| node_llm_factory
node_router -->|"reads context"| node_context_model
node_router -->|"dispatches to"| node_chat_agent
node_router -->|"dispatches to"| node_text2sql_agent
node_router -->|"dispatches to"| node_viz_agent
node_chat_agent -->|"builds prompt"| node_prompt_manager
node_chat_agent -->|"uses model"| node_llm_factory
node_chat_agent -->|"uses context"| node_context_model
node_chat_agent -->|"returns"| node_response_model
node_text2sql_agent -->|"generates sql with"| node_prompt_manager
node_text2sql_agent -->|"uses model"| node_llm_factory
node_text2sql_agent -->|"executes query"| node_db_connector
node_text2sql_agent -->|"returns"| node_response_model
node_viz_agent -->|"shapes data"| node_dataframe_utils
node_viz_agent -->|"uses model"| node_llm_factory
node_viz_agent -->|"returns"| node_response_model
node_prompt_manager -->|"loads"| node_prompt_templates
node_settings -->|"configures"| node_llm_factory
node_settings -->|"configures"| node_db_connector
node_db_connector -.->|"connects to"| node_compose
node_compose -.->|"seeds"| node_seed_sql
node_text2sql_agent -->|"inherits"| node_agent_base
node_chat_agent -->|"inherits"| node_agent_base
node_viz_agent -->|"inherits"| node_agent_base

click node_app_main "https://github.com/aspisov/database-agent/blob/main/app/main.py"
click node_streamlit_app "https://github.com/aspisov/database-agent/blob/main/app/ui/streamlit_app.py"
click node_streamlit_wrapper "https://github.com/aspisov/database-agent/blob/main/app/ui/streamlit_wrapper.py"
click node_router "https://github.com/aspisov/database-agent/blob/main/app/agents/router.py"
click node_chat_agent "https://github.com/aspisov/database-agent/blob/main/app/agents/chat.py"
click node_text2sql_agent "https://github.com/aspisov/database-agent/blob/main/app/agents/text2sql.py"
click node_viz_agent "https://github.com/aspisov/database-agent/blob/main/app/agents/visualization.py"
click node_agent_base "https://github.com/aspisov/database-agent/blob/main/app/agents/base.py"
click node_context_model "https://github.com/aspisov/database-agent/blob/main/app/models/context.py"
click node_response_model "https://github.com/aspisov/database-agent/blob/main/app/models/response.py"
click node_db_connector "https://github.com/aspisov/database-agent/blob/main/app/database/connector.py"
click node_prompt_manager "https://github.com/aspisov/database-agent/blob/main/app/prompts/prompt_manager.py"
click node_prompt_templates "https://github.com/aspisov/database-agent/tree/main/app/prompts/templates"
click node_llm_factory "https://github.com/aspisov/database-agent/blob/main/app/utils/llm_factory.py"
click node_settings "https://github.com/aspisov/database-agent/blob/main/app/config/settings.py"
click node_dataframe_utils "https://github.com/aspisov/database-agent/blob/main/app/utils/dataframe_utils.py"
click node_compose "https://github.com/aspisov/database-agent/blob/main/docker/docker-compose.yml"
click node_seed_sql "https://github.com/aspisov/database-agent/blob/main/docker/data/schema.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_app_main,node_streamlit_app,node_streamlit_wrapper toneBlue
class node_router,node_chat_agent,node_text2sql_agent,node_viz_agent,node_agent_base toneAmber
class node_context_model,node_response_model toneMint
class node_db_connector,node_prompt_manager,node_prompt_templates,node_llm_factory,node_settings,node_dataframe_utils toneRose
class node_compose,node_seed_sql toneIndigo
```

## Components

### UI

Streamlit entrypoints and rendering

- [App entry](https://github.com/aspisov/database-agent/blob/main/app/main.py): python entrypoint · Starts the application and hands off to the UI · `app/main.py`
- [Chat UI](https://github.com/aspisov/database-agent/blob/main/app/ui/streamlit_app.py): streamlit app · Main conversational interface and result rendering · `app/ui/streamlit_app.py`
- [UI wrapper](https://github.com/aspisov/database-agent/blob/main/app/ui/streamlit_wrapper.py): ui adapter · Isolates Streamlit-specific concerns from core logic · `app/ui/streamlit_wrapper.py`

### Agents

Intent routing and specialized response handlers

- [Router](https://github.com/aspisov/database-agent/blob/main/app/agents/router.py): intent router · Classifies requests and selects the handling path · `app/agents/router.py`
- [Chat agent](https://github.com/aspisov/database-agent/blob/main/app/agents/chat.py): conversational handler · Handles general chat and clarification requests · `app/agents/chat.py`
- [Text-to-SQL](https://github.com/aspisov/database-agent/blob/main/app/agents/text2sql.py): query agent · Generates, verifies, and executes analytical SQL · `app/agents/text2sql.py`
- [Visualization](https://github.com/aspisov/database-agent/blob/main/app/agents/visualization.py): chart agent · Turns structured results into charts · `app/agents/visualization.py`
- [Agent base](https://github.com/aspisov/database-agent/blob/main/app/agents/base.py): shared agent base · Common agent behavior and interfaces · `app/agents/base.py`

### Core models

Shared conversation and response contracts

- [Context](https://github.com/aspisov/database-agent/blob/main/app/models/context.py): conversation state · Shared conversation state passed through the system · `app/models/context.py`
- [Response](https://github.com/aspisov/database-agent/blob/main/app/models/response.py): output model · Normalized response contract for UI and agents · `app/models/response.py`

### Infrastructure

Database, prompts, config, and runtime support

- [PostgreSQL](https://github.com/aspisov/database-agent/blob/main/app/database/connector.py): db connector · Manages connections, schema inspection, and SQL execution · `app/database/connector.py`
- [Prompts](https://github.com/aspisov/database-agent/blob/main/app/prompts/prompt_manager.py): prompt orchestration · Builds prompts from templates for routing and generation · `app/prompts/prompt_manager.py`
- [Templates](https://github.com/aspisov/database-agent/tree/main/app/prompts/templates): jinja templates · System and user prompt templates for core behaviors · `app/prompts/templates`
- [LLM factory](https://github.com/aspisov/database-agent/blob/main/app/utils/llm_factory.py): model factory · Creates or configures LLM client instances · `app/utils/llm_factory.py`
- [Settings](https://github.com/aspisov/database-agent/blob/main/app/config/settings.py): runtime config · Centralized configuration for models and database access · `app/config/settings.py`
- [Data utils](https://github.com/aspisov/database-agent/blob/main/app/utils/dataframe_utils.py): data shaping · Shapes query results for charting and display · `app/utils/dataframe_utils.py`

### Local deploy

Docker seed and environment bootstrap

- [Docker compose](https://github.com/aspisov/database-agent/blob/main/docker/docker-compose.yml): local infra · Bootstraps the local PostgreSQL environment · `docker/docker-compose.yml`
- [DB seed](https://github.com/aspisov/database-agent/blob/main/docker/data/schema.sql): schema and data · Schema and sample data used to initialize PostgreSQL · `docker/data/schema.sql`

## Connections

- App entry → Chat UI: launches · Application entrypoint starts the Streamlit experience
- Chat UI → UI wrapper: uses · UI delegates Streamlit-specific handling to a wrapper
- Chat UI → Router: submits message · User messages are routed to the orchestration layer
- Chat UI → Context: stores state · Conversation state is preserved across turns
- Router → Prompts: classifies with · Routing decisions are driven by prompts
- Router → LLM factory: gets model · Router uses an LLM client for classification
- Router → Context: reads context · Conversation context influences intent selection
- Router → Chat agent: dispatches to · General chat requests go to the chat agent
- Router → Text-to-SQL: dispatches to · Analytical requests go to text-to-SQL
- Router → Visualization: dispatches to · Visualization requests go to chart generation
- Chat agent → Prompts: builds prompt · Chat responses are generated from managed prompts
- Chat agent → LLM factory: uses model · Chat agent calls the LLM provider
- Chat agent → Context: uses context · Maintains coherent conversation state
- Chat agent → Response: returns · Outputs normalized chat responses
- Text-to-SQL → Prompts: generates sql with · SQL generation and verification prompts are centralized
- Text-to-SQL → LLM factory: uses model · Text-to-SQL agent calls the LLM provider
- Text-to-SQL → PostgreSQL: executes query · Generated SQL is run against PostgreSQL
- Text-to-SQL → Response: returns · Outputs tabular or textual results in a common shape
- Visualization → Data utils: shapes data · Query results are normalized for charting
- Visualization → LLM factory: uses model · Visualization instructions may use the LLM provider
- Visualization → Response: returns · Outputs chart-ready responses
- Prompts → Templates: loads · Prompt manager renders the template set
- Settings → LLM factory: configures · Model/provider settings feed client creation
- Settings → PostgreSQL: configures · Database credentials and runtime settings feed connection management
- PostgreSQL → Docker compose: connects to · Local database is provisioned through Docker
- Docker compose → DB seed: seeds · Docker setup initializes schema and sample data
- Text-to-SQL → Agent base: inherits · Specialized agent builds on shared agent behavior
- Chat agent → Agent base: inherits · Specialized agent builds on shared agent behavior
- Visualization → Agent base: inherits · Specialized agent builds on shared agent behavior
