# ryan-yang125/chatllm-web architecture

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

- Interactive diagram: https://gitdiagram.com/ryan-yang125/chatllm-web
- Repository: https://github.com/ryan-yang125/chatllm-web

## Overview

Key Components to Include:

##### Frontend Architecture (React/Next.js Layer)
- Main components:
- ChatBox (chat interface)
- SideBar (conversation management)
- InitModal (initialization interface)
- Connection to the store layer (using hooks)

##### State Management Layer
- Store directory with chat state management
- Hooks integration (useStore.ts)
- Local storage connection for persistence

##### Web Worker Layer
- Web-llm.worker.ts as the main processing unit
- Isolation from the main thread

##### WebLLM Integration
- WebLLM library integration
- Connection to the model files
- WebGPU acceleration

##### Model Layer
- Vicuna-7b model structure
- Tokenizer and model files
- Caching mechanism

**[Browser Environment]**
- [User Interface Layer]
- Components (React/Next.js)
- Styles (Tailwind CSS)

- [Application Logic Layer]
- State Management
- Hooks

- [Processing Layer]
- Web Worker
- WebLLM Instance

- [Model Layer]
- Model Files
- Cache Storage

Design Guidelines:

- UI Layer: Blue
- Application Logic: Green
- Processing Layer: Orange
- Model Layer: Purple

- User input → UI → State → Worker → Model
- Model → Worker → State → UI → User output

##### Specific technical details
- WebGPU acceleration
- Local storage for conversations
- PWA capabilities
- Browser cache for model storage

##### Important notes
- Browser compatibility (Chrome 113+)
- GPU memory requirements (6.4GB)
- Offline capability
- Privacy aspects (all local processing)

Special Considerations:

##### Client-side nature
- No server components
- All processing happens in browser
- Local storage for data persistence

##### Performance optimizations
- Web Worker isolation
- Model caching mechanism
- WebGPU acceleration

##### Security and privacy
- Local data storage
- No external API calls
- Isolated processing

##### PWA architecture
- Service worker
- Offline capabilities
- App manifest

Remember to indicate the asynchronous nature of the communication between the main thread and web worker, and how the WebGPU acceleration integrates with the model processing pipeline.

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Browser["Browser Environment"]
        subgraph UI["User Interface Layer"]
            ChatBox["Chat Interface"]:::ui
            SideBar["Sidebar"]:::ui
            InitModal["Init Modal"]:::ui
            MarkdownRenderer["Markdown Renderer"]:::ui
        end

        subgraph Logic["Application Logic Layer"]
            ChatState["Chat State"]:::state
            StoreHook["Store Hook"]:::state
            WebLLMHook["WebLLM Hook"]:::state
            Types["Type Definitions"]:::types
        end

        subgraph Processing["Processing Layer"]
            WebWorker["Web Worker"]:::worker
            WebLLMCore["WebLLM Core"]:::webllm
            WebLLMRuntime["WebLLM Runtime"]:::webllm
            WebLLMConfig["WebLLM Config"]:::webllm
        end

        subgraph ModelLayer["Model Layer"]
            ModelBinary["Model Binary (Vicuna-7b)"]:::model
            Tokenizer["Tokenizer"]:::model
            CacheConfig["Cache Configuration"]:::model
        end

        Entry["Application Entry"]:::entry
    end

    %% Connections
    Entry --> UI
    ChatBox --> ChatState
    SideBar --> ChatState
    InitModal --> WebLLMHook
    ChatState --> WebWorker
    WebWorker --> WebLLMCore
    WebLLMCore --> ModelBinary
    WebLLMCore --> Tokenizer
    WebLLMCore --> CacheConfig
    WebLLMRuntime --> WebLLMCore
    WebLLMConfig --> WebLLMCore
    WebLLMHook --> WebWorker
    StoreHook --> ChatState

    %% Styling
    classDef ui fill:#2563eb,color:white
    classDef state fill:#22c55e,color:white
    classDef worker fill:#f97316,color:white
    classDef webllm fill:#ea580c,color:white
    classDef model fill:#9333ea,color:white
    classDef types fill:#64748b,color:white
    classDef entry fill:#475569,color:white

    %% Click events
    click ChatBox "https://github.com/Ryan-yang125/ChatLLM-Web/blob/main/components/ChatBox.tsx"
    click SideBar "https://github.com/Ryan-yang125/ChatLLM-Web/blob/main/components/SideBar.tsx"
    click InitModal "https://github.com/Ryan-yang125/ChatLLM-Web/blob/main/components/InitModal.tsx"
    click MarkdownRenderer "https://github.com/Ryan-yang125/ChatLLM-Web/blob/main/components/markdown.tsx"
    click ChatState "https://github.com/Ryan-yang125/ChatLLM-Web/blob/main/store/chat.ts"
    click StoreHook "https://github.com/Ryan-yang125/ChatLLM-Web/blob/main/hooks/useStore.ts"
    click WebLLMHook "https://github.com/Ryan-yang125/ChatLLM-Web/blob/main/hooks/web-llm.ts"
    click WebWorker "https://github.com/Ryan-yang125/ChatLLM-Web/blob/main/web-worker/web-llm.worker.ts"
    click WebLLMCore "https://github.com/Ryan-yang125/ChatLLM-Web/blob/main/public/lib/WebLLM/tvmjs.bundle.js"
    click WebLLMRuntime "https://github.com/Ryan-yang125/ChatLLM-Web/blob/main/public/lib/WebLLM/tvmjs_runtime.wasi.js"
    click WebLLMConfig "https://github.com/Ryan-yang125/ChatLLM-Web/blob/main/public/lib/WebLLM/config.json"
    click ModelBinary "https://github.com/Ryan-yang125/ChatLLM-Web/blob/main/public/lib/WebLLM/vicuna-7b/vicuna-7b-v1_webgpu.wasm"
    click Tokenizer "https://github.com/Ryan-yang125/ChatLLM-Web/blob/main/public/lib/WebLLM/vicuna-7b/tokenizer.model"
    click CacheConfig "https://github.com/Ryan-yang125/ChatLLM-Web/blob/main/public/lib/WebLLM/vicuna-7b/ndarray-cache.json"
    click Types "https://github.com/Ryan-yang125/ChatLLM-Web/tree/main/types"
    click Entry "https://github.com/Ryan-yang125/ChatLLM-Web/tree/main/pages"
```
