# ruizguille/tech-trends-chatbot architecture

> Architecture diagram of the GitHub repository ruizguille/tech-trends-chatbot, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ruizguille/tech-trends-chatbot
- Repository: https://github.com/ruizguille/tech-trends-chatbot

## Overview

##### Main Components
- Frontend Component:
- Built with React and bundled using Vite.
- Contains UI components (e.g., Chatbot, ChatMessages, ChatInput) which interact with the backend API.
- Has utilities and hooks for handling UI behavior (like auto-scrolling and resizing).
- Backend Component:
- Built with Python using FastAPI.
- Contains several modules such as:
- API endpoints (located in app/api.py)
- Assistant logic (in assistants/assistant.py, local_assistant.py, prompts.py, tools.py).
- Utility functions (e.g., in utils/ for stream handling and text-splitting).
- Data processing and document loading (loader.py and export.py for exporting chats).
- Data Storage:
- Uses Redis Stack Server as the vector database (with RedisJSON and RediSearch) to store and search for vector embeddings of documents.
- Data Source:
- Raw source documents (PDF reports, etc.) stored under backend/data/docs, which are processed to build the knowledge base.
- External Services:
- OpenAI API, used by the backend to generate responses based on the provided prompts and context.

##### Relationships and Interactions
- The Backend includes:
- A request handling layer (main.py and api.py) that incoming requests pass through.
- Business logic embedded in the assistants module and associated configurations (config.py).
- The Backend connects to Redis for querying/storing vector embeddings, especially during:
- Document ingestion/processing, where the loader reads PDFs, transforms them into embeddings, and stores them.
- Real-time retrieval when the chatbot answers queries.
- The Backend makes outbound calls to the OpenAI API to generate responses using the intelligent prompt handling in the assistants modules.
- Optional local testing mode (“local” script) and export functionality showcase additional utility paths.

##### Architectural Patterns and Principles
- Separation of concerns is clearly visible: Frontend and Backend are isolated, with each having its own configuration and build system.
- Modular design: Backend components such as API endpoints, assistants, and utilities are decoupled, easing maintenance and extension.
- Customizable data ingestion pipeline: The loader system is designed to process and convert new documents into embeddings, enabling adaptivity for different data sources.
- Use of environment variables and configuration management (via Pydantic settings) ensures scalability and easy environment-specific adjustments.
- API-driven design for frontend-backend communication.

## Diagram (Mermaid)

```mermaid
flowchart TD
    user["User Browser"]:::external

    subgraph "Frontend (React/Vite)"
        F1["Chatbot (UI)"]:::frontend
        F2["ChatMessages (UI)"]:::frontend
        F3["ChatInput (UI)"]:::frontend
        F4["useAutoScroll (Hook)"]:::frontend
        F5["useAutosize (Hook)"]:::frontend
    end

    subgraph "Backend (FastAPI)"
        B1["API Endpoint (api.py)"]:::backend
        B2["App Entrypoint (main.py)"]:::backend
        subgraph "Assistant Logic"
            A1["assistant.py"]:::backend
            A2["local_assistant.py"]:::backend
            A3["prompts.py"]:::backend
            A4["tools.py"]:::backend
        end
        subgraph "Utility Functions"
            U1["Text Splitter (splitter.py)"]:::backend
            U2["SSE Stream (sse_stream.py)"]:::backend
        end
        B3["Configuration (config.py)"]:::backend
        B4["OpenAI Integration (openai.py)"]:::backend
        B5["Document Loader (loader.py)"]:::backend
        B6["Export Chat (export.py)"]:::backend
        B7["Data Storage Handler (db.py)"]:::backend
    end

    DS["Redis Data Store"]:::datastore
    OD["OpenAI API"]:::external
    RD["Raw Documents"]:::datastore

    user --> F1
    F1 -->|"API_call"| B1
    F3 -->|"Input"| B1
    B1 -->|"uses"| A1
    B1 -->|"uses"| U1
    B1 -->|"configures"| B3
    B1 -->|"integrates"| B4
    A1 -->|"retrieves"| DS
    B5 -->|"ingests"| DS
    RD --> B5
    B4 -->|"calls"| OD
    B1 -->|"triggers"| B6

    click F1 "https://github.com/ruizguille/tech-trends-chatbot/blob/master/frontend/src/components/Chatbot.jsx"
    click F2 "https://github.com/ruizguille/tech-trends-chatbot/blob/master/frontend/src/components/ChatMessages.jsx"
    click F3 "https://github.com/ruizguille/tech-trends-chatbot/blob/master/frontend/src/components/ChatInput.jsx"
    click F4 "https://github.com/ruizguille/tech-trends-chatbot/blob/master/frontend/src/hooks/useAutoScroll.js"
    click F5 "https://github.com/ruizguille/tech-trends-chatbot/blob/master/frontend/src/hooks/useAutosize.js"

    click B1 "https://github.com/ruizguille/tech-trends-chatbot/blob/master/backend/app/api.py"
    click B2 "https://github.com/ruizguille/tech-trends-chatbot/blob/master/backend/app/main.py"
    click A1 "https://github.com/ruizguille/tech-trends-chatbot/blob/master/backend/app/assistants/assistant.py"
    click A2 "https://github.com/ruizguille/tech-trends-chatbot/blob/master/backend/app/assistants/local_assistant.py"
    click A3 "https://github.com/ruizguille/tech-trends-chatbot/blob/master/backend/app/assistants/prompts.py"
    click A4 "https://github.com/ruizguille/tech-trends-chatbot/blob/master/backend/app/assistants/tools.py"
    click U1 "https://github.com/ruizguille/tech-trends-chatbot/blob/master/backend/app/utils/splitter.py"
    click U2 "https://github.com/ruizguille/tech-trends-chatbot/blob/master/backend/app/utils/sse_stream.py"
    click B3 "https://github.com/ruizguille/tech-trends-chatbot/blob/master/backend/app/config.py"
    click B4 "https://github.com/ruizguille/tech-trends-chatbot/blob/master/backend/app/openai.py"
    click B5 "https://github.com/ruizguille/tech-trends-chatbot/blob/master/backend/app/loader.py"
    click B6 "https://github.com/ruizguille/tech-trends-chatbot/blob/master/backend/app/export.py"
    click B7 "https://github.com/ruizguille/tech-trends-chatbot/blob/master/backend/app/db.py"
    click RD "https://github.com/ruizguille/tech-trends-chatbot/tree/master/backend/data/docs/"

    classDef frontend fill:#a2d2ff,stroke:#000,stroke-width:2px;
    classDef backend fill:#cdeac0,stroke:#000,stroke-width:2px;
    classDef datastore fill:#ffe066,stroke:#000,stroke-width:2px;
    classDef external fill:#f4cccc,stroke:#000,stroke-width:2px;
```
