Module 5 · Data agents & production

Evaluation: faithfulness, relevancy, correctness

5 min read

"It seems to work well" is not a measurement. Every setting in this course — chunk size, embedding model, similarity threshold, prompt, reranking — can improve some questions and degrade others, without anyone noticing until a customer receives a wrong answer. Evaluation turns these intuitions into numbers, and makes it possible to compare two versions before putting them in production. This lesson builds Lumen's evaluation, on its two levels: retrieval and answer.

Two levels, two questions

LevelQuestionIf it fails
RetrievalIs the right passage among those retrieved?No prompt will save the answer: fix chunking, embeddings, filters
AnswerIs the answer faithful to the passages, relevant, correct?Fix the prompt, the model, the synthesis mode

Evaluating only the final answer hides where errors come from. Evaluating both levels tells you where to act.

The evaluation set

Everything starts with a set of representative questions, each with the expected source and a reference answer:

DATASET = [
    {"query": "Combien de temps ai-je pour retourner un article ?", "source": "policies/returns.md",
     "reference": "30 jours après la livraison."},
    {"query": "Combien coûte la livraison express ?", "source": "policies/shipping.md",
     "reference": "9,90 euros."},
    ...
]

The best questions come from real users: questions asked to support, unsuccessful searches, complaints. Add hard questions (wording far from the documents' vocabulary), multi-source questions, and out-of-scope questions, to check that the system knows how to say it doesn't know. Fifty to a hundred well-chosen questions are worth more than a thousand automatically generated questions that nobody reviewed.

LlamaIndex can generate questions from documents (DatasetGenerator, in llama_index.core.evaluation): handy to get started, but generated questions resemble the documents too much, and therefore overestimate retrieval quality.

Measuring retrieval

Two metrics are enough to start:

  • Hit rate: the proportion of questions for which the expected source is among the retrieved passages.
  • MRR (Mean Reciprocal Rank): the average of 1/rank of the first correct source (1 if first, 0.5 if second…). It rewards systems that put the right source on top.
nodes = await retriever.aretrieve(case["query"])
sources = [n.node.metadata.get("source") for n in nodes]
rank = sources.index(case["source"]) + 1 if case["source"] in sources else None

Lumen compares sources (files) rather than node identifiers: the test set stays valid when the chunking changes. LlamaIndex also provides a RetrieverEvaluator that computes these metrics (and others) from expected node identifiers.

Measuring the answer: the LLM as a judge

Judging a natural-language answer requires… a model. LlamaIndex provides ready-made evaluators:

EvaluatorQuestion asked to the judgeReference needed
FaithfulnessEvaluatorIs the answer supported by the retrieved passages? (detects hallucinations)No
RelevancyEvaluatorDo the answer and passages answer the question?No
CorrectnessEvaluatorIs the answer correct compared to the reference? (score from 1 to 5)Yes
SemanticSimilarityEvaluatorIs the answer close to the reference (by embeddings)?Yes
GuidelineEvaluatorDoes the answer follow guidelines (tone, format, citation)?No
judge = models.main_llm()
f = await FaithfulnessEvaluator(llm=judge).aevaluate_response(query=q, response=response)
c = await CorrectnessEvaluator(llm=judge).aevaluate(query=q, response=str(response), reference=ref)

Faithfulness is the most important metric for a documentation assistant: a faithful answer may be incomplete, but an unfaithful answer invents.

The limits of the LLM judge

  • The judge must be at least as capable as the model evaluated: Lumen judges with the large model.
  • The judge has biases: preference for long answers, for its own style, sensitivity to presentation order. Regularly check a sample of its verdicts by hand.
  • The judge costs: several calls per question. Run the full evaluation before every release, not on every commit.
  • Thresholds are relative: a score only makes sense compared to the previous version, on the same set.

Lumen's evaluation report

report = await evaluate(kb)
print(f"Recherche  : taux de réussite {report.hit_rate:.0%}, MRR {report.mrr:.2f}")
print(f"Réponses   : fidélité {report.faithfulness:.0%}, pertinence {report.relevancy:.0%}, "
      f"exactitude {report.correctness:.1f}/5")
for failure in report.failures:
    print("  échec :", failure)

The report also lists the failures one by one. Those are what you must read: a 90% overall rate says nothing about the 10% that fail, and they are often the most important questions.

The test_evaluation_report test runs all this machinery offline, with the fake judge: it guarantees that the evaluation code works, not that quality is good. The real evaluation runs with a real model (examples/06_evaluation.py).

Making evaluation a habit

  1. A baseline: evaluate the current version before any change.
  2. One change at a time: chunk size, then embedding model, then reranking. Otherwise, impossible to know what helped.
  3. An evaluation before every release, compared to the baseline; a regression in faithfulness blocks the deployment.
  4. A living set: every error reported by a user becomes a new question in the set.

Key takeaways

  • Evaluate retrieval (hit rate, MRR) and answer (faithfulness, relevancy, correctness) separately: it tells you where to act.
  • The evaluation set comes from real users, includes hard and out-of-scope questions; generated questions overestimate quality.
  • Faithfulness is the key metric of a documentation assistant: it detects inventions.
  • The LLM judge must be capable, has biases, costs: check it by hand on a sample.
  • Baseline, one change at a time, evaluation before every release, set enriched by real errors.

Practice

  1. Run examples/06_evaluation.py with a real model and record the baseline.
  2. Add five real questions and two out-of-scope questions to the set; check that the latter produce "je ne trouve pas".
  3. Change CHUNK_SIZE from 512 to 256, reingest, reevaluate: did hit rate and faithfulness move?

Resources