# harsh-sensei/ai-chan architecture

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

- Interactive diagram: https://gitdiagram.com/harsh-sensei/ai-chan
- Repository: https://github.com/harsh-sensei/ai-chan

## Overview

##### Project Type and Purpose
- The project implements an AI chatbot named “AI-Chan” that supports conversational memory by retrieving relevant past interactions.
- It is primarily a full-stack application where the frontend is built with Streamlit, and the backend consists of conversational model integrations (Mistral7B) along with a retrieval system.

##### Analyzing the File Structure
- Top-Level Files:
- app.py: Likely the main entry point of the application.
- README.md: Provides high-level project information.
- requirements.txt: Lists dependencies.
- .gitignore: Version control configuration.
- Directories:
- assets/ (with subfolder images): Likely holds static assets used by the UI.
- chatbots/ (with mistral.py): Contains chatbot-related implementations, in this case, using Mistral7B.
- retreival_system/ (with bm25.py and retriever.py): Provides the logic for retrieving past conversation context using BM25 (and ColBERT can be used based on context token limits).
- tests/: Contains test scripts that likely verify the functionality of embeddings in the retrieval system and the Mistral chatbot model.

##### Key Components and Their Relationships
a. Frontend Component:
- Based on Streamlit for building the user interface.
- Likely handled or launched via app.py.
- Connects to the backend to display responses and collect user inputs.
b. Chatbot Backend:
- Located in the chatbots directory (mistral.py module).
- Uses the Mistral7B model for generating responses.
- This component is invoked by the main application (app.py) after processing user input.
c. Retrieval System:
- Found in the retreival_system directory.
- Utilizes BM25 for fast retrieval of relevant past conversation segments.
- Supports an alternative retrieval engine (ColBERT) if the context is limited to 512 tokens.
- Interfaces with the chatbot backend to supply conversation memory for enhancing context.
d. Assets:
- A collection of images and static files used in the UI.
e. Tests:
- Validates the integrity and performance of both the chatbot model integration and the retrieval logic.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Layer
    subgraph "Frontend Layer"
        UI["Streamlit UI"]:::frontend
        Assets["Assets (Static UI Assets)"]:::assets
    end

    %% Backend Layer
    subgraph "Backend Layer"
        App["Application Entry (app.py)"]:::backend
        Chatbot["Chatbot Module (Mistral7B)"]:::backend
        subgraph "Retrieval Subsystem"
            BM25["BM25 Module"]:::retrieval
            Retriever["Retriever Module"]:::retrieval
        end
    end

    %% Testing Layer
    subgraph "Testing Layer"
        Testing["Testing Modules (tests/)"]:::tests
        Jina["Jina Embeddings Test"]:::tests
        MistralTest["Mistral Test"]:::tests
    end

    %% Data Flow
    UI -->|"sends_input"| App
    App -->|"processes_input"| Chatbot
    Chatbot -->|"requests_context"| BM25
    Chatbot -->|"requests_context"| Retriever
    BM25 -->|"returns_context"| Chatbot
    Retriever -->|"returns_context"| Chatbot
    Chatbot -->|"sends_response"| App
    App -->|"delivers_response"| UI
    UI ---|"loads_assets"| Assets
    Testing ---|"validates"| Chatbot
    Testing ---|"validates"| BM25
    Testing ---|"validates"| Retriever
    Testing ---|"validates"| Jina
    Testing ---|"validates"| MistralTest

    %% Click Events
    click App "https://github.com/harsh-sensei/ai-chan/blob/master/app.py"
    click UI "https://github.com/harsh-sensei/ai-chan/blob/master/app.py"
    click Assets "https://github.com/harsh-sensei/ai-chan/tree/master/assets/"
    click Chatbot "https://github.com/harsh-sensei/ai-chan/blob/master/chatbots/mistral.py"
    click BM25 "https://github.com/harsh-sensei/ai-chan/blob/master/retreival_system/bm25.py"
    click Retriever "https://github.com/harsh-sensei/ai-chan/blob/master/retreival_system/retriever.py"
    click Testing "https://github.com/harsh-sensei/ai-chan/tree/master/tests/"
    click Jina "https://github.com/harsh-sensei/ai-chan/blob/master/tests/jina_embeddings.py"
    click MistralTest "https://github.com/harsh-sensei/ai-chan/blob/master/tests/mistral_test.py"

    %% Styles
    classDef frontend fill:#cce5ff,stroke:#004085,stroke-width:2px;
    classDef backend fill:#d4edda,stroke:#155724,stroke-width:2px;
    classDef retrieval fill:#fff3cd,stroke:#856404,stroke-width:2px;
    classDef assets fill:#f8d7da,stroke:#721c24,stroke-width:2px;
    classDef tests fill:#d1ecf1,stroke:#0c5460,stroke-width:2px;
```
