# utkarsh-aman/documind architecture

> Architecture diagram of the GitHub repository utkarsh-aman/documind, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/utkarsh-aman/documind
- Repository: https://github.com/utkarsh-aman/documind

## Overview

#### Purpose and Entry Points
DocuMind is a multi-user document RAG system: users authenticate with Google, upload documents, and query a chat interface grounded in only their own indexed content. The backend FastAPI application starts at `backend/main.py`; the Next.js client routes from `frontend/src/app/page.tsx` into login and protected dashboard experiences.

#### Client and Access Boundary
The Next.js App Router frontend provides the landing, Google sign-in, and dashboard workspace. Route middleware enforces client-side/server-edge access gating before dashboard rendering, while the backend remains the authority for authenticated API operations.
- `frontend/src/middleware.ts`
- `frontend/src/app/login/page.tsx`
- `frontend/src/app/dashboard/page.tsx`

#### Authentication and Tenant Isolation
Google OAuth verification provisions or identifies users, then backend JWT logic issues signed session tokens intended for HttpOnly/SameSite cookies. Backend auth dependencies resolve the current user for protected endpoints. That authenticated database user ID is the critical tenancy boundary and must be propagated into document access and retrieval filters.
- `backend/src/auth/google_auth.py`
- `backend/src/auth/jwt_auth.py`
- `backend/src/auth/dependencies.py`

#### API Orchestration and Persistence
`backend/main.py` owns FastAPI routing for authentication, document lifecycle, and chat operations. SQLAlchemy connection/model layers persist users, documents, chat/message records, and vector-related data in PostgreSQL. Alembic establishes the evolving relational schema, including chats/messages and pgvector/summaries.
- `backend/main.py`
- `backend/src/database/connection.py`
- `backend/src/database/models.py`
- `backend/alembic/versions/b2c3d4e5f6g7_add_pgvector_and_summaries.py`

#### Document Ingestion Pipeline
Upload handling parses supported source formats into text, splits content into chunks, generates embeddings, and records processing state for dashboard polling. Parsed content and generated metadata/summaries are persisted; embeddings feed the PostgreSQL pgvector store. This pipeline separates potentially expensive extraction/embedding work from later interactive retrieval.
- `backend/src/data_loader.py`
- `backend/src/embedding.py`
- `backend/src/pgvector_store.py`

#### RAG Query Pipeline
Chat requests first classify intent. Greetings/small talk can route directly to generation; document-overview requests use stored summaries; document questions retrieve user-filtered semantic candidates, with search/reranking support, then generate an answer using retrieved context. The generator is the boundary to the Groq LLM API.
- `backend/src/rag/intent_router.py`
- `backend/src/rag/retriever.py`
- `backend/src/rag/generator.py`
- `backend/src/search.py`

#### Runtime and Infrastructure
The system runs a Python 3.10+ FastAPI backend and Node/Next.js frontend. PostgreSQL is used either as hosted Neon (with SSL) or a local Compose-managed instance; pgvector supports similarity search. External dependencies include Google OAuth client credentials and a Groq API key. Docker builds the backend and Compose provisions backend plus local PostgreSQL.
- `backend/Dockerfile`
- `docker-compose.yml`
- `backend/requirements.txt`
- `backend/.env.example`

Main entry points: [`backend/main.py`](https://github.com/utkarsh-aman/documind/blob/main/backend/main.py) (FastAPI application), [`backend/src/database/models.py`](https://github.com/utkarsh-aman/documind/blob/main/backend/src/database/models.py) (SQLAlchemy data model), [`backend/src/rag/intent_router.py`](https://github.com/utkarsh-aman/documind/blob/main/backend/src/rag/intent_router.py) (Intent routing), [`backend/src/rag/retriever.py`](https://github.com/utkarsh-aman/documind/blob/main/backend/src/rag/retriever.py) (Tenant-filtered retrieval)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Next.js client"]
  node_landing["Landing entry<br/>Next.js route<br/>[page.tsx]"]
  node_login["Google sign-in<br/>Next.js route<br/>[page.tsx]"]
  node_middleware["Access middleware<br/>Next.js middleware<br/>[middleware.ts]"]
  node_dashboard["Dashboard workspace<br/>Next.js route<br/>[page.tsx]"]
end

subgraph group_backend["FastAPI backend"]
  node_api{{"FastAPI application<br/>API router<br/>[main.py]"}}
  node_google_auth["Google OAuth verification<br/>auth service<br/>[google_auth.py]"]
  node_jwt_auth["JWT sessions<br/>auth service<br/>[jwt_auth.py]"]
  node_current_user["Current-user dependency<br/>auth dependency<br/>[dependencies.py]"]
  node_ingestion["Document ingestion<br/>pipeline<br/>[data_loader.py]"]
  node_embedding["Embedding generation<br/>ML service<br/>[embedding.py]"]
end

subgraph group_rag["RAG pipeline"]
  node_intent["Intent routing<br/>RAG stage<br/>[intent_router.py]"]
  node_retriever["Tenant-filtered retrieval<br/>RAG stage<br/>[retriever.py]"]
  node_search["Search and reranking<br/>retrieval support<br/>[search.py]"]
  node_generator["Answer generation<br/>RAG stage<br/>[generator.py]"]
end

subgraph group_data["Data services"]
  node_database_layer["SQLAlchemy data model<br/>persistence layer<br/>[models.py]"]
  node_postgres[("PostgreSQL + pgvector<br/>database<br/>[pgvector_store.py]")]
  node_schema["Vector and summary schema<br/>Alembic migration"]
end

node_groq{{"Groq LLM API<br/>external API"}}

node_landing -->|"sign in"| node_login
node_login -->|"OAuth request"| node_api
node_middleware -->|"permits protected route"| node_dashboard
node_dashboard -->|"upload and chat API"| node_api
node_api -->|"verifies Google identity"| node_google_auth
node_google_auth -->|"provisions user and session"| node_jwt_auth
node_api -->|"protects operations"| node_current_user
node_current_user -->|"user-scoped access"| node_database_layer
node_api -->|"document upload"| node_ingestion
node_ingestion -->|"text chunks"| node_embedding
node_ingestion -->|"documents and status"| node_database_layer
node_embedding -->|"chunk vectors"| node_postgres
node_api -->|"chat request"| node_intent
node_intent -->|"small talk"| node_generator
node_intent -->|"document question"| node_retriever
node_intent -->|"overview summaries"| node_database_layer
node_retriever -->|"candidate ranking"| node_search
node_search -->|"similarity search"| node_postgres
node_current_user -->|"tenant filter"| node_retriever
node_retriever -->|"retrieved context"| node_generator
node_generator -->|"LLM completion"| node_groq
node_api -->|"chat persistence"| node_database_layer
node_database_layer -->|"relational records"| node_postgres
node_schema -.->|"creates pgvector and summaries"| node_postgres

click node_landing "https://github.com/utkarsh-aman/documind/blob/main/frontend/src/app/page.tsx"
click node_login "https://github.com/utkarsh-aman/documind/blob/main/frontend/src/app/login/page.tsx"
click node_middleware "https://github.com/utkarsh-aman/documind/blob/main/frontend/src/middleware.ts"
click node_dashboard "https://github.com/utkarsh-aman/documind/blob/main/frontend/src/app/dashboard/page.tsx"
click node_api "https://github.com/utkarsh-aman/documind/blob/main/backend/main.py"
click node_google_auth "https://github.com/utkarsh-aman/documind/blob/main/backend/src/auth/google_auth.py"
click node_jwt_auth "https://github.com/utkarsh-aman/documind/blob/main/backend/src/auth/jwt_auth.py"
click node_current_user "https://github.com/utkarsh-aman/documind/blob/main/backend/src/auth/dependencies.py"
click node_ingestion "https://github.com/utkarsh-aman/documind/blob/main/backend/src/data_loader.py"
click node_embedding "https://github.com/utkarsh-aman/documind/blob/main/backend/src/embedding.py"
click node_intent "https://github.com/utkarsh-aman/documind/blob/main/backend/src/rag/intent_router.py"
click node_retriever "https://github.com/utkarsh-aman/documind/blob/main/backend/src/rag/retriever.py"
click node_search "https://github.com/utkarsh-aman/documind/blob/main/backend/src/search.py"
click node_generator "https://github.com/utkarsh-aman/documind/blob/main/backend/src/rag/generator.py"
click node_database_layer "https://github.com/utkarsh-aman/documind/blob/main/backend/src/database/models.py"
click node_postgres "https://github.com/utkarsh-aman/documind/blob/main/backend/src/pgvector_store.py"
click node_schema "https://github.com/utkarsh-aman/documind/blob/main/backend/alembic/versions/b2c3d4e5f6g7_add_pgvector_and_summaries.py"

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_landing,node_login,node_middleware,node_dashboard toneBlue
class node_api,node_google_auth,node_jwt_auth,node_current_user,node_ingestion,node_embedding toneAmber
class node_intent,node_retriever,node_search,node_generator toneMint
class node_database_layer,node_postgres,node_schema toneRose
class node_groq toneNeutral
```

## Components

### Next.js client

- [Landing entry](https://github.com/utkarsh-aman/documind/blob/main/frontend/src/app/page.tsx): Next.js route · `frontend/src/app/page.tsx`
- [Google sign-in](https://github.com/utkarsh-aman/documind/blob/main/frontend/src/app/login/page.tsx): Next.js route · `frontend/src/app/login/page.tsx`
- [Access middleware](https://github.com/utkarsh-aman/documind/blob/main/frontend/src/middleware.ts): Next.js middleware · Gates dashboard access before rendering. · `frontend/src/middleware.ts`
- [Dashboard workspace](https://github.com/utkarsh-aman/documind/blob/main/frontend/src/app/dashboard/page.tsx): Next.js route · Uploads documents and runs chat. · `frontend/src/app/dashboard/page.tsx`

### FastAPI backend

- [FastAPI application](https://github.com/utkarsh-aman/documind/blob/main/backend/main.py): API router · Owns auth, document, and chat endpoints. · `backend/main.py`
- [Google OAuth verification](https://github.com/utkarsh-aman/documind/blob/main/backend/src/auth/google_auth.py): auth service · `backend/src/auth/google_auth.py`
- [JWT sessions](https://github.com/utkarsh-aman/documind/blob/main/backend/src/auth/jwt_auth.py): auth service · Issues signed cookie-oriented session tokens. · `backend/src/auth/jwt_auth.py`
- [Current-user dependency](https://github.com/utkarsh-aman/documind/blob/main/backend/src/auth/dependencies.py): auth dependency · Provides the user ID used as the tenancy boundary. · `backend/src/auth/dependencies.py`
- [Document ingestion](https://github.com/utkarsh-aman/documind/blob/main/backend/src/data_loader.py): pipeline · Extracts text, chunks content, and tracks processing state. · `backend/src/data_loader.py`
- [Embedding generation](https://github.com/utkarsh-aman/documind/blob/main/backend/src/embedding.py): ML service · `backend/src/embedding.py`

### RAG pipeline

- [Intent routing](https://github.com/utkarsh-aman/documind/blob/main/backend/src/rag/intent_router.py): RAG stage · Routes small talk, overview, and document questions. · `backend/src/rag/intent_router.py`
- [Tenant-filtered retrieval](https://github.com/utkarsh-aman/documind/blob/main/backend/src/rag/retriever.py): RAG stage · Finds semantic candidates only for the authenticated user. · `backend/src/rag/retriever.py`
- [Search and reranking](https://github.com/utkarsh-aman/documind/blob/main/backend/src/search.py): retrieval support · `backend/src/search.py`
- [Answer generation](https://github.com/utkarsh-aman/documind/blob/main/backend/src/rag/generator.py): RAG stage · Sends prompts and retrieved context to Groq. · `backend/src/rag/generator.py`

### Data services

- [SQLAlchemy data model](https://github.com/utkarsh-aman/documind/blob/main/backend/src/database/models.py): persistence layer · Persists users, documents, chats, messages, and vector-related records. · `backend/src/database/models.py`
- [PostgreSQL + pgvector](https://github.com/utkarsh-aman/documind/blob/main/backend/src/pgvector_store.py): database · Hosted Neon or Compose-managed PostgreSQL similarity store. · `backend/src/pgvector_store.py`
- [Vector and summary schema](https://github.com/utkarsh-aman/documind/blob/main/backend/alembic/versions/b2c3d4e5f6g7_add_pgvector_and_summaries.py): Alembic migration · `backend/alembic/versions/b2c3d4e5f6g7_add_pgvector_and_summaries.py`

### Other components

- **Groq LLM API**: external API

## Connections

- Landing entry → Google sign-in: sign in
- Google sign-in → FastAPI application: OAuth request
- Access middleware → Dashboard workspace: permits protected route
- Dashboard workspace → FastAPI application: upload and chat API
- FastAPI application → Google OAuth verification: verifies Google identity
- Google OAuth verification → JWT sessions: provisions user and session
- FastAPI application → Current-user dependency: protects operations
- Current-user dependency → SQLAlchemy data model: user-scoped access
- FastAPI application → Document ingestion: document upload
- Document ingestion → Embedding generation: text chunks
- Document ingestion → SQLAlchemy data model: documents and status
- Embedding generation → PostgreSQL + pgvector: chunk vectors
- FastAPI application → Intent routing: chat request
- Intent routing → Answer generation: small talk
- Intent routing → Tenant-filtered retrieval: document question
- Intent routing → SQLAlchemy data model: overview summaries
- Tenant-filtered retrieval → Search and reranking: candidate ranking
- Search and reranking → PostgreSQL + pgvector: similarity search
- Current-user dependency → Tenant-filtered retrieval: tenant filter
- Tenant-filtered retrieval → Answer generation: retrieved context
- Answer generation → Groq LLM API: LLM completion
- FastAPI application → SQLAlchemy data model: chat persistence
- SQLAlchemy data model → PostgreSQL + pgvector: relational records
- Vector and summary schema → PostgreSQL + pgvector: creates pgvector and summaries
