Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Evaluating RAG Output: Relevance and Faithfulness

Hello! In our last lesson, you successfully built a complete, local Retrieval-Augmented Generation (RAG) pipeline. You now have a working system that can answer questions based on a private knowledge base. But this raises a critical question for any engineer: "Is it any good?"

Just building a system isn't enough; we need to measure its performance to understand its weaknesses and improve it. This lesson addresses that exact challenge. Our learning outcome is to evaluate RAG output quality using relevance and faithfulness as criteria.

We will explore why traditional text-generation metrics fall short for RAG and dive into the specialized metrics that form the modern foundation for RAG evaluation. By the end, you'll understand how to quantify the quality of your RAG system and use those metrics to diagnose and fix potential issues.

The Limits of Traditional Metrics

You might have encountered metrics like BLEU and ROUGE in the context of machine translation or text summarization. These metrics work by comparing the generated text to a "golden" reference text, looking for overlapping words or n-grams.

However, for a RAG system, this approach is insufficient. Why? Because the primary goal of RAG isn't just to generate fluent text, but to generate text that is factually grounded in the retrieved context and relevant to the user's query. A perfectly fluent answer that hallucinates facts or ignores the user's question is a failure.

This is why the field has developed a specialized set of metrics tailored to the unique challenges of RAG.

The RAG Evaluation Triad: Faithfulness and Relevance

The quality of a RAG system's output can be broken down into three core components, often called the "RAG Triad."

The RAG Triad: Evaluation Criteria
This diagram illustrates the three primary evaluation criteria for RAG systems: Context Relevance, Faithfulness, and Answer Relevance. Source: Zilliz.

Let's define these key criteria:

  1. Faithfulness (or Groundedness): This measures whether the generated answer is factually consistent with the retrieved context. A high faithfulness score means the model is not "hallucinating" or inventing information; it is sticking to the provided facts.

  2. Answer Relevance: This measures how well the generated answer addresses the user's original question. The answer might be faithful to the context, but if it doesn't actually answer what the user asked, it's not useful.

  3. Context Relevance: This measures the quality of the retrieval step. Are the document chunks retrieved from the vector store actually relevant to the user's query? If not, the system fails before the generation step even begins ("garbage in, garbage out").

The following video provides an excellent conceptual overview of these metrics and explains how they are measured in practice, often by using another powerful LLM as an impartial "judge."

LLM Chronicles #6.6: Hallucination Detection and Evaluation for RAG systems (RAGAS, Lynx)

Watch this segment from 'LLM Chronicles #6.6' by Donato Capitella. It clearly explains why we need these specific metrics for RAG and introduces how each one—faithfulness, answer relevance, and context relevance—is evaluated.

Watch from the beginning to 10:13. Focus on understanding: Why RAG requires a different evaluation approach than standard LLMs. The definitions of Faithfulness, Answer Relevance, and Context Relevance. The high-level techniques used to calculate each score (e.g., using LLMs as judges, breaking down answers into statements).

Introducing RAGAS: A Framework for RAG Evaluation

The concepts we've just discussed are operationalized in a popular open-source framework called RAGAS (RAG Assessment). RAGAS provides a standardized toolkit for computing these metrics. It formalizes the "LLM-as-judge" approach and gives you concrete scores to benchmark your pipeline.

RAGAS uses four primary metrics that map closely to our triad:

  • Faithfulness
  • Answer Relevancy
  • Context Precision (a measure of Context Relevance)
  • Context Recall (another measure of retrieval quality)
RAGAS Evaluation Metrics for RAG Systems
The RAGAS framework maps specific metrics to evaluate the relationship between the query, the retrieved context, the generated response, and an optional ground-truth answer. Source: Zilliz.

To solidify your understanding of these metrics, let's look at their formal definitions and some simple examples.

RAGAS for RAG in LLMs: A Comprehensive Guide to Evaluation ...

This article, 'RAGAS for RAG in LLMs' by D. Kaarthick, provides clear, concise definitions and formulas for the core RAGAS metrics.

Read the sections titled 'Faithfulness' and 'Answer Relevancy'. These sections give you straightforward examples of how these scores are calculated conceptually.

Hands-On: Evaluating a RAG Pipeline with RAGAS

Now, let's move from theory to practice. As a software engineer, the best way to understand this is to see it in action. The following video provides a comprehensive walkthrough of setting up and running a RAG evaluation pipeline using RAGAS and LangChain.

This is a critical part of the MLOps lifecycle for LLM applications. You'll learn how to create an evaluation dataset and then use it to benchmark your RAG system's performance.

Session 7: RAG Evaluation with RAGAS and How to Improve Retrieval

This video, 'Session 7: RAG Evaluation with RAGAS' from AI Makerspace, demonstrates the entire evaluation process programmatically. It's a fantastic practical guide that builds directly on the concepts we've covered.

Watch from 04:45 to 24:10. This is the core of our practical exercise. Pay close attention to: The four components needed for evaluation: question, answer, retrieved context, and ground truth. How to use an LLM (like GPT-4) to synthetically generate an evaluation dataset (questions and ground truth answers) from your documents. This is a powerful and common technique. The Python code for setting up the RAGAS evaluation and running it on the dataset. How to interpret the final scores for each metric (faithfulness, answer relevancy, context recall, etc.).

This process gives you a repeatable, automated way to measure the impact of any change you make to your system.

From Metrics to Action: Improving Your Pipeline

Getting a set of scores is the first step. The real value lies in using them to diagnose problems and guide improvements. Here’s how you can interpret the results:

  • Low Context Precision / Recall: This points to a problem with your retriever. Your system isn't finding the right information.

    • Potential Fixes:
      • Adjust your chunking strategy (as we discussed in a previous lesson).
      • Use a better embedding model.
      • Implement more advanced retrieval strategies.
  • High Context Scores, but Low Faithfulness: This suggests a problem with your generator. The system found the right documents but the LLM failed to use them properly, either by hallucinating or ignoring the context.

    • Potential Fixes:
      • Improve your prompt to be more strict (e.g., "Answer only based on the provided context.").
      • Use a more capable LLM.
      • Lower the LLM's temperature to reduce creativity.
  • Low Answer Relevancy: This can be caused by either the retriever or the generator. If the retrieved context is irrelevant (low context scores), the answer will naturally be irrelevant. If the context is relevant but the answer is not, the generator is likely drifting off-topic.

The video you just watched provides a perfect example of this diagnostic process.

Session 7: RAG Evaluation with RAGAS and How to Improve Retrieval

Let's look at the final part of the 'Session 7' video, where the presenters use RAGAS scores to compare different retrieval strategies.

Watch from 27:46 to 37:22. Notice how they swap out a basic retriever for more advanced ones (Parent Document Retriever, Ensemble Retriever) and then re-run the evaluation. They analyze how the scores for context_recall and other metrics change, demonstrating how evaluation guides system improvement.

This demonstrates the iterative loop of building LLM systems: build, evaluate, and refine.

Conclusion

In this lesson, we moved beyond just building a RAG pipeline to understanding how to measure its quality. You now have the conceptual and practical tools to assess the performance of your RAG applications.

Key Takeaways:

  • Standard NLP metrics like BLEU/ROUGE are not suitable for RAG; we need specialized metrics like faithfulness and relevance.
  • Faithfulness ensures the answer is factually grounded in the retrieved context, combating hallucination.
  • Relevance is a two-part problem: Context Relevance assesses the retriever's quality, while Answer Relevance assesses the generator's ability to address the user's query.
  • Frameworks like RAGAS provide an automated way to measure these criteria using an "LLM-as-judge" approach.
  • Evaluation scores are not just a report card; they are diagnostic tools that point to specific weaknesses in your RAG pipeline (e.g., a poor retriever vs. an unconstrained generator).

You've learned how to measure the performance of your retriever. In our next lesson, we will focus on one of the most effective techniques to improve it: implementing metadata filtering to refine RAG search results. This will allow you to narrow down the search space and provide the generator with even more relevant context.

Can't find a good explanation? Sign up and we'll make it for you

Sign up