Sub topic 15
Measuring Citation Precision and Recall
This chapter assumes you can read Python and JSON. It also assumes you have a RAG pipeline that returns answers with numeric markers such as [2], and that your code can extract those markers. Validation checks that a citation is well formed. Evaluation asks a different question: across many answers, how often are the citations the right ones?
Why measure at all
A pipeline can pass every validation check and still cite the wrong sources. A marker [3] that points to a real source is valid. It is still wrong if source 3 does not support the claim. To find out how often this happens, compare the citations your system produces against a small set of answers that a person has labeled.
Two numbers
For one answer, let the predicted set be the source ids the answer cited. Let the gold set be the source ids a human judged to be correct.
- Precision is the share of cited sources that are in the gold set. Low precision means the answer cites irrelevant or unsupportive sources.
- Recall is the share of gold sources that the answer cited. Low recall means the answer leaves out sources that support it.
The scoring function
def citation_precision_recall(predicted, gold):
"""predicted and gold are sets of source ids for one answer."""
if not predicted:
precision = 1.0 if not gold else 0.0
else:
precision = len(predicted & gold) / len(predicted)
if not gold:
recall = 1.0
else:
recall = len(predicted & gold) / len(gold)
return precision, recallThe function handles empty sets on purpose. If the answer cites nothing and the gold set is empty, as in a correct abstention, precision is 1.0. If the answer cites nothing but a source was needed, precision is 0.0. If nothing was needed, recall is 1.0 because nothing was missed.
A worked example
Suppose the gold set for a question is {2, 4}. The model cites {2, 3}.
- The overlap is
{2}, which has one element. - Precision is 1 / 2 = 0.5, because one of two cited sources is correct.
- Recall is 1 / 2 = 0.5, because one of two needed sources was cited.
Now suppose the model cites {2, 4, 5}. Recall is 1.0, but precision falls to 2 / 3, about 0.67.
Averaging over a test set
Score each labeled question, then average the results. The code below uses the function above and the extract_citation_ids helper from the validation chapter.
import re
def extract_citation_ids(answer):
return [int(n) for n in re.findall(r"\[(\d+)\]", answer)]
def citation_precision_recall(predicted, gold):
if not predicted:
precision = 1.0 if not gold else 0.0
else:
precision = len(predicted & gold) / len(predicted)
recall = 1.0 if not gold else len(predicted & gold) / len(gold)
return precision, recall
def evaluate(examples):
"""examples: list of dicts with 'answer' (str) and 'gold' (set of ints)."""
scores = []
for ex in examples:
predicted = set(extract_citation_ids(ex["answer"]))
scores.append(citation_precision_recall(predicted, ex["gold"]))
n = len(scores)
return (sum(p for p, _ in scores) / n, sum(r for _, r in scores) / n)
examples = [
{"answer": "Requests are retried three times [2].", "gold": {2}},
{"answer": "The limit is 10 MB [1][3].", "gold": {1}},
{"answer": "I could not find this in the provided sources.", "gold": set()},
]
print(evaluate(examples))This prints about (0.833, 1.0). The second answer has precision 0.5, and the other two have 1.0.
Common errors
- Labeling too few examples. A set of ten questions gives noisy averages. Aim for at least a few dozen that include unanswerable questions.
- Treating a high score as proof of grounding. These metrics compare ids only. They do not show that the cited text supports the claim. Pair them with the quote and claim-support checks from earlier chapters.
- Ignoring abstention cases. Without questions that have an empty gold set, a system that always cites something can look better than it is.
- Changing source numbering between runs. Gold labels refer to ids, so the same chunk must keep the same id, or you should label by stable chunk identifier instead.
Exercise
The gold set is {1, 2, 3}. The model cites {1, 4}. What are the precision and recall?
Show answer
The overlap is {1}. Precision is 1/2 = 0.5, and recall is 1/3, about 0.33.
Exercise
Why does a correct abstention (no citations, empty gold set) score 1.0 for both metrics in the function above, and why does that matter for test design?
Show answer
Nothing was cited and nothing was needed, so no citation is wrong and none is missing. It matters because unanswerable questions in the test set reward systems that decline correctly and penalize systems that cite sources for questions they cannot answer.