# viinayakpk/rag-pipeline architecture

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

- Interactive diagram: https://gitdiagram.com/viinayakpk/rag-pipeline
- Repository: https://github.com/viinayakpk/rag-pipeline

## Overview

1. **What this repository is**
   - A FastAPI-based knowledge-retrieval backend, not a generic web app.
   - Its core contract is: ingest documents, index them in PostgreSQL/pgvector, retrieve relevant chunks with hybrid search, then optionally generate grounded answers with citations.

2. **Top-level request boundary**
   - `app/main.py` wires the application together.
   - `app/api/*` contains the HTTP boundary split by capability: text upload, PDF upload, search, chat, single-document lookup, and document/entity-related endpoints.
   - `app/schemas/*` defines the Pydantic request/response shapes that validate and constrain the API surface.

3. **Ingestion pipeline**
   - `POST /text` and `POST /document` both feed the same ingestion service path in `app/services/ingestion.py`.
   - The pipeline is: deduplicate via SHA-256 → detect/resolve language → normalize metadata → chunk text → embed chunks → persist document/chunks/vectors → best-effort entity extraction.
   - This means ingestion is the main write path into the system.

4. **Document processing responsibilities**
   - `app/services/chunking.py` owns text splitting.
   - `app/services/embeddings.py` produces vector representations using the multilingual E5 model.
   - `app/services/entities.py` performs language-aware NER enrichment.
   - These are separate service boundaries so the ingestion flow can orchestrate them without embedding model or NLP details leaking into API code.

5. **Persistence layer**
   - `app/db/models.py` defines the schema for documents, chunks, embeddings, entities, and their linking tables.
   - `app/db/session.py` manages database sessions; `app/db/base.py` centralizes model registration.
   - Alembic migrations live under `app/db/migrations/`, so schema evolution is explicit and versioned.
   - PostgreSQL is the single system of record, with pgvector storing embeddings and SQL/JSONB handling metadata.

6. **Retrieval/search pipeline**
   - `GET /search` is implemented through `app/services/retrieval.py`.
   - The pipeline combines vector similarity and PostgreSQL full-text search, applies metadata filters, merges candidate lists with reciprocal rank fusion, then reranks the short list with a cross-encoder.
   - This is the key read path and the main place where retrieval quality is enforced.

7. **Chat/QA pipeline**
   - `POST /chat` reuses the retrieval pipeline, then hands the top chunks to `app/services/chat.py`.
   - `app/services/llm.py` abstracts the provider boundary: mock by default, with Ollama or OpenRouter as optional real LLM backends.
   - The answer is intentionally grounded in retrieved context and returns source citations plus retrieval notes, so the LLM is downstream of retrieval rather than the source of truth.

8. **Language and multilingual design**
   - The system is built for EN, SV, DE, NO, and FR.
   - Language affects multiple subsystems: metadata validation, FTS configuration, chunking/ingestion assumptions, entity extraction models, and cross-lingual embedding retrieval.
   - This multilingual support is architectural, not cosmetic: an English query can retrieve Swedish/Norwegian/French documents through the shared embedding space.

9. **Configuration and operational boundaries**
   - `app/config.py` centralizes environment-driven settings such as LLM provider, model names, candidate counts, ports, and auth.
   - `app/errors.py` likely standardizes API error mapping so upload/search/chat failures return consistent HTTP responses.
   - The repo is container-first: `Dockerfile` and `docker-compose.yml` package the app, PostgreSQL, and optional LLM runtime into a single deployable unit.

10. **Auth and public health endpoints**
   - API key protection is optional and configured via environment variables, so it is an operational toggle rather than a hard dependency.
   - `/health` and `/ready` remain open for monitoring regardless of auth.
   - That creates a clean separation between business endpoints and infrastructure probes.

11. **Tests and evaluation as part of architecture**
   - `tests/` covers the major boundaries: config, health, chunking, ingestion, retrieval, and API behavior.
   - `examples/*.sh` provide reproducible benchmark and robustness checks, which function as an evaluation harness around the retrieval system.
   - This repo is therefore built around measurable retrieval quality, not just endpoint correctness.

12. **Important architectural shape to diagram**
   - One can draw it as: HTTP API layer → service layer → PostgreSQL/pgvector + NLP/embedding/rerank components → LLM provider for chat output.
   - The critical data flow is document ingestion into DB-backed indexed chunks, then hybrid retrieval from that store, then optional LLM synthesis over retrieved evidence.

Main entry points: [`app/main.py`](https://github.com/viinayakpk/rag-pipeline/blob/main/app/main.py) (App wire-up), [`app/services/ingestion.py`](https://github.com/viinayakpk/rag-pipeline/blob/main/app/services/ingestion.py) (Ingestion), [`app/services/retrieval.py`](https://github.com/viinayakpk/rag-pipeline/blob/main/app/services/retrieval.py) (Retrieval), [`app/db/models.py`](https://github.com/viinayakpk/rag-pipeline/blob/main/app/db/models.py) (DB models)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_grp_api["HTTP API"]
  node_node_main["App wire-up<br/>FastAPI app<br/>[main.py]"]
  node_node_api_text["Text ingest<br/>HTTP route<br/>[text.py]"]
  node_node_api_document["PDF ingest<br/>HTTP route<br/>[document.py]"]
  node_node_api_search["Search API<br/>HTTP route<br/>[search.py]"]
  node_node_api_chat["Chat API<br/>HTTP route<br/>[chat.py]"]
  node_node_api_documents["Docs API<br/>HTTP routes<br/>[documents.py]"]
  node_node_schema_ingest["Ingest schemas<br/>Pydantic models<br/>[ingest.py]"]
  node_node_schema_search["Search schemas<br/>Pydantic models<br/>[search.py]"]
  node_node_schema_chat["Chat schemas<br/>Pydantic models<br/>[chat.py]"]
end

subgraph group_grp_service["Service Layer"]
  node_node_ingestion{{"Ingestion<br/>pipeline<br/>[ingestion.py]"}}
  node_node_chunking["Chunking<br/>text splitter<br/>[chunking.py]"]
  node_node_embeddings["Embeddings<br/>vector encoder<br/>[embeddings.py]"]
  node_node_entities["Entity NER<br/>nlp enrichment<br/>[entities.py]"]
  node_node_retrieval{{"Retrieval<br/>hybrid search<br/>[retrieval.py]"}}
  node_node_reranking["Reranking<br/>cross-encoder<br/>[reranking.py]"]
  node_node_chat{{"Chat synthesis<br/>QA pipeline<br/>[chat.py]"}}
  node_node_llm["LLM provider<br/>model adapter<br/>[llm.py]"]
end

subgraph group_grp_db["Persistence"]
  node_node_db_models[("DB models<br/>SQLAlchemy models<br/>[models.py]")]
  node_node_db_session["DB session<br/>session mgr<br/>[session.py]"]
  node_node_db_migrations["Migrations<br/>Alembic<br/>[env.py]"]
end

subgraph group_grp_ops["Operations"]
  node_node_config["Config<br/>settings<br/>[config.py]"]
  node_node_errors["Errors<br/>error mapping<br/>[errors.py]"]
  node_node_compose["Container stack<br/>deployment<br/>[docker-compose.yml]"]
end

node_node_main -->|"mounts"| node_node_api_text
node_node_main -->|"mounts"| node_node_api_document
node_node_main -->|"mounts"| node_node_api_search
node_node_main -->|"mounts"| node_node_api_chat
node_node_main -->|"mounts"| node_node_api_documents
node_node_main -->|"loads"| node_node_config
node_node_main -->|"uses"| node_node_errors
node_node_api_text -->|"validates"| node_node_schema_ingest
node_node_api_document -->|"validates"| node_node_schema_ingest
node_node_api_search -->|"validates"| node_node_schema_search
node_node_api_chat -->|"validates"| node_node_schema_chat
node_node_api_text -->|"calls"| node_node_ingestion
node_node_api_document -->|"calls"| node_node_ingestion
node_node_api_search -->|"calls"| node_node_retrieval
node_node_api_chat -->|"calls"| node_node_chat
node_node_api_documents -->|"reads"| node_node_db_models
node_node_ingestion -->|"orchestrates"| node_node_chunking
node_node_ingestion -->|"orchestrates"| node_node_embeddings
node_node_ingestion -->|"orchestrates"| node_node_entities
node_node_ingestion -->|"persists via"| node_node_db_session
node_node_ingestion -->|"writes"| node_node_db_models
node_node_retrieval -->|"queries"| node_node_db_session
node_node_retrieval -->|"feeds"| node_node_reranking
node_node_retrieval -->|"reads"| node_node_db_models
node_node_chat -->|"reuses"| node_node_retrieval
node_node_chat -->|"calls"| node_node_llm
node_node_db_models -->|"evolves through"| node_node_db_migrations
node_node_compose -->|"runs"| node_node_main
node_node_compose -->|"provisions"| node_node_db_session

click node_node_main "https://github.com/viinayakpk/rag-pipeline/blob/main/app/main.py"
click node_node_api_text "https://github.com/viinayakpk/rag-pipeline/blob/main/app/api/text.py"
click node_node_api_document "https://github.com/viinayakpk/rag-pipeline/blob/main/app/api/document.py"
click node_node_api_search "https://github.com/viinayakpk/rag-pipeline/blob/main/app/api/search.py"
click node_node_api_chat "https://github.com/viinayakpk/rag-pipeline/blob/main/app/api/chat.py"
click node_node_api_documents "https://github.com/viinayakpk/rag-pipeline/blob/main/app/api/documents.py"
click node_node_schema_ingest "https://github.com/viinayakpk/rag-pipeline/blob/main/app/schemas/ingest.py"
click node_node_schema_search "https://github.com/viinayakpk/rag-pipeline/blob/main/app/schemas/search.py"
click node_node_schema_chat "https://github.com/viinayakpk/rag-pipeline/blob/main/app/schemas/chat.py"
click node_node_ingestion "https://github.com/viinayakpk/rag-pipeline/blob/main/app/services/ingestion.py"
click node_node_chunking "https://github.com/viinayakpk/rag-pipeline/blob/main/app/services/chunking.py"
click node_node_embeddings "https://github.com/viinayakpk/rag-pipeline/blob/main/app/services/embeddings.py"
click node_node_entities "https://github.com/viinayakpk/rag-pipeline/blob/main/app/services/entities.py"
click node_node_retrieval "https://github.com/viinayakpk/rag-pipeline/blob/main/app/services/retrieval.py"
click node_node_reranking "https://github.com/viinayakpk/rag-pipeline/blob/main/app/services/reranking.py"
click node_node_chat "https://github.com/viinayakpk/rag-pipeline/blob/main/app/services/chat.py"
click node_node_llm "https://github.com/viinayakpk/rag-pipeline/blob/main/app/services/llm.py"
click node_node_db_models "https://github.com/viinayakpk/rag-pipeline/blob/main/app/db/models.py"
click node_node_db_session "https://github.com/viinayakpk/rag-pipeline/blob/main/app/db/session.py"
click node_node_db_migrations "https://github.com/viinayakpk/rag-pipeline/blob/main/app/db/migrations/env.py"
click node_node_config "https://github.com/viinayakpk/rag-pipeline/blob/main/app/config.py"
click node_node_errors "https://github.com/viinayakpk/rag-pipeline/blob/main/app/errors.py"
click node_node_compose "https://github.com/viinayakpk/rag-pipeline/blob/main/docker-compose.yml"

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_node_main,node_node_api_text,node_node_api_document,node_node_api_search,node_node_api_chat,node_node_api_documents,node_node_schema_ingest,node_node_schema_search,node_node_schema_chat toneBlue
class node_node_ingestion,node_node_chunking,node_node_embeddings,node_node_entities,node_node_retrieval,node_node_reranking,node_node_chat,node_node_llm toneAmber
class node_node_db_models,node_node_db_session,node_node_db_migrations toneMint
class node_node_config,node_node_errors,node_node_compose toneRose
```

## Components

### HTTP API

FastAPI routes and request/response schemas

- [App wire-up](https://github.com/viinayakpk/rag-pipeline/blob/main/app/main.py): FastAPI app · Creates the application and connects routes, config, and middleware. · `app/main.py`
- [Text ingest](https://github.com/viinayakpk/rag-pipeline/blob/main/app/api/text.py): HTTP route · Endpoint for raw text upload and ingestion. · `app/api/text.py`
- [PDF ingest](https://github.com/viinayakpk/rag-pipeline/blob/main/app/api/document.py): HTTP route · Endpoint for document upload and ingestion. · `app/api/document.py`
- [Search API](https://github.com/viinayakpk/rag-pipeline/blob/main/app/api/search.py): HTTP route · Hybrid retrieval endpoint for ranked chunk lookup. · `app/api/search.py`
- [Chat API](https://github.com/viinayakpk/rag-pipeline/blob/main/app/api/chat.py): HTTP route · Grounded QA endpoint that answers from retrieved context. · `app/api/chat.py`
- [Docs API](https://github.com/viinayakpk/rag-pipeline/blob/main/app/api/documents.py): HTTP routes · Document and entity lookup endpoints. · `app/api/documents.py`
- [Ingest schemas](https://github.com/viinayakpk/rag-pipeline/blob/main/app/schemas/ingest.py): Pydantic models · Validation shapes for ingestion requests and responses. · `app/schemas/ingest.py`
- [Search schemas](https://github.com/viinayakpk/rag-pipeline/blob/main/app/schemas/search.py): Pydantic models · Validation shapes for search requests and results. · `app/schemas/search.py`
- [Chat schemas](https://github.com/viinayakpk/rag-pipeline/blob/main/app/schemas/chat.py): Pydantic models · Validation shapes for chat requests and answers. · `app/schemas/chat.py`

### Service Layer

Ingestion, retrieval, chat, and NLP orchestration

- [Ingestion](https://github.com/viinayakpk/rag-pipeline/blob/main/app/services/ingestion.py): pipeline · Deduplicates, normalizes, chunks, embeds, persists, and enriches documents. · `app/services/ingestion.py`
- [Chunking](https://github.com/viinayakpk/rag-pipeline/blob/main/app/services/chunking.py): text splitter · Splits documents into retrieval-friendly chunks. · `app/services/chunking.py`
- [Embeddings](https://github.com/viinayakpk/rag-pipeline/blob/main/app/services/embeddings.py): vector encoder · Produces multilingual E5 embeddings for chunks and queries. · `app/services/embeddings.py`
- [Entity NER](https://github.com/viinayakpk/rag-pipeline/blob/main/app/services/entities.py): nlp enrichment · Performs language-aware entity extraction during ingestion. · `app/services/entities.py`
- [Retrieval](https://github.com/viinayakpk/rag-pipeline/blob/main/app/services/retrieval.py): hybrid search · Combines vector search, full-text search, fusion, and filtering. · `app/services/retrieval.py`
- [Reranking](https://github.com/viinayakpk/rag-pipeline/blob/main/app/services/reranking.py): cross-encoder · Scores the shortlist to improve result ordering. · `app/services/reranking.py`
- [Chat synthesis](https://github.com/viinayakpk/rag-pipeline/blob/main/app/services/chat.py): QA pipeline · Builds grounded answers from retrieved chunks and citations. · `app/services/chat.py`
- [LLM provider](https://github.com/viinayakpk/rag-pipeline/blob/main/app/services/llm.py): model adapter · Abstracts mock, Ollama, and OpenRouter backends. · `app/services/llm.py`

### Persistence

Database models, session management, and migrations

- [DB models](https://github.com/viinayakpk/rag-pipeline/blob/main/app/db/models.py): SQLAlchemy models · Defines documents, chunks, embeddings, entities, and links. · `app/db/models.py`
- [DB session](https://github.com/viinayakpk/rag-pipeline/blob/main/app/db/session.py): session mgr · Creates and manages PostgreSQL sessions. · `app/db/session.py`
- [Migrations](https://github.com/viinayakpk/rag-pipeline/blob/main/app/db/migrations/env.py): Alembic · Versioned schema evolution for the database. · `app/db/migrations/env.py`

### Operations

Runtime config, app wiring, and deployment packaging

- [Config](https://github.com/viinayakpk/rag-pipeline/blob/main/app/config.py): settings · Environment-driven settings for providers, auth, and runtime limits. · `app/config.py`
- [Errors](https://github.com/viinayakpk/rag-pipeline/blob/main/app/errors.py): error mapping · Normalizes failures into API responses. · `app/errors.py`
- [Container stack](https://github.com/viinayakpk/rag-pipeline/blob/main/docker-compose.yml): deployment · Docker packaging for the app, PostgreSQL, and optional LLM runtime. · `docker-compose.yml`

## Connections

- App wire-up → Text ingest: mounts · Registers the text ingest route.
- App wire-up → PDF ingest: mounts · Registers the PDF/document ingest route.
- App wire-up → Search API: mounts · Registers the search route.
- App wire-up → Chat API: mounts · Registers the chat route.
- App wire-up → Docs API: mounts · Registers document lookup routes.
- App wire-up → Config: loads · Pulls runtime settings into the app.
- App wire-up → Errors: uses · Maps exceptions to consistent HTTP responses.
- Text ingest → Ingest schemas: validates · Validates text ingestion payloads.
- PDF ingest → Ingest schemas: validates · Validates document upload payloads.
- Search API → Search schemas: validates · Validates search requests and result shapes.
- Chat API → Chat schemas: validates · Validates chat requests and responses.
- Text ingest → Ingestion: calls · Hands text uploads to the ingestion pipeline.
- PDF ingest → Ingestion: calls · Hands document uploads to the ingestion pipeline.
- Search API → Retrieval: calls · Executes hybrid retrieval for search.
- Chat API → Chat synthesis: calls · Starts grounded answer generation.
- Docs API → DB models: reads · Fetches persisted documents and entities.
- Ingestion → Chunking: orchestrates · Splits normalized text into chunks.
- Ingestion → Embeddings: orchestrates · Creates embeddings for chunks.
- Ingestion → Entity NER: orchestrates · Optionally enriches chunks with entities.
- Ingestion → DB session: persists via · Writes documents, chunks, vectors, and entities.
- Ingestion → DB models: writes · Uses the relational schema for stored knowledge.
- Retrieval → DB session: queries · Reads indexed chunks and metadata from PostgreSQL.
- Retrieval → Reranking: feeds · Sends candidate chunks to reranking.
- Retrieval → DB models: reads · Uses document/chunk/embedding schema for search.
- Chat synthesis → Retrieval: reuses · Retrieves evidence before answering.
- Chat synthesis → LLM provider: calls · Delegates synthesis to the configured model backend.
- DB models → Migrations: evolves through · Schema changes are tracked in Alembic.
- Container stack → App wire-up: runs · Packages the API service for deployment.
- Container stack → DB session: provisions · Connects the app to PostgreSQL.
