To evaluate the retrieval half of a RAG pipeline, annotators mark the character spans of a document that answer each question. Score the retriever's ranked chunks against those spans.
Implement context_metrics(retrieved, references, k).
retrieved: one ranked list per query of chunk spans (start, end), half-open character offsets into the same document.references: one list per query of gold spans (start, end), also half-open.k: only the first k chunks of each list are used.A chunk is relevant if it overlaps at least one reference span by at least one character: spans and overlap when . For one query, with for the chunk at rank and the fraction of relevant chunks among the first :
Queries with no reference spans are skipped. Return a dict with context_precision, context_recall and hit_rate (means over evaluated queries, rounded to 4 decimals, 0.0 if none) and evaluated (the count).