# virajbhutada/ai-database-query-bot architecture

> Architecture diagram of the GitHub repository virajbhutada/ai-database-query-bot, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-02-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/virajbhutada/ai-database-query-bot
- Repository: https://github.com/virajbhutada/ai-database-query-bot

## Overview

##### Project Type & Purpose
- This is a full-stack application that uses a web-based interface (built with Streamlit) to provide an AI-powered chatbot for querying a PostgreSQL database.
- Its goal is to allow users (both business analysts and developers) to interact naturally with the database using natural language, leveraging OpenAI’s language models.

##### Analyzing the File Structure
- Top-Level Files:
– app.py: Likely the main entry point for running the Streamlit application.
– README.md & requirements.txt: Provide documentation and dependency information.
– .gitignore: Standard file for ignoring unnecessary files in Git.
- assets Directory:
– Contains theme-related Python files (dark_theme.py, light_theme.py) and additional styling elements (made_by_sdw.py) that are probably used to customize the UI.
- chatlogs Directory:
– Stores past conversation logs in markdown format that serve as a history of chatbot interactions.
- utils Directory:
– Contains helper modules for different functionalities such as API interactions (api_functions.py), handling chatbot logic (chat_functions.py), configuration (config.py), database interactions (database_functions.py), and other helper and system prompt functionalities.
– This indicates that the back-end logic, including database querying and processing AI interactions, is modularized into separate functions.

##### Key Components & Their Relationships
a. User Interface (Frontend):
– Built using Streamlit (as mentioned in the README).
– Managed primarily by app.py that ties together the UI components, theming (assets), and user interactions.
b. Chatbot Engine & AI Integration:
– Utilizes OpenAI’s models internally to process natural language input. The processing is likely managed through functions included in utils (chat_functions.py, api_functions.py, system_prompts.py).
c. Database Integration:
– Uses PostgreSQL as the backend database. The related operations (querying and data retrieval) are managed by database_functions.py in utils.
d. Conversation History:
– Chatlogs are saved in the chatlogs folder as markdown files, ensuring that every conversation is archived.
e. Configuration & Management:
– The config.py file within utils handles the setup for environment variables or runtime configuration such as database credentials and API keys.

##### Important Architectural Patterns & Design Elements
- Separation of Concerns: The project separates UI, business logic, and data access into distinct modules.
- Modular Design: The “utils” directory illustrates modularity, where specific functionalities (API calls, database operations, helper functions) are encapsulated.
- Scalability & Maintainability: By isolating configuration and using external integrations (OpenAI API), the project is designed for ease of updates and scaling.
- Extensibility: With chatlogs stored as markdown files and configurable theming options, extending user interaction and UI customization is straightforward.

##### Final Recommendations for the Principal Software Engineer
- To detail dependencies, e.g., the Streamlit UI depending on the theming options from the assets folder, and the chatbot’s processing modules in utils interfacing with both OpenAI and PostgreSQL.

## Diagram (Mermaid)

```mermaid
flowchart TD

    %% Global entities
    U["User"]:::user

    %% Frontend/UI Layer
    subgraph "Frontend/UI Layer"
        SUI["Streamlit Application (app.py)"]:::frontend
        subgraph "Assets"
            DT["Dark Theme"]:::assets
            LT["Light Theme"]:::assets
            MB["Made By SDW"]:::assets
        end
    end

    %% Backend/Logic Layer
    subgraph "Backend/Logic Layer"
        subgraph "Chatbot Engine & AI Integration"
            CF["Chat Functions"]:::backend
            AF["API Functions"]:::backend
            SP["System Prompts"]:::backend
        end
        subgraph "Backend Services"
            DBF["Database Functions"]:::backend
            CM["Configuration Management"]:::backend
        end
    end

    %% External Integration
    OAI["OpenAI API"]:::external

    %% Data Persistence Layer
    subgraph "Data Persistence Layer"
        DB["PostgreSQL Database"]:::database
        CL["Conversation History (chatlogs)"]:::data
    end

    %% Data Flow & Interactions
    U -->|"submitsQuery"| SUI
    SUI -->|"appliesTheming"| DT
    SUI -->|"appliesTheming"| LT
    SUI -->|"appliesTheming"| MB

    SUI -->|"forwardsQuery"| CF
    CF -->|"callsAPI"| OAI
    CF -->|"queriesDB"| DBF
    CF -->|"logsConversation"| CL
    CF -->|"retrievesConfig"| CM

    AF ---|"integratedWith"| CF
    SP ---|"integratedWith"| CF
    DBF -->|"executesQuery"| DB
    DBF ---|"retrievesConfig"| CM

    CF -->|"returnsResponse"| SUI

    %% Click Events
    click SUI "https://github.com/virajbhutada/ai-database-query-bot/blob/main/app.py"
    click DT "https://github.com/virajbhutada/ai-database-query-bot/blob/main/assets/dark_theme.py"
    click LT "https://github.com/virajbhutada/ai-database-query-bot/blob/main/assets/light_theme.py"
    click MB "https://github.com/virajbhutada/ai-database-query-bot/blob/main/assets/made_by_sdw.py"
    click CF "https://github.com/virajbhutada/ai-database-query-bot/blob/main/utils/chat_functions.py"
    click AF "https://github.com/virajbhutada/ai-database-query-bot/blob/main/utils/api_functions.py"
    click SP "https://github.com/virajbhutada/ai-database-query-bot/blob/main/utils/system_prompts.py"
    click DBF "https://github.com/virajbhutada/ai-database-query-bot/blob/main/utils/database_functions.py"
    click CM "https://github.com/virajbhutada/ai-database-query-bot/blob/main/utils/config.py"
    click CL "https://github.com/virajbhutada/ai-database-query-bot/tree/main/chatlogs/"

    %% Styles
    classDef user fill:#e0f7fa,stroke:#006064,stroke-width:2px;
    classDef frontend fill:#e8f5e9,stroke:#1b5e20,stroke-width:2px;
    classDef assets fill:#e3f2fd,stroke:#0d47a1,stroke-width:2px;
    classDef backend fill:#fff9c4,stroke:#f57f17,stroke-width:2px;
    classDef external fill:#ffebee,stroke:#b71c1c,stroke-width:2px;
    classDef database fill:#ffe0b2,stroke:#e65100,stroke-width:2px;
    classDef data fill:#fce4ec,stroke:#880e4f,stroke-width:2px;
```
