# immohitsen/rag-chat architecture

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

- Interactive diagram: https://gitdiagram.com/immohitsen/rag-chat
- Repository: https://github.com/immohitsen/rag-chat

## Overview

#### Purpose and entry points
Maester is a multi-user document RAG chat system: users upload documents, select files as query context, and receive Groq/Llama-generated answers with retrieved-source citations. The FastAPI application starts in `backend/main.py`; the React client is rooted at `frontend/app/page.jsx` and composes chat/upload UI from `frontend/src/components/`.

#### Frontend client and session state
The frontend provides authentication, document selection/upload, conversation history, and streaming/request-response chat interactions. `frontend/src/components/ChatInterface.jsx` is the primary interaction surface; `frontend/src/services/api.js` defines the client-to-backend boundary. Authentication and theme state are held in `frontend/src/context/AuthContext.jsx` and `frontend/src/context/ThemeContext.jsx`.

#### API routing and request boundaries
FastAPI routes partition public health checks, JWT auth, document operations, chats, persisted history, and logs. Core route modules are `backend/routers/auth.py`, `backend/routers/chat.py`, and `backend/routers/upload.py`; file retrieval and conversation management reside in `backend/routers/files.py` and `backend/routers/history.py`. Requests pass JWT dependency enforcement before user-scoped operations via `backend/auth/dependencies.py`.

#### Identity and tenant isolation
User registration/login creates and validates bcrypt password credentials and issues 15-day JWTs. The auth implementation is centered in `backend/auth/password.py` and `backend/auth/jwt_handler.py`. User identity is propagated from route dependencies into database, vector retrieval, file, and history operations so document and conversation data remain user-isolated.

#### Ingestion and document storage
Upload handling accepts supported office/text formats, stores original files in AWS S3, then loads and chunks content for retrieval. The ingestion pipeline is represented by `backend/routers/upload.py` and `backend/src/data_loader.py`. Original-document download is a separate file-service boundary in `backend/routers/files.py`; S3 credentials, bucket, and region are runtime configuration.

#### Retrieval and answer generation
Document chunks are embedded with Sentence Transformers `all-MiniLM-L6-v2`, stored/searched through MongoDB Atlas Vector Search, and filtered by user and selected file context. `backend/src/embedding.py`, `backend/src/mongodb_store.py`, and `backend/src/search.py` define this retrieval layer. `backend/services/rag_service.py` orchestrates retrieval, citation/confidence construction, conversation summary memory, and Llama 3.1 inference through Groq.

#### Query routing and persistence
Before retrieval, intent classification distinguishes conversational requests from document-grounded questions, avoiding unnecessary RAG calls for chitchat. This decision stage is in `backend/src/intent_classifier.py`; chat execution is exposed by `backend/routers/chat.py`. MongoDB-backed application models define users, files, chunks, chats, messages, and related persisted metadata in `backend/models/db_models.py` and request/response contracts in `backend/models/schemas.py`.

#### Deployment runtime
The backend is packaged as a Docker-based AWS Lambda deployment, with deployment artifacts in `backend/Dockerfile`, `backend/apprunner.yaml`, and `backend/deploy-lambda.sh`. It requires Groq, MongoDB Atlas, S3, JWT secret, and AWS-region environment variables. The frontend is independently deployable to Vercel through `frontend/vercel.json`; it targets the FastAPI `/api` endpoint.

Main entry points: [`backend/main.py`](https://github.com/immohitsen/rag-chat/blob/main/backend/main.py) (FastAPI application), [`backend/routers/upload.py`](https://github.com/immohitsen/rag-chat/blob/main/backend/routers/upload.py) (Upload & file routes), [`backend/services/rag_service.py`](https://github.com/immohitsen/rag-chat/blob/main/backend/services/rag_service.py) (RAG orchestration), [`frontend/app/page.jsx`](https://github.com/immohitsen/rag-chat/blob/main/frontend/app/page.jsx) (Client app)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["React client"]
  node_web_app["Client app<br/>React entry<br/>[page.jsx]"]
  node_chat_ui["Chat &amp; upload UI<br/>React components<br/>[ChatInterface.jsx]"]
  node_api_client["API client<br/>HTTP boundary<br/>[api.js]"]
  node_auth_state["Auth state<br/>React context<br/>[AuthContext.jsx]"]
  node_theme_state["Theme state<br/>React context<br/>[ThemeContext.jsx]"]
end

subgraph group_api["FastAPI API"]
  node_fastapi["FastAPI application<br/>API entry<br/>[main.py]"]
  node_auth_routes["Auth routes<br/>route module<br/>[auth.py]"]
  node_chat_routes["Chat routes<br/>route module<br/>[chat.py]"]
  node_document_routes["Upload &amp; file routes<br/>route modules<br/>[upload.py]"]
  node_history_routes["History routes<br/>route module<br/>[history.py]"]
  node_jwt_guard["JWT user guard<br/>dependency<br/>[dependencies.py]"]
end

subgraph group_rag["RAG pipeline"]
  node_ingestion["Document ingestion<br/>loader &amp; chunker<br/>[data_loader.py]"]
  node_embeddings["Embeddings<br/>Sentence Transformer<br/>[embedding.py]"]
  node_vector_search["Filtered vector search<br/>retrieval service<br/>[search.py]"]
  node_intent["Intent classifier<br/>query router"]
  node_rag_service["RAG orchestration<br/>answer service<br/>[rag_service.py]"]
  node_groq{{"Groq Llama 3.1<br/>LLM inference"}}
end

subgraph group_data["Data &amp; identity"]
  node_credentials["Passwords &amp; JWTs<br/>identity service<br/>[jwt_handler.py]"]
  node_mongo[("MongoDB Atlas<br/>metadata &amp; vector store<br/>[mongodb_store.py]")]
  node_s3[("AWS S3<br/>original document store")]
end

subgraph group_deploy["Deployment"]
  node_backend_deploy["Lambda container deployment<br/>AWS deployment"]
  node_frontend_deploy["Vercel deployment<br/>frontend deployment<br/>[vercel.json]"]
end

node_web_app -->|"composes"| node_chat_ui
node_web_app -->|"uses"| node_auth_state
node_web_app -->|"uses"| node_theme_state
node_chat_ui -->|"requests"| node_api_client
node_api_client -->|"/api"| node_fastapi
node_fastapi -->|"routes"| node_auth_routes
node_fastapi -->|"routes"| node_chat_routes
node_fastapi -->|"routes"| node_document_routes
node_fastapi -->|"routes"| node_history_routes
node_auth_routes -->|"registers and logs in"| node_credentials
node_chat_routes -->|"authenticates"| node_jwt_guard
node_document_routes -->|"authenticates"| node_jwt_guard
node_history_routes -->|"authenticates"| node_jwt_guard
node_document_routes -->|"stores/downloads originals"| node_s3
node_document_routes -->|"loads uploads"| node_ingestion
node_ingestion -->|"chunks for embedding"| node_embeddings
node_embeddings -->|"stores vectors"| node_mongo
node_chat_routes -->|"classifies query"| node_intent
node_intent -->|"document question"| node_rag_service
node_rag_service -->|"retrieves context"| node_vector_search
node_vector_search -->|"Atlas Vector Search"| node_mongo
node_rag_service -->|"generates answer"| node_groq
node_rag_service -->|"reads/writes chat memory"| node_mongo
node_history_routes -->|"loads conversations"| node_mongo
node_backend_deploy -->|"packages"| node_fastapi
node_frontend_deploy -->|"deploys"| node_web_app

click node_web_app "https://github.com/immohitsen/rag-chat/blob/main/frontend/app/page.jsx"
click node_chat_ui "https://github.com/immohitsen/rag-chat/blob/main/frontend/src/components/ChatInterface.jsx"
click node_api_client "https://github.com/immohitsen/rag-chat/blob/main/frontend/src/services/api.js"
click node_auth_state "https://github.com/immohitsen/rag-chat/blob/main/frontend/src/context/AuthContext.jsx"
click node_theme_state "https://github.com/immohitsen/rag-chat/blob/main/frontend/src/context/ThemeContext.jsx"
click node_fastapi "https://github.com/immohitsen/rag-chat/blob/main/backend/main.py"
click node_auth_routes "https://github.com/immohitsen/rag-chat/blob/main/backend/routers/auth.py"
click node_chat_routes "https://github.com/immohitsen/rag-chat/blob/main/backend/routers/chat.py"
click node_document_routes "https://github.com/immohitsen/rag-chat/blob/main/backend/routers/upload.py"
click node_history_routes "https://github.com/immohitsen/rag-chat/blob/main/backend/routers/history.py"
click node_jwt_guard "https://github.com/immohitsen/rag-chat/blob/main/backend/auth/dependencies.py"
click node_credentials "https://github.com/immohitsen/rag-chat/blob/main/backend/auth/jwt_handler.py"
click node_ingestion "https://github.com/immohitsen/rag-chat/blob/main/backend/src/data_loader.py"
click node_embeddings "https://github.com/immohitsen/rag-chat/blob/main/backend/src/embedding.py"
click node_vector_search "https://github.com/immohitsen/rag-chat/blob/main/backend/src/search.py"
click node_intent "https://github.com/immohitsen/rag-chat/blob/main/backend/src/intent_classifier.py"
click node_rag_service "https://github.com/immohitsen/rag-chat/blob/main/backend/services/rag_service.py"
click node_mongo "https://github.com/immohitsen/rag-chat/blob/main/backend/src/mongodb_store.py"
click node_backend_deploy "https://github.com/immohitsen/rag-chat/blob/main/backend/Dockerfile"
click node_frontend_deploy "https://github.com/immohitsen/rag-chat/blob/main/frontend/vercel.json"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_web_app,node_chat_ui,node_api_client,node_auth_state,node_theme_state toneBlue
class node_fastapi,node_auth_routes,node_chat_routes,node_document_routes,node_history_routes,node_jwt_guard toneAmber
class node_ingestion,node_embeddings,node_vector_search,node_intent,node_rag_service,node_groq toneMint
class node_credentials,node_mongo,node_s3 toneRose
class node_backend_deploy,node_frontend_deploy toneIndigo
```

## Components

### React client

- [Client app](https://github.com/immohitsen/rag-chat/blob/main/frontend/app/page.jsx): React entry · `frontend/app/page.jsx`
- [Chat & upload UI](https://github.com/immohitsen/rag-chat/blob/main/frontend/src/components/ChatInterface.jsx): React components · Primary user interaction surface. · `frontend/src/components/ChatInterface.jsx`
- [API client](https://github.com/immohitsen/rag-chat/blob/main/frontend/src/services/api.js): HTTP boundary · `frontend/src/services/api.js`
- [Auth state](https://github.com/immohitsen/rag-chat/blob/main/frontend/src/context/AuthContext.jsx): React context · `frontend/src/context/AuthContext.jsx`
- [Theme state](https://github.com/immohitsen/rag-chat/blob/main/frontend/src/context/ThemeContext.jsx): React context · `frontend/src/context/ThemeContext.jsx`

### FastAPI API

- [FastAPI application](https://github.com/immohitsen/rag-chat/blob/main/backend/main.py): API entry · `backend/main.py`
- [Auth routes](https://github.com/immohitsen/rag-chat/blob/main/backend/routers/auth.py): route module · `backend/routers/auth.py`
- [Chat routes](https://github.com/immohitsen/rag-chat/blob/main/backend/routers/chat.py): route module · `backend/routers/chat.py`
- [Upload & file routes](https://github.com/immohitsen/rag-chat/blob/main/backend/routers/upload.py): route modules · `backend/routers/upload.py`
- [History routes](https://github.com/immohitsen/rag-chat/blob/main/backend/routers/history.py): route module · `backend/routers/history.py`
- [JWT user guard](https://github.com/immohitsen/rag-chat/blob/main/backend/auth/dependencies.py): dependency · Enforces user-scoped operations. · `backend/auth/dependencies.py`

### RAG pipeline

- [Document ingestion](https://github.com/immohitsen/rag-chat/blob/main/backend/src/data_loader.py): loader & chunker · `backend/src/data_loader.py`
- [Embeddings](https://github.com/immohitsen/rag-chat/blob/main/backend/src/embedding.py): Sentence Transformer · Uses all-MiniLM-L6-v2. · `backend/src/embedding.py`
- [Filtered vector search](https://github.com/immohitsen/rag-chat/blob/main/backend/src/search.py): retrieval service · Filters chunks by user and selected files. · `backend/src/search.py`
- [Intent classifier](https://github.com/immohitsen/rag-chat/blob/main/backend/src/intent_classifier.py): query router · Separates chitchat from document questions. · `backend/src/intent_classifier.py`
- [RAG orchestration](https://github.com/immohitsen/rag-chat/blob/main/backend/services/rag_service.py): answer service · Builds citations, confidence, and summary memory. · `backend/services/rag_service.py`
- **Groq Llama 3.1**: LLM inference

### Data & identity

- [Passwords & JWTs](https://github.com/immohitsen/rag-chat/blob/main/backend/auth/jwt_handler.py): identity service · Bcrypt credentials and 15-day token issuance. · `backend/auth/jwt_handler.py`
- [MongoDB Atlas](https://github.com/immohitsen/rag-chat/blob/main/backend/src/mongodb_store.py): metadata & vector store · Persists users, files, chunks, chats, and messages. · `backend/src/mongodb_store.py`
- **AWS S3**: original document store

### Deployment

- [Lambda container deployment](https://github.com/immohitsen/rag-chat/blob/main/backend/Dockerfile): AWS deployment · `backend/Dockerfile`
- [Vercel deployment](https://github.com/immohitsen/rag-chat/blob/main/frontend/vercel.json): frontend deployment · `frontend/vercel.json`

## Connections

- Client app → Chat & upload UI: composes
- Client app → Auth state: uses
- Client app → Theme state: uses
- Chat & upload UI → API client: requests
- API client → FastAPI application: /api
- FastAPI application → Auth routes: routes
- FastAPI application → Chat routes: routes
- FastAPI application → Upload & file routes: routes
- FastAPI application → History routes: routes
- Auth routes → Passwords & JWTs: registers and logs in
- Chat routes → JWT user guard: authenticates
- Upload & file routes → JWT user guard: authenticates
- History routes → JWT user guard: authenticates
- Upload & file routes → AWS S3: stores/downloads originals
- Upload & file routes → Document ingestion: loads uploads
- Document ingestion → Embeddings: chunks for embedding
- Embeddings → MongoDB Atlas: stores vectors
- Chat routes → Intent classifier: classifies query
- Intent classifier → RAG orchestration: document question
- RAG orchestration → Filtered vector search: retrieves context
- Filtered vector search → MongoDB Atlas: Atlas Vector Search
- RAG orchestration → Groq Llama 3.1: generates answer
- RAG orchestration → MongoDB Atlas: reads/writes chat memory
- History routes → MongoDB Atlas: loads conversations
- Lambda container deployment → FastAPI application: packages
- Vercel deployment → Client app: deploys
