Knowledge Sprint SeriesMT24 Learning

Phase 13 · Sprint 41/90 · Semantic Search & RetrievalLewis · Rajpurkar · Barnett

Retrieval-Augmented Generation: A Working Demo

A retriever always hands something back, whether the answer is there or not.

In short. Retrieve then generate, recall as the ceiling on every answer, a retriever that always returns something, and the choice to cite or abstain.

Sprint 41 of 90 infographic: Retrieval-Augmented Generation: A Working Demo. A retriever always hands something back, whether the answer is there or not. The full text of the image is on this page.
Full resolution2160 × 2700 PNGPhase summary

The part of the image you are reading lights up. Tap a part to jump to its text.

Fig. 1 in words

Retrieval side of RAG · Cranfield 1 400 · same data as code 05

  1. 1 The pipeline

    • what the code builds, top to bottom
    • question → BM25: top k passages → top score ≥ 14.6? (no: abstain) → prompt with [n] citations → language model
    • dashed: not run here, no model in reach
  2. 2 Recall ceiling

    • share of questions with a judged passage
    • k passages returned 1 · 3 · 5 · 10 · 20: .511 · .671 · .769 · .853 · .907
    • at k 5, 52 of 225 get nothing judged
  3. 3 Answerable or not?

    • top-1 BM25 score, 225 questions each
    • answerable vs hidden · floor 14.6 · AUC 0.599 · scores 0 to 60
    • 84% of hidden ones still pass
  4. 4 The hand-off

    • printed by build_prompt(), first 30 chars
    • Answer only from these; cite […
    • [859] flutter of aerodynamical…
    • [1008] free-flight measurement…
    • [948] panel flutter tests on f…
    • Q: how does heating change pan…
    • “how do i bake bread” → ABSTAIN · top score 8.9 < 14.6
    • off-topic stops; a near miss does not

01 Retrieve, Then Generate RAG

  • Lewis et al. (2020): a retriever fetches passages, a generator writes from them
  • Sources can be updated without retraining, and cited
  • The answer can be no better than what the retriever hands over

This sprint tests the retriever half, on real judgments.

02 Recall Sets the Ceiling Recall

  • A judged passage in the top 1 · 5 · 20: 0.511 · 0.769 · 0.907
  • At k 5, 52 of 225 questions get no judged passage
  • Barnett et al. (2024) list it as a failure point: “missed the top ranked documents”

Five abstracts already carry 869 words on average.

03 It Always Hands Back k Claim

  • Claim: “if the answer is not in your documents, RAG will tell you”
  • Hide each question’s judged papers: top score 22.5 → 19.9, AUC 0.599
  • A floor that keeps 90% of answerable questions lets 84% of the hidden ones through

Verdict: fails for the retriever’s top score here. Judgments are not exhaustive.

04 Cite or Abstain Prompt

  • Number every passage [n] so each sentence can be traced
  • SQuAD 2.0 asks systems to “abstain from answering” when no answer is supported (Rajpurkar et al. 2018)
  • “Bake bread” scores 8.9 and stops; a near miss does not

An abstain rule needs its own test set.

05 Try It Yourself PYTHON 3.13.17 · NUMPY 2.5.3

sprint41_retrieval_augmented_generation_working_demo.py

import numpy as np
from s39 import blocks, tok, qs, rel, bm25, N    # Sprint 39's code

text = [" ".join(b[1].split()) for b in blocks("cran.all.1400")]
J = [rel[i + 1] for i in range(len(qs))]
good = [{d for d, r in j.items() if r > 0} for j in J]
base = [{d for d, r in j.items() if r == -1} for j in J]

def retrieve(q, k=5, hide=()):   # BM25 always returns k passages
    s = bm25(q); s[list(hide)] = -np.inf
    top = np.argsort(-s, kind="stable")[:k]
    return top, s[top]

K = (1, 3, 5, 10, 20)
hit = np.array([[bool(g & set(retrieve(q, k, b)[0])) for k in K]
                for q, g, b in zip(qs, good, base)])
print("a judged passage in top 1/3/5/10/20:", hit.mean(0).round(3))
print("questions with none in the top 5:", (~hit[:, 2]).sum())

# make each question unanswerable: hide every judged passage
a = np.array([retrieve(q, 1, b)[1][0] for q, b in zip(qs, base)])
u = np.array([retrieve(q, 1, b | g)[1][0]
              for q, g, b in zip(qs, good, base)])
auc = (a[:, None] > u).mean() + (a[:, None] == u).mean() / 2
floor = np.quantile(a, 0.10)     # keeps 90% of answerable questions
print(f"top-1 score {a.mean():.1f} vs {u.mean():.1f}, AUC {auc:.3f}")
print(f"floor {floor:.1f} lets through {(u >= floor).mean():.0%}"
      " of unanswerable questions")

def build_prompt(question, k=3):
    top, sc = retrieve(tok(question), k)
    if sc[0] < floor:
        return "ABSTAIN: no passage scores above the floor."
    ctx = "\n".join(f"[{d + 1}] {text[d][:52]}…" for d in top)
    return f"Answer only from these; cite [n].\n{ctx}\nQ: {question}"

print(build_prompt("how does heating change panel flutter"))
print(build_prompt("how do i bake bread"))

Output, as printed on the image

a judged passage in top 1/3/5/10/20: [0.511 0.671 0.769 0.853 0.907]
questions with none in the top 5: 52
top-1 score 22.5 vs 19.9, AUC 0.599
floor 14.6 lets through 84% of unanswerable questions
Answer only from these; cite [n].
[859] flutter of aerodynamically heated aluminium-alloy an…
[1008] free-flight measurements of the static and dynamic .…
[948] panel flutter tests on full scale x-15 lower vertica…
Q: how does heating change panel flutter
ABSTAIN: no passage scores above the floor.

Run it. sprint41_retrieval_augmented_generation_working_demo.py · versions, data and steps

Check: 52 ÷ 225 = 0.231 = 1 − 0.769 · 22.5 − 19.9 = 2.6 · floor = 10th percentile of answerable scores

06 General Notes Practice

  • Retrieval does not end hallucination: RAG legal tools hallucinated on 17–33% of queries (Magesh et al. 2025)
  • Long contexts are used unevenly; the middle suffers (Liu et al. 2024)
  • Test the retriever alone (recall at k, hidden answers) before judging the answers

Bridge: Barnett et al.’s first failure point is missing content. Measure it.

07 Mini Project Hands-on

  • Swap in Sprint 40’s hybrid: top-5 recall 0.782 against 0.769
  • Write 20 questions the collection cannot answer; count what passes
  • Plug in a language model and check every [n] it cites

A small gain, untested: run Sprint 40’s randomization test on it.

R Trusted Sources

  1. Lewis et al. (2020) NeurIPS 33, 9459–9474
  2. Barnett, Kurniawan, Thudumu, Brannelly & Abdelrazek (2024) Seven Failure Points When Engineering a RAG System, CAIN
  3. Rajpurkar, Jia & Liang (2018) ACL, 784–789
  4. Magesh, Surani, Dahl, Suzgun, Manning & Ho (2025) J. Empir. Leg. Stud. 22(2), 216–242
  5. Liu et al. (2024) Lost in the Middle, TACL 12, 157–173
  6. Robertson & Zaragoza (2009) FnTIR 3(4), 333–389
  7. Cleverdon, Mills & Keen (1966) ASLIB Cranfield Research Project

Cite this phase

Tahri, M. (2026). MT24 Learning — Knowledge Sprint Series, Phase 13: Semantic Search & Retrieval (Version 1.0) [Educational resource]. Zenodo. https://doi.org/10.5281/zenodo.24012904

Version DOI 10.5281/zenodo.24012904 · all versions 10.5281/zenodo.24012903 · first published 27 October 2026