# bagumeow/iuh_fit_chatbot architecture

> Architecture diagram of the GitHub repository bagumeow/iuh_fit_chatbot, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/bagumeow/iuh_fit_chatbot
- Repository: https://github.com/bagumeow/iuh_fit_chatbot

## Overview

##### Main Components
- Frontend/Client: Although not elaborated in the repository, the browser-based interaction (http://localhost:5000 from the README) implies a client interface.
- Backend API: The FastAPI application defined in main_app.py which serves as the entry point.
- Routers: Under the “router” directory, you have modules like chatbot.py, tools.py, and user.py. These represent different endpoints that handle specific API calls.
- Service/Business Logic:
- ChatbotFIT/src: Contains two major submodules:
- base/llm_model.py: Likely handles interaction with the Language Model.
- rag: This directory includes the core of the RAG pipeline with file_loader (to load PDF files from the “data” folder), rag_chain (to manage the RAG workflow), main (possibly an entry point for RAG tasks), and vector_store (for managing embedding storage and similarity search).
- Data Layer:
- The “data” directory stores PDF files, which are used as the knowledge base for the RAG process.
- Tools:
- Supplementary utilities in “tools/tools.py” which might support additional functionalities.
- Database:
- PostgreSQL or Supabase is referenced in the README for persistent storage of application data.

##### Relationships and Interactions
- The client (browser) sends requests which are routed through FastAPI (main_app.py) to the appropriate endpoints in the “router” modules.
- When a chat request is made, the chatbot endpoint interacts with the RAG components:
- The LLM model (from base/llm_model.py) is used to process language.
- The RAG chain (in src/rag/rag_chain.py) leverages file_loader and vector_store to retrieve data from PDFs and generate informed responses.
- Database interactions occur to handle user data, configuration, or caching responses and search indexes; this can be integrated within business logic or router-level functions.
- Additional utilities in the “tools” directory provide other support features.

##### Architectural Patterns
- Separation of Concerns: There is a clear division between the API layer (FastAPI, routers), business logic (LLM models, RAG pipeline), and the data layer (PDF data and external database).
- Modular Design: Distinct modules for different functionalities (LLM, RAG, routing) make the system easier to understand and scale.
- RAG Pipeline: The use of file loaders, vector stores, and chain processing indicates a typical RAG architecture where documents are ingested and embeddings are generated for retrieval.

##### Relevant Technologies and Frameworks
- FastAPI for the backend server.
- LangChain for managing interactions with language models and the RAG pipeline.
- PostgreSQL/Supabase for database operations.
- Possibly Uvicorn as the server runner (as mentioned in the README).

##### Detail and Clarity
- Brief notes on important functions like language model integration and document retrieval which are central to the RAG approach.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Client Layer"
        Client["Client (Browser)"]:::client
    end

    subgraph "API Layer"
        FastAPI["FastAPI Server (main_app.py)"]:::api
        subgraph "Routers"
            ChatbotRouter["Chatbot Router (chatbot.py)"]:::router
            ToolsRouter["Tools Router (tools.py)"]:::router
            UserRouter["User Router (user.py)"]:::router
        end
    end

    subgraph "Business Logic Layer"
        LLM["LLM Module (llm_model.py)"]:::logic
        subgraph "RAG Pipeline"
            FileLoader["File Loader (file_loader.py)"]:::logic
            RAGChain["RAG Chain (rag_chain.py)"]:::logic
            PipelineMain["Pipeline Main (main.py)"]:::logic
            VectorStore["Vector Store (vector_store.py)"]:::logic
        end
    end

    subgraph "Data Layer"
        PDF["PDF Documents (data)"]:::data
        DB["Database (PostgreSQL/Supabase)"]:::database
    end

    subgraph "Utilities"
        Tools["Utilities (tools.py)"]:::tools
    end

    Client -->|"requests"| FastAPI
    FastAPI -->|"routes_to"| ChatbotRouter
    FastAPI -->|"routes_to"| ToolsRouter
    FastAPI -->|"routes_to"| UserRouter

    ChatbotRouter -->|"invokes"| LLM
    ChatbotRouter -->|"invokes"| RAGChain
    LLM -->|"processes"| RAGChain
    RAGChain -->|"loads_docs_from"| FileLoader
    RAGChain -->|"retrieves_using"| VectorStore
    RAGChain -->|"executes"| PipelineMain
    FileLoader -->|"reads"| PDF

    FastAPI -->|"interacts_with"| DB
    UserRouter -->|"manages"| DB
    ToolsRouter -->|"calls"| Tools

    style Client fill:#ffe6e6,stroke:#cc0000,stroke-width:2px
    style FastAPI fill:#e6f2ff,stroke:#0066cc,stroke-width:2px
    style ChatbotRouter fill:#e6f2ff,stroke:#0066cc,stroke-width:2px
    style ToolsRouter fill:#e6f2ff,stroke:#0066cc,stroke-width:2px
    style UserRouter fill:#e6f2ff,stroke:#0066cc,stroke-width:2px
    style LLM fill:#e6ffe6,stroke:#009933,stroke-width:2px
    style FileLoader fill:#e6ffe6,stroke:#009933,stroke-width:2px
    style RAGChain fill:#e6ffe6,stroke:#009933,stroke-width:2px
    style PipelineMain fill:#e6ffe6,stroke:#009933,stroke-width:2px
    style VectorStore fill:#e6ffe6,stroke:#009933,stroke-width:2px
    style PDF fill:#fff2cc,stroke:#cc9900,stroke-width:2px
    style DB fill:#ffe6cc,stroke:#cc6600,stroke-width:2px
    style Tools fill:#f9e6ff,stroke:#9933cc,stroke-width:2px

    click FastAPI "https://github.com/bagumeow/iuh_fit_chatbot/blob/master/main_app.py"
    click ChatbotRouter "https://github.com/bagumeow/iuh_fit_chatbot/blob/master/router/chatbot.py"
    click ToolsRouter "https://github.com/bagumeow/iuh_fit_chatbot/blob/master/router/tools.py"
    click UserRouter "https://github.com/bagumeow/iuh_fit_chatbot/blob/master/router/user.py"
    click LLM "https://github.com/bagumeow/iuh_fit_chatbot/blob/master/ChatbotFIT/src/base/llm_model.py"
    click FileLoader "https://github.com/bagumeow/iuh_fit_chatbot/blob/master/ChatbotFIT/src/rag/file_loader.py"
    click RAGChain "https://github.com/bagumeow/iuh_fit_chatbot/blob/master/ChatbotFIT/src/rag/rag_chain.py"
    click PipelineMain "https://github.com/bagumeow/iuh_fit_chatbot/blob/master/ChatbotFIT/src/rag/main.py"
    click VectorStore "https://github.com/bagumeow/iuh_fit_chatbot/blob/master/ChatbotFIT/src/rag/vector_store.py"
    click PDF "https://github.com/bagumeow/iuh_fit_chatbot/tree/master/data/"
    click Tools "https://github.com/bagumeow/iuh_fit_chatbot/blob/master/tools/tools.py"
```
