# princetechs/rails_rag_system architecture

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

- Interactive diagram: https://gitdiagram.com/princetechs/rails_rag_system
- Repository: https://github.com/princetechs/rails_rag_system

## Overview

##### Main Components

A. Frontend Layer:
- Rails Views layer (/app/views)
- JavaScript components (Importmaps and Stimulus controllers)
- PWA capabilities (manifest.json and service-worker.js)

B. Application Layer:
- Controllers (Documents, Sessions, Users, Passwords)
- Models (Document, User, Session)
- Authentication system
- Mailer system (PasswordsMailer)

C. Database Layer:
- PostgreSQL database (indicated by vector extension)
- Active Storage for file handling
- Vector embeddings storage (for AI/ML features)

D. External Services:
- OpenAI integration (shown by config/initializers/openai.rb)
- Active Storage service

E. Background Processing:
- Job processing system (indicated by bin/jobs)
- Queue system (config/queue.yml)

**┌─ Client Layer ─────────────────────┐**
- Web Browsers                     │
- Progressive Web App              │
- ┬───────────────────┘
- ↓
**┌─ Application Layer ─────────────────────────────────┐**
- ┌─ Web Layer ─────┐  ┌─ Business Logic ─────────┐  │
- Controllers   │  │ - Models                 │  │
- Views        │  │ - Services               │  │
- Assets       │  │ - Background Jobs        │  │
- ┘  └────────────────────────────┘ │
- ┬───────────────────────────────────┘
- ↓
**┌─ Data Layer ──────────────────────┐**
- PostgreSQL + Vector Extension   │
- Active Storage                  │
- Cache                          │
- ┬──────────────────┘
- ↓
**┌─ External Services ───────────────┐**
- OpenAI API                     │
- Storage Service               │
- ┘

##### Key Relationships to Highlight

- Authentication flow between Users, Sessions, and Controllers
- Document processing flow including vector embeddings
- Background job processing for async operations
- File storage flow through Active Storage
- OpenAI integration for document processing

##### Technical Details

- Ruby on Rails framework
- Stimulus for JavaScript
- PostgreSQL with vector extension
- Active Storage
- Background job processing
- OpenAI integration
- PWA capabilities

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Client
        Browser["Web Browser"]
        PWA["Progressive Web App"]:::pwa
        click PWA "https://github.com/princetechs/rails_rag_system/blob/main/app/views/pwa/manifest.json.erb"
    end

    subgraph AppLayer["Application Layer"]
        subgraph WebLayer["Web Layer"]
            Controllers["Controllers"]:::controller
            Views["Views"]:::view
            Assets["Assets & JS"]:::assets
            click Controllers "https://github.com/princetechs/rails_rag_system/tree/main/app/controllers"
            click Views "https://github.com/princetechs/rails_rag_system/tree/main/app/views"
            click Assets "https://github.com/princetechs/rails_rag_system/tree/main/app/javascript/controllers"
        end

        subgraph BusinessLogic["Business Logic"]
            Models["Models"]:::model
            Auth["Authentication"]:::security
            Mailer["Password Mailer"]:::mailer
            Jobs["Background Jobs"]:::job
            click Models "https://github.com/princetechs/rails_rag_system/tree/main/app/models"
            click Auth "https://github.com/princetechs/rails_rag_system/blob/main/app/controllers/concerns/authentication.rb"
            click Mailer "https://github.com/princetechs/rails_rag_system/blob/main/app/mailers/passwords_mailer.rb"
            click Jobs "https://github.com/princetechs/rails_rag_system/blob/main/app/jobs/application_job.rb"
        end
    end

    subgraph DataLayer["Data Layer"]
        DB[(PostgreSQL)]:::database
        Vector["Vector Extension"]:::vector
        ActiveStorage["Active Storage"]:::storage
        Cache["Cache"]:::cache
        click Vector "https://github.com/princetechs/rails_rag_system/blob/main/db/migrate/20250118215731_enable_vector_extension.rb"
        click ActiveStorage "https://github.com/princetechs/rails_rag_system/blob/main/db/migrate/20250118203322_create_active_storage_tables.active_storage.rb"
    end

    subgraph ExternalServices["External Services"]
        OpenAI["OpenAI API"]:::external
        StorageService["Storage Service"]:::external
        click OpenAI "https://github.com/princetechs/rails_rag_system/blob/main/config/initializers/openai.rb"
    end

    subgraph Infrastructure["Infrastructure"]
        Docker["Docker"]:::infra
        Kamal["Kamal Deploy"]:::infra
        click Docker "https://github.com/princetechs/rails_rag_system/tree/main/Dockerfile"
        click Kamal "https://github.com/princetechs/rails_rag_system/blob/main/.kamal"
    end

    Browser --> PWA
    PWA --> Controllers
    Controllers --> Models
    Controllers --> Views
    Controllers --> Auth
    Models --> DB
    Models --> Vector
    Models --> ActiveStorage
    Models --> OpenAI
    ActiveStorage --> StorageService
    Jobs --> Models
    Auth --> Models
    Mailer --> Models

    %% Styles
    classDef controller fill:#90EE90,stroke:#333,stroke-width:2px
    classDef model fill:#98FB98,stroke:#333,stroke-width:2px
    classDef view fill:#87CEEB,stroke:#333,stroke-width:2px
    classDef assets fill:#ADD8E6,stroke:#333,stroke-width:2px
    classDef database fill:#FFA500,stroke:#333,stroke-width:2px
    classDef storage fill:#FFB84D,stroke:#333,stroke-width:2px
    classDef cache fill:#FFD700,stroke:#333,stroke-width:2px
    classDef vector fill:#FF8C00,stroke:#333,stroke-width:2px
    classDef external fill:#DDA0DD,stroke:#333,stroke-width:2px
    classDef security fill:#FF6B6B,stroke:#333,stroke-width:2px
    classDef mailer fill:#20B2AA,stroke:#333,stroke-width:2px
    classDef job fill:#98FB98,stroke:#333,stroke-width:2px
    classDef pwa fill:#B0C4DE,stroke:#333,stroke-width:2px
    classDef infra fill:#A9A9A9,stroke:#333,stroke-width:2px
```
