# vungocthanhtruc/nlu-ai-bot architecture

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

- Interactive diagram: https://gitdiagram.com/vungocthanhtruc/nlu-ai-bot
- Repository: https://github.com/vungocthanhtruc/nlu-ai-bot

## Overview

##### Project Type & Purpose
- It’s clearly a full-stack chat application with Natural Language Understanding powered by LLMs.
- The frontend is built using TypeScript (React) that provides the chat UI (inspired by Figma designs for both Light and Dark themes), while the backend uses FastAPI (Python) for REST endpoints and real-time communication (via websockets).
- Additionally, the project leverages external technologies such as HuggingFace for LLM integration and PostgreSQL for persistent storage.

##### Analyzing the File Structure
- The top-level structure splits between backend (FastAPI) and frontend (React-based, found under src and public).
- The backend directory is further structured with clear separation for:
- CRUD modules (in the “crud” folder) for handling core operations on chats, rooms, and users.
- Data models (in the “models” folder) and corresponding schemas (in “schemas”) ensuring data validation and structure.
- Routers (in “routers”) that form the API endpoints for various functionalities (chats, rooms, users, dataset).
- A dedicated “websocket” folder that suggests a real-time messaging component.
- The frontend comes with detailed components (e.g., chatFooter, header, mainChat, sidebar) and also includes Redux slices for state management plus a standalone websocket server in the src/websocket directory (likely to simulate or proxy real-time communication).
- Presence of configuration files (like .env, package.json, tsconfig.json, and backend/requirements.txt) indicates dependency and environment management.

##### Additional Insight from the README
- The README mentions technical details like integrating LLMs, HuggingFace, FastAPI, and PostgreSQL.
- It outlines steps to run both the frontend (npm start) and backend (uvicorn command).
- The reference to a Figma design further confirms the importance of a strong, well-architected frontend.

##### Components
a. Main Components:
- Frontend Application:
- React-based UI (components for header, chat list, chat footer, sidebar, etc.)
- Redux state management (store, slices) for managing UI state and chat data.
- Static assets (public folder) supporting design (manifest, index.html, etc.)
- Backend Application:
- REST API endpoints using FastAPI, including routers for chats, rooms, users, and dataset.
- Data processing layers including CRUD modules, data models, and schema validations.
- Websocket layer for real-time bidirectional communication.
- Database:
- PostgreSQL used for persisting user, chat, and room data (implied by README).
- External Services:
- Third-party LLM integrations (HuggingFace API/LLMs).
- Possibly additional services/tools (popper, tessera referenced in README notes for Windows).

b. Relationships and Interactions:
- The frontend sends HTTP requests to FastAPI REST endpoints and establishes websocket connections for real-time chat updates.
- FastAPI controllers (routers) interact with the CRUD layer to perform database operations.
- The CRUD layer communicates with PostgreSQL through a database connection module.
- Integration points for LLM processing can be highlighted where backend processing calls external HuggingFace APIs for chat responses.
- The frontend’s Redux store refreshes state based on responses from the backend, ensuring the UI reflects real-time data and updates.

c. Architectural Patterns & Design Principles:
- Separation of Concerns: Clear division between frontend (UI) and backend (API, business logic).
- Modular Design: The backend is broken into routers, CRUD, models, schemas, and websockets, supporting maintainability and scalability.
- Real-Time Communication: Use of websockets to provide immediate feedback within the chat system.
- Integration of external machine learning models (LLMs via HuggingFace) to process and generate chat responses.

d. Relevant Technologies/Frameworks:
- Frontend: React, TypeScript, Redux, CSS modules.
- Backend: Python, FastAPI, Uvicorn.
- Database: PostgreSQL.
- External/Misc: HuggingFace’s LLMs, supporting tools for environment management (.env files), and a node.js–based websocket server for additional support.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Subgraph
    subgraph "Frontend"
        UIComp["UI Components (React)"]:::frontend
        Redux["Redux State Management"]:::frontend
        InstAssets["Static Assets"]:::frontend
    end

    %% Backend Subgraph
    subgraph "Backend"
        Entry["Entry Points & Configuration"]:::backend
        Routers["REST API Endpoints (Routers)"]:::backend
        CRUD["Business Logic/CRUD Modules"]:::backend
        Models["Data Models"]:::backend
        Schemas["Schema Validations"]:::backend
        WS["Websocket Communication"]:::backend
        DBConn["Database Connection"]:::backend
    end

    %% Database Subgraph
    subgraph "Database"
        Database["PostgreSQL Database"]:::database
    end

    %% External Services Subgraph
    subgraph "External Services"
        HF["HuggingFace LLM Integration"]:::external
        ExtDeps["External Dependencies"]:::external
        EnvVars["Environment Variables"]:::external
    end

    %% Frontend to Backend Connections
    UIComp -->|"HTTP_Request"| Routers
    UIComp -->|"Websocket_Connection"| WS
    UIComp <-->|"State_Sync"| Redux

    %% Backend Internal Connections
    Entry --> Routers
    Entry --> WS
    Routers -->|"Calls"| CRUD
    CRUD -->|"Uses"| Models
    CRUD -->|"Validates"| Schemas
    CRUD -->|"DB_Operations"| DBConn
    DBConn -->|"Queries"| Database
    CRUD -->|"LLM_Processing"| HF

    %% External Connections
    EnvVars --- Entry
    ExtDeps --- UIComp

    %% Styling
    classDef frontend fill:#AED6F1,stroke:#2471A3,stroke-width:2px;
    classDef backend fill:#F9E79F,stroke:#B7950B,stroke-width:2px;
    classDef database fill:#D5F5E3,stroke:#1E8449,stroke-width:2px;
    classDef external fill:#FAD7A0,stroke:#D35400,stroke-width:2px;

    %% Click Events for Frontend Components
    click UIComp "https://github.com/vungocthanhtruc/nlu-ai-bot/tree/master/src/components"
    click InstAssets "https://github.com/vungocthanhtruc/nlu-ai-bot/tree/master/public"
    click Redux "https://github.com/vungocthanhtruc/nlu-ai-bot/tree/master/src/redux"

    %% Click Events for Backend Components
    click Routers "https://github.com/vungocthanhtruc/nlu-ai-bot/tree/master/backend/app/routers"
    click CRUD "https://github.com/vungocthanhtruc/nlu-ai-bot/tree/master/backend/app/crud"
    click Models "https://github.com/vungocthanhtruc/nlu-ai-bot/tree/master/backend/app/models"
    click Schemas "https://github.com/vungocthanhtruc/nlu-ai-bot/tree/master/backend/app/schemas"
    click WS "https://github.com/vungocthanhtruc/nlu-ai-bot/blob/master/backend/app/websocket/websocket.py"
    click DBConn "https://github.com/vungocthanhtruc/nlu-ai-bot/blob/master/backend/app/database/database_connection.py"
    click Entry "https://github.com/vungocthanhtruc/nlu-ai-bot/blob/master/backend/main.py"
    click Entry "https://github.com/vungocthanhtruc/nlu-ai-bot/blob/master/backend/run_local.py"
    click Entry "https://github.com/vungocthanhtruc/nlu-ai-bot/blob/master/backend/requirements.txt"
    click Entry "https://github.com/vungocthanhtruc/nlu-ai-bot/blob/master/backend/.env"

    %% Click Events for External Components
    click EnvVars "https://github.com/vungocthanhtruc/nlu-ai-bot/blob/master/.env"
    click EnvVars "https://github.com/vungocthanhtruc/nlu-ai-bot/blob/master/backend/.env"
    click ExtDeps "https://github.com/vungocthanhtruc/nlu-ai-bot/blob/master/package.json"
    click ExtDeps "https://github.com/vungocthanhtruc/nlu-ai-bot/blob/master/tsconfig.json"
```
