# nguyen-tho/simple-chatbot architecture

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

- Interactive diagram: https://gitdiagram.com/nguyen-tho/simple-chatbot
- Repository: https://github.com/nguyen-tho/simple-chatbot

## Overview

##### Main Components
- Chatbot Core Logic: The core functionality is likely encapsulated in files like common.py and perhaps parts of common.csv (which might hold reference data).
- Offline Mode Integration: This is supported by (likely) open_ai_api.py and possibly index_cpp.py which may interface with the Llama_cpp library and the Mistral 7B Open Orca model.
- Online Mode Integration: Handled by google_api.py, with the API key details stored in key.txt or api_key.json.
- Terminal Interface: Shown in terminal_app.py, which serves as the command-line entry point for running the chatbot.
- Web API: Implemented in app.py, which is designed to run with FastAPI on localhost (127.0.0.1:80).
- Web UI: Static assets (HTML, CSS, JS) in the static folder and possibly UI.py to support a browser-based user interface.

##### Interactions and Connections
- The Terminal App and the Web API both connect to the core chatbot logic.
- In offline mode, the chatbot core interacts with Llama_cpp (via open_ai_api.py/index_cpp.py) to process data using the Mistral model.
- In online mode, the system calls out to Google Gemini through google_api.py and uses the provided API key (from key.txt or api_key.json) to authenticate.
- The web UI (in static) consumes the API endpoints exposed by app.py.

##### Architectural Patterns and Design Principles
- Separation of Concerns: Distinct modules for handling offline and online processing.
- Modularity: Clear division of functionalities where each script handles dedicated tasks (e.g., UI.py for the user interface, terminal_app.py for command-line usage, and app.py for the API).
- Extensibility: The design supports both terminal and web interactions with potential future expansions (e.g., additional API endpoints or integrations).

##### Incorporate Technologies and Frameworks
- Python Environment: Using a virtual environment with dependencies specified in requirements.txt.
- FastAPI: Used in app.py to create a RESTful API service.
- Llama_cpp & Mistral 7B Open Orca Model: For processing offline chat operations.
- Google Gemini API: For the online mode of the chatbot.
- Basic web technologies (HTML, CSS, JS) to form the simple UI.
- If applicable, integration with libraries referenced in UI.py.

## Diagram (Mermaid)

```mermaid
graph TD
    %% User Interfaces
    T["Terminal Interface (terminal_app.py)"]:::cli
    WUI["Web UI (UI.py + static)"]:::ui
    API["Web API (app.py, FastAPI)"]:::api
    
    %% Core Logic
    CORE["Chatbot Core Logic (common.py, common.csv)"]:::backend

    %% External Integrations
    OFF["Offline Mode Integration (open_ai_api.py, index_cpp.py, Llama_cpp + Mistral 7B)"]:::external
    ON["Online Mode Integration (google_api.py, key management: key.txt, api_key.json, Google Gemini API)"]:::external

    %% Connections
    T -->|"sends_input"| CORE
    API -->|"sends_request"| CORE
    WUI -->|"calls_API"| API
    CORE -->|"offline_request"| OFF
    CORE -->|"online_request"| ON

    %% Click Events for User Interfaces
    click T "https://github.com/nguyen-tho/simple-chatbot/blob/main/terminal_app.py"
    click WUI "https://github.com/nguyen-tho/simple-chatbot/blob/main/UI.py"
    click WUI "https://github.com/nguyen-tho/simple-chatbot/blob/main/static/index.html"
    click WUI "https://github.com/nguyen-tho/simple-chatbot/blob/main/static/style.css"
    click WUI "https://github.com/nguyen-tho/simple-chatbot/blob/main/static/scripts.js"
    click API "https://github.com/nguyen-tho/simple-chatbot/blob/main/app.py"

    %% Click Events for Chatbot Core Logic
    click CORE "https://github.com/nguyen-tho/simple-chatbot/blob/main/common.py"
    click CORE "https://github.com/nguyen-tho/simple-chatbot/blob/main/common.csv"

    %% Click Events for Offline Mode Integration
    click OFF "https://github.com/nguyen-tho/simple-chatbot/blob/main/open_ai_api.py"
    click OFF "https://github.com/nguyen-tho/simple-chatbot/blob/main/index_cpp.py"

    %% Click Events for Online Mode Integration
    click ON "https://github.com/nguyen-tho/simple-chatbot/blob/main/google_api.py"
    click ON "https://github.com/nguyen-tho/simple-chatbot/blob/main/key.txt"
    click ON "https://github.com/nguyen-tho/simple-chatbot/blob/main/api_key.json"

    %% Styles and Colors
    classDef cli fill:#90ee90,stroke:#333,stroke-width:2px;
    classDef ui fill:#dda0dd,stroke:#333,stroke-width:2px;
    classDef api fill:#add8e6,stroke:#333,stroke-width:2px;
    classDef backend fill:#ffdead,stroke:#333,stroke-width:2px;
    classDef external fill:#f4a460,stroke:#333,stroke-width:2px;
```
