# learner6131/retrieval-augmented-generation-rag- architecture

> Architecture diagram of the GitHub repository learner6131/retrieval-augmented-generation-rag-, 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/learner6131/retrieval-augmented-generation-rag-
- Repository: https://github.com/learner6131/retrieval-augmented-generation-rag-

## Overview

#### Purpose and CLI Entry Points
This repository is a production-oriented, local-persistent PDF RAG system for enterprise document search. `app.py` is the command-line entry point: `ingest` builds or incrementally updates indexes, while `ask` runs hybrid retrieval and Gemini-grounded answer generation. Runtime settings, paths, and model choices are centralized in `config.py`.

Paths: `app.py`, `config.py`

#### Ingestion and Incremental Document Lifecycle
`ingest.py` accepts PDFs from `data/raw/`, converts them to Markdown with Docling, and tracks file hashes in a processed manifest. Unchanged PDFs are skipped; changed documents have prior dense and sparse records removed before replacement. Document IDs are deterministic from paths relative to the raw corpus root, preserving provenance and stable evaluation labels.

Paths: `ingest.py`, `models.py`, `config.py`

#### Structure-Aware Chunk Construction
Markdown output is split first by headers and then recursively into overlapping character chunks. Chunk records retain document identity, source path, Markdown/header metadata, and text needed for citation, indexing, and reranking. This is the boundary between document parsing and retrieval-ready evidence.

Paths: `ingest.py`, `models.py`

#### Persistent Hybrid Indexes
Dense retrieval uses Sentence Transformers BGE embeddings stored in a persistent ChromaDB collection. Sparse retrieval maintains provenance-preserving chunk records plus a tokenized BM25 corpus serialized under the BM25 artifact directory. Both indexes are updated together during ingestion so retrieval can combine compatible document versions.

Paths: `ingest.py`, `retrieval.py`, `models.py`

#### Retrieval, Fusion, and Reranking
`retrieval.py` explicitly implements the retrieval pipeline rather than delegating to LangChain retriever abstractions. A question and Gemini-generated Multi-HyDE hypothetical passages drive dense search; the original query drives BM25 search. Candidate rankings are merged through reciprocal-rank fusion, then a BGE cross-encoder selects the final evidence set. Retrieval results preserve source and relevance metadata for downstream citations and confidence scoring.

Paths: `retrieval.py`, `generation.py`, `models.py`

#### Answer Generation and Evidence Boundary
`generation.py` assembles a bounded evidence context from reranked chunks and calls Gemini (`gemini-2.5-flash` by default) for a grounded response. It produces inline source labels, source paths, reranker scores, and an evidence-strength confidence estimate. Gemini is required for Multi-HyDE and answer generation, while local ChromaDB, BM25, Docling, PyTorch, and BGE models support ingestion and retrieval.

Paths: `generation.py`, `retrieval.py`, `config.py`

#### Evaluation and Supporting Contracts
`evaluation.py` benchmarks the persisted retrieval system using labeled document IDs, reporting Precision@K, Recall@K, MRR, and latency. Shared typed/domain records and utility behavior reside in `models.py` and `utils.py`; these form the contracts between ingestion, retrieval, generation, and evaluation.

Paths: `evaluation.py`, `models.py`, `utils.py`

Main entry points: [`ingest.py`](https://github.com/learner6131/retrieval-augmented-generation-rag-/blob/main/ingest.py) (Incremental ingest), [`retrieval.py`](https://github.com/learner6131/retrieval-augmented-generation-rag-/blob/main/retrieval.py) (Hybrid retrieval), [`config.py`](https://github.com/learner6131/retrieval-augmented-generation-rag-/blob/main/config.py) (Runtime settings), [`generation.py`](https://github.com/learner6131/retrieval-augmented-generation-rag-/blob/main/generation.py) (Grounded answer generation)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_cli["CLI &amp; Settings"]
  node_app["CLI commands<br/>Python CLI<br/>[app.py]"]
  node_config["Runtime settings<br/>configuration module<br/>[config.py]"]
end

subgraph group_ingestion["Ingestion &amp; Indexing"]
  node_raw_pdfs["Raw PDF corpus<br/>local documents"]
  node_ingest["Incremental ingest<br/>ingestion pipeline<br/>[ingest.py]"]
  node_markdown_chunks["Markdown chunks<br/>retrieval evidence<br/>[ingest.py]"]
  node_manifest[("Processed manifest<br/>persistent state")]
  node_chroma[("Chroma dense index<br/>persistent vector index")]
  node_bm25[("BM25 sparse artifacts<br/>persistent sparse index")]
end

subgraph group_retrieval["Hybrid Retrieval"]
  node_retrieval_pipeline["Hybrid retrieval<br/>retrieval pipeline<br/>[retrieval.py]"]
  node_gemini_hyde{{"Gemini Multi-HyDE<br/>LLM query expansion<br/>[generation.py]"}}
  node_reranked_evidence["Reranked evidence<br/>ranked chunks<br/>[retrieval.py]"]
end

subgraph group_answering["Answering &amp; Evaluation"]
  node_generation["Grounded answer generation<br/>generation pipeline<br/>[generation.py]"]
  node_gemini_answer{{"Gemini answer model<br/>LLM<br/>[generation.py]"}}
  node_evaluation["Retrieval evaluation<br/>benchmark runner<br/>[evaluation.py]"]
end

node_models["Domain contracts<br/>typed records<br/>[models.py]"]
node_utils["Shared utilities<br/>utility module<br/>[utils.py]"]

node_app -->|"ingest"| node_ingest
node_app -->|"ask"| node_retrieval_pipeline
node_config -.->|"paths and models"| node_ingest
node_config -.->|"retrieval settings"| node_retrieval_pipeline
node_config -.->|"model settings"| node_generation
node_raw_pdfs -->|"PDFs"| node_ingest
node_ingest -->|"reads and updates hashes"| node_manifest
node_ingest -->|"Docling parse and chunk"| node_markdown_chunks
node_models -.->|"record contracts"| node_ingest
node_models -.->|"result contracts"| node_retrieval_pipeline
node_utils -.->|"shared behavior"| node_ingest
node_markdown_chunks -->|"BGE embeddings"| node_chroma
node_markdown_chunks -->|"tokenized corpus"| node_bm25
node_gemini_hyde -->|"hypothetical passages"| node_retrieval_pipeline
node_chroma -->|"dense candidates"| node_retrieval_pipeline
node_bm25 -->|"sparse candidates"| node_retrieval_pipeline
node_retrieval_pipeline -->|"RRF and cross-encoder"| node_reranked_evidence
node_reranked_evidence -->|"bounded evidence"| node_generation
node_gemini_answer -->|"grounded response"| node_generation
node_chroma -->|"persisted retrieval corpus"| node_evaluation
node_bm25 -->|"persisted retrieval corpus"| node_evaluation

click node_app "https://github.com/learner6131/retrieval-augmented-generation-rag-/blob/main/app.py"
click node_config "https://github.com/learner6131/retrieval-augmented-generation-rag-/blob/main/config.py"
click node_ingest "https://github.com/learner6131/retrieval-augmented-generation-rag-/blob/main/ingest.py"
click node_models "https://github.com/learner6131/retrieval-augmented-generation-rag-/blob/main/models.py"
click node_markdown_chunks "https://github.com/learner6131/retrieval-augmented-generation-rag-/blob/main/ingest.py"
click node_retrieval_pipeline "https://github.com/learner6131/retrieval-augmented-generation-rag-/blob/main/retrieval.py"
click node_gemini_hyde "https://github.com/learner6131/retrieval-augmented-generation-rag-/blob/main/generation.py"
click node_reranked_evidence "https://github.com/learner6131/retrieval-augmented-generation-rag-/blob/main/retrieval.py"
click node_generation "https://github.com/learner6131/retrieval-augmented-generation-rag-/blob/main/generation.py"
click node_gemini_answer "https://github.com/learner6131/retrieval-augmented-generation-rag-/blob/main/generation.py"
click node_evaluation "https://github.com/learner6131/retrieval-augmented-generation-rag-/blob/main/evaluation.py"
click node_utils "https://github.com/learner6131/retrieval-augmented-generation-rag-/blob/main/utils.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_config toneBlue
class node_raw_pdfs,node_ingest,node_markdown_chunks,node_manifest,node_chroma,node_bm25 toneAmber
class node_retrieval_pipeline,node_gemini_hyde,node_reranked_evidence toneMint
class node_generation,node_gemini_answer,node_evaluation toneRose
class node_models,node_utils toneNeutral
```

## Components

### CLI & Settings

- [CLI commands](https://github.com/learner6131/retrieval-augmented-generation-rag-/blob/main/app.py): Python CLI · Runs ingest and ask workflows. · `app.py`
- [Runtime settings](https://github.com/learner6131/retrieval-augmented-generation-rag-/blob/main/config.py): configuration module · Centralizes paths and model choices. · `config.py`

### Ingestion & Indexing

- **Raw PDF corpus**: local documents
- [Incremental ingest](https://github.com/learner6131/retrieval-augmented-generation-rag-/blob/main/ingest.py): ingestion pipeline · Hashes PDFs, skips unchanged files, and replaces changed records. · `ingest.py`
- [Markdown chunks](https://github.com/learner6131/retrieval-augmented-generation-rag-/blob/main/ingest.py): retrieval evidence · Header-aware overlapping chunks retaining provenance and metadata. · `ingest.py`
- **Processed manifest**: persistent state · Stores file hashes for incremental lifecycle decisions.
- **Chroma dense index**: persistent vector index · Stores BGE embeddings and chunk records.
- **BM25 sparse artifacts**: persistent sparse index · Serialized tokenized corpus with provenance-preserving records.

### Hybrid Retrieval

- [Hybrid retrieval](https://github.com/learner6131/retrieval-augmented-generation-rag-/blob/main/retrieval.py): retrieval pipeline · Runs dense and sparse search, RRF fusion, and cross-encoder reranking. · `retrieval.py`
- [Gemini Multi-HyDE](https://github.com/learner6131/retrieval-augmented-generation-rag-/blob/main/generation.py): LLM query expansion · Generates hypothetical passages for dense retrieval. · `generation.py`
- [Reranked evidence](https://github.com/learner6131/retrieval-augmented-generation-rag-/blob/main/retrieval.py): ranked chunks · Source-preserving final evidence with relevance metadata. · `retrieval.py`

### Answering & Evaluation

- [Grounded answer generation](https://github.com/learner6131/retrieval-augmented-generation-rag-/blob/main/generation.py): generation pipeline · Builds bounded evidence context and returns citations and confidence. · `generation.py`
- [Gemini answer model](https://github.com/learner6131/retrieval-augmented-generation-rag-/blob/main/generation.py): LLM · gemini-2.5-flash by default. · `generation.py`
- [Retrieval evaluation](https://github.com/learner6131/retrieval-augmented-generation-rag-/blob/main/evaluation.py): benchmark runner · Reports Precision@K, Recall@K, MRR, and latency against document IDs. · `evaluation.py`

### Other components

- [Domain contracts](https://github.com/learner6131/retrieval-augmented-generation-rag-/blob/main/models.py): typed records · Shared document, chunk, and retrieval contracts. · `models.py`
- [Shared utilities](https://github.com/learner6131/retrieval-augmented-generation-rag-/blob/main/utils.py): utility module · `utils.py`

## Connections

- CLI commands → Incremental ingest: ingest
- CLI commands → Hybrid retrieval: ask
- Runtime settings → Incremental ingest: paths and models
- Runtime settings → Hybrid retrieval: retrieval settings
- Runtime settings → Grounded answer generation: model settings
- Raw PDF corpus → Incremental ingest: PDFs
- Incremental ingest → Processed manifest: reads and updates hashes
- Incremental ingest → Markdown chunks: Docling parse and chunk
- Domain contracts → Incremental ingest: record contracts
- Domain contracts → Hybrid retrieval: result contracts
- Shared utilities → Incremental ingest: shared behavior
- Markdown chunks → Chroma dense index: BGE embeddings
- Markdown chunks → BM25 sparse artifacts: tokenized corpus
- Gemini Multi-HyDE → Hybrid retrieval: hypothetical passages
- Chroma dense index → Hybrid retrieval: dense candidates
- BM25 sparse artifacts → Hybrid retrieval: sparse candidates
- Hybrid retrieval → Reranked evidence: RRF and cross-encoder
- Reranked evidence → Grounded answer generation: bounded evidence
- Gemini answer model → Grounded answer generation: grounded response
- Chroma dense index → Retrieval evaluation: persisted retrieval corpus
- BM25 sparse artifacts → Retrieval evaluation: persisted retrieval corpus
