How aspisov/database-agent works
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: router.py Router · chat.py Chat agent · text2sql.py Text-to-SQL · visualization.py Visualization
Components 18
UI
Streamlit entrypoints and rendering
- App entry · Starts the application and hands off to the UI
app/main.py - Chat UI · Main conversational interface and result rendering
app/ui/streamlit_app.py - UI wrapper · Isolates Streamlit-specific concerns from core logic
app/ui/streamlit_wrapper.py
Agents
Intent routing and specialized response handlers
- Router · Classifies requests and selects the handling path
app/agents/router.py - Chat agent · Handles general chat and clarification requests
app/agents/chat.py - Text-to-SQL · Generates, verifies, and executes analytical SQL
app/agents/text2sql.py - Visualization · Turns structured results into charts
app/agents/visualization.py - Agent base · Common agent behavior and interfaces
app/agents/base.py
Core models
Shared conversation and response contracts
Infrastructure
Database, prompts, config, and runtime support
- PostgreSQL · Manages connections, schema inspection, and SQL execution
app/database/connector.py - Prompts · Builds prompts from templates for routing and generation
app/prompts/prompt_manager.py - Templates · System and user prompt templates for core behaviors
app/prompts/templates - LLM factory · Creates or configures LLM client instances
app/utils/llm_factory.py - Settings · Centralized configuration for models and database access
app/config/settings.py - Data utils · Shapes query results for charting and display
app/utils/dataframe_utils.py
Local deploy
Docker seed and environment bootstrap
- Docker compose · Bootstraps the local PostgreSQL environment
docker/docker-compose.yml - DB seed · Schema and sample data used to initialize PostgreSQL
docker/data/schema.sql
Connections 29
- 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