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.

Chapter 21 — Capstone Project: A Course Assistant You Can Trust

Open in Colab

Overview

Students of this course would like an assistant that answers questions about it. A plain language model would answer fluently, sometimes wrongly, and without sources. Your assistant must be grounded (every answer comes from the course text and cites it), honest (it says “I don’t know” rather than guess) and evaluated (you measure how often it is right, wrong, or silent).

You will build it from parts you have met throughout the course: text processing and TF-IDF (Chapter 13), ranking metrics (Chapter 6), a decision threshold chosen on data (Chapter 11), and the agent loop with tools (this chapter). No API key is needed.

PartTaskPoints
ABuild the knowledge base from the course chapters15
BRetrieval: TF-IDF against BM2525
CKnowing when to say “I don’t know”20
DAn agent that routes questions20
ESystem card20

Part A — The Knowledge Base (15 points)

A1 (checked). Complete build_chunks(texts, max_words=250, overlap=50). For each chapter, split the text at lines starting with ## (one piece per section; drop anything before the first section, and drop the sections titled Exercises and Further Reading). A section longer than max_words words is cut into word windows of max_words with overlap (your Exercise 3 of the lab). Return a list of dicts with keys chapter, section (the title after ## ), part (0, 1, ... within the section) and text (the window’s words; the section title is not repeated in later windows).


Part B — Retrieval (25 points)

Twenty test questions, each with the section that answers it:

A retriever is a function retrieve(question, k) returning the indices of the k best chunks. We evaluate it by the rank of the first chunk from the right section, as Hits@1, Hits@3 and MRR (Chapter 6). The TF-IDF retriever is given.

B1 (checked). Implement BM25, the classic search-engine ranking function. For a query QQ and a chunk DD (as lists of tokens), with NN chunks, n(q)n(q) the number of chunks containing term qq, f(q,D)f(q, D) its count in DD, ∣D∣|D| the length of DD and avgdl\text{avgdl} the average length:

BM25(D,Q)=∑q∈QIDF(q) f(q,D) (k1+1)f(q,D)+k1(1−b+b ∣D∣/avgdl),IDF(q)=ln⁡ ⁣(N−n(q)+0.5n(q)+0.5+1)\text{BM25}(D, Q) = \sum_{q \in Q} \text{IDF}(q)\, \frac{f(q, D)\,(k_1 + 1)}{f(q, D) + k_1\big(1 - b + b\,|D| / \text{avgdl}\big)}, \qquad \text{IDF}(q) = \ln\!\Big(\frac{N - n(q) + 0.5}{n(q) + 0.5} + 1\Big)

with k1=1.5k_1 = 1.5, b=0.75b = 0.75. Each query term counts once; terms absent from every chunk contribute 0. Complete the class: __init__ receives the tokenised chunks; scores(query_tokens) returns one score per chunk.

B2. Compare the two retrievers in a table, list the questions on which each fails (rank > 3), and explain one failure by looking at the chunks that beat the right one.

Your analysis:


Part C — Knowing When to Say “I Don’t Know” (20 points)

The assistant answers only when its best retrieval score is at least a threshold τ\tau; otherwise it abstains. We score its behaviour on the 20 course questions and on 8 questions it cannot answer from the course:

answers from the right sectionanswers from a wrong sectionabstains
course question+1−2 (a wrong answer misleads)0
out-of-scope question−2+1

C1 (checked). Complete utility(score_fn, tau), returning the total score of the table above (use the top chunk of score_fn; its section is the answer).

C2. For BM25, plot the utility against τ\tau (try the distinct top scores of all 28 questions as candidate thresholds), choose the best τ\tau, and report how many course questions are answered correctly, answered wrongly and abstained, and how many out-of-scope questions are correctly refused. Why is choosing τ\tau on these same 28 questions optimistic, and what should be done instead?

Your answer:


Part D — An Agent That Routes Questions (20 points)

The assistant becomes an agent with three tools: calculator (safe arithmetic, from the lab), course_answer (retrieve with BM25; abstain below τ\tau; otherwise return the best section’s two most relevant sentences with a citation), and nothing else: anything else gets a polite refusal.

D1 (checked). Complete route(question), the decision function, returning 'calculator' when the question contains an arithmetic expression (digits and an operator such as + - * / ^), and 'course_answer' otherwise. Then complete assistant(question), which calls the chosen tool and returns its text.

D2. Run the assistant on the mixed questions below and judge each answer yourself (correct and supported / wrong / rightly refused / wrongly refused). Summarise in a table.

Your judgement table:


Part E — System Card (20 points)

Write a one-page system card (300–400 words) for the assistant, for the course coordinator who would decide whether to offer it to students:

  1. Purpose and users: what it answers, what it does not.

  2. How it works: in plain words, the three parts (search, threshold, tools), and that it extracts sentences rather than generating new text.

  3. Evaluation: your Part B and C numbers; what “right”, “wrong” and “abstained” mean.

  4. Limits and risks: word-matching retrieval, near-miss questions, outdated or incomplete course text, over-reliance by students, privacy (questions may reveal what a student does not understand).

  5. What would change with a language model: fluent answers and better matching, at the cost of possible hallucination; how grounding, citations and abstention would still be enforced.

  6. Recommendation: offer it or not, and with what safeguards.

Your system card:


Grading Rubric

PartFull marks if…
A (15)chunking correct (checked)
B (25)BM25 correct (checked); a fair comparison; failures explained from the retrieved chunks
C (20)utility correct (checked); threshold chosen and behaviour reported; the optimism of tuning on the test questions explained
D (20)routing and assistant correct (checked); an honest judgement of every mixed answer
E (20)all six sections, accurate numbers, honest limits, a clear recommendation