How learner6131/retrieval-augmented-generation-rag- works
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 Incremental ingest · retrieval.py Hybrid retrieval · config.py Runtime settings · generation.py Grounded answer generation
Components 16
CLI & Settings
- CLI commands · Runs ingest and ask workflows.
app.py - Runtime settings · Centralizes paths and model choices.
config.py
Ingestion & Indexing
- Raw PDF corpus
- Incremental ingest · Hashes PDFs, skips unchanged files, and replaces changed records.
ingest.py - Markdown chunks · Header-aware overlapping chunks retaining provenance and metadata.
ingest.py - Processed manifest · Stores file hashes for incremental lifecycle decisions.
- Chroma dense index · Stores BGE embeddings and chunk records.
- BM25 sparse artifacts · Serialized tokenized corpus with provenance-preserving records.
Hybrid Retrieval
- Hybrid retrieval · Runs dense and sparse search, RRF fusion, and cross-encoder reranking.
retrieval.py - Gemini Multi-HyDE · Generates hypothetical passages for dense retrieval.
generation.py - Reranked evidence · Source-preserving final evidence with relevance metadata.
retrieval.py
Answering & Evaluation
- Grounded answer generation · Builds bounded evidence context and returns citations and confidence.
generation.py - Gemini answer model · gemini-2.5-flash by default.
generation.py - Retrieval evaluation · Reports Precision@K, Recall@K, MRR, and latency against document IDs.
evaluation.py
Other components
- Domain contracts · Shared document, chunk, and retrieval contracts.
models.py - Shared utilities
utils.py
Connections 21
- 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