# d-artisan/ai-chatbot architecture

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

- Interactive diagram: https://gitdiagram.com/d-artisan/ai-chatbot
- Repository: https://github.com/d-artisan/ai-chatbot

## Overview

##### Main Components
- User Interfaces:
- Terminal UI (TerminalChat.py) for command-line interaction.
- Web UI (UIChat.py) powered by Streamlit, featuring avatars, personality settings, and voice toggles.
- Chatbot Engine:
- Core logic that processes inputs, maintains chat context, and generates responses using LangChain.
- Integration with local LLM models (e.g., mistral:7b) run via Ollama.
- Document Processing:
- Components/module(s) for handling document Q&A, including file loaders (PyPDFLoader, TextLoader) and FAISS-based document retrieval.
- Voice Support:
- Modules or subcomponents leveraging SpeechRecognition (speech-to-text) and pyttsx3 (text-to-speech) to handle voice input and output.
- Persistence Layer:
- Mechanisms to save and reload chat history from disk.
- External Dependencies & Configurations:
- The requirements.txt file to manage all the third-party libraries and dependencies.

##### Out the Relationships and Interactions
- The UI components (TerminalChat.py and UIChat.py) serve as entry-points where user inputs are captured.
- These inputs are sent to the Chatbot Engine, which then:
- Interacts with the local language model through LangChain/Ollama.
- If a document-based query is detected, the engine routes the request to the Document Processing component.
- The Document Processing component uses FAISS for quick retrieval and leverages file loaders (PyPDFLoader/TextLoader) to parse and understand the document content.
- For voice interactions, the engine temporarily passes audio input through the SpeechRecognition module, converts it to text, and then processes it through the same chatbot engine. The response may be converted back into speech using pyttsx3.
- Chat sessions and histories are maintained on disk, so there should be persistence hooks within the engine.

##### Architectural Patterns and Design Principles
- Separation of Concerns:
- Different files handle different interfaces (terminal vs. Streamlit-based web UI).
- Core logic, document handling, voice processing, and persistence are functionally separated.
- Modular Design:
- Clear Input/Output Flow:

##### Technologies and Frameworks
- Python with dependencies (LangChain, Ollama, FAISS, PyPDFLoader, TextLoader, SpeechRecognition, pyttsx3) serve as the backbone.
- Streamlit for the web interface.
- Local LLM models (e.g., mistral:7b) via Ollama.
- FAISS for vector-based retrieval in the Document Q&A feature.

## Diagram (Mermaid)

```mermaid
graph TD
    %% User Interfaces
    subgraph "User Interfaces"
        UI1["Terminal UI"]:::ui
        UI2["Streamlit Web UI"]:::ui
    end

    %% Core Processing Layer
    subgraph "Core Processing Layer"
        CE["Chatbot Engine"]:::engine
        LLM["Local LLM (mistral:7b via Ollama)"]:::engine
    end

    %% Document & Voice Services
    subgraph "Document & Voice Services"
        DQ["Document Q&A Module"]:::service
        VI["Voice Input Module"]:::service
        VO["Voice Output Module"]:::service
    end

    %% Data Persistence
    subgraph "Data Persistence"
        P["Persistence Layer"]:::persistence
    end

    %% External Dependencies
    ED["External Dependencies"]:::dependency

    %% Flow Connections
    UI1 -->|"user input"| CE
    UI2 -->|"user input"| CE
    CE -->|"calls_LLM"| LLM
    CE -->|"document_query"| DQ
    CE -->|"voice_query"| VI
    VI -->|"converted_text"| CE
    CE -->|"response_audio"| VO
    CE -->|"save_history"| P
    ED -->|"provides"| CE

    %% Click Events
    click UI1 "https://github.com/d-artisan/ai-chatbot/blob/main/TerminalChat.py"
    click UI2 "https://github.com/d-artisan/ai-chatbot/blob/main/UIChat.py"
    click ED "https://github.com/d-artisan/ai-chatbot/blob/main/requirements.txt"

    %% Styles
    classDef ui fill:#add8e6,stroke:#0000ff,stroke-width:2px;
    classDef engine fill:#90ee90,stroke:#006400,stroke-width:2px;
    classDef service fill:#ffa500,stroke:#ff8c00,stroke-width:2px;
    classDef persistence fill:#dda0dd,stroke:#800080,stroke-width:2px;
    classDef dependency fill:#d3d3d3,stroke:#696969,stroke-width:2px;
```
