# antonymittul/multi_tenant_rag architecture

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

- Interactive diagram: https://gitdiagram.com/antonymittul/multi_tenant_rag
- Repository: https://github.com/antonymittul/multi_tenant_rag

## Overview

#### Purpose and Runtime Topology
This is a multi-tenant RAG system: organizations upload documents, background workers ingest and embed them, and users query isolated tenant knowledge to generate answers. The primary backend is FastAPI, paired with PostgreSQL tenant metadata, Qdrant vector retrieval, Redis/Celery asynchronous jobs, Gemini generation, and a React/Vite frontend served through Nginx. Container wiring is defined in `docker-compose.yml`; backend startup is `app/main.py`.

#### API and Request Boundary
FastAPI mounts the API router from `app/api/api.py`, with endpoint modules separating tenant administration, document ingestion, search, answer generation, and evaluation:
- `app/api/endpoints/tenants.py`
- `app/api/endpoints/documents.py`
- `app/api/endpoints/search.py`
- `app/api/endpoints/generate.py`
- `app/api/endpoints/evaluate.py`

Request dependencies in `app/api/deps.py` establish the API’s tenant/authentication boundary. Tenant identity must propagate into ingestion, retrieval, and generation so vector queries cannot cross organization boundaries.

#### Tenant Persistence and Isolation
Tenant profiles and API-key-backed identity are persisted in PostgreSQL through:
- `app/models/tenant.py`
- `app/schemas/tenant.py`
- `app/crud/crud_tenant.py`

Database lifecycle and ORM setup live in `app/db/session.py` and `app/db/base_class.py`. PostgreSQL holds relational tenant metadata; Qdrant holds document vectors and must receive tenant filters on every retrieval operation.

#### Asynchronous Document Ingestion
Document submission enters through `app/api/endpoints/documents.py` and is delegated to Celery rather than processed in the API request path. The worker runtime is defined by:
- `app/worker/celery_app.py`
- `app/worker/tasks.py`
- `app/services/ingestion.py`

This stage performs document processing, chunking, and embedding before writing tenant-scoped records to the vector store. Redis is the Celery broker/cache layer, represented by `app/services/cache.py`.

#### Retrieval Pipeline
Query retrieval is encapsulated in `app/services/retrieval.py` and exposed independently through `app/api/endpoints/search.py` or as context for generation. The intended search path is hybrid dense-vector plus sparse BM25 retrieval, reciprocal-rank fusion, then cross-encoder reranking. Tenant filtering is a mandatory boundary at the Qdrant query layer.

#### Generation, Guardrails, and Evaluation
Generation endpoints accept a tenant-scoped question, retrieve relevant context, apply safety/policy checks, and invoke Gemini:
- `app/services/generation.py`
- `app/services/guardrails.py`
- `app/schemas/generation.py`

Evaluation is a separate API/service path in `app/api/endpoints/evaluate.py` and `app/services/evaluation.py`, allowing quality assessment without conflating it with the serving flow.

#### Frontend and Deployment Edges
The React client starts at `frontend/src/main.tsx` and is composed from `frontend/src/App.tsx`; backend communication is centralized in `frontend/src/api.ts`. Its production container uses `frontend/nginx.conf` as the proxy/security edge. Local deployment builds the service cluster through `docker-compose.yml`; cloud deployment can split frontend, API, worker, Redis, PostgreSQL, and Qdrant into managed services. Runtime configuration, including Gemini and infrastructure credentials, is centralized in `app/core/config.py` and `.env.example`.

Main entry points: [`app/api/api.py`](https://github.com/antonymittul/multi_tenant_rag/blob/main/app/api/api.py) (API router), [`app/api/endpoints/search.py`](https://github.com/antonymittul/multi_tenant_rag/blob/main/app/api/endpoints/search.py) (Search, answer & evaluation), [`app/worker/celery_app.py`](https://github.com/antonymittul/multi_tenant_rag/blob/main/app/worker/celery_app.py) (Celery worker), [`docker-compose.yml`](https://github.com/antonymittul/multi_tenant_rag/blob/main/docker-compose.yml) (Service cluster)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Frontend"]
  node_web["React client<br/>React/Vite<br/>[main.tsx]"]
  node_frontend_api["Backend client<br/>API client<br/>[api.ts]"]
  node_nginx["Proxy edge<br/>Nginx<br/>[nginx.conf]"]
end

subgraph group_api["FastAPI"]
  node_main["Backend startup<br/>FastAPI app<br/>[main.py]"]
  node_router["API router<br/>route composition<br/>[api.py]"]
  node_tenant_boundary["Tenant auth boundary<br/>request dependencies<br/>[deps.py]"]
  node_tenant_api["Tenant administration<br/>endpoint module<br/>[tenants.py]"]
  node_document_api["Document submission<br/>endpoint module<br/>[documents.py]"]
  node_query_api["Search, answer &amp; evaluation<br/>endpoint modules<br/>[search.py]"]
end

subgraph group_ingestion["Async ingestion"]
  node_celery["Celery worker<br/>worker runtime<br/>[celery_app.py]"]
  node_ingestion_tasks["Ingestion tasks<br/>Celery tasks<br/>[tasks.py]"]
  node_ingestion["Process, chunk &amp; embed<br/>ingestion service<br/>[ingestion.py]"]
end

subgraph group_rag["RAG pipeline"]
  node_retrieval["Hybrid retrieval<br/>retrieval service<br/>[retrieval.py]"]
  node_generation["Guarded generation<br/>generation service<br/>[generation.py]"]
  node_evaluation["Quality evaluation<br/>evaluation service<br/>[evaluation.py]"]
end

subgraph group_data["Data &amp; external services"]
  node_tenant_crud["Tenant repository<br/>CRUD<br/>[crud_tenant.py]"]
  node_postgres[("Tenant metadata<br/>PostgreSQL<br/>[tenant.py]")]
  node_redis[("Job broker &amp; cache<br/>Redis<br/>[cache.py]")]
  node_qdrant[("Tenant vector store<br/>Qdrant")]
  node_gemini["Gemini<br/>LLM provider"]
end

node_compose{{"Service cluster<br/>Docker Compose<br/>[docker-compose.yml]"}}

node_compose -.->|"builds"| node_nginx
node_compose -.->|"runs"| node_main
node_compose -.->|"runs"| node_celery
node_web -->|"uses"| node_frontend_api
node_frontend_api -->|"requests"| node_nginx
node_nginx -->|"proxies"| node_main
node_main -->|"mounts"| node_router
node_router -->|"applies"| node_tenant_boundary
node_router -->|"routes"| node_tenant_api
node_router -->|"routes"| node_document_api
node_router -->|"routes"| node_query_api
node_tenant_boundary -->|"resolves identity"| node_tenant_crud
node_tenant_crud -->|"persists"| node_postgres
node_document_api -->|"queues ingestion"| node_celery
node_celery -->|"uses broker"| node_redis
node_celery -->|"executes"| node_ingestion_tasks
node_ingestion_tasks -->|"processes"| node_ingestion
node_ingestion -->|"writes tenant vectors"| node_qdrant
node_query_api -->|"searches"| node_retrieval
node_retrieval -->|"queries with tenant filter"| node_qdrant
node_query_api -->|"answers"| node_generation
node_generation -->|"gets context"| node_retrieval
node_generation -->|"invokes"| node_gemini
node_query_api -->|"evaluates"| node_evaluation

click node_compose "https://github.com/antonymittul/multi_tenant_rag/blob/main/docker-compose.yml"
click node_web "https://github.com/antonymittul/multi_tenant_rag/blob/main/frontend/src/main.tsx"
click node_frontend_api "https://github.com/antonymittul/multi_tenant_rag/blob/main/frontend/src/api.ts"
click node_nginx "https://github.com/antonymittul/multi_tenant_rag/blob/main/frontend/nginx.conf"
click node_main "https://github.com/antonymittul/multi_tenant_rag/blob/main/app/main.py"
click node_router "https://github.com/antonymittul/multi_tenant_rag/blob/main/app/api/api.py"
click node_tenant_boundary "https://github.com/antonymittul/multi_tenant_rag/blob/main/app/api/deps.py"
click node_tenant_api "https://github.com/antonymittul/multi_tenant_rag/blob/main/app/api/endpoints/tenants.py"
click node_document_api "https://github.com/antonymittul/multi_tenant_rag/blob/main/app/api/endpoints/documents.py"
click node_query_api "https://github.com/antonymittul/multi_tenant_rag/blob/main/app/api/endpoints/search.py"
click node_tenant_crud "https://github.com/antonymittul/multi_tenant_rag/blob/main/app/crud/crud_tenant.py"
click node_postgres "https://github.com/antonymittul/multi_tenant_rag/blob/main/app/models/tenant.py"
click node_celery "https://github.com/antonymittul/multi_tenant_rag/blob/main/app/worker/celery_app.py"
click node_ingestion_tasks "https://github.com/antonymittul/multi_tenant_rag/blob/main/app/worker/tasks.py"
click node_ingestion "https://github.com/antonymittul/multi_tenant_rag/blob/main/app/services/ingestion.py"
click node_redis "https://github.com/antonymittul/multi_tenant_rag/blob/main/app/services/cache.py"
click node_retrieval "https://github.com/antonymittul/multi_tenant_rag/blob/main/app/services/retrieval.py"
click node_generation "https://github.com/antonymittul/multi_tenant_rag/blob/main/app/services/generation.py"
click node_evaluation "https://github.com/antonymittul/multi_tenant_rag/blob/main/app/services/evaluation.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_web,node_frontend_api,node_nginx toneBlue
class node_main,node_router,node_tenant_boundary,node_tenant_api,node_document_api,node_query_api toneAmber
class node_celery,node_ingestion_tasks,node_ingestion toneMint
class node_retrieval,node_generation,node_evaluation toneRose
class node_tenant_crud,node_postgres,node_redis,node_qdrant,node_gemini toneIndigo
class node_compose toneNeutral
```

## Components

### Frontend

- [React client](https://github.com/antonymittul/multi_tenant_rag/blob/main/frontend/src/main.tsx): React/Vite · `frontend/src/main.tsx`
- [Backend client](https://github.com/antonymittul/multi_tenant_rag/blob/main/frontend/src/api.ts): API client · `frontend/src/api.ts`
- [Proxy edge](https://github.com/antonymittul/multi_tenant_rag/blob/main/frontend/nginx.conf): Nginx · `frontend/nginx.conf`

### FastAPI

- [Backend startup](https://github.com/antonymittul/multi_tenant_rag/blob/main/app/main.py): FastAPI app · `app/main.py`
- [API router](https://github.com/antonymittul/multi_tenant_rag/blob/main/app/api/api.py): route composition · `app/api/api.py`
- [Tenant auth boundary](https://github.com/antonymittul/multi_tenant_rag/blob/main/app/api/deps.py): request dependencies · Establishes tenant identity for every protected flow. · `app/api/deps.py`
- [Tenant administration](https://github.com/antonymittul/multi_tenant_rag/blob/main/app/api/endpoints/tenants.py): endpoint module · `app/api/endpoints/tenants.py`
- [Document submission](https://github.com/antonymittul/multi_tenant_rag/blob/main/app/api/endpoints/documents.py): endpoint module · `app/api/endpoints/documents.py`
- [Search, answer & evaluation](https://github.com/antonymittul/multi_tenant_rag/blob/main/app/api/endpoints/search.py): endpoint modules · `app/api/endpoints/search.py`

### Async ingestion

- [Celery worker](https://github.com/antonymittul/multi_tenant_rag/blob/main/app/worker/celery_app.py): worker runtime · `app/worker/celery_app.py`
- [Ingestion tasks](https://github.com/antonymittul/multi_tenant_rag/blob/main/app/worker/tasks.py): Celery tasks · `app/worker/tasks.py`
- [Process, chunk & embed](https://github.com/antonymittul/multi_tenant_rag/blob/main/app/services/ingestion.py): ingestion service · `app/services/ingestion.py`

### RAG pipeline

- [Hybrid retrieval](https://github.com/antonymittul/multi_tenant_rag/blob/main/app/services/retrieval.py): retrieval service · Dense and BM25 retrieval, rank fusion, and reranking. · `app/services/retrieval.py`
- [Guarded generation](https://github.com/antonymittul/multi_tenant_rag/blob/main/app/services/generation.py): generation service · Applies safety checks to tenant-scoped retrieved context. · `app/services/generation.py`
- [Quality evaluation](https://github.com/antonymittul/multi_tenant_rag/blob/main/app/services/evaluation.py): evaluation service · Runs separately from the serving path. · `app/services/evaluation.py`

### Data & external services

- [Tenant repository](https://github.com/antonymittul/multi_tenant_rag/blob/main/app/crud/crud_tenant.py): CRUD · `app/crud/crud_tenant.py`
- [Tenant metadata](https://github.com/antonymittul/multi_tenant_rag/blob/main/app/models/tenant.py): PostgreSQL · Stores tenant profiles and API-key identity. · `app/models/tenant.py`
- [Job broker & cache](https://github.com/antonymittul/multi_tenant_rag/blob/main/app/services/cache.py): Redis · `app/services/cache.py`
- **Tenant vector store**: Qdrant · Retrieval must apply tenant filters.
- **Gemini**: LLM provider

### Other components

- [Service cluster](https://github.com/antonymittul/multi_tenant_rag/blob/main/docker-compose.yml): Docker Compose · Builds and wires local runtime services. · `docker-compose.yml`

## Connections

- Service cluster → Proxy edge: builds
- Service cluster → Backend startup: runs
- Service cluster → Celery worker: runs
- React client → Backend client: uses
- Backend client → Proxy edge: requests
- Proxy edge → Backend startup: proxies
- Backend startup → API router: mounts
- API router → Tenant auth boundary: applies
- API router → Tenant administration: routes
- API router → Document submission: routes
- API router → Search, answer & evaluation: routes
- Tenant auth boundary → Tenant repository: resolves identity
- Tenant repository → Tenant metadata: persists
- Document submission → Celery worker: queues ingestion
- Celery worker → Job broker & cache: uses broker
- Celery worker → Ingestion tasks: executes
- Ingestion tasks → Process, chunk & embed: processes
- Process, chunk & embed → Tenant vector store: writes tenant vectors
- Search, answer & evaluation → Hybrid retrieval: searches
- Hybrid retrieval → Tenant vector store: queries with tenant filter
- Search, answer & evaluation → Guarded generation: answers
- Guarded generation → Hybrid retrieval: gets context
- Guarded generation → Gemini: invokes
- Search, answer & evaluation → Quality evaluation: evaluates
