Skip to content

Chapter 17: Advanced RAG & Knowledge Systems

Go beyond top-k: hybrid sparse+dense retrieval, Reciprocal Rank Fusion, reranking, chunking strategies, and retrieval evaluation for production knowledge systems.


Metadata

Field Value
Track Advanced
Time 10 hours
Prerequisites Chapters 1, 13

Learning Objectives

  • Implement BM25 from scratch and explain term frequency, IDF, and length normalization
  • Build dense retrieval with cosine similarity over embeddings
  • Fuse rankings with Reciprocal Rank Fusion (RRF) to combine sparse and dense signals
  • Rerank a candidate set with a cross-encoder-style scorer
  • Chunk documents with size/overlap trade-offs and attach metadata for filtering
  • Evaluate retrieval with Recall@k, MRR, and nDCG
  • Reason about hybrid search — when sparse beats dense and vice versa
  • Design a production knowledge system with caching, indexing, and quality monitoring

What's Included

Notebooks

Notebook Description
01_sparse_and_dense.ipynb BM25 from scratch, dense cosine retrieval, and where each wins
02_fusion_and_reranking.ipynb Reciprocal Rank Fusion and a phrase-aware reranker
03_chunking_and_evaluation.ipynb Chunking strategies, metadata filtering, and retrieval metrics

Scripts

  • config.py — Chapter config: seeds, toy corpus, fusion constant k
  • hybrid_search.py — BM25, dense cosine retrieval, Reciprocal Rank Fusion, reranker
  • evaluation.py — Retrieval metrics: Recall@k, MRR, nDCG

Exercises

  • Problem Set 1: BM25 & Dense — Implement IDF, score a document with BM25, and rank with cosine similarity
  • Problem Set 2: Fusion & Metrics — Fuse rankings with RRF and compute nDCG over a labelled query
  • Solutions — in exercises/solutions/ (notebooks and solutions.py for CI)

Diagrams (Mermaid)

  • hybrid_retrieval.mermaid, chunking.mermaid

Read Online

  • 17.1 Introduction — BM25 from scratch, dense cosine retrieval, and where each wins
  • 17.2 Intermediate — Reciprocal Rank Fusion and a phrase-aware reranker
  • 17.3 Advanced — Chunking strategies, metadata filtering, and retrieval metrics

Or try the code in the Playground.

How to Use This Chapter

Quick Start

Clone the repo, install dependencies, and open the first notebook.

git clone https://github.com/luigipascal/berta-chapters.git
cd berta-chapters/chapters/chapter-17-advanced-rag-and-knowledge-systems
pip install -r requirements.txt
jupyter notebook notebooks/01_sparse_and_dense.ipynb

GitHub Folder

All chapter materials live in: chapters/chapter-17-advanced-rag-and-knowledge-systems/


Created by Luigi Pascal Rondanini | Generated by Berta AI