# demarijm/demo-ai-rag architecture

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

- Interactive diagram: https://gitdiagram.com/demarijm/demo-ai-rag
- Repository: https://github.com/demarijm/demo-ai-rag

## Overview

##### Main Components

a) Client Layer (Next.js Frontend):
- Main Next.js application structure
- Workspace and Chat components
- Vercel AI SDK integration
- API routes for chat and file handling

b) Server Layer (Node.js Backend):
- Document processing pipeline
- Context management
- Route handlers
- Storage systems (both local and DO Spaces)

c) External Services:
- Pinecone Vector Database (with namespace handling)
- OpenAI (for embeddings and chat)
- Digital Ocean Spaces (optional storage)

##### Key Flows

a) Document Processing Flow:
**Upload → PDF Processing → Chunking → Embedding → Pinecone Storage**

b) Chat Flow:
**User Query → Context Retrieval → LLM Processing → Response Generation**

**┌─ Client Layer ─────────────────────────┐**
- ┌─ Next.js App ──┐  ┌─ API Routes ──┐ │
- Workspaces   │  │ - /chat       │ │
- Chat UI      │  │ - /files      │ │
- ┘  └──────────────┘ │
- ┘
- ↕
**┌─ Server Layer ────────────────────────┐**
- ┌─ Controllers ─┐  ┌─ Utils ───────┐ │
- Context     │  │ - Embeddings  │ │
- Document    │  │ - Processing  │ │
- ┘  └──────────────┘ │
- ┘
- ↕
**┌─ External Services ─────────────────┐**
- ┌─ Pinecone ───┐  ┌─ OpenAI ────┐ │
- (Namespaces) │  │ - Embedding │ │
- ┘  └────────────┘ │
- ┘

##### Important Details

a) Tenant Isolation:
- How namespaces separate different workspaces
- Document ID prefixing system

b) Document Processing:
- Chunking process
- Batch processing of embeddings
- Upsert operations to Pinecone

c) Context Management:
- Context window handling
- Demonstrate prompt construction flow

##### Technical Specifications

- Embedding dimensions (1536)
- Chunk sizes (min: 500, max: 1500)
- Batch sizes (200 for upserts)
- Used models (text-embedding-3-small)

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Client["Client Layer"]
        direction TB
        NextApp["Next.js Application"]:::frontend
        WorkspaceComp["Workspace Component"]:::frontend
        ChatUI["Chat Interface"]:::frontend
        APIRoutes["API Routes"]:::frontend
        FileAPI["File Upload API"]:::api
        ChatAPI["Chat API"]:::api

        NextApp --> WorkspaceComp
        NextApp --> ChatUI
        NextApp --> APIRoutes
        APIRoutes --> FileAPI
        APIRoutes --> ChatAPI
    end

    subgraph Server["Server Layer"]
        direction TB
        DocProcessor["Document Processing Pipeline"]:::server
        ContextMgmt["Context Management"]:::server
        RouteHandlers["Route Handlers"]:::server
        Storage["Storage Systems"]:::server
        Workers["Document Processing Worker"]:::server
        Utils["Utilities"]:::server
        
        DocProcessor --> Workers
        ContextMgmt --> Utils
        RouteHandlers --> Storage
    end

    subgraph External["External Services"]
        direction TB
        Pinecone["Pinecone Vector DB"]:::external
        OpenAI["OpenAI Service"]:::external
        DOSpaces["Digital Ocean Spaces"]:::external
    end

    subgraph Specs["Technical Specifications"]
        direction TB
        TechSpecs["
        • Embedding: 1536 dimensions
        • Chunks: 500-1500 tokens
        • Batch Size: 200 upserts
        • Model: text-embedding-3-small
        "]:::specs
    end

    %% Flow Connections
    ChatUI --> ChatAPI
    FileAPI --> DocProcessor
    DocProcessor --> OpenAI
    DocProcessor --> Pinecone
    Storage --> DOSpaces
    ContextMgmt --> Pinecone
    ChatAPI --> ContextMgmt

    %% Click Events
    click NextApp "https://github.com/demarijm/demo-ai-rag/tree/master/client/src/app"
    click WorkspaceComp "https://github.com/demarijm/demo-ai-rag/tree/master/client/src/app/(workspace)/workspace"
    click ChatUI "https://github.com/demarijm/demo-ai-rag/tree/master/client/src/components/chat"
    click FileAPI "https://github.com/demarijm/demo-ai-rag/tree/master/client/src/app/api/files"
    click ChatAPI "https://github.com/demarijm/demo-ai-rag/tree/master/client/src/app/api/chat"
    click DocProcessor "https://github.com/demarijm/demo-ai-rag/blob/master/server/src/utils/documentProcessor.ts"
    click ContextMgmt "https://github.com/demarijm/demo-ai-rag/blob/master/server/src/controllers/contextController.ts"
    click RouteHandlers "https://github.com/demarijm/demo-ai-rag/tree/master/server/src/routes"
    click Storage "https://github.com/demarijm/demo-ai-rag/tree/master/server/src/utils/storage"
    click Workers "https://github.com/demarijm/demo-ai-rag/blob/master/server/src/utils/workers/fileProcessorWorker.ts"
    click Utils "https://github.com/demarijm/demo-ai-rag/tree/master/server/src/utils"

    %% Styling
    classDef frontend fill:#2196F3,color:white
    classDef server fill:#4CAF50,color:white
    classDef external fill:#9C27B0,color:white
    classDef api fill:#FF9800,color:white
    classDef specs fill:#757575,color:white

    %% Legend
    subgraph Legend
        direction TB
        Frontend["Frontend Component"]:::frontend
        ServerComp["Server Component"]:::server
        ExternalServ["External Service"]:::external
        APIComp["API Component"]:::api
    end
```
