# rfypych/neuro-ai-ollama architecture

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

- Interactive diagram: https://gitdiagram.com/rfypych/neuro-ai-ollama
- Repository: https://github.com/rfypych/neuro-ai-ollama

## Overview

##### Project Type and Purpose
- This is a full-stack web application built with Next.js (using the new “app” directory structure) that serves as a self-hosted chatbot interface.
- Its purpose is to allow Indonesian users to interact with large language models hosted locally via Ollama, with features including streaming responses, model switching, file uploads, and integrated API security.

##### File Structure
- The top-level structure divides clearly between the Next.js application (served from the “app” directory) and configuration/deployment files (Dockerfile, docker-compose.yml, nginx configuration).
- The “app” directory holds route handlers, pages (e.g., chat, admin, bug-report), and shared UI components (in the “app/components” subdirectory).
- The “app/api” directory contains API routes, such as the chat endpoint.
- There’s a “prisma” folder which suggests use of a relational database managed through Prisma.
- “lib” and “utils” directories provide support utilities like rate limiting, theming, and debugging.
- The presence of Docker and nginx configuration files indicates containerized deployment and use of a reverse proxy.

##### README for Additional Technical Insights
- The README clearly explains the self-hosted Chatbot functionality, prerequisites (Node.js, Ollama installation), and deployment via Docker.
- It highlights key features such as real-time LLM integration, streaming responses (token-by-token), API security (API key auth, rate limiting), and system prompt customization.
- It also details specific configurations like timeout settings for API requests and chunk streaming.

##### Main Components
a. Frontend (Client):
- Next.js client-side application which includes the interactive UI (pages like chat, admin, bug-report) and components (ChatWindow, Navbar, Layout, Sidebar, ThemeToggle).
- That the client communicates with the backend API endpoints to handle chat interactions.
b. Backend (Server):
- Next.js server layer that handles both SSR (server-side rendering) and API routes (located in “app/api”).
- Middleware for authentication, rate limiting, and error handling.
c. Database:
- Database used via Prisma (details of the underlying DB engine aren’t specified, but it’s managed via the “prisma/schema.prisma” file).
- That database interactions (e.g., for chat histories or configuration) are abstracted through Prisma.
d. External Services:
- This as a key external dependency communicating over HTTP (or similar protocols) with the backend.
e. Deployment and Infrastructure:
- Docker and docker-compose for containerization.
- Nginx as the reverse proxy or load balancer in front of the Next.js application.
- Optionally, show the caching/file storage mechanisms (like Next.js’ internal caching in the .next folder) as part of the deployment pipeline.

##### Architectural Patterns and Design Elements
- Use a layered approach: Separate client, server API, database, and external integration.
- Interaction flows: For example, user actions from the Next.js frontend trigger API requests to the server, which then calls the local Ollama service and stores or retrieves data from the database.
- Patterns like rate limiting and request timeout management (as described in the README).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Layer
    U("User"):::ui

    %% Deployment & Infrastructure
    subgraph "Deployment & Infrastructure"
        Nginx["Nginx Reverse Proxy"]:::infra
        Docker["Docker Containerization"]:::infra
        Compose["Docker Compose"]:::infra
    end

    %% Frontend Layer
    subgraph "Frontend"
        UI["Next.js Client/UI"]:::ui
        Theme["Theming and UI Support"]:::ui
    end

    %% Backend Layer
    subgraph "Backend"
        API["Next.js API Server"]:::server
        RL["Rate Limiting Utility"]:::utility
        DB[("Prisma Managed Database")]:::db
    end

    %% External Service
    OLLAMA(("Ollama LLM Service")):::external

    %% Connections from User to Deployment
    U -->|"sends request"| Nginx
    Nginx -->|"forwards to"| Docker
    Docker -->|"orchestrates"| Compose
    Docker -->|"hosts"| UI
    Docker -->|"hosts"| API

    %% Frontend interactions
    Theme -->|"supports"| UI
    UI -->|"triggers API call"| API

    %% Backend interactions
    API -->|"applies"| RL
    API -->|"queries"| DB
    API -->|"calls"| OLLAMA
    DB -->|"returns data"| API
    OLLAMA -->|"responds"| API
    API -->|"sends response"| UI
    UI -->|"renders output"| U

    %% Click Events
    click UI "https://github.com/rfypych/neuro-ai-ollama/tree/main/app"
    click API "https://github.com/rfypych/neuro-ai-ollama/tree/main/app/api"
    click DB "https://github.com/rfypych/neuro-ai-ollama/blob/main/prisma/schema.prisma"
    click RL "https://github.com/rfypych/neuro-ai-ollama/blob/main/lib/rate-limit.ts"
    click Theme "https://github.com/rfypych/neuro-ai-ollama/blob/main/lib/theme-provider.tsx"
    click Docker "https://github.com/rfypych/neuro-ai-ollama/tree/main/Dockerfile"
    click Compose "https://github.com/rfypych/neuro-ai-ollama/blob/main/docker-compose.yml"
    click Nginx "https://github.com/rfypych/neuro-ai-ollama/blob/main/nginx/nginx.conf"

    %% Styles
    classDef ui fill:#B3E5FC,stroke:#0277BD,stroke-width:2px;
    classDef server fill:#C8E6C9,stroke:#388E3C,stroke-width:2px;
    classDef db fill:#FFF9C4,stroke:#FBC02D,stroke-width:2px;
    classDef external fill:#FFCDD2,stroke:#C62828,stroke-width:2px;
    classDef infra fill:#D1C4E9,stroke:#7B1FA2,stroke-width:2px;
    classDef utility fill:#FFECB3,stroke:#F57C00,stroke-width:2px;
```
