Practice in Lab: Evaluate retrieval with relevance and rank →
Evaluate retrieval with relevance and rank
A fluent generated answer can hide weak retrieval. Evaluate the retrieval stage directly using judged relevant document identifiers. Recall at k measures coverage; reciprocal rank measures how soon the first relevant result appears. Duplicate identifiers must not inflate either metric.
This exercise deduplicates rankings before taking k and returns None for both metrics when no relevant documents are defined. That is unknown relevance, not evidence of perfect retrieval. Relevance judgments can be incomplete or outdated, so retain the query set version and inspect failure cases rather than optimizing a single aggregate score blindly.
ranked=["x","a","b"]
relevant={"a","b"}
print(len(set(ranked[:2])&relevant)/len(relevant))Review the design
Retrieval metrics evaluate the judged identifiers and ordering. They do not establish answer faithfulness, citation correctness or resistance to prompt injection; those require separate evaluation cases.
Your task
Implement retrieval_metrics(ranked,relevant,k) returning dict recall,mrr. Deduplicate ranked preserving order, then take k. mrr is reciprocal first relevant rank or 0. No relevant ids gives None values. Require k>=1 integer.
The explanation opens here once every check passes.