Skip to content

Ch 17: Advanced RAG & Knowledge Systems - Introduction

Track: Advanced | Try code in Playground | Back to chapter overview

Read online or run locally

You can read this content here on the web. To run the code interactively, either use the Playground or clone the repo and open chapters/chapter-17-advanced-rag-and-knowledge-systems/notebooks/01_sparse_and_dense.ipynb in Jupyter.


Chapter 17: Advanced RAG — Notebook 01 (Sparse & Dense Retrieval)

Two ways to find relevant text: lexical overlap (BM25) and semantic similarity (dense embeddings). We build both and see where they disagree.

What you'll learn

Topic Section
Tokenization and term statistics §1
BM25: TF, IDF, length normalization §2
Dense retrieval with cosine similarity §3
Sparse vs dense trade-offs §4

Time estimate: 3 hours


Key concepts

  • BM25 — a strong sparse baseline; rewards rare matching terms, penalizes long docs.
  • Dense retrieval — embeddings capture meaning, catching paraphrases BM25 misses.
  • IDF — rare terms are more informative than common ones.
  • Vocabulary mismatch — the core problem dense retrieval solves.

BM25 is a strong lexical baseline; dense retrieval captures meaning across vocabulary mismatch. Neither dominates — which motivates combining them.

Run the full notebook in the chapter folder for code and outputs.


Generated by Berta AI