# apna-college/sigmagpt architecture

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

- Interactive diagram: https://gitdiagram.com/apna-college/sigmagpt
- Repository: https://github.com/apna-college/sigmagpt

## Overview

##### Project Type and Purpose
- SigmaGPT is a full-stack MERN (MongoDB, Express, React, Node.js) application that replicates ChatGPT functionality by proxying requests to OpenAI’s API.
- The goal is to provide a web-based chat interface, store and retrieve conversation threads, and orchestrate OpenAI calls from the backend.

##### File Structure
- Top-level folders:
- Backend – an Express server with routes, Mongoose models, and OpenAI helper
- Frontend – a React/Vite single-page application
- Backend details:
- server.js – entry point, sets up Express, connects to MongoDB, loads routes
- /routes/chat.js – REST API endpoints for messaging and thread retrieval
- /models/Thread.js – Mongoose schema to persist conversation history
- /utils/openai.js – wrapper around OpenAI client configuration and calls
- Frontend details:
- src/App.jsx, Chat.jsx, ChatWindow.jsx, Sidebar.jsx – core React components
- src/MyContext.jsx – React Context for shared state (current thread, messages)
- public/index.html + vite.config.js – bootstrapping the dev/build environment
- Config & deployment files:
- package.json in both layers, .gitignore, ESLint config – standard JS build/tooling

##### Extract Insights from the README
- Confirms MERN stack and OpenAI integration

a. Frontend (React SPA)
- Chat UI components (Sidebar, ChatWindow)
- State management (React Context)
- HTTP client to backend API
b. Backend (Express API Server)
- API Layer (chat route handlers)
- Business Logic (openai.js)
- Data Layer (Thread model with MongoDB)
c. Database (MongoDB Atlas or local MongoDB)
d. External Service (OpenAI API)
e. Deployment/Hosting (optional: Vercel/Netlify for frontend, Heroku/AWS for backend)

##### Relationships and Data Flows
- Frontend → Backend: HTTP POST for new messages, GET for thread history
- Backend → OpenAI API: REST calls with chat history, receives AI response
- Backend → Database:
- Save incoming user messages + AI replies into Thread documents
- Query Thread collection for history on client request
- Backend → Frontend: returns JSON payloads with updated thread data

##### Architectural Patterns & Design Principles
- Separation of concerns: UI, API, business logic, persistence
- Stateless API servers with state stored in MongoDB
- Use of React Context for client-side state propagation
- Single-page application communicating via REST endpoints

##### Technologies & Frameworks to Annotate
- React + Vite, React Context, CSS modules
- Node.js, Express.js, Mongoose
- OpenAI SDK
- MongoDB (with connection URI in environment variables)

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% User layer
    Browser["User Browser"]:::external

    %% Frontend Layer
    subgraph "Frontend (React SPA)"
        FDir["Frontend/"]:::api
        IndexHTML["index.html"]:::ui
        ViteConfig["vite.config.js"]:::ui
        Main["main.jsx"]:::ui
        App["App.jsx"]:::ui
        ChatComp["Chat.jsx"]:::ui
        ChatWindow["ChatWindow.jsx"]:::ui
        Sidebar["Sidebar.jsx"]:::ui
        MyContext["MyContext.jsx"]:::ui
    end

    %% Backend Layer
    subgraph "Backend (Express API Server)"
        BDir["Backend/"]:::api
        Server["server.js"]:::api
        Routes["chat.js"]:::api
        OpenAILogic["openai.js"]:::api
        ThreadModel["Thread.js"]:::db
        BackendPkg["package.json"]:::api
    end

    %% Database and External Services
    DB["MongoDB"]:::db
    OpenAI["OpenAI API"]:::external
    RootReadme["README.md"]:::api
    GitIgnore[".gitignore"]:::api

    %% Connections
    Browser -->|"GET /"| IndexHTML
    IndexHTML -->|"Loads App"| App
    App --> MyContext
    App --> ChatComp
    App --> Sidebar
    ChatComp --> ChatWindow
    ChatComp --> MyContext
    ChatComp -->|"POST /chat"| Server
    ChatComp -->|"GET /threads"| Server

    Server --> Routes
    Server --> ThreadModel
    Server --> DB
    Routes --> OpenAILogic
    Routes --> ThreadModel
    OpenAILogic --> OpenAI
    ThreadModel --> DB

    %% Config files
    BDir -.-> BackendPkg
    BDir -.-> Server
    BDir -.-> Routes
    BDir -.-> OpenAILogic
    BDir -.-> ThreadModel
    FDir -.-> IndexHTML
    FDir -.-> ViteConfig
    FDir -.-> Main
    FDir -.-> App
    FDir -.-> ChatComp
    FDir -.-> ChatWindow
    FDir -.-> Sidebar
    FDir -.-> MyContext
    RootReadme --- GitIgnore

    %% Click Events
    click FDir "https://github.com/apna-college/sigmagpt/tree/main/Frontend/"
    click IndexHTML "https://github.com/apna-college/sigmagpt/blob/main/Frontend/index.html"
    click ViteConfig "https://github.com/apna-college/sigmagpt/blob/main/Frontend/vite.config.js"
    click Main "https://github.com/apna-college/sigmagpt/blob/main/Frontend/src/main.jsx"
    click App "https://github.com/apna-college/sigmagpt/blob/main/Frontend/src/App.jsx"
    click ChatComp "https://github.com/apna-college/sigmagpt/blob/main/Frontend/src/Chat.jsx"
    click ChatWindow "https://github.com/apna-college/sigmagpt/blob/main/Frontend/src/ChatWindow.jsx"
    click Sidebar "https://github.com/apna-college/sigmagpt/blob/main/Frontend/src/Sidebar.jsx"
    click MyContext "https://github.com/apna-college/sigmagpt/blob/main/Frontend/src/MyContext.jsx"
    click BDir "https://github.com/apna-college/sigmagpt/tree/main/Backend/"
    click Server "https://github.com/apna-college/sigmagpt/blob/main/Backend/server.js"
    click Routes "https://github.com/apna-college/sigmagpt/blob/main/Backend/routes/chat.js"
    click OpenAILogic "https://github.com/apna-college/sigmagpt/blob/main/Backend/utils/openai.js"
    click ThreadModel "https://github.com/apna-college/sigmagpt/blob/main/Backend/models/Thread.js"
    click BackendPkg "https://github.com/apna-college/sigmagpt/blob/main/Backend/package.json"
    click RootReadme "https://github.com/apna-college/sigmagpt/blob/main/README.md"
    click GitIgnore "https://github.com/apna-college/sigmagpt/blob/main/.gitignore"

    %% Styles
    classDef ui fill:#D0E8FF,stroke:#0366d6,color:#0366d6
    classDef api fill:#D4F8E8,stroke:#059669,color:#059669
    classDef db fill:#FFE5B4,stroke:#EA580C,color:#92400E
    classDef external fill:#E9D5FF,stroke:#7C3AED,color:#5B21B6
```
