# sajib2019/python-rag- architecture

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

- Interactive diagram: https://gitdiagram.com/sajib2019/python-rag-
- Repository: https://github.com/sajib2019/python-rag-

## Overview

This is a FastAPI RAG service: clients submit text or supported files, documents are queued and processed into searchable chunks, and question requests retrieve hybrid results, generate an answer, and return citations. The graph also preserves the documented health/metrics, optional external-search, and provider capabilities. Source evidence is incomplete for several modules, so those relationships are included only where the README or sampled callers support them.

Main entry points: [`app/main.py`](https://github.com/sajib2019/python-rag-/blob/main/app/main.py) (FastAPI app), [`app/workers/ingestion.py`](https://github.com/sajib2019/python-rag-/blob/main/app/workers/ingestion.py) (Ingestion worker), [`app/resources.py`](https://github.com/sajib2019/python-rag-/blob/main/app/resources.py) (Resource wiring), [`app/services/search.py`](https://github.com/sajib2019/python-rag-/blob/main/app/services/search.py) (Hybrid search)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["HTTP API"]
  node_app["FastAPI app<br/>[main.py]"]
  node_auth["Auth and throttling<br/>[dependencies.py]"]
  node_document_routes["Document routes<br/>[documents.py]"]
  node_query_route["Query route<br/>[query.py]"]
  node_health["Health and metrics<br/>[health.py]"]
  node_schemas["Request and response models"]
end

subgraph group_ingestion["Document Ingestion"]
  node_documents_service["Document service<br/>[documents.py]"]
  node_ingestion_service["Extract and chunk<br/>[ingestion.py]"]
  node_worker["Ingestion worker<br/>[ingestion.py]"]
end

subgraph group_retrieval["Retrieval and Answers"]
  node_query_service["RAG query service<br/>[retrieval.py]"]
  node_search["Hybrid search<br/>[search.py]"]
  node_providers["Embedding and LLM providers<br/>[providers.py]"]
end

subgraph group_platform["Shared Runtime"]
  node_redis["Redis job queue"]
  node_postgres[("PostgreSQL and pgvector")]
  node_repository["SQL repository<br/>[repository.py]"]
  node_resources["Resource wiring<br/>[resources.py]"]
  node_config["Runtime settings<br/>[config.py]"]
  node_middleware["Request middleware<br/>[middleware.py]"]
  node_exceptions["HTTP error handlers<br/>[exceptions.py]"]
  node_telemetry["OpenTelemetry<br/>[telemetry.py]"]
end

node_client(("API client"))
node_external_search[("Qdrant and Elasticsearch")]
node_llm_api["OpenAI-compatible API"]

node_client -->|"sends requests"| node_app
node_app -->|"mounts routes"| node_document_routes
node_app -->|"mounts routes"| node_query_route
node_app -->|"mounts routes"| node_health
node_app -->|"installs middleware"| node_middleware
node_app -->|"registers handlers"| node_exceptions
node_document_routes -->|"requires authorization"| node_auth
node_query_route -->|"requires authorization"| node_auth
node_document_routes -->|"calls service"| node_documents_service
node_query_route -->|"calls service"| node_query_service
node_document_routes -->|"validates and returns"| node_schemas
node_query_route -->|"validates and returns"| node_schemas
node_health -->|"checks readiness"| node_postgres
node_health -->|"checks readiness"| node_redis
node_documents_service -->|"queues ingestion"| node_redis
node_worker -->|"dispatches queued jobs"| node_redis
node_worker -->|"cleans and chunks"| node_ingestion_service
node_worker -->|"reads and updates documents"| node_repository
node_worker -->|"embeds chunks"| node_providers
node_worker -->|"syncs indexed document"| node_search
node_ingestion_service -->|"chunks persisted by worker"| node_postgres
node_query_service -->|"embeds and generates"| node_providers
node_query_service -->|"runs hybrid retrieval"| node_search
node_query_service -->|"builds response models"| node_schemas
node_search -->|"uses PostgreSQL search"| node_repository
node_search -.->|"uses optional backend"| node_external_search
node_providers -->|"calls configured provider"| node_llm_api
node_resources -->|"constructs"| node_repository
node_resources -->|"constructs"| node_search
node_resources -->|"configures providers"| node_providers
node_resources -->|"configures telemetry"| node_telemetry
node_app -->|"opens shared resources"| node_resources
node_app -->|"loads settings"| node_config
node_middleware -.->|"exposes metrics behavior"| node_health

click node_app "https://github.com/sajib2019/python-rag-/blob/main/app/main.py"
click node_auth "https://github.com/sajib2019/python-rag-/blob/main/app/api/dependencies.py"
click node_document_routes "https://github.com/sajib2019/python-rag-/blob/main/app/api/routes/documents.py"
click node_query_route "https://github.com/sajib2019/python-rag-/blob/main/app/api/routes/query.py"
click node_health "https://github.com/sajib2019/python-rag-/blob/main/app/api/routes/health.py"
click node_schemas "https://github.com/sajib2019/python-rag-/tree/main/app/schemas"
click node_documents_service "https://github.com/sajib2019/python-rag-/blob/main/app/services/documents.py"
click node_ingestion_service "https://github.com/sajib2019/python-rag-/blob/main/app/services/ingestion.py"
click node_worker "https://github.com/sajib2019/python-rag-/blob/main/app/workers/ingestion.py"
click node_repository "https://github.com/sajib2019/python-rag-/blob/main/app/db/repository.py"
click node_resources "https://github.com/sajib2019/python-rag-/blob/main/app/resources.py"
click node_config "https://github.com/sajib2019/python-rag-/blob/main/app/core/config.py"
click node_middleware "https://github.com/sajib2019/python-rag-/blob/main/app/core/middleware.py"
click node_exceptions "https://github.com/sajib2019/python-rag-/blob/main/app/core/exceptions.py"
click node_telemetry "https://github.com/sajib2019/python-rag-/blob/main/app/core/telemetry.py"
click node_query_service "https://github.com/sajib2019/python-rag-/blob/main/app/services/retrieval.py"
click node_search "https://github.com/sajib2019/python-rag-/blob/main/app/services/search.py"
click node_providers "https://github.com/sajib2019/python-rag-/blob/main/app/integrations/providers.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_app,node_auth,node_document_routes,node_query_route,node_health,node_schemas,node_client toneBlue
class node_documents_service,node_ingestion_service,node_worker,node_external_search toneAmber
class node_query_service,node_search,node_providers,node_llm_api toneMint
class node_redis,node_postgres,node_repository,node_resources,node_config,node_middleware,node_exceptions,node_telemetry toneRose
```

## Components

### HTTP API

- [FastAPI app](https://github.com/sajib2019/python-rag-/blob/main/app/main.py): `app/main.py`
- [Auth and throttling](https://github.com/sajib2019/python-rag-/blob/main/app/api/dependencies.py): `app/api/dependencies.py`
- [Document routes](https://github.com/sajib2019/python-rag-/blob/main/app/api/routes/documents.py): `app/api/routes/documents.py`
- [Query route](https://github.com/sajib2019/python-rag-/blob/main/app/api/routes/query.py): `app/api/routes/query.py`
- [Health and metrics](https://github.com/sajib2019/python-rag-/blob/main/app/api/routes/health.py): `app/api/routes/health.py`
- [Request and response models](https://github.com/sajib2019/python-rag-/tree/main/app/schemas): `app/schemas`

### Document Ingestion

- [Document service](https://github.com/sajib2019/python-rag-/blob/main/app/services/documents.py): `app/services/documents.py`
- [Extract and chunk](https://github.com/sajib2019/python-rag-/blob/main/app/services/ingestion.py): `app/services/ingestion.py`
- [Ingestion worker](https://github.com/sajib2019/python-rag-/blob/main/app/workers/ingestion.py): `app/workers/ingestion.py`

### Retrieval and Answers

- [RAG query service](https://github.com/sajib2019/python-rag-/blob/main/app/services/retrieval.py): `app/services/retrieval.py`
- [Hybrid search](https://github.com/sajib2019/python-rag-/blob/main/app/services/search.py): `app/services/search.py`
- [Embedding and LLM providers](https://github.com/sajib2019/python-rag-/blob/main/app/integrations/providers.py): `app/integrations/providers.py`

### Shared Runtime

- **Redis job queue**
- **PostgreSQL and pgvector**
- [SQL repository](https://github.com/sajib2019/python-rag-/blob/main/app/db/repository.py): `app/db/repository.py`
- [Resource wiring](https://github.com/sajib2019/python-rag-/blob/main/app/resources.py): `app/resources.py`
- [Runtime settings](https://github.com/sajib2019/python-rag-/blob/main/app/core/config.py): `app/core/config.py`
- [Request middleware](https://github.com/sajib2019/python-rag-/blob/main/app/core/middleware.py): `app/core/middleware.py`
- [HTTP error handlers](https://github.com/sajib2019/python-rag-/blob/main/app/core/exceptions.py): `app/core/exceptions.py`
- [OpenTelemetry](https://github.com/sajib2019/python-rag-/blob/main/app/core/telemetry.py): `app/core/telemetry.py`

### Other components

- **API client**
- **Qdrant and Elasticsearch**
- **OpenAI-compatible API**

## Connections

- API client → FastAPI app: sends requests (evidence: `README.md`)
- FastAPI app → Document routes: mounts routes (evidence: `app/main.py`)
- FastAPI app → Query route: mounts routes (evidence: `app/main.py`)
- FastAPI app → Health and metrics: mounts routes (evidence: `app/main.py`)
- FastAPI app → Request middleware: installs middleware (evidence: `app/main.py`)
- FastAPI app → HTTP error handlers: registers handlers (evidence: `app/main.py`)
- Document routes → Auth and throttling: requires authorization (evidence: `README.md`)
- Query route → Auth and throttling: requires authorization (evidence: `app/api/routes/query.py`)
- Document routes → Document service: calls service (evidence: `README.md`)
- Query route → RAG query service: calls service (evidence: `app/api/routes/query.py`)
- Document routes → Request and response models: validates and returns (evidence: `app/api/routes/documents.py`)
- Query route → Request and response models: validates and returns (evidence: `app/api/routes/query.py`)
- Health and metrics → PostgreSQL and pgvector: checks readiness (evidence: `README.md`)
- Health and metrics → Redis job queue: checks readiness (evidence: `README.md`)
- Document service → Redis job queue: queues ingestion (evidence: `README.md`)
- Ingestion worker → Redis job queue: dispatches queued jobs (evidence: `app/workers/ingestion.py`)
- Ingestion worker → Extract and chunk: cleans and chunks (evidence: `app/workers/ingestion.py`)
- Ingestion worker → SQL repository: reads and updates documents (evidence: `app/workers/ingestion.py`)
- Ingestion worker → Embedding and LLM providers: embeds chunks (evidence: `app/workers/ingestion.py`)
- Ingestion worker → Hybrid search: syncs indexed document (evidence: `app/workers/ingestion.py`)
- Extract and chunk → PostgreSQL and pgvector: chunks persisted by worker (evidence: `README.md`)
- RAG query service → Embedding and LLM providers: embeds and generates (evidence: `app/services/retrieval.py`)
- RAG query service → Hybrid search: runs hybrid retrieval (evidence: `app/services/retrieval.py`)
- RAG query service → Request and response models: builds response models (evidence: `app/services/retrieval.py`)
- Hybrid search → SQL repository: uses PostgreSQL search (evidence: `README.md`)
- Hybrid search → Qdrant and Elasticsearch: uses optional backend (evidence: `README.md`)
- Embedding and LLM providers → OpenAI-compatible API: calls configured provider (evidence: `app/integrations/providers.py`)
- Resource wiring → SQL repository: constructs (evidence: `app/resources.py`)
- Resource wiring → Hybrid search: constructs (evidence: `app/resources.py`)
- Resource wiring → Embedding and LLM providers: configures providers (evidence: `app/resources.py`)
- Resource wiring → OpenTelemetry: configures telemetry (evidence: `app/resources.py`)
- FastAPI app → Resource wiring: opens shared resources (evidence: `app/main.py`)
- FastAPI app → Runtime settings: loads settings (evidence: `app/main.py`)
- Request middleware → Health and metrics: exposes metrics behavior (evidence: `README.md`)
