How immohitsen/rag-chat works
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: main.py FastAPI application · upload.py Upload & file routes · rag_service.py RAG orchestration · page.jsx Client app
Components 22
React client
- Client app
frontend/app/page.jsx - Chat & upload UI · Primary user interaction surface.
frontend/src/components/ChatInterface.jsx - API client
frontend/src/services/api.js - Auth state
frontend/src/context/AuthContext.jsx - Theme state
frontend/src/context/ThemeContext.jsx
FastAPI API
- FastAPI application
backend/main.py - Auth routes
backend/routers/auth.py - Chat routes
backend/routers/chat.py - Upload & file routes
backend/routers/upload.py - History routes
backend/routers/history.py - JWT user guard · Enforces user-scoped operations.
backend/auth/dependencies.py
RAG pipeline
- Document ingestion
backend/src/data_loader.py - Embeddings · Uses all-MiniLM-L6-v2.
backend/src/embedding.py - Filtered vector search · Filters chunks by user and selected files.
backend/src/search.py - Intent classifier · Separates chitchat from document questions.
backend/src/intent_classifier.py - RAG orchestration · Builds citations, confidence, and summary memory.
backend/services/rag_service.py - Groq Llama 3.1
Data & identity
- Passwords & JWTs · Bcrypt credentials and 15-day token issuance.
backend/auth/jwt_handler.py - MongoDB Atlas · Persists users, files, chunks, chats, and messages.
backend/src/mongodb_store.py - AWS S3
Deployment
- Lambda container deployment
backend/Dockerfile - Vercel deployment
frontend/vercel.json
Connections 26
- 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