Contents Projects Code Course home

Chapter 35Project 8: A Retrieval System, Audited

What you will build

The full retrieval-augmented loop, end to end. You implement BM25 yourself, audit what it retrieves, and write the prompt that turns retrieved passages into grounded answers, including the refusal to answer when the knowledge base does not contain the answer. This is the capstone, so it draws on most of the book.

Allow eight to ten hours. Read Chapters 4, 22 and 24 first.

What you need

A small knowledge base of invented facts, so that no language model can answer from memory. Write fourteen short documents about a fictional institution, with at least five questions whose answers are in them and at least one whose answer is not. Access to a language model is needed for the grounding stage. The retriever must use only the standard library.

The conventions

  • Chapter 22 quotes the common defaults k1=1.2k_1 = 1.2 and b=0.75b = 0.75. This project fixes k1=1.5k_1 = 1.5, so that every implementation gives the same numbers.

  • Tokenise by lower-casing and taking maximal runs of the letters aa to zz, so that digits and punctuation separate tokens.

  • BM25 with k1=1.5k_1 = 1.5 and b=0.75b = 0.75 over NN documents, summing over the unique query terms tt, where df\mathrm{df} is the number of documents containing tt, tf\mathrm{tf} its count in the document being scored, dl\mathrm{dl} that document’s length in tokens and avgdl\mathrm{avgdl} the average length: idf⁡(t)=ln⁡(N−df+0.5df+0.5+1),score⁡(q,d)=∑tidf⁡(t)tf(k1+1)tf+k1(1−b+bdl/avgdl).\operatorname{idf}(t) = \ln\!\Big(\frac{N - \mathrm{df} + 0.5}{\mathrm{df} + 0.5} + 1\Big), \qquad \operatorname{score}(q, d) = \sum_{t}\operatorname{idf}(t)\,\frac{\mathrm{tf}\,(k_1 + 1)}{\mathrm{tf} + k_1\big(1 - b + b\,\mathrm{dl}/\mathrm{avgdl}\big)}.

  • A query term that does not occur in the document has tf=0\mathrm{tf} = 0 and contributes nothing, so a document with no query term scores 00.

  • retrieve(query, docs, k) takes docs as a list of dictionaries with an id and a text, and returns the kk ids ranked by score, with ties broken by id ascending.

Stages

  1. BM25 by hand. Take one term with N=14N = 14, a document frequency of 33, a term frequency of 22, a document of 4040 tokens and an average length of 5050. Compute its contribution.

    Check. The idf is 1.45531.4553, the saturated term-frequency factor is 1.52671.5267, and the contribution is 2.22182.2218. This is the formula your retriever must implement.

  2. Build the retriever. Write retrieve(query, docs, k). Test it on small synthetic corpora with deliberate ties and with documents of different lengths. The length normalisation and the tie break are where careless implementations fail, and so is the argument shape, since docs is a list and not a dictionary.

  3. Audit the retrieval. Write five queries. For each, list the top three documents your retriever returns, in order, and say whether the document that answers the question is among them. For one query say whether BM25 or an embedding retriever would do better on your knowledge base, and why. Run a small embedding model to settle it, or do the extension below first.

  4. Grounded answering. Write a prompt template with the placeholders for the retrieved context and for the question. Fill it with the top three passages and send it to the model. At least one question must be unanswerable from the knowledge base, and the template must make the model reply with exactly one fixed token, such as NOT_IN_KB, in place of a plausible guess. A template with no abstention rule typically loses exactly the unanswerable questions. Explain how your template forces abstention, and why unconstrained models guess.

Extensions

  • Add reciprocal rank fusion of BM25 with a dense retriever, as in Chapter 22, and measure whether the answering passage moves up.

  • Add a reranker and measure its effect on the rank of the correct passage and on latency.

  • Run the evaluation of Chapter 24: a fixed set of questions with known answers that you rerun after every change to the prompt or the retriever.

From Natural Language Processing: From Corpus Statistics to Grounded LLMs, by Ramaseshan Ramachandran.