Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Lab 10: Documents, retrieval, and context

Objectives: LO-10.1, LO-10.2, LO-10.3, LO-10.4
Prerequisites: the Module 10 reading
Time: about 2.5 hours

In this lab you build a grounded prompt from tagged handbook sections, rank the sections with BM25, measure recall at kk on 40 labeled questions, check that a quotation occurs exactly in the section it names, and compare the cost of a cached prompt prefix with an uncached one.

The handbook belongs to the course’s fictional bicycle shop: 10 short sections, H01–H10, and 40 questions, each labeled with the sections that answer it. Nothing in the lab calls a model, and every number you compute describes this small data set only.

Each exercise ends with a check cell that prints passed or fails with a message. Run the cells in order, from the top. Some cells fail on purpose until you finish the exercise above them.

Setup

Read the handbook records

Each section is a dictionary with an id, a title, and its text. Each question has an id, the question, and a list of relevant section IDs. Read the first question, and predict which section should rank first, before you rank them.

Exercise L10.1 (LO-10.1, LO-10.3, ★★, about 25 minutes)

Build a grounded prompt. Write prompt(docs, question), which returns the prompt as an XML string. Its root element holds three children, in this order: documents, instructions, and question. documents holds one doc element for each section, with the section’s ID in an id attribute and its original text. The instructions ask for an exact quote and its section ID, or the reply “I don’t know”. If you build the tree with ElementTree, it escapes text containing < or & for you.

Exercise L10.2 (LO-10.2, ★★, about 25 minutes)

Rank sections with BM25. Write bm25(query, docs, *, k1=1.2, b=0.75), using the formula in the reading. Lowercase the text, and take as terms the matches of the regular expression [a-z]+; count each distinct query term once. Return a list of (section ID, score) pairs, ordered by decreasing score, then by ascending ID. Include sections that score zero. If docs is empty, return an empty list.

Exercise L10.3 (LO-10.2, ★★, about 25 minutes)

Measure recall and full coverage. Write recall(ranked, relevant, k), which returns the share of the relevant IDs that appear among the first k IDs of ranked. Raise ValueError for a negative k or an empty relevant list.

The check then ranks the handbook for all 40 questions and plots two lines against kk, the number of sections retrieved, on the horizontal axis. The vertical axis is a proportion. The line with circles is mean recall, and the line with squares is full coverage: the share of questions with every required section retrieved. Read the plot, and note where full coverage falls below mean recall, and the smallest kk at which both reach 1.

Exercise L10.4 (LO-10.3, ★★, about 25 minutes)

Verify verbatim quotations. Write valid_quote(docs, ident, quote). A quote passes if it contains at least one visible character and occurs, exactly as written, in the section with ID ident. Check only the sections in docs, which stand for the ones retrieved into the prompt. In your notes, give a true quote that would still fail to support a claim.

Exercise L10.5 (LO-10.4, ★★, about 25 minutes)

Compare cached and uncached input. Write cost(requests, prefix, suffix, rates), which returns the pair (uncached, cached) in US dollars for requests requests, each with a prefix of stable tokens and a suffix of tokens that change. The cached case writes the prefix once to the 5-minute cache, then hits the cache on every later request. Charge the suffix at the input rate on every request in both cases, and leave out output, which is the same either way. Raise ValueError for a requests count of zero or less.

Summary

  • A grounded prompt puts tagged sections, each with an ID, before its instructions and its question, and asks for an exact quote or “I don’t know”.

  • BM25 ranks sections by the query terms they contain, weighting rare terms up, limiting repeats, and adjusting for length. It matches words exactly, so a question in other words can miss its section.

  • Recall at kk and full coverage measure retrieval, and full coverage caps how many answers can quote all their evidence.

  • An exact-match check proves where a quote came from, not that it supports the claim.

  • A cached prefix pays one write and then cheap reads, so it saves money only when requests reuse it.