# kodekloudhub/fundamentals-of-rag architecture

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

- Interactive diagram: https://gitdiagram.com/kodekloudhub/fundamentals-of-rag
- Repository: https://github.com/kodekloudhub/fundamentals-of-rag

## Overview

#### 1. Repository Purpose
A collection of independent Python RAG learning demos, not a single deployable application. It covers document-to-retrieval stages: parsing, chunking, vector indexing, keyword/semantic retrieval, and LLM answer generation. The most complete runtime is BookSearch; other directories are focused utilities or experiments.

#### 2. Primary End-to-End RAG Runtime
BookSearch is the main QA system over local book files. Its command-line entry points evolve from simple in-memory retrieval to file ingestion and hybrid retrieval:
- `BookSearch/app_v1.py`
- `BookSearch/app_v2.py`
- `BookSearch/hybrid_rag.py`

Control flow is: local text corpus → overlapping chunks plus source metadata → vector/BM25 indexing → retrieve relevant context → prompt local LLM → answer with citations. `app_v2.py` exposes setup, ingest, and ask operations; `hybrid_rag.py` is the highest-quality retrieval path.

#### 3. Retrieval and Persistence Boundary
BookSearch uses ChromaDB as its vector-store boundary and Ollama for both generation and embeddings. Semantic retrieval is backed by persisted vector data; hybrid retrieval combines it with a local BM25 corpus and applies Reciprocal Rank Fusion (RRF).
- `BookSearch/hybrid_rag.py`
- `BookSearch/index/bm25_corpus_tokens.pkl`
- `BookSearch/index/bm25_ids.pkl`

Required external runtime services are an Ollama server with `llama3.3` and `nomic-embed-text` models. ChromaDB is locally persistent rather than a managed infrastructure service.

#### 4. Corpus and Ingestion Inputs
The BookSearch corpus is local classic-literature text. Ingestion owns chunk construction, source association, and index population; stored BM25 artifacts imply index state must remain aligned with ingested chunks.
- `BookSearch/books/adventuresofsherlockholmes.txt`
- `BookSearch/books/completeworkswilliamshakespeare.txt`
- `BookSearch/books/frankenstein.txt`

A broader reusable public-domain search corpus is available separately under `SearchFiles/`, but it is not structurally coupled to the BookSearch runtime.

#### 5. Standalone Document Normalization Components
Format-specific utilities turn source files into text/chunks or RAG-ready records. They are reusable conceptual predecessors to ingestion but do not form a shared package boundary:
- PDF extraction and JSON/text chunk output: `PDFParser/pdf_parser.py`, `PDFParser/main.py`
- DOCX extraction, metadata, and hierarchical chunking: `WordParser/main.py`
- Plain-text metadata extraction and sentence-aware chunks: `TextParser/main.py`
- CSV row-to-searchable-document conversion: `CSVParser/main.py`

Their outputs are suitable inputs to a vector-store ingestion layer, but each demo is independently invoked.

#### 6. Chunking Strategy Layer
`ChunkingDemo` is the repository’s configurable chunking laboratory, supporting line, fixed-size, sliding-window, sentence, paragraph, page, section, and token strategies across multiple document formats.
- `ChunkingDemo/document_chunker.py`
- `ChunkingDemo/demo.pdf`
- `ChunkingDemo/demo.docx`

Chunk size and overlap are the central quality boundary between extraction and retrieval: they affect context preservation, embedding granularity, and downstream answer grounding.

#### 7. Vector and Search Learning Paths
A minimal semantic-search implementation provides the clearest vector-store-only flow: text files are chunked, embedded with Sentence Transformers, persisted in ChromaDB, and queried with optional metadata filtering.
- `ChromaDemo/ingest_and_query.py`
- `ChromaDemo/data/adventuresofsherlockholmes.txt`

Search tradeoffs and hybrid-search concepts are explored interactively rather than through production modules:
- `SearchTool/BM25-vs-Semantic.ipynb`
- `SearchTool/Semantic-Demo.ipynb`

Main entry points: [`BookSearch/hybrid_rag.py`](https://github.com/kodekloudhub/fundamentals-of-rag/blob/main/BookSearch/hybrid_rag.py) (Hybrid retrieval and RRF), [`BookSearch/app_v2.py`](https://github.com/kodekloudhub/fundamentals-of-rag/blob/main/BookSearch/app_v2.py) (Setup and ingestion), [`ChunkingDemo/document_chunker.py`](https://github.com/kodekloudhub/fundamentals-of-rag/blob/main/ChunkingDemo/document_chunker.py) (Multi-strategy chunker), [`ChromaDemo/ingest_and_query.py`](https://github.com/kodekloudhub/fundamentals-of-rag/blob/main/ChromaDemo/ingest_and_query.py) (Ingest and query)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_booksearch["BookSearch"]
  node_book_corpus["Classic book corpus<br/>local text corpus"]
  node_book_ingest["Setup and ingestion<br/>CLI ingestion<br/>[app_v2.py]"]
  node_book_chunks["Chunks and source metadata<br/>retrieval records"]
  node_chroma_store[("Persistent ChromaDB<br/>vector store")]
  node_bm25_artifacts[("Persisted BM25 corpus<br/>keyword index")]
  node_hybrid_retrieval["Hybrid retrieval and RRF<br/>retrieval pipeline<br/>[hybrid_rag.py]"]
  node_book_qa["Ask CLI<br/>QA entry point<br/>[app_v2.py]"]
  node_early_book_cli["In-memory retrieval demo<br/>CLI prototype<br/>[app_v1.py]"]
  node_ollama{{"Ollama server<br/>local model service"}}
end

subgraph group_normalization["Document normalization"]
  node_pdf_parser["PDF extraction<br/>parser demo<br/>[pdf_parser.py]"]
  node_pdf_parser_cli["PDF parser runner<br/>CLI entry point<br/>[main.py]"]
  node_docx_parser["DOCX hierarchical chunks<br/>parser demo<br/>[main.py]"]
  node_text_parser["Text metadata chunks<br/>parser demo<br/>[main.py]"]
  node_csv_parser["CSV document conversion<br/>parser demo<br/>[main.py]"]
end

subgraph group_chunking["Chunking lab"]
  node_chunker["Multi-strategy chunker<br/>chunking engine"]
  node_chunking_samples["PDF and DOCX samples<br/>sample documents"]
end

subgraph group_semantic["Semantic search"]
  node_chroma_demo["Ingest and query<br/>semantic search script"]
  node_chroma_demo_corpus["Sherlock input text<br/>local text corpus"]
end

subgraph group_experiments["Search experiments"]
  node_search_notebooks["BM25 and semantic notebooks<br/>interactive experiments"]
end

node_public_corpus["Public-domain corpus<br/>search corpus"]

node_book_corpus -->|"ingests"| node_book_ingest
node_book_ingest -->|"creates"| node_book_chunks
node_book_chunks -->|"embeds and persists"| node_chroma_store
node_book_chunks -->|"builds"| node_bm25_artifacts
node_ollama -->|"embeddings"| node_book_ingest
node_book_qa -->|"queries"| node_hybrid_retrieval
node_hybrid_retrieval -->|"semantic ranking"| node_chroma_store
node_hybrid_retrieval -->|"keyword ranking"| node_bm25_artifacts
node_hybrid_retrieval -->|"answer generation"| node_ollama
node_early_book_cli -.->|"evolves into"| node_book_qa
node_pdf_parser_cli -->|"runs"| node_pdf_parser
node_pdf_parser -.->|"text/chunks inform"| node_chunker
node_docx_parser -.->|"document input pattern"| node_chunker
node_text_parser -.->|"RAG-ready records can feed"| node_book_ingest
node_csv_parser -.->|"searchable records can feed"| node_book_ingest
node_chunking_samples -->|"exercises"| node_chunker
node_chunker -.->|"strategy informs"| node_book_chunks
node_chroma_demo_corpus -->|"ingests"| node_chroma_demo
node_chroma_demo -.->|"contrasts with"| node_ollama
node_chroma_demo -.->|"demonstrates persistence"| node_chroma_store
node_search_notebooks -.->|"explores concepts behind"| node_hybrid_retrieval

click node_book_ingest "https://github.com/kodekloudhub/fundamentals-of-rag/blob/main/BookSearch/app_v2.py"
click node_hybrid_retrieval "https://github.com/kodekloudhub/fundamentals-of-rag/blob/main/BookSearch/hybrid_rag.py"
click node_book_qa "https://github.com/kodekloudhub/fundamentals-of-rag/blob/main/BookSearch/app_v2.py"
click node_early_book_cli "https://github.com/kodekloudhub/fundamentals-of-rag/blob/main/BookSearch/app_v1.py"
click node_pdf_parser "https://github.com/kodekloudhub/fundamentals-of-rag/blob/main/PDFParser/pdf_parser.py"
click node_pdf_parser_cli "https://github.com/kodekloudhub/fundamentals-of-rag/blob/main/PDFParser/main.py"
click node_docx_parser "https://github.com/kodekloudhub/fundamentals-of-rag/blob/main/WordParser/main.py"
click node_text_parser "https://github.com/kodekloudhub/fundamentals-of-rag/blob/main/TextParser/main.py"
click node_csv_parser "https://github.com/kodekloudhub/fundamentals-of-rag/blob/main/CSVParser/main.py"
click node_chunker "https://github.com/kodekloudhub/fundamentals-of-rag/blob/main/ChunkingDemo/document_chunker.py"
click node_chroma_demo "https://github.com/kodekloudhub/fundamentals-of-rag/blob/main/ChromaDemo/ingest_and_query.py"
click node_chroma_demo_corpus "https://github.com/kodekloudhub/fundamentals-of-rag/blob/main/ChromaDemo/data/adventuresofsherlockholmes.txt"

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_book_corpus,node_book_ingest,node_book_chunks,node_chroma_store,node_bm25_artifacts,node_hybrid_retrieval,node_book_qa,node_early_book_cli,node_ollama toneBlue
class node_pdf_parser,node_pdf_parser_cli,node_docx_parser,node_text_parser,node_csv_parser toneAmber
class node_chunker,node_chunking_samples toneMint
class node_chroma_demo,node_chroma_demo_corpus toneRose
class node_search_notebooks toneIndigo
class node_public_corpus toneNeutral
```

## Components

### BookSearch

Main local-book RAG runtime.

- **Classic book corpus**: local text corpus · Local literature used by BookSearch ingestion.
- [Setup and ingestion](https://github.com/kodekloudhub/fundamentals-of-rag/blob/main/BookSearch/app_v2.py): CLI ingestion · Builds chunks, source metadata, and index state. · `BookSearch/app_v2.py`
- **Chunks and source metadata**: retrieval records
- **Persistent ChromaDB**: vector store · Locally persisted semantic index.
- **Persisted BM25 corpus**: keyword index · Token and chunk-ID artifacts must align with ingested chunks.
- [Hybrid retrieval and RRF](https://github.com/kodekloudhub/fundamentals-of-rag/blob/main/BookSearch/hybrid_rag.py): retrieval pipeline · Fuses vector and BM25 rankings. · `BookSearch/hybrid_rag.py`
- [Ask CLI](https://github.com/kodekloudhub/fundamentals-of-rag/blob/main/BookSearch/app_v2.py): QA entry point · Retrieves context and returns cited answers. · `BookSearch/app_v2.py`
- [In-memory retrieval demo](https://github.com/kodekloudhub/fundamentals-of-rag/blob/main/BookSearch/app_v1.py): CLI prototype · `BookSearch/app_v1.py`
- **Ollama server**: local model service · Serves llama3.3 generation and nomic-embed-text embeddings.

### Document normalization

Independent format-specific extraction demos.

- [PDF extraction](https://github.com/kodekloudhub/fundamentals-of-rag/blob/main/PDFParser/pdf_parser.py): parser demo · Emits text or JSON chunk output. · `PDFParser/pdf_parser.py`
- [PDF parser runner](https://github.com/kodekloudhub/fundamentals-of-rag/blob/main/PDFParser/main.py): CLI entry point · `PDFParser/main.py`
- [DOCX hierarchical chunks](https://github.com/kodekloudhub/fundamentals-of-rag/blob/main/WordParser/main.py): parser demo · Extracts text, metadata, and hierarchical chunks. · `WordParser/main.py`
- [Text metadata chunks](https://github.com/kodekloudhub/fundamentals-of-rag/blob/main/TextParser/main.py): parser demo · Extracts metadata and sentence-aware chunks. · `TextParser/main.py`
- [CSV document conversion](https://github.com/kodekloudhub/fundamentals-of-rag/blob/main/CSVParser/main.py): parser demo · Converts rows into searchable documents. · `CSVParser/main.py`

### Chunking lab

Configurable chunking strategy experiment.

- [Multi-strategy chunker](https://github.com/kodekloudhub/fundamentals-of-rag/blob/main/ChunkingDemo/document_chunker.py): chunking engine · Supports format-aware line through token chunking strategies. · `ChunkingDemo/document_chunker.py`
- **PDF and DOCX samples**: sample documents

### Semantic search

Minimal ChromaDB learning path.

- [Ingest and query](https://github.com/kodekloudhub/fundamentals-of-rag/blob/main/ChromaDemo/ingest_and_query.py): semantic search script · Chunks files, embeds them, persists to ChromaDB, and queries with metadata filters. · `ChromaDemo/ingest_and_query.py`
- [Sherlock input text](https://github.com/kodekloudhub/fundamentals-of-rag/blob/main/ChromaDemo/data/adventuresofsherlockholmes.txt): local text corpus · `ChromaDemo/data/adventuresofsherlockholmes.txt`

### Search experiments

Interactive retrieval tradeoff notebooks.

- **BM25 and semantic notebooks**: interactive experiments · Explores keyword, semantic, and hybrid-search tradeoffs.

### Other components

- **Public-domain corpus**: search corpus · Separate reusable corpus, not coupled to BookSearch.

## Connections

- Classic book corpus → Setup and ingestion: ingests
- Setup and ingestion → Chunks and source metadata: creates
- Chunks and source metadata → Persistent ChromaDB: embeds and persists
- Chunks and source metadata → Persisted BM25 corpus: builds
- Ollama server → Setup and ingestion: embeddings
- Ask CLI → Hybrid retrieval and RRF: queries
- Hybrid retrieval and RRF → Persistent ChromaDB: semantic ranking
- Hybrid retrieval and RRF → Persisted BM25 corpus: keyword ranking
- Hybrid retrieval and RRF → Ollama server: answer generation
- In-memory retrieval demo → Ask CLI: evolves into
- PDF parser runner → PDF extraction: runs
- PDF extraction → Multi-strategy chunker: text/chunks inform
- DOCX hierarchical chunks → Multi-strategy chunker: document input pattern
- Text metadata chunks → Setup and ingestion: RAG-ready records can feed
- CSV document conversion → Setup and ingestion: searchable records can feed
- PDF and DOCX samples → Multi-strategy chunker: exercises
- Multi-strategy chunker → Chunks and source metadata: strategy informs
- Sherlock input text → Ingest and query: ingests
- Ingest and query → Ollama server: contrasts with · Uses Sentence Transformers rather than Ollama embeddings.
- Ingest and query → Persistent ChromaDB: demonstrates persistence
- BM25 and semantic notebooks → Hybrid retrieval and RRF: explores concepts behind
