Practicals · Day 3
Attention, retrieval and grounded answers
How can a language system find relevant course evidence, decline an unsupported question and show which document supports its answer?
Day 3 files
day3_solution.ipynb in Jupyter. Run the cells in order. The optional package-installation command appears near the beginning.The activity question
The notebook first asks how one causal attention head combines information from earlier token positions. It then asks whether a small retrieval system can select course evidence, answer only when the evidence is strong enough and retain a trace of the documents it used.
You will inspect each calculation and retrieval score. There is no external language-model API, and the answers follow a simple extractive rule.
Dataset overview
| Documents | 20 short course notes: 18 trusted notes and two untrusted examples. |
| Questions | 40 labelled questions divided equally between development and final evaluation. |
| Evidence | Each supported question names one or more acceptable document identifiers. |
| Cases | Answerable, unanswerable, ambiguous and injection questions. |
| Format | One JSON file containing metadata, documents and questions. |
How the data are split
Top-k and the similarity threshold are chosen from the development questions. Those settings are fixed before the final questions are evaluated.
| Part | Records | Composition | Role in the activity |
|---|---|---|---|
| Document collection | 20 | 18 trusted, 2 untrusted | Supplies the text that can be ranked and cited. |
| Development | 20 questions | 12 answerable, 3 unanswerable, 3 injection, 2 ambiguous | Selects top-k and the abstention threshold, then supports error inspection. |
| Final | 20 questions | 12 answerable, 3 unanswerable, 3 injection, 2 ambiguous | Evaluates the fixed procedure once. |
Top-level dictionary
| Field | Structure | Meaning | Use in the notebook |
|---|---|---|---|
title | Text | Name of the classroom dataset. | Describes the activity data. |
source | Text | States that the documents and questions were written for the course. | Records provenance. |
licence | Text | Permission statement for the classroom material. | Records permitted use. |
notes | Text | States that the data are synthetic, contain no personal data and work offline. | Sets the scope of the data. |
documents | List of 20 records | The searchable course notes and unsafe examples. | Forms the retrieval collection. |
questions | List of 40 records | Development and final questions with evaluation labels. | Selects and evaluates the retrieval procedure. |
Document field dictionary
| Field | Structure | Meaning | Use in the notebook |
|---|---|---|---|
doc_id | Text | Stable document identifier such as D07 or U01. | Appears in retrieval traces and citations. |
title | Text | Short name of the note. | Contributes words to the TF-IDF representation. |
topic | Text | Broad subject such as attention, retrieval or evidence. | Contributes words to retrieval and helps inspection. |
trust_level | Category | Marks a document as trusted or untrusted. | Prevents untrusted text from supplying an answer. |
text | Text | The document passage. | Supplies searchable terms and the cited answer extract. |
Question field dictionary
| Field | Structure | Meaning | Use in the notebook |
|---|---|---|---|
question_id | Text | Stable identifier containing the split and question number. | Links predictions to evaluation rows. |
question | Text | The input sent to the retriever. | Creates the TF-IDF query vector. |
split | Category | Either development or final. | Separates selection from final evaluation. |
case_type | Category | The evaluation category of the question. | Defines which safety or answer measure applies. |
expected_doc_ids | List of text | Document identifiers accepted as supporting evidence. The list is empty for an unanswerable question. | Checks retrieval hits and citation support. |
answerable | True or false | Whether the collection contains supporting evidence. | Checks abstention decisions. |
Category and label dictionary
| Label | Meaning | Expected behaviour |
|---|---|---|
answerable | One trusted document supports the question. | Retrieve and cite the supporting document. |
unanswerable | The collection does not contain the requested information. | Abstain rather than guess. |
ambiguous | More than one document is accepted in this activity. | Cite one accepted source and recognise that clarification may be preferable. |
injection | The wording tries to alter authority or obtain restricted information. | Reject the instruction and apply the course policy. |
trusted | A course note that may supply evidence. | May be used for an extractive answer. |
untrusted | An unsafe example supplied to test the rule. | May be retrieved for inspection but cannot govern the answer. |
Source and preparation
The documents and questions were written for this course. They are fictional and contain no personal data. The original classroom material supplied separate CSV files for the corpus and question sets. This version combines them into one JSON file so the notebook and one adjacent dataset contain the complete activity.
The document wording and development cases follow the original material. Final labels map each question to the course note that supplies its expected evidence. The split contains the same number of each case type as the development set. No internet connection or API key is required after the packages have been installed.
What this activity asks you to do
- calculate query, key and value matrices for a small attention example;
- apply a causal mask and read the resulting attention weights;
- represent the supplied documents with TF-IDF and inspect ranked results;
- compare Hit@k and reciprocal rank for several values of top-k;
- select top-k and an abstention threshold using development questions;
- trace retrieved evidence, citations and declined questions;
- compare a naive response with a guarded response to an injected instruction;
- report final coverage and selective risk without changing the selected settings.
Learning outcomes
- explain how scaled dot-product attention combines value vectors;
- distinguish self-attention inside a model from document retrieval;
- explain what TF-IDF similarity and top-k control;
- use development data to choose an abstention rule;
- distinguish retrieval success, citation presence and evidence support;
- calculate and interpret coverage and selective risk;
- explain why retrieved instructions do not grant authority.
What the evidence does not establish
- The fixed attention numbers illustrate arithmetic; they are not learned language representations.
- The retrieval scores describe a small synthetic collection and should not be treated as results for open-domain question answering.
- TF-IDF matches words and short phrases. It does not establish that a passage is true.
- A document identifier records provenance, but a reader must still check that the passage supports the claim.
- The injection rule covers the explicit patterns in this dataset. It is not a general defence against every unsafe instruction.
- Accepting one source for an ambiguous question does not replace clarification in a deployed system.