How kodekloudhub/fundamentals-of-rag works
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: hybrid_rag.py Hybrid retrieval and RRF · app_v2.py Setup and ingestion · document_chunker.py Multi-strategy chunker · ingest_and_query.py Ingest and query
Components 20
BookSearch
Main local-book RAG runtime.
- Classic book corpus · Local literature used by BookSearch ingestion.
- Setup and ingestion · Builds chunks, source metadata, and index state.
BookSearch/app_v2.py - Chunks and source metadata
- Persistent ChromaDB · Locally persisted semantic index.
- Persisted BM25 corpus · Token and chunk-ID artifacts must align with ingested chunks.
- Hybrid retrieval and RRF · Fuses vector and BM25 rankings.
BookSearch/hybrid_rag.py - Ask CLI · Retrieves context and returns cited answers.
BookSearch/app_v2.py - In-memory retrieval demo
BookSearch/app_v1.py - Ollama server · Serves llama3.3 generation and nomic-embed-text embeddings.
Document normalization
Independent format-specific extraction demos.
- PDF extraction · Emits text or JSON chunk output.
PDFParser/pdf_parser.py - PDF parser runner
PDFParser/main.py - DOCX hierarchical chunks · Extracts text, metadata, and hierarchical chunks.
WordParser/main.py - Text metadata chunks · Extracts metadata and sentence-aware chunks.
TextParser/main.py - CSV document conversion · Converts rows into searchable documents.
CSVParser/main.py
Chunking lab
Configurable chunking strategy experiment.
- Multi-strategy chunker · Supports format-aware line through token chunking strategies.
ChunkingDemo/document_chunker.py - PDF and DOCX samples
Semantic search
Minimal ChromaDB learning path.
- Ingest and query · Chunks files, embeds them, persists to ChromaDB, and queries with metadata filters.
ChromaDemo/ingest_and_query.py - Sherlock input text
ChromaDemo/data/adventuresofsherlockholmes.txt
Search experiments
Interactive retrieval tradeoff notebooks.
- BM25 and semantic notebooks · Explores keyword, semantic, and hybrid-search tradeoffs.
Other components
- Public-domain corpus · Separate reusable corpus, not coupled to BookSearch.
Connections 21
- 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