Reading preferences
Concept

Building and Evaluating RAG Context

Measure retrieval separately from answer grounding and end-to-end usefulness.

generative ai, rag, retrieval evaluation, grounding

Construct a context

After retrieving permitted passages, choose relevant text and source identifiers within the model's token budget. Preserve enough surrounding context to avoid misleading fragments. Place task instructions separately from retrieved content; passages are untrusted data, not authority to call tools or reveal secrets.

Measure one retrieval

Suppose four passages in an evaluation collection are relevant. The retriever returns three passages, of which two are relevant.

  1. Precision is 2 / 3: two of the three retrieved are relevant.
  2. Recall is 2 / 4 = 1 / 2: two of the four relevant passages were found.
  3. Then inspect whether the generated answer uses those passages correctly and cites the right source. Retrieval success alone is not answer success.

See the reasoning

Count relevant passages

Separate precision and recall for one labelled query. This is a prepared demonstration, not live code execution.

Inspect results

Four relevant passages; three retrieved
  1. Retrieved3 passagesCurrent focus
  2. Relevant retrieved2 passages
  3. All relevant4 passages

The labelled evaluation set identifies four relevant passages.

Evaluate the pipeline

Use labelled queries and relevant passages, including no-answer cases. Report retrieval precision or recall at a specified cutoff and evaluate answer correctness, grounding, citation accuracy, latency and permissions. Relevant-set labels can be incomplete, and an automatic judge can be wrong; use human review and report uncertainty. A model may ignore or misread a correct passage, and a cited source can be stale.

Reflect on the concept

Four fixed questions to reinforce the reasoning. Answers are not saved or scored.

1. If two of three retrieved passages are relevant what is precision?
2. If two of four relevant passages are retrieved what is recall?
3. Does retrieval of a relevant passage prove the answer is grounded?
4. What should happen when sufficient evidence is missing?

References and further reading