# srivatsav-k/rag-chat-pgvector architecture

> Architecture diagram of the GitHub repository srivatsav-k/rag-chat-pgvector, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/srivatsav-k/rag-chat-pgvector
- Repository: https://github.com/srivatsav-k/rag-chat-pgvector

## Overview

<explanation>
1. Identify the project type and purpose  
   - This is a full‑stack RAG (Retrieval‑Augmented Generation) chatbot starter built on Next.js 14 (App Router).  
   - It uses the Vercel AI SDK and OpenAI for LLM calls, Drizzle ORM with Postgres+pgvector for embedding storage, and shadcn‑ui/TailwindCSS for styling.  

2. Analyze the file structure  
   - **app/**: Next.js App Router entrypoints  
     • app/page.tsx, app/layout.tsx, app/globals.css – your React UI and global styles  
     • app/api/chat/route.ts – serverless API endpoint for chat functionality  
   - **components/**: UI primitives (button.tsx, input.tsx, label.tsx) used by pages  
   - **lib/**: core business logic  
     • lib/ai/embedding.ts – embedding utilities  
     • lib/actions/resources.ts – load/store resource texts and embeddings  
     • lib/db/** – Drizzle ORM setup, migrations, schema definitions  
     • lib/utils.ts – shared helpers  
   - **drizzle.config.ts**, **next.config.mjs**, **tailwind.config.ts**, **tsconfig.json** – build/config files  
   - **public/** – static assets  

3. Examine the README for additional insights  
   - Confirms the use of Next.js App Router, Vercel AI SDK, OpenAI, Drizzle ORM, Postgres+pgvector, and shadcn‑ui/TailwindCSS.  
   - Describes a chatbot that “will only respond with information in its knowledge base,” implying a RAG flow: ingest → embed → store → retrieve → generate.  

4. How to create the system design diagram  

   a. Main components  
      • Frontend (Next.js React pages & UI components)  
      • Backend/API (Next.js serverless route + business logic in lib/)  
      • Database (Postgres instance with pgvector extension via Drizzle ORM)  
      • External LLM service (OpenAI)  
      • Developer environment (env vars, config files)  

   b. Relationships & interactions  
      1. User types a message in the UI →  
      2. Frontend sends HTTP POST to `/api/chat` →  
      3. API handler (app/api/chat/route.ts)  
         – calls lib/actions/resources to retrieve relevant docs from DB  
         – calls lib/ai/embedding to compute user query embedding  
         – queries Postgres via Drizzle ORM for nearest‑neighbor embeddings  
         – sends context + query to OpenAI via Vercel AI SDK → receives a response  
         – persists new Q&A pair or updated docs back to DB  
      4. API returns the generated chat response → Frontend renders it  

   c. Architectural patterns & design principles  
      - _Separation of concerns_: UI vs serverless API vs data layer vs AI logic  
      - _Serverless functions_: Next.js App Router edge or lambda for the chat endpoint  
      - _Modular lib folder_: pluggable actions (resources, embedding) and DB logic  
      - _RAG pipeline_: embed → store → retrieve → generate  
      - _Infrastructure as code_-style migrations with Drizzle  

   d. Relevant technologies  
      - Next.js 14 App Router (React, server components)  
      - Vercel AI SDK (LLM orchestration)  
      - OpenAI API (model calls)  
      - Drizzle ORM + Postgres + pgvector (vector storage & search)  
      - TailwindCSS & shadcn‑ui (styling & components)  

5. Tailoring the diagram for this full‑stack RAG application  
   - Show clear separation:  
     • Frontend (browser) → HTTP/HTTPS → API layer (serverless) → Data store & LLM service  
   - Emphasize the RAG flow: ingestion of resources, embedding, vector search, LLM generation  
   - Illustrate config boundaries (env vars for DB URL, OpenAI key, etc.)  

6. Diagram elements to include  
   - **Shapes & colors**:  
     • Rectangles (UI, API, library modules)  
     • Cylinder for the Postgres DB  
     • Cloud icon for OpenAI service  
   - **Labels**:  
     • UI components, API route, lib/actions, lib/ai, lib/db  
     • HTTP arrows labeled “POST /api/chat”  
     • Data flow arrows for “embed → vector search → generate → store”  
   - **Directional arrows**: clearly show request/response paths and internal module calls  
   - **Color coding**:  
     • UI layer (e.g., blue)  
     • Server/API & lib modules (e.g., green)  
     • Data layer (e.g., orange)  
     • External services (e.g., gray/cloud)  

7. Tips for maximum accuracy  
   - Drill down one level: under “API,” break out lib/actions/resources vs lib/ai/embedding vs lib/db interactions  
   - Annotate with file names or module names (e.g., “route.ts → resources.ts → embedding.ts”)  
   - Don’t skip the migration folder—show that Drizzle migrations drive the schema in Postgres.  

By following these steps and using the above points as a checklist, you’ll produce a detailed, accurate system‑design diagram that reflects every critical piece of this RAG chatbot starter project.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Frontend Layer
    subgraph "Frontend UI" 
        direction TB
        UI_Page["app/page.tsx"]:::frontend
        UI_Layout["app/layout.tsx"]:::frontend
        UI_Styles["app/globals.css"]:::frontend
        UI_Primitives["UI Primitives"]:::frontend
        UI_Page --> UI_Layout
        UI_Layout --> UI_Styles
        UI_Layout --> UI_Primitives
    end

    %% API Layer
    subgraph "Serverless API" 
        direction TB
        API_Route["app/api/chat/route.ts"]:::api
        Lib_Resources["lib/actions/resources.ts"]:::api
        Lib_Embedding["lib/ai/embedding.ts"]:::api
        Lib_Utils["lib/utils.ts"]:::api
        API_Route -->|calls resources| Lib_Resources
        API_Route -->|calls embedding| Lib_Embedding
        API_Route -->|uses utils| Lib_Utils
    end

    %% DB Layer
    subgraph "Data Layer" 
        direction TB
        DB["Postgres + pgvector"]:::data
        DB_Migrate["lib/db/migrate.ts"]:::data
        DB_Index["lib/db/index.ts"]:::data
        DB_Schema_Emb["lib/db/schema/embeddings.ts"]:::data
        DB_Schema_Res["lib/db/schema/resources.ts"]:::data
        Migrations["lib/db/migrations/*"]:::data
        DB_Migrate --> DB_Index
        DB_Index --> DB_Schema_Emb
        DB_Index --> DB_Schema_Res
        DB_Migrate --> Migrations
        DB_Index --> DB
    end

    %% External Services
    subgraph "External" 
        direction TB
        OpenAI["OpenAI API"]:::external
    end

    %% Config & Env
    subgraph "Configuration"
        direction TB
        Env[".env.example\nlib/env.mjs"]:::config
        Configs["drizzle.config.ts\nnext.config.mjs\ntailwind.config.ts\ntsconfig.json"]:::config
    end

    %% Flow
    User["User"]:::external
    User -->|"types message"| UI_Page
    UI_Page -->|"POST /api/chat"| API_Route
    Lib_Embedding -->|embed query| DB
    Lib_Resources -->|vector search| DB
    API_Route -->|"send context+query"| OpenAI
    OpenAI -->|"LLM response"| API_Route
    API_Route -->|"store Q&A"| Lib_Resources
    API_Route -->|"response"| UI_Page

    %% Click Events
    click UI_Page "https://github.com/srivatsav-k/rag-chat-pgvector/blob/main/app/page.tsx"
    click UI_Layout "https://github.com/srivatsav-k/rag-chat-pgvector/blob/main/app/layout.tsx"
    click UI_Styles "https://github.com/srivatsav-k/rag-chat-pgvector/blob/main/app/globals.css"
    click API_Route "https://github.com/srivatsav-k/rag-chat-pgvector/blob/main/app/api/chat/route.ts"
    click UI_Primitives "https://github.com/srivatsav-k/rag-chat-pgvector/tree/main/components/ui"
    click Lib_Resources "https://github.com/srivatsav-k/rag-chat-pgvector/blob/main/lib/actions/resources.ts"
    click Lib_Embedding "https://github.com/srivatsav-k/rag-chat-pgvector/blob/main/lib/ai/embedding.ts"
    click DB_Index "https://github.com/srivatsav-k/rag-chat-pgvector/blob/main/lib/db/index.ts"
    click DB_Migrate "https://github.com/srivatsav-k/rag-chat-pgvector/blob/main/lib/db/migrate.ts"
    click Migrations "https://github.com/srivatsav-k/rag-chat-pgvector/tree/main/lib/db/migrations"
    click DB_Schema_Emb "https://github.com/srivatsav-k/rag-chat-pgvector/blob/main/lib/db/schema/embeddings.ts"
    click DB_Schema_Res "https://github.com/srivatsav-k/rag-chat-pgvector/blob/main/lib/db/schema/resources.ts"
    click Lib_Utils "https://github.com/srivatsav-k/rag-chat-pgvector/blob/main/lib/utils.ts"
    click Env "https://github.com/srivatsav-k/rag-chat-pgvector/blob/main/lib/env.mjs"
    click Env "https://github.com/srivatsav-k/rag-chat-pgvector/blob/main/.env.example"
    click Configs "https://github.com/srivatsav-k/rag-chat-pgvector/blob/main/drizzle.config.ts"

    %% Styles
    classDef frontend fill:#D0E8FF,stroke:#0366d6,color:#0366d6;
    classDef api fill:#E6F4EA,stroke:#1B8F5E,color:#1B8F5E;
    classDef data fill:#FFE8CC,stroke:#D97706,color:#B45309;
    classDef external fill:#F0F0F0,stroke:#6B7280,color:#4B5563;
    classDef config fill:#F5E1FF,stroke:#9333EA,color:#6B21A8;
```
