Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 8.3 — Citations You Can Check
RAG and Vector Search · Example 1 of 1

Checking every citation in four answers

A valid marker only proves the source was in the prompt. Topic 8.7 checks whether the source supports the sentence.

A citation is a claim about the evidence, and claims can be wrong. Parse the markers in the answer, check each against the numbers in the prompt, report unused and invalid ones, and show the reader the real source behind every marker.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 5 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write check_citations(answer, n_sources) returning (cited, invalid), both sorted lists of ints, using re.findall. Print the result for "Pass [1], late fee [3], refund [3]" with 2 sources.

02

Write strip_invalid(answer, n_sources) that removes any [n] marker whose number is not between 1 and n_sources, keeping valid ones. Print the result for "Pass costs 25 [1] and rides last 45 [5]." with 2 sources.

Explain it without notes

01

Name the checks you would run on an answer's citations without calling a model.

02

What does a valid citation prove, and what does it not?

03

Why should your code, not the model, map a marker to the document shown to the reader?

04

What should happen when the check fails?

Checking every citation in four answers
Sign in to run this example in your browser.

Expected output

A: cited 1 | invalid none | unused 2,3 | ok
B: cited 2,4 | invalid 4 | unused 1,3 | FAIL
C: cited none | invalid none | unused 1,2,3 | FAIL
D: cited 2,3 | invalid none | unused 1 | ok