AI Leaf Global Learn

Test RAG Retrieval Before Changing the Answer Prompt

By AI Leaf Global Learn · 25 November 2026

Score three fictional retrieval lists with a small Python exercise, locate a missing source and explain what a retrieval hit still cannot prove.

Did retrieval find the evidence the answer needs?

A document assistant can produce a weak answer because its search step returned the wrong material. Inspect the retrieved document IDs before changing the generation prompt. Start with a small set of questions whose relevant documents you have checked by hand.

This exercise scores an invented search result list. It uses no model, vector database or paid service. It is a local evaluation of fixed lists; connecting it to a real retriever is a later step.

Define the learning metric

For this exercise, a query is a hit when at least one document labelled relevant appears among the first k returned IDs. The hit rate is the number of hit queries divided by the number of labelled queries. We call this hit_rate_at_k in the code. It does not measure whether every relevant document was found, or whether the generated answer is correct.

Our fictional handbook has reset-note, export-note and limits-note. The labels say which note each question needs. Ranked lists represent output from a hypothetical search step, ordered best first. They are invented fixtures, not measured results from an AI Leaf service.

Run the Python example

Save and run this with Python 3 in a local practice file.

labels = {
    "reset": {"reset-note"},
    "export": {"export-note"},
    "limits": {"limits-note"},
}
ranked = {
    "reset": ["welcome-note", "reset-note"],
    "export": ["welcome-note", "reset-note", "export-note"],
    "limits": ["limits-note", "welcome-note"],
}
k = 2
hits = 0
for query, relevant in labels.items():
    found = bool(relevant.intersection(ranked[query][:k]))
    hits += int(found)
    print(query, "HIT" if found else "MISS")
print(f"hit_rate_at_{k}={hits / len(labels):.3f}")

Expected output:

reset HIT
export MISS
limits HIT
hit_rate_at_2=0.667

This code and three additional boundary assertions were executed with Python 3.12.14 before publication. The three fixture decisions and displayed rate matched the output above.

Explain the missing export answer

The export-note exists in the returned list, but it is third. At k=2, the model would receive only welcome-note and reset-note under this simplified setup. A better wording instruction cannot supply the omitted export instructions. Inspect the ranking, chunk boundaries and question wording when diagnosing this miss.

Increasing k to 3 makes all three fixture questions hits. That observation applies to these lists. More retrieved text can increase input size and introduce distracting passages, so compare a representative evaluation set before changing a real application. Record retrieval results separately from answer quality.

Practise with a harder case

First set k to 1. Expected fixture decisions are MISS, MISS and HIT, giving 0.333. Then remove export-note from its list completely and retry k=3. Increasing the cutoff will no longer recover it. Finally, add a fourth question and manually label its relevant source before scoring it. Keep evaluation questions separate from examples used to tune the retriever.

Those suggested list edits are exercises; the full modified programs were not run for this article. The original example and the stated boundary assertions were executed. When moving to real questions, keep stable document IDs and review the relevance labels. Questions with no answer in the handbook need a separate expected-abstention test; do not quietly count them as ordinary misses against an empty label set.

Bring evidence to an online course demo

Learners in Chennai or Tambaram preparing for a GenAI demo can bring three fictional questions, the source notes and a saved result list from this exercise. Ask the teaching team to show which evidence reaches the model and how they investigate a missing source. Use practice notes rather than student or employer records. Tambaram is the admission enquiry contact; confirm current learning format and session availability with the team.

Limits and next steps

This tiny set cannot estimate production quality or compare providers. A hit can still include a low-quality passage, and a model can still misuse a relevant passage. This check ignores ranking differences within the first k results, permissions, freshness and answer faithfulness. Microsoft's RAG evaluation documentation discusses retrieval evaluation and answer evaluation as distinct stages. The simple local metric here is defined for teaching and does not reproduce its evaluator suite.

Explore the Generative AI course syllabus and project pathway and request a relevant demo. For a related lesson on why evaluation needs a separate test set, read preventing train-test leakage.

Official reference

Microsoft RAG retrieval and answer evaluation documentation

Continue learning

More Example Code guidesExplore courses