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?

← All practicals

To begin. Save both files in the same folder and open 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

Documents20 short course notes: 18 trusted notes and two untrusted examples.
Questions40 labelled questions divided equally between development and final evaluation.
EvidenceEach supported question names one or more acceptable document identifiers.
CasesAnswerable, unanswerable, ambiguous and injection questions.
FormatOne 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.

PartRecordsCompositionRole in the activity
Document collection2018 trusted, 2 untrustedSupplies the text that can be ranked and cited.
Development20 questions12 answerable, 3 unanswerable, 3 injection, 2 ambiguousSelects top-k and the abstention threshold, then supports error inspection.
Final20 questions12 answerable, 3 unanswerable, 3 injection, 2 ambiguousEvaluates the fixed procedure once.

Top-level dictionary

FieldStructureMeaningUse in the notebook
titleTextName of the classroom dataset.Describes the activity data.
sourceTextStates that the documents and questions were written for the course.Records provenance.
licenceTextPermission statement for the classroom material.Records permitted use.
notesTextStates that the data are synthetic, contain no personal data and work offline.Sets the scope of the data.
documentsList of 20 recordsThe searchable course notes and unsafe examples.Forms the retrieval collection.
questionsList of 40 recordsDevelopment and final questions with evaluation labels.Selects and evaluates the retrieval procedure.

Document field dictionary

FieldStructureMeaningUse in the notebook
doc_idTextStable document identifier such as D07 or U01.Appears in retrieval traces and citations.
titleTextShort name of the note.Contributes words to the TF-IDF representation.
topicTextBroad subject such as attention, retrieval or evidence.Contributes words to retrieval and helps inspection.
trust_levelCategoryMarks a document as trusted or untrusted.Prevents untrusted text from supplying an answer.
textTextThe document passage.Supplies searchable terms and the cited answer extract.

Question field dictionary

FieldStructureMeaningUse in the notebook
question_idTextStable identifier containing the split and question number.Links predictions to evaluation rows.
questionTextThe input sent to the retriever.Creates the TF-IDF query vector.
splitCategoryEither development or final.Separates selection from final evaluation.
case_typeCategoryThe evaluation category of the question.Defines which safety or answer measure applies.
expected_doc_idsList of textDocument identifiers accepted as supporting evidence. The list is empty for an unanswerable question.Checks retrieval hits and citation support.
answerableTrue or falseWhether the collection contains supporting evidence.Checks abstention decisions.

Category and label dictionary

LabelMeaningExpected behaviour
answerableOne trusted document supports the question.Retrieve and cite the supporting document.
unanswerableThe collection does not contain the requested information.Abstain rather than guess.
ambiguousMore than one document is accepted in this activity.Cite one accepted source and recognise that clarification may be preferable.
injectionThe wording tries to alter authority or obtain restricted information.Reject the instruction and apply the course policy.
trustedA course note that may supply evidence.May be used for an extractive answer.
untrustedAn 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

  1. calculate query, key and value matrices for a small attention example;
  2. apply a causal mask and read the resulting attention weights;
  3. represent the supplied documents with TF-IDF and inspect ranked results;
  4. compare Hit@k and reciprocal rank for several values of top-k;
  5. select top-k and an abstention threshold using development questions;
  6. trace retrieved evidence, citations and declined questions;
  7. compare a naive response with a guarded response to an injected instruction;
  8. 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.
Oxford · United Kingdom TeachingCV
University of Oxford