Lecture translation + study notes

Evaluating a RAG Retriever

Build a component-level evaluation suite for a retrieval-augmented generation application using contextual recall, contextual precision, and golden data.

About this document: This is a condensed English translation of the supplied Hindi–English lecture. Repeated class dialogue and screen-navigation remarks have been streamlined while retaining the learning method.

English translation

1. Goal: build a RAG evaluation suite

This session continues the RAG-evaluation roadmap. Over the next three classes, the goal is to create a reusable RAG evaluation suite at three levels: component level, pipeline/workflow level, and application level. Together, these checks become an eval suite that can later support regression testing.

Today focuses on the component level. The two most important RAG components are the retriever, which fetches relevant context from a vector database, and the generator, which uses the user question plus retrieved context to produce an answer. This lesson starts with the retriever.

2. Project setup and incremental development

The project is organized into data/ for lecture transcripts, src/ for application code, evals/ for evaluation pipelines, and goldens/ for golden datasets. A virtual environment and the required libraries are set up, with secrets stored in an environment file rather than in source code.

The important engineering principle is to evaluate as you build. Do not build the entire RAG app first and test it only at the end. Build the retriever, evaluate it, then build and evaluate the generator, then evaluate the full pipeline, and finally evaluate the application.

3. How the retriever works

The retriever takes a query, converts it to an embedding, searches a vector database containing embedded text chunks, and returns the nearest k chunks as context. To create it, load the documents, split them into chunks, embed those chunks, store them, and expose a retriever object that can fetch relevant context for a query.

4. Two retriever failure modes

A retriever can fail in two opposite ways. First, it may miss relevant context: information needed to answer the question exists in the knowledge base but was not retrieved. This is measured by recall. Second, it may retrieve too much irrelevant context (noise), which distracts the generator and wastes context window. This is measured by precision.

An ideal retriever has both high recall and high precision. However, there is usually a trade-off. Increasing k, the number of retrieved chunks, tends to raise recall because more relevant chunks may be included; it can lower precision because more irrelevant chunks are also included.

5. Why retrieval evaluation is reference-based

Recall and precision are reference-based evaluations. To know whether the retriever found the correct information, you need a golden reference for each query. A traditional approach labels the exact relevant chunks. But if chunk size or chunking strategy changes, those chunk labels must be redone.

The preferred approach in this lesson avoids that brittle dependency. For each realistic user question, store an ideal answer. The ideal answer remains valid even if the document chunks are changed. An LLM judge can break the ideal answer into atomic claims and check whether the retrieved context supports those claims.

6. Contextual recall

For contextual recall, the LLM judge decomposes the ideal answer into claims. It then inspects all retrieved chunks and identifies which claims are supported anywhere in the context. Contextual recall is the proportion of ideal-answer claims covered by the retrieved context. If all required claims are present, recall is 1.0; if only one of two claims is supported, it is 0.5.

This differs from simple Recall@K. Recall@K depends on pre-labelled relevant chunks; contextual recall uses an ideal answer and an LLM judge, making it more resilient to changes in chunking.

7. Contextual precision

Contextual precision asks whether relevant information appears early in the retrieved list and whether the list is free of noise. The judge examines each retrieved chunk in rank order and marks whether it helps answer the question. Relevant chunks near the top improve the score; irrelevant chunks, especially near the top, reduce it.

Both contextual recall and contextual precision are useful. High recall with poor precision means the correct material exists somewhere in a long noisy context. High precision with poor recall means the returned chunks look relevant but omit essential facts.

8. Creating the golden dataset

A golden dataset contains at least a user-like question and its ideal answer. The lecture reviews four ways to create it: write examples manually, have a capable LLM generate examples under strong instructions, use a synthetic-data tool, or mine successful production interactions. Production logs are valuable later but cannot be the only starting point.

For this course chatbot, the chosen approach is LLM-assisted authoring with detailed instructions and manual quality control. Examples are generated in small batches so they sound like real student questions—not artificial or overly technical questions copied from random transcript text.

9. Run the retriever evaluation

For every row in the golden dataset, send the question to the retriever, collect the returned chunks, and pass the question, ideal answer, and retrieved context to the evaluation metrics. Aggregate the per-question contextual recall and contextual precision scores. Inspect low-scoring cases—not only the average—to discover whether chunk size, embedding model, retrieval k, metadata filters, or the source documents need improvement.

Retriever metrics

Contextual recall

Supported ideal-answer claims ÷ total ideal-answer claims

Did retrieval bring all information required for the answer?

Contextual precision

Rank-aware relevance of retrieved chunks

Are useful chunks retrieved early, without irrelevant noise?

Failure modeSymptomMetric to inspectTypical response
Missing informationThe answer cannot be fully grounded in retrieved context.Low contextual recallImprove retrieval, chunking, embeddings, or raise k carefully.
Too much noiseRelevant content is buried among unrelated chunks.Low contextual precisionImprove ranking/filtering, reduce k, or use reranking.

Component-level workflow

1

Prepare documents

Load transcripts or other knowledge-base content.

2

Build retriever

Chunk, embed, index, and retrieve top-k contexts.

3

Create goldens

Write user-like questions and ideal answers.

4

Retrieve

Fetch context for every golden question.

5

Judge claims

Use an LLM judge to assess coverage and relevance.

6

Analyze & iterate

Aggregate scores and inspect individual failures.

Golden-dataset guidance

  • Use questions that resemble real user language and intent.
  • Store an ideal answer rather than only exact chunk IDs; the answer survives chunking changes.
  • Generate examples in small batches and review them—synthetic data can be unnatural or off-topic.
  • Use feedback and successful production interactions to expand coverage over time.
  • Keep the suite as a regression test: compare a new retriever version against the previous one before release.

Evaluation pseudocode

for row in retriever_golden_dataset: retrieved_context = retriever.retrieve(row.question, k=K) recall = contextual_recall( question=row.question, expected_answer=row.ideal_answer, context=retrieved_context ) precision = contextual_precision( question=row.question, expected_answer=row.ideal_answer, context=retrieved_context ) report(mean(recall_scores), mean(precision_scores))

Self-check questions

  1. What are the two main failure modes of a RAG retriever?
  2. Why can increasing k improve recall but harm precision?
  3. Why are contextual recall and contextual precision reference-based evaluations?
  4. Why is an ideal answer often more robust than a list of relevant chunk IDs?
  5. What should you inspect in addition to an average retrieval score?