方寸 Portal

AI Engineer Portal

Your personal operating system for career transition.

Private mode

Exercise

Build a RAG evaluation harness with Recall@K and MRR

Build a RAG Evaluation Harness with Recall@K and MRR

You cannot improve what you cannot measure. Most RAG pipelines are deployed and then tuned based on user complaints — the worst possible feedback loop. A proper evaluation harness gives you Recall@K and MRR measurements before deploying a change, so you know whether a new chunking strategy, embedding model, or retrieval configuration actually improved anything.

What you are building

Create a RAGEvalHarness class that:

  1. Accepts a test set — a list of TestCase objects, each with a query and relevant_doc_ids (IDs of documents that should be retrieved).
  2. Accepts an injectable retrieval functionCallable[[str, int], list[str]] returning document IDs given query and k.
  3. Computes Recall@K for K values 1, 3, 5, and 10 — fraction of test cases where at least one relevant document appears in the top K.
  4. Computes Mean Reciprocal Rank (MRR) — mean of 1/rank of the first relevant result (0 if not found).
  5. Returns an EvalReport with per-query results, aggregate metrics, and failure cases.
  6. Identifies failure cases — queries where no relevant document appears in the top 10.

Why this matters

Recall@K tells you whether the right document is even in the retrieved set. MRR tells you whether it is near the top. Without these numbers, retrieval changes are guesswork. A 10-minute eval harness saves weeks of debugging in production.

Constraints

  • Standard library only. Injectable retrieve function. Type-hint everything.

Retrieval / medium / Step 11 of 15

Practice stage

Retrieval quality and ranking

Hint

Do not trust a single score blindly. Combine ranking logic with metadata and think about why a candidate deserves to rise.

Success criteria
  • - Ranking rule is explainable
  • - Supports future iteration without rewriting everything
  • - Feels tied to product trust rather than pure math trivia
Review checklist
  • - Did I account for both relevance and metadata?
  • - Would I be able to explain the ranking rule in an interview?
  • - Does the code make future tuning easy?

Practice

Generate a variation

Generate a new exercise variation to deepen understanding or practice a related concept.

Attempt history

Recent submissions

Before you submit, decide what a strong answer should make obvious to the reviewer.

No attempts yet.