Level 0: Foundation
★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.An open-book exam for language models
A language model only knows what was in its training data, and it may misremember rare facts or not know recent ones. Retrieval-augmented generation turns the test into an open-book exam: before answering, the system searches a library of documents for relevant passages and hands them to the model, which answers from them and can point to its sources. The demo shows the balance: too few passages may miss the answer; too many bury it.
RAG is the standard way to build assistants over internal manuals, procedures and reports: knowledge stays in documents that can be updated and audited, rather than in model weights.
- 1. RAG adds to the prompt…
- 2. RAG mainly helps with…
- 3. Too many retrieved passages can…
Level 1: Beginner
★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.By hand
- Indexing (offline): split documents into chunks (F10-2), embed them, store in a vector index (F10-3) and/or an inverted index for keyword search.
- Retrieval (online): embed the question, search (sparse BM25, dense or hybrid, F10-4), optionally rerank; take the top k.
- Generation: prompt with numbered passages, instruct the model to answer only from them, cite, and say ‘I don’t know’ when the passages lack the answer.
- Failure modes: the answer is not in the corpus; retrieval misses it (low recall); the right passage is retrieved but ignored or misread, especially in the middle of a long context (‘lost in the middle’); distractor passages mislead; the model ignores context in favour of memory, or vice versa.
- When to retrieve: long-tail and changing facts benefit most; popular facts are often known already (Mallen et al., 2023); adaptive retrieval decides per query.
- Evaluation: retrieval (recall@k, MRR) and generation (faithfulness, answer correctness) separately (F10-5).
- 1. Indexing happens…
- 2. 'Lost in the middle' refers to…
- 3. Retrieval helps most for…
Level 2: Intermediate
★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.Building a RAG pipeline
Exercise 1.
- Build a grounded prompt with numbered, budgeted passages and an abstain instruction.
- Compute recall@k of a ranking.
Practice.
- Frameworks: LlamaIndex, LangChain, Haystack; or a few lines with an embedding model and a vector database (F16-4).
- Start simple: chunks of a few hundred tokens with overlap, a good embedding model, top-k around 5, a reranker if needed.
- Log the retrieved passages with every answer; most RAG failures are retrieval failures.
- Keep the index up to date and record document versions for traceability.
from sentence_transformers import SentenceTransformer
import numpy as np
emb = SentenceTransformer("BAAI/bge-small-en-v1.5")
C = emb.encode(chunks, normalize_embeddings=True) # index once
def answer(question, k=5):
q = emb.encode([question], normalize_embeddings=True)[0]
top = np.argsort(-C @ q)[:k]
prompt = build_rag_prompt(question, [chunks[i] for i in top])
return llm(prompt), top- 1. A framework for RAG pipelines:
- 2. Most RAG failures are…
- 3. Log with every answer…
Level 3: Advanced
★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.Theory
Accuracy decomposition. End-to-end accuracy ≈ recall@k × P(correct | evidence retrieved) + (1 − recall@k) × P(correct | evidence missing). Retrieval sets a ceiling; the reader’s use of the evidence sets the slope; the fallback (memory or abstention) matters when retrieval fails.
The k trade-off. Recall@k rises with k, quickly at first. The reader’s accuracy given the evidence falls slowly with k as distractors accumulate and the gold passage moves into the middle of a longer context. The product peaks at a moderate k; rerankers raise precision so smaller k suffices.
Parametric vs retrieved knowledge. Models recall facts about popular entities well and long-tail ones poorly. Retrieval is most valuable where memory is weakest; for very popular facts it adds cost and a risk of being misled. Adaptive retrieval uses a signal such as entity popularity or the model’s own confidence to decide.
- 1. End-to-end accuracy is capped by…
- 2. As k grows, recall…
- 3. Adaptive retrieval decides…
Level 4: Expert
★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.Where does retrieval help, how many passages should be retrieved, and can retrieval be skipped?
History.
- P. Lewis et al., “Retrieval-augmented generation for knowledge-intensive NLP tasks” (NeurIPS 2020); K. Guu et al., “REALM” (2020).
- N. Liu et al., “Lost in the middle” (TACL 2024).
- A. Mallen et al., “When not to trust language models” (ACL 2023): popularity and adaptive retrieval.
Exercise 2. A simulation with a real BM25 retriever.
- Knowledge base: 2000 facts ‘entity relation value’. Entity names are pairs of tokens from 300, so they overlap; popularity is Zipfian. 30% of questions use only one name token (an alias).
- Closed book: the model knows a fact with probability rising steeply with the entity’s popularity percentile, otherwise hallucinates.
- RAG: BM25 top-k; with the gold passage the reader is right 95% of the time (−0.5 points per extra passage); without it, it is misled half the time and uses memory half the time.
- Adaptive: retrieve only for entities below the 75th popularity percentile.
| system | all | least popular quarter | 2nd | 3rd | most popular quarter |
|---|---|---|---|---|---|
| closed book | 0.311 | 0.003 | 0.033 | 0.353 | 0.865 |
| RAG, k = 1 | 0.807 | 0.757 | 0.785 | 0.824 | 0.860 |
| RAG, k = 5 | 0.928 | 0.932 | 0.923 | 0.926 | 0.931 |
| RAG, k = 20 | 0.863 | 0.887 | 0.844 | 0.864 | 0.860 |
| adaptive, k = 5 | 0.912 | 0.932 | 0.923 | 0.926 | 0.865 |
(BM25 recall: 0.853 at k = 1, 1.0 at k = 5.)
- Retrieval rescues the long tail: from 0% to 93% on the least popular facts.
- k = 5 was the sweet spot: k = 1 missed 15% of gold passages (alias questions); k = 20 retrieved everything but distraction cost 7 points.
- Adaptive retrieval saved 25% of retrieval calls for a 1.6-point loss: popular facts were already known almost as well as RAG answered them.
- Seeds 1 and 2: k = 5 0.938 and 0.934; closed book 0.33 and 0.31.
- Caveat: the reader is a simple probability model; distraction effects in real models depend on the model and passage order.
RAG in AI, robotics and aerospace
- Technical documentation assistants: answering from maintenance manuals, standards and procedures with citations to the exact section.
- Mission operations: retrieval over anomaly reports and telemetry notes helps engineers find precedents.
- Robotics: retrieving past demonstrations or object knowledge for a language-model planner is retrieval-augmented planning.
References
- P. Lewis et al. (2020). “Retrieval-augmented generation for knowledge-intensive NLP tasks.” NeurIPS.
- K. Guu, K. Lee, Z. Tung, P. Pasupat and M.-W. Chang (2020). “REALM: retrieval-augmented language model pre-training.” ICML.
- A. Mallen et al. (2023). “When not to trust language models: investigating effectiveness of parametric and non-parametric memories.” ACL.
- N. F. Liu et al. (2024). “Lost in the middle: how language models use long contexts.” TACL.