# togetherai4/nextjs-ollama-llm-ui architecture

> Architecture diagram of the GitHub repository togetherai4/nextjs-ollama-llm-ui, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-01-08). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/togetherai4/nextjs-ollama-llm-ui
- Repository: https://github.com/togetherai4/nextjs-ollama-llm-ui

## Overview

##### Main System Components

a) Frontend Layer:
- Next.js Application (Client-side)
- UI Components
- Theme System
- Local Storage Management

b) API Layer:
- Next.js API Routes
- Chat Management
- Model Management
- Tags Management

c) External Integration:
- Ollama LLM Service

##### Component Relationships

**[Client Layer] → [API Layer] → [Ollama Service]**
- ↕               ↕
**[Local Storage]   [Model Management]**

##### Detailed Component Breakdown

Frontend Components (src/components):
- Chat interface components (chat-layout, chat-list, chat-bottombar)
- Core UI elements (buttons, forms, dialogs)
- User interface elements (sidebar, mode-toggle)

API Routes (src/app/api):
- Three main endpoints:
- /api/chat
- /api/model
- /api/tags

State Management:
- Local Storage integration
- Custom hooks (useLocalStorageData, useSpeechRecognition)

##### Key Interactions to Highlight

- Chat Flow:
- User Input → Chat Component → API Route → Ollama Service → Response → UI Update

- Model Management:
- Pull Model Form → Model API → Ollama Service → Local Model Storage

- Theme System:
- Theme Provider → Local Storage → UI Components

##### Technical Details

Environment Configuration:
- NEXT_PUBLIC_OLLAMA_URL connection
- Local storage implementation
- API route handlers

Features to Emphasize:
- Code syntax highlighting system
- Voice input processing
- Model switching mechanism
- Chat history management

##### External Services Layer

Left to Right:
##### External Integrations

Remember to emphasize the local-first nature of the application, as it's a key feature mentioned in the README, and show how the application maintains state without a traditional database by utilizing local storage.

This system design should provide a clear understanding of how the application works as a fully-featured web interface for Ollama LLMs, emphasizing its modern architecture and efficient state management approach.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Frontend["Frontend Layer"]
        UI[User Interface]
        Chat[Chat Interface]:::chat
        CoreUI[Core UI Components]:::ui
        ThemeP[Theme Provider]:::theme
        Settings[User Settings]:::settings
        Nav[Navigation Components]:::nav
        Utils[Utility Components]:::utils
    end

    subgraph APILayer["API Layer"]
        ChatAPI["Chat API"]:::api
        ModelAPI["Model API"]:::api
        TagsAPI["Tags API"]:::api
    end

    subgraph State["State Management"]
        LocalStorage[(Local Storage)]:::storage
        LSHook[Local Storage Hook]:::hook
        SpeechHook[Speech Recognition Hook]:::hook
    end

    subgraph External["External Services"]
        Ollama[Ollama LLM Service]:::external
    end

    %% Frontend Detailed Components
    Chat --> |Contains| ChatLayout[Chat Layout]:::chat
    Chat --> |Contains| ChatInput[Chat Input]:::chat
    Chat --> |Contains| ChatMessages[Chat Messages]:::chat
    Chat --> |Contains| ChatHeader[Chat Header]:::chat

    CoreUI --> |Contains| Buttons[Buttons]:::ui
    CoreUI --> |Contains| Forms[Forms]:::ui
    CoreUI --> |Contains| Dialogs[Dialogs]:::ui
    CoreUI --> |Contains| Inputs[Input Components]:::ui

    Nav --> |Contains| Sidebar[Sidebar]:::nav
    Nav --> |Contains| ModeToggle[Mode Toggle]:::nav

    Settings --> |Contains| UserSettings[User Settings]:::settings
    Settings --> |Contains| UsernameForm[Username Form]:::settings
    Settings --> |Contains| EditUsername[Edit Username Form]:::settings

    Utils --> |Contains| CodeDisplay[Code Display]:::utils
    Utils --> |Contains| EmojiPicker[Emoji Picker]:::utils

    %% Relationships
    UI --> Chat
    UI --> CoreUI
    UI --> Nav
    UI --> Settings
    UI --> Utils

    Chat --> ChatAPI
    ChatAPI --> Ollama
    ModelAPI --> Ollama
    TagsAPI --> Ollama

    UI --> LocalStorage
    ThemeP --> LocalStorage
    LSHook --> LocalStorage
    SpeechHook --> Chat

    %% Styles
    classDef frontend fill:#e1f5fe,stroke:#01579b
    classDef chat fill:#e8f5e9,stroke:#2e7d32
    classDef ui fill:#f3e5f5,stroke:#7b1fa2
    classDef api fill:#fff3e0,stroke:#ff6f00
    classDef storage fill:#fffde7,stroke:#f57f17
    classDef external fill:#fce4ec,stroke:#c2185b
    classDef hook fill:#e8eaf6,stroke:#3f51b5
    classDef theme fill:#e0f2f1,stroke:#004d40
    classDef settings fill:#f1f8e9,stroke:#33691e
    classDef nav fill:#f3e5f5,stroke:#7b1fa2
    classDef utils fill:#e0f7fa,stroke:#006064

    %% Click Events
    click ChatLayout "https://github.com/TogetherAI4/nextjs-ollama-llm-ui/blob/master/src/components/chat/chat-layout.tsx"
    click ChatInput "https://github.com/TogetherAI4/nextjs-ollama-llm-ui/blob/master/src/components/chat/chat-bottombar.tsx"
    click ChatMessages "https://github.com/TogetherAI4/nextjs-ollama-llm-ui/blob/master/src/components/chat/chat-list.tsx"
    click ChatHeader "https://github.com/TogetherAI4/nextjs-ollama-llm-ui/blob/master/src/components/chat/chat-topbar.tsx"
    click Buttons "https://github.com/TogetherAI4/nextjs-ollama-llm-ui/blob/master/src/components/ui/button.tsx"
    click Forms "https://github.com/TogetherAI4/nextjs-ollama-llm-ui/blob/master/src/components/ui/form.tsx"
    click Dialogs "https://github.com/TogetherAI4/nextjs-ollama-llm-ui/blob/master/src/components/ui/dialog.tsx"
    click Inputs "https://github.com/TogetherAI4/nextjs-ollama-llm-ui/blob/master/src/components/ui/input.tsx"
    click ChatAPI "https://github.com/TogetherAI4/nextjs-ollama-llm-ui/blob/master/src/app/api/chat/route.ts"
    click ModelAPI "https://github.com/TogetherAI4/nextjs-ollama-llm-ui/blob/master/src/app/api/model/route.ts"
    click TagsAPI "https://github.com/TogetherAI4/nextjs-ollama-llm-ui/blob/master/src/app/api/tags/route.ts"
    click LSHook "https://github.com/TogetherAI4/nextjs-ollama-llm-ui/blob/master/src/app/hooks/useLocalStorageData.ts"
    click SpeechHook "https://github.com/TogetherAI4/nextjs-ollama-llm-ui/blob/master/src/app/hooks/useSpeechRecognition.ts"
    click ThemeP "https://github.com/TogetherAI4/nextjs-ollama-llm-ui/blob/master/src/providers/theme-provider.tsx"
    click Sidebar "https://github.com/TogetherAI4/nextjs-ollama-llm-ui/blob/master/src/components/sidebar.tsx"
    click ModeToggle "https://github.com/TogetherAI4/nextjs-ollama-llm-ui/blob/master/src/components/mode-toggle.tsx"
    click UserSettings "https://github.com/TogetherAI4/nextjs-ollama-llm-ui/blob/master/src/components/user-settings.tsx"
    click UsernameForm "https://github.com/TogetherAI4/nextjs-ollama-llm-ui/blob/master/src/components/username-form.tsx"
    click EditUsername "https://github.com/TogetherAI4/nextjs-ollama-llm-ui/blob/master/src/components/edit-username-form.tsx"
    click CodeDisplay "https://github.com/TogetherAI4/nextjs-ollama-llm-ui/blob/master/src/components/code-display-block.tsx"
    click EmojiPicker "https://github.com/TogetherAI4/nextjs-ollama-llm-ui/blob/master/src/components/emoji-picker.tsx"
```
