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.
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.
Chapter 22 quotes the common defaults and . This project fixes , so that every implementation gives the same numbers.
Tokenise by lower-casing and taking maximal runs of the letters to , so that digits and punctuation separate tokens.
BM25 with and over documents, summing over the unique query terms , where is the number of documents containing , its count in the document being scored, that document’s length in tokens and the average length:
A query term that does not occur in the document has and contributes nothing, so a document with no query term scores .
retrieve(query, docs, k) takes docs as
a list of dictionaries with an id and a text, and returns the
ids ranked by score, with ties broken by id ascending.
BM25 by hand. Take one term with , a document frequency of , a term frequency of , a document of tokens and an average length of . Compute its contribution.
Check. The idf is , the saturated term-frequency factor is , and the contribution is . This is the formula your retriever must implement.
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.
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.
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.
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.