Papa Programmer
Lesson 13 of 15

Measuring retrieval quality

9 min read

Optional

Test the search on its own

A RAG answer can be wrong for two separate reasons. The search may have missed the right passage. Or the search may have found it and the model may have misused it. These need different fixes, so measure them one at a time.

Retrieval is the first stage and the easier one to measure. You do not need a model, and you do not need to judge writing. You only need to know, for each question, which passage holds the answer, and then check whether the search returned it.

Build the question list

The list is an Evaluation setA list of tasks, each with a way to check the result, that you run again after every change to an agent. You measure how often the agent gets each task right. for retrieval. Each row has a question and the passage, or passages, that answer it.

  • Start with 30 to 50 questions. That is enough to see a pattern, and small enough to write by hand.
  • Use real questions. Take them from support tickets, search logs or the people who will use the system, in their own words.
  • Include questions with no answer. The system should find nothing useful for them, and the Lesson Citations and honest refusals uses them.
  • Do not tune on your examples alone. Keep some questions aside to check that an improvement is real.

Two numbers that do most of the work

Recall@k asks: for what share of the questions did the right passage appear in the top k results? With k = 3, a recall@3 of 0.9 means the answer was in the top three results for 90 per cent of the questions. It matches how a RAG system works, because the system sends the top few passages to the model.

Mean reciprocal rank (MRR) asks how high the right passage ranked. For each question, take 1 divided by the position of the right passage: 1 for first place, 0.5 for second, 0.33 for third. Then average over all questions. Mean reciprocal rankThe average, over test questions, of 1 divided by the position of the right passage. It is 1 when the answer is always first and falls as the answer ranks lower. rewards putting the answer first, which matters when you send only one or two passages to the model.

Recall@kThe share of test questions for which the right passage appears in the top k search results. A recall@3 of 0.9 means the answer was in the top three for 90 per cent of questions. tells you whether the passage reached the model. MRR tells you how near the top it was. Report both.

Scoring a retriever in code

The program below runs four questions through a simple word-overlap search, and then through the same search with a small table of synonyms added to the question. It prints recall@2, MRR and the position of the right passage for each question.

python
docs = {
	"returns": "return an unused bike within 30 days for a refund",
	"sale": "sale items can be returned within 14 days",
	"warranty": "the frame has a 5 year warranty and the battery has a 2 year warranty",
	"shipping": "standard shipping takes 3 to 5 business days",
}
tests = [
	("how long do i have to return a bike", "returns"),
	("can i return a sale item", "sale"),
	("how long is the frame warranty", "warranty"),
	("when will my parcel arrive", "shipping"),
]
synonyms = {"parcel": "shipping", "arrive": "takes"}

def rank(question, expand=False):
	words = set(question.split())
	if expand:
		words |= {synonyms[word] for word in words if word in synonyms}
	return sorted(docs, key=lambda name: -len(words & set(docs[name].split())))

def report(label, expand):
	positions = [rank(question, expand).index(expected) + 1 for question, expected in tests]
	recall = sum(position <= 2 for position in positions) / len(tests)
	mrr = sum(1 / position for position in positions) / len(tests)
	print(f"{label:<9} recall@2 {recall:.2f}   MRR {mrr:.2f}   positions {positions}")

report("plain", False)
report("synonyms", True)

It prints:

text
plain     recall@2 0.75   MRR 0.69   positions [1, 2, 1, 4]
synonyms  recall@2 1.00   MRR 0.88   positions [1, 2, 1, 1]

The plain search puts the right passage in the top two for three of the four questions. The failure is the parcel question, which shares no word with the shipping passage. The synonym table fixes that one question and raises recall@2 to 1.00. MRR rises too, but it stays below 1.00, because for one question the right passage is still second instead of first.

Read the positions as well as the averages. A list such as [1, 2, 1, 4] points at the exact questions to inspect. An average hides them.

Choose k from how you use the results

Recall@k only means something with the k you actually use. If the prompt holds three passages, measure recall@3. A recall@10 of 0.95 does not help when only three passages reach the model. Measure both a small and a large k. When recall at a large k is high and at a small k is low, the right passage is being found and ranked too low, which is the job of a reranker, covered in the Lesson Hybrid search and reranking.

Recall@kMRRWhat it means
MissedLowLowThe right passage is not found. Check chunking, embeddings and coverage.
Found, ranked lowHighLowThe passage arrives, but late. Try hybrid search or a reranker.
WorkingHighHighRetrieval is fine. Look at the prompt and the model.
Reading the two numbers together

Use the failures

A score is only a signal. The value is in the failed questions. For each one, look at what was returned and ask why the right passage lost.

  1. Was the answer in the index at all? If not, it is a loading problem, covered in Loading and cleaning documents.
  2. Was it split badly? The answer may sit across a chunk boundary, as How to chunk documents shows.
  3. Was the wording different? The question and the passage may use different words, which suggests embeddings or hybrid search.
  4. Was a different passage just as good? Then the label is too narrow. Accept both.

Change one setting at a time, rerun the same list, and keep the numbers. Without a fixed list, every change feels like an improvement.

What these numbers do not tell you

A high recall means the right passage reached the model. It does not mean the answer was right. The next Lesson, Measuring answer faithfulness, covers the second half. Also, a small list written by one person can miss kinds of questions that real users ask, so add new questions whenever a real failure shows up.

For a standard reference on these metrics, see chapter 8 of the Stanford book Introduction to Information Retrieval.

Measure search aloneA missed passage and a misused passage have different fixes.
Keep a fixed question listThe same questions let you compare every change.
Report recall and MRROne says if the passage arrived, the other says how high it ranked.

Your reading position is saved

0%

0 of 15 complete

Contents

Learning tools