Sub topic 10
Judging context relevance when you have no labels
This chapter assumes you can read Python, know how precision and recall work, and have seen precision@k and recall@k computed against labelled relevant documents. Here you will estimate retrieval quality when no such labels exist.
Why labels are not always available
Precision@k and recall@k need a list of relevant document IDs for each question. Writing that list costs human time. For a new product, a changing corpus, or live traffic, you often have questions and retrieved passages but no labels.
Context relevance fills the gap. It asks whether each retrieved passage is useful for answering the question. You get the answer from a judge, usually an LLM, instead of from a label file.
The working model
For each question, you take the top k retrieved passages and ask a judge to rate each one. The ratings give you a label for every retrieved passage, so you can reuse the same arithmetic as before.
- Context precision is the share of retrieved passages the judge rates relevant. It is precision@k with judge labels.
- Rank-weighted precision gives more credit when relevant passages appear near the top.
- Context recall cannot be computed this way. A judge only sees what was retrieved, so it cannot tell you what was missed.
That last point is the main limit. Judge-based scores detect noise in the context, but they cannot detect missing documents.
A runnable sketch
The function below takes a judge as an argument, as the faithfulness example does. The toy judge checks word overlap so the code runs without a model. Replace it with an LLM call in real use.
def context_precision(question, passages, is_relevant):
"""Share of retrieved passages the judge rates relevant to the question."""
if not passages:
return None # nothing was retrieved; do not report 0.0 or 1.0
labels = [is_relevant(question, p) for p in passages]
return sum(labels) / len(labels)
def rank_weighted_precision(question, passages, is_relevant):
"""Average of precision at each rank where a relevant passage appears."""
hits = 0
scores = []
for rank, p in enumerate(passages, start=1):
if is_relevant(question, p):
hits += 1
scores.append(hits / rank)
return sum(scores) / len(scores) if scores else 0.0
# Toy judge for illustration only: shared words, ignoring very short ones.
def toy_judge(question, passage):
q = {w for w in question.lower().split() if len(w) > 3}
p = set(passage.lower().split())
return len(q & p) >= 1
question = "How long does the warranty last"
passages = [
"Our office is closed on public holidays",
"The warranty lasts 24 months from purchase",
"Shipping takes five days",
"The warranty covers manufacturing defects",
]
print(context_precision(question, passages, toy_judge)) # 0.5
print(rank_weighted_precision(question, passages, toy_judge)) # 0.5The second passage is relevant at rank 2 (precision 1/2). The fourth is relevant at rank 4 (precision 2/4). The mean of 0.5 and 0.5 is 0.5.
Writing the judge prompt
Keep the judge's task narrow. Ask one question about one passage and request a constrained answer.
- Give the question and a single passage.
- Ask whether the passage contains information that helps answer the question.
- Require a fixed output, such as
relevantornot_relevant, and a one-sentence reason. - Run at temperature 0 so repeated runs agree.
Rating passages one at a time avoids a judge being swayed by the other passages in the list.
Common errors
- Reporting it as recall. A high context precision does not mean the right documents were found.
- Confusing relevant with sufficient. A passage can be on topic and still lack the fact needed. If you care about this, add a separate check on whether the whole context answers the question.
- Trusting the judge unchecked. LLM judges can favour long passages or passages that repeat the question's wording. Compare the judge's labels with human labels on a sample before relying on it. The next chapters cover that check.
- Scoring empty retrieval as perfect. Return
Noneand count those questions separately.
Exercise
A retriever returns five passages for a question. A judge rates passages 1, 3 and 4 as relevant. What are context precision and rank-weighted precision?
Show answer
Context precision is 3/5 = 0.6. For rank-weighted precision, the precision at rank 1 is 1/1 = 1.0, at rank 3 is 2/3 ≈ 0.667, and at rank 4 is 3/4 = 0.75. The mean is (1.0 + 0.667 + 0.75) / 3 ≈ 0.806.
Exercise
Why can a judge-based method not give you recall@k?
Show answer
Recall needs to know all relevant documents in the corpus. The judge only sees the retrieved passages, so it has no way to know which relevant documents were not retrieved.