# tamustafa/local_chat_rag architecture

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

- Interactive diagram: https://gitdiagram.com/tamustafa/local_chat_rag
- Repository: https://github.com/tamustafa/local_chat_rag

## Overview

##### Project Type and Purpose
- This is a **full-stack application** for local, document-based question answering (RAG) using open-source LLMs.
- Purpose: upload files (PDF, DOCX), index them in ChromaDB, accept chat queries, retrieve and generate answers with source citations, all locally.

##### File Structure
- Top-level directories:
- `frontend/` – React + Vite + TypeScript + Zustand + Chakra UI
- `backend/` – FastAPI + SQLAlchemy + LangChain + ChromaDB + unstructured
- `data/` – persistent ChromaDB index files + uploaded raw documents
- Backend subfolders:
- `app/api/` – FastAPI routers & endpoints
- `app/db/` – ORM models & session management
- `app/rag/` – RAG pipeline (chunking, embedding, retrieval)
- `app/services/` – business logic services (file upload, chat, admin)
- Notice configuration files: `requirements.txt`, `package.json`, `vite.config.ts`, `.gitignore`.

##### README for Additional Insights
- Key tech stack: Ollama (LLM + embeddings), ChromaDB, LangChain, unstructured libraries.
- Workflow outline: file upload → parsing → embeddings → storage → query → retrieval → LLM answer.

a. **User & Browser**
- Interacts via HTTP at `http://localhost:5173`.
b. **Frontend**
- Vite development server, React components, state stores (Zustand).
- API clients (`chatApi.ts`, `filesApi.ts`, `healthApi.ts`).
c. **Backend API (FastAPI)**
- `/api/files` for uploads, `/api/chat` for queries, `/api/admin` etc.
- Organized in `app/main.py` (entrypoint), `app/api/` (routers), `app/services/` (logic), `app/db/` (ORM).
d. **RAG Pipeline**
- Chunker & parser (unstructured), embedding via Ollama embeddings model, vector store in ChromaDB.
e. **ChromaDB**
- Local vector database under `data/chroma_db`, holds vector index + metadata.
f. **Document Storage**
- Raw file storage under `data/files`.
g. **Ollama Server** (external service)
- Provides LLM for chat (`mistral`) and embedding (`nomic-embed-text`) via HTTP interface.
h. **SQLite** (optional)
- Under Chroma’s files or separate DB for metadata if used (`chroma.sqlite3`).
i. **Local File System**
- Persists uploaded documents & Chroma index.

##### Architectural Patterns and Design Principles
- **Layered Architecture**: UI → API Layer → Service Layer → Data Layer
- **Modular Components**: clear separation of file parsing, embedding, retrieval, chat.
- **Single Monolith with External Service**: backend is one codebase but relies on Ollama microservice.
- **State Management**: frontend uses Zustand for reactive local state.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Layer
    User["User & Browser"]:::ui

    %% Frontend Layer
    subgraph "Frontend (React, Vite, TS)" 
        direction TB
        ViteDev["Vite Dev Server"]:::frontend
        Main["main.tsx"]:::frontend
        AppComp["App.tsx"]:::frontend

        subgraph "UI Components" 
            direction TB
            ChatComp["Chat Component"]:::frontend
            FilesComp["Files Component"]:::frontend
            AdminPanel["AdminPanel.tsx"]:::frontend
            LayoutComp["Layout Components"]:::frontend
        end

        subgraph "API Clients"
            direction TB
            ChatApi["chatApi.ts"]:::frontend
            FilesApi["filesApi.ts"]:::frontend
            HealthApi["healthApi.ts"]:::frontend
        end

        subgraph "State Stores"
            direction TB
            ChatStore["chatStore.ts"]:::frontend
            FilesStore["filesStore.ts"]:::frontend
        end

        packageJson["package.json"]:::frontend
        viteConfig["vite.config.ts"]:::frontend
    end

    %% Backend Layer
    subgraph "Backend API (FastAPI, SQLAlchemy, LangChain)" 
        direction TB
        MainPy["main.py"]:::backend

        subgraph "API Routers & Schemas"
            direction TB
            APIRouters["app/api/"]:::backend
            Schemas["schemas.py"]:::backend
            ErrorHandlers["error_handlers.py"]:::backend
            LogUtils["log_utils.py"]:::backend
        end

        subgraph "Service Layer"
            direction TB
            ChatService["chat_service.py"]:::services
            FileService["file_service.py"]:::services
            AdminService["admin_service.py"]:::services
        end

        subgraph "RAG Pipeline"
            direction TB
            Pipeline["pipeline.py"]:::services
        end

        subgraph "Data Layer"
            direction TB
            Models["models.py"]:::data
            Session["session.py"]:::data
        end

        requirements["requirements.txt"]:::backend
    end

    %% External Service
    Ollama["Ollama Server"]:::external

    %% Data Stores
    ChromaDB["ChromaDB Index (data/chroma_db/)"]:::data
    FilesStoreDisc["File Storage (data/files/)"]:::data

    %% Connections
    User -->|uses| ViteDev
    ViteDev --> Main
    Main --> AppComp
    AppComp --> ChatComp
    AppComp --> FilesComp
    AppComp --> AdminPanel
    AppComp --> LayoutComp

    ChatComp --> ChatApi
    FilesComp --> FilesApi
    AdminPanel --> HealthApi

    ChatComp --> ChatStore
    FilesComp --> FilesStore

    ChatApi --> MainPy
    FilesApi --> MainPy
    HealthApi --> MainPy

    MainPy --> APIRouters
    APIRouters --> ChatService
    APIRouters --> FileService
    APIRouters --> AdminService

    ChatService --> Pipeline
    FileService --> Pipeline
    Pipeline -->|embed calls| Ollama
    Pipeline -->|index vectors| ChromaDB
    ChatService -->|chat calls| Ollama

    Pipeline --> Models
    Pipeline --> Session

    FileService --> FilesStoreDisc

    ChatService --> MainPy
    FileService --> MainPy
    AdminService --> MainPy

    MainPy -->|returns data| ChatApi
    MainPy -->|returns data| FilesApi
    MainPy -->|returns data| HealthApi

    ChatApi --> ChatComp
    FilesApi --> FilesComp

    %% Click Events
    click User "https://github.com/tamustafa/local_chat_rag/tree/main/frontend/"
    click ViteDev "https://github.com/tamustafa/local_chat_rag/blob/main/frontend/vite.config.ts"
    click Main "https://github.com/tamustafa/local_chat_rag/blob/main/frontend/src/main.tsx"
    click AppComp "https://github.com/tamustafa/local_chat_rag/blob/main/frontend/src/App.tsx"
    click ChatComp "https://github.com/tamustafa/local_chat_rag/tree/main/frontend/src/components/Chat/"
    click FilesComp "https://github.com/tamustafa/local_chat_rag/tree/main/frontend/src/components/Files/"
    click AdminPanel "https://github.com/tamustafa/local_chat_rag/blob/main/frontend/src/components/Admin/AdminPanel.tsx"
    click LayoutComp "https://github.com/tamustafa/local_chat_rag/tree/main/frontend/src/components/Layout/"
    click ChatApi "https://github.com/tamustafa/local_chat_rag/blob/main/frontend/src/api/chatApi.ts"
    click FilesApi "https://github.com/tamustafa/local_chat_rag/blob/main/frontend/src/api/filesApi.ts"
    click HealthApi "https://github.com/tamustafa/local_chat_rag/blob/main/frontend/src/api/healthApi.ts"
    click ChatStore "https://github.com/tamustafa/local_chat_rag/blob/main/frontend/src/state/chatStore.ts"
    click FilesStore "https://github.com/tamustafa/local_chat_rag/blob/main/frontend/src/state/filesStore.ts"
    click packageJson "https://github.com/tamustafa/local_chat_rag/blob/main/frontend/package.json"
    click viteConfig "https://github.com/tamustafa/local_chat_rag/blob/main/frontend/vite.config.ts"

    click MainPy "https://github.com/tamustafa/local_chat_rag/blob/main/backend/app/main.py"
    click APIRouters "https://github.com/tamustafa/local_chat_rag/tree/main/backend/app/api/"
    click Schemas "https://github.com/tamustafa/local_chat_rag/blob/main/backend/app/schemas.py"
    click ErrorHandlers "https://github.com/tamustafa/local_chat_rag/blob/main/backend/app/error_handlers.py"
    click LogUtils "https://github.com/tamustafa/local_chat_rag/blob/main/backend/app/log_utils.py"
    click ChatService "https://github.com/tamustafa/local_chat_rag/blob/main/backend/app/services/chat_service.py"
    click FileService "https://github.com/tamustafa/local_chat_rag/blob/main/backend/app/services/file_service.py"
    click AdminService "https://github.com/tamustafa/local_chat_rag/blob/main/backend/app/services/admin_service.py"
    click Pipeline "https://github.com/tamustafa/local_chat_rag/blob/main/backend/app/rag/pipeline.py"
    click Models "https://github.com/tamustafa/local_chat_rag/blob/main/backend/app/db/models.py"
    click Session "https://github.com/tamustafa/local_chat_rag/blob/main/backend/app/db/session.py"
    click requirements "https://github.com/tamustafa/local_chat_rag/blob/main/backend/requirements.txt"
    click ChromaDB "https://github.com/tamustafa/local_chat_rag/tree/main/data/chroma_db/"
    click FilesStoreDisc "https://github.com/tamustafa/local_chat_rag/tree/main/data/files/"

    %% Styles
    classDef ui fill:#D0E8FF,stroke:#69AFFF,color:#000;
    classDef frontend fill:#E0F7FA,stroke:#26C6DA,color:#000;
    classDef backend fill:#E8F5E9,stroke:#66BB6A,color:#000;
    classDef services fill:#C8E6C9,stroke:#43A047,color:#000;
    classDef data fill:#FFF3E0,stroke:#FFB74D,color:#000;
    classDef external fill:#F3E5F5,stroke:#AB47BC,color:#000;
```
