Evaluating RAG Systems
local graph
Chapter 28 — Evaluating RAG Systems
Section titled “Chapter 28 — Evaluating RAG Systems”RAG systems strive to produce more accurate, relevant, and factually grounded responses. However, evaluating the performance of these systems presents unique challenges. Unlike traditional information retrieval or question-answering (QA) systems, RAG evaluation must consider both the quality of the retrieved information and the effectiveness of the LLM in utilizing that information to generate a high-quality response.
In this chapter, we’ll explore the intricacies of evaluating RAG systems. We’ll examine the challenges inherent in this task, dissect the key metrics used to assess retrieval quality and generation performance, and discuss various strategies for conducting comprehensive evaluations.
This chapter aims to provide you with a thorough understanding of the principles and practices of RAG evaluation, equipping you with the knowledge you’ll need to assess and improve these powerful systems.
In this chapter, we will be covering the following topics:
- Challenges in evaluating RAG systems for LLMs
- Metrics for assessing retrieval quality in LLM-based RAG
- Considerations for retrieval metrics in RAG
- Evaluating the relevance of retrieved information for LLMs
- Measuring the impact of retrieval on LLM generation
- End-to-end evaluation of RAG systems in LLMs
- Human evaluation techniques for LLM-based RAG
- Benchmarks and datasets for RAG evaluation
Challenges in evaluating RAG systems for LLMs
Section titled “Challenges in evaluating RAG systems for LLMs”Evaluating RAG systems presents a unique set of challenges that distinguish it from evaluating traditional information retrieval or QA systems. These challenges stem from the interplay between the retrieval and generation components and the need to assess both the factual accuracy and the quality of the generated text.
The following sections will detail the specific challenges that are encountered when evaluating RAG systems for LLMs.
The interplay between retrieval and generation
Section titled “The interplay between retrieval and generation”The performance of a RAG system is a product of both its retrieval component and its generation component. Strong retrieval can provide the LLM with relevant and accurate information, leading to a better-generated response. Conversely, poor retrieval can mislead the LLM, resulting in an inaccurate or irrelevant answer, even if the generator itself is highly capable. Therefore, evaluating a RAG system requires assessing not only the quality of the retrieved information but also how effectively the LLM utilizes that information in its generation process.
Context-sensitive evaluation
Section titled “Context-sensitive evaluation”Unlike traditional information retrieval, where relevance is often assessed based on the query alone, RAG evaluation must consider the context in which the retrieved information is used. A document might be relevant to the query in isolation but not provide the specific information needed to answer the question accurately in the context of the generated response. This necessitates context-sensitive evaluation metrics that consider both the query and the generated text when assessing the relevance of retrieved documents.
Beyond factual accuracy
Section titled “Beyond factual accuracy”While factual accuracy is a primary concern in RAG evaluation, it is not the only factor that determines the quality of a generated response. The response must also be fluent, coherent, and relevant to the user’s query. These aspects of text quality are typically assessed through human evaluation, which can be expensive and time-consuming. Developing automated metrics that correlate well with human judgments of these qualitative aspects remains an open research challenge.
Limitations of automated metrics
Section titled “Limitations of automated metrics”Automated metrics, such as those borrowed from information retrieval (e.g., precision, recall) or machine translation (e.g., BLEU, ROUGE), can provide useful insights into RAG system performance. However, they often fall short of capturing the full picture. Retrieval metrics might not fully reflect the usefulness of a document for generation, while generation metrics might not adequately assess the factual grounding of the generated text in the retrieved context.
Difficulty in error analysis
Section titled “Difficulty in error analysis”When a RAG system produces an incorrect or low-quality response, it can be challenging to pinpoint the root cause. Was the retrieval component unable to find relevant documents? Did the LLM fail to utilize the retrieved information properly? Did the LLM hallucinate or generate a response that is not grounded in the provided context? Disentangling these factors requires careful error analysis and potentially the development of new diagnostic tools.
The need for diverse evaluation scenarios
Section titled “The need for diverse evaluation scenarios”RAG systems can be deployed in a wide range of applications, from open-domain QA to domain-specific chatbots. The specific challenges and evaluation criteria may vary, depending on the use case. Evaluating a RAG system’s performance across diverse scenarios and domains is crucial for understanding its strengths and weaknesses.
Dynamic knowledge and evolving information
Section titled “Dynamic knowledge and evolving information”In many real-world applications, the underlying knowledge base is constantly evolving. New information is added, and existing information is updated or becomes outdated. Evaluating how well a RAG system adapts to these changes and maintains the accuracy of its responses over time is a significant challenge.
Computational cost
Section titled “Computational cost”Evaluating RAG systems, especially those that use bigger LLMs, can be computationally expensive. Running inference with large models and performing human evaluations on a large scale can require significant resources. Finding ways to balance evaluation thoroughness with computational cost is an important consideration.
Let’s take a look at some key metrics for evaluating the retrieval component in LLM-based RAG systems while focusing on relevance and utility for response generation.
Metrics for assessing retrieval quality in LLM-based RAG
Section titled “Metrics for assessing retrieval quality in LLM-based RAG”The retrieval component plays a crucial role in the overall performance of a RAG system. It is responsible for providing the LLM with relevant and accurate information that serves as the basis for generating a response. Therefore, assessing the quality of the retrieval component is a vital aspect of RAG evaluation. We can adapt traditional information retrieval metrics to the RAG setting, focusing on the ability of the retriever to find documents that are not only relevant to the query but also useful for the LLM to generate a high-quality answer.
Recall@k
Section titled “Recall@k”Recall@k measures the proportion of relevant documents that are successfully retrieved within the top k results. In the context of RAG, we can define a relevant document as one that contains the necessary information to answer the query accurately:
- Formula: Recall@k = (Number of relevant documents retrieved in top k) / (Total number of relevant documents)
- Interpretation: A higher Recall@k indicates that the retrieval component can find a larger proportion of the relevant documents
- Example: If five documents in the entire corpus contain information needed to answer a specific query, and the RAG system retrieves three of them within the top 10 results, then Recall@10 for that query would be 3/5 = 0.6
Precision@k
Section titled “Precision@k”Precision@k measures the proportion of retrieved documents within the top k results that are relevant:
- Formula: *Precision@k = (Number of relevant documents retrieved in top k) / (*k)
- Interpretation: A higher Precision@k indicates that a larger proportion of the retrieved documents are relevant
- Example: If a RAG system retrieves 10 documents for a query, and four of them are relevant, then Precision@10 would be 4/10 = 0.4
Mean Reciprocal Rank (MRR)
Section titled “Mean Reciprocal Rank (MRR)”MRR considers the rank of the first relevant document retrieved. It emphasizes the importance of retrieving relevant documents early in the ranking:
- Formula: MRR = (1 / |Q|) * Σ (1 / rank_i) for i = 1 to |Q|, where |Q| is the number of queries and rank_i is the rank of the first relevant document for query i.
- Interpretation: A higher MRR indicates that relevant documents are retrieved at higher ranks (closer to the top).
- Example: If the first relevant document for a query is retrieved at rank three, the reciprocal rank is 1/3. MRR averages these reciprocal ranks across multiple queries.
Normalized Discounted Cumulative Gain (NDCG@k)
Section titled “Normalized Discounted Cumulative Gain (NDCG@k)”NDCG@k is a more sophisticated metric that considers both the relevance of retrieved documents and their position in the ranking. It uses a graded relevance scale (e.g., 0, 1, 2, where 2 is highly relevant) and assigns higher scores to relevant documents retrieved at higher ranks:
- Formula: NDCG@k involves calculating the Discounted Cumulative Gain (DCG) of the retrieved list and normalizing it by the Ideal Discounted Cumulative Gain (IDCG), which is the DCG of the perfectly ranked list. The formula is complex but can be easily computed using libraries such as sklearn.
- Interpretation: A higher NDCG@k indicates that highly relevant documents are retrieved at higher ranks.
Next, let’s discuss how to decide which retrieval metrics to use.
Considerations for retrieval metrics in RAG
Section titled “Considerations for retrieval metrics in RAG”In the context of RAG, we need to define relevance carefully. A document might be relevant to the query but not contain the specific information needed to answer it accurately. We might need to use a stricter definition of relevance, such as “contains the answer to the query.” As mentioned earlier, relevance in RAG is often context-sensitive. A document might be relevant to the query in isolation but not be the most helpful document for generating a specific answer given the other retrieved documents.
While metrics such as Recall@k and Precision@k focus on the top k retrieved documents, it’s also important to consider the overall quality of the retrieval across a wider range of results. Metrics such as Average Precision (AP) can provide a more holistic view.
Let’s illustrate how to calculate Recall@k, Precision@k, MRR, and NDCG@k in Python using the sklearn library:
- We first import the necessary libraries and define sample data representing a set of queries, the ground truth relevant documents for each query, and the documents that are retrieved by a RAG system for
import numpy as npfrom sklearn.metrics import ndcg_score# Sample dataqueries = ["What is the capital of France?","Who painted the Mona Lisa?","What is the highest mountain in the world?"]ground_truth = [[0, 1, 2], # Indices of relevant documents for query 1[3, 4], # Indices of relevant documents for query 2[5, 6, 7] # Indices of relevant documents for query 3]retrieved = [[1, 5, 0, 2, 8, 9, 3, 4, 6, 7], # Ranked list of retrieved document indices for query 1[4, 3, 0, 1, 2, 5, 6, 7, 8, 9], # Ranked list of retrieved document indices for query 2[6, 5, 7, 0, 1, 2, 3, 4, 8, 9] # Ranked list of retrieved document indices for query 3]
- We then define a function,
calculate_recall_at_k, to calculate Recall@k for a given set of queries, ground truth relevant documents, and retrieveddef calculate_recall_at_k(ground_truth, retrieved, k):"""Calculates Recall@k for a set of queries."""recall_scores = []for gt, ret in zip(ground_truth, retrieved):num_relevant = len(gt)retrieved_k = ret[:k]num_relevant_retrieved = len(set(gt).intersection(set(retrieved_k)))recall = (num_relevant_retrieved / num_relevantif num_relevant > 0 else 0)recall_scores.append(recall)return np.mean(recall_scores) - We next define a function,
calculate_precision_at_k, to calculate Precision@k for a given set of queries, ground truth, anddef calculate_precision_at_k(ground_truth, retrieved, k):"""Calculates Precision@k for a set of queries."""precision_scores = []for gt, ret in zip(ground_truth, retrieved):retrieved_k = ret[:k]num_relevant_retrieved = len(set(gt).intersection(set(retrieved_k)))precision = num_relevant_retrieved / k if k > 0 else 0precision_scores.append(precision)return np.mean(precision_scores) - We define a function,
calculate_mrr, to calculate the MRR for a given set of queries, ground truth, and retrieved lists. A higher MRR indicates that the system consistently retrieves relevant documents atdef calculate_mrr(ground_truth, retrieved):"""Calculates Mean Reciprocal Rank (MRR) for a set of queries."""mrr_scores = []for gt, ret in zip(ground_truth, retrieved):for i, doc_id in enumerate(ret):if doc_id in gt:mrr_scores.append(1 / (i + 1))breakelse:mrr_scores.append(0) # No relevant document foundreturn np.mean(mrr_scores) - We also define a function,
calculate_ndcg_at_k, to calculate NDCG@k. We’ll use a simplified version here where relevance scores are binary (0def calculate_ndcg_at_k(ground_truth, retrieved, k):"""Calculates NDCG@k for a set of queries."""ndcg_scores = []for gt, ret in zip(ground_truth, retrieved):relevance_scores = np.zeros(len(ret))for i, doc_id in enumerate(ret):if doc_id in gt:relevance_scores[i] = 1# sklearn.metrics.ndcg_score requires 2D arraytrue_relevance = np.array([relevance_scores])retrieved_relevance = np.array([relevance_scores])ndcg = ndcg_score(true_relevance, retrieved_relevance, k=k)ndcg_scores.append(ndcg)return np.mean(ndcg_scores) - Finally, we calculate and print the retrieval metrics for different values
k_values = [1, 3, 5, 10]for k in k_values:recall_at_k = calculate_recall_at_k(ground_truth,retrieved, k)precision_at_k = calculate_precision_at_k(ground_truth, retrieved, k)ndcg_at_k = calculate_ndcg_at_k(ground_truth, retrieved, k)print(f"Recall@{k}: {recall_at_k:.3f}")print(f"Precision@{k}: {precision_at_k:.3f}")print(f"NDCG@{k}: {ndcg_at_k:.3f}")mrr = calculate_mrr(ground_truth, retrieved)print(f"MRR: {mrr:.3f}")
Evaluating the relevance of retrieved information for LLMs
Section titled “Evaluating the relevance of retrieved information for LLMs”While the retrieval metrics discussed in the previous section provide a general assessment of retrieval quality, they do not fully capture the nuances of relevance in the context of RAG. In RAG, the retrieved information is not the end product but rather an intermediate step that serves as input to an LLM. Therefore, we need to evaluate the relevance of the retrieved information not just to the query but also to the specific task of generating a high-quality response via the LLM.
Traditional information retrieval often focuses on finding documents that are topically relevant to the query. However, in RAG, we need a more nuanced notion of relevance that considers the following aspects:
- Answerability: Does the retrieved information contain the specific information needed to answer the query accurately? A document might be generally relevant to the query but not contain the precise answer.
- Contextual utility: Is the retrieved information useful in the context of the other retrieved documents? A document might be relevant in isolation but redundant or even contradictory when combined with other retrieved information.
- LLM compatibility: Is the retrieved information in a format that the LLM can easily understand and utilize? For example, a long and complex document might be relevant but difficult for the LLM to process effectively.
- Faithfulness support: Does the retrieved information provide sufficient evidence to support the claims made in the generated answer? This is crucial for ensuring that the LLM’s response is grounded in the retrieved context.
Methods for evaluating the relevance of retrieved information
Section titled “Methods for evaluating the relevance of retrieved information”Here are some methods for evaluating the relevance of retrieved information that involve moving beyond traditional query relevance:
- Human evaluation:
- Direct assessment: Human annotators can directly assess the relevance of retrieved documents to the query and the generated response. They can be asked to rate the relevance on a Likert scale (e.g., 1 to 5) or to provide binary judgments (relevant/not relevant).
- Comparative evaluation: Annotators can be presented with multiple sets of retrieved documents and asked to rank them based on their usefulness for answering the query or to choose the best set.
- Task-based evaluation: Annotators can be asked to use the retrieved documents to answer the query themselves. The accuracy and quality of their answers can serve as an indirect measure of the relevance and utility of the retrieved information.
- Direct assessment: Human annotators can directly assess the relevance of retrieved documents to the query and the generated response. They can be asked to rate the relevance on a Likert scale (e.g., 1 to 5) or to provide binary judgments (relevant/not relevant).
- Automated metrics: Let’s consider some commonly used automated metrics. Keep in mind that while automated metrics provide a quantitative measure of performance, human evaluation offers valuable qualitative insights into the relevance, coherence, and usefulness of the generated responses:
- Answer overlap: We can automatically measure the overlap between the generated answer and the retrieved documents using metrics such as ROUGE or BLEU. Higher overlap suggests that the LLM is utilizing the retrieved information.
- QA metrics: If we have ground truth answers, we can treat the retrieved context as the input to a QA system and evaluate its performance using standard QA metrics such as Exact Match (EM) and F1 score.
- Faithfulness metrics: We can use techniques such as Natural Language Inference (NLI) to assess whether the generated answer is entailed by the retrieved context. We’ll discuss NLI models in detail in later sections of this chapter.
- Perplexity: We can measure the perplexity of the LLM when it’s conditioned on the retrieved context. Lower perplexity suggests that the LLM finds the context informative and useful for generation.
- Answer overlap: We can automatically measure the overlap between the generated answer and the retrieved documents using metrics such as ROUGE or BLEU. Higher overlap suggests that the LLM is utilizing the retrieved information.
As an example, let’s illustrate how to implement simple answer overlap metrics using the rouge-score library in Python:
- First, we run the following command to install the
rouge-scorelibrary, which provides implementations of ROUGE metrics, and import thepip install rouge-scorefrom rouge_score import rouge_scorer - Then, we define sample data representing a query, a generated answer, and a list of
query = "What is the capital of France?"answer = "The capital of France is Paris."retrieved_documents = ["Paris is the capital city of France.","France is a country in Europe.","The Eiffel Tower is a famous landmark in Paris.","London is the capital of the United Kingdom."]
- We next define a function,
calculate_rouge_scores, to calculate ROUGE scores between the generated answer and eachdef calculate_rouge_scores(answer, documents):"""Calculates ROUGE scores between the answer and each document."""scorer = rouge_scorer.RougeScorer(['rouge1', 'rouge2', 'rougeL'],use_stemmer=True)scores = []for doc in documents:score = scorer.score(answer, doc)scores.append(score)return scores - We then calculate and print the ROUGE scores for
rouge_scores = calculate_rouge_scores(answer,retrieved_documents)for i, score in enumerate(rouge_scores):print(f"Document {i+1}:")print(f" ROUGE-1: {score['rouge1'].fmeasure:.3f}")print(f" ROUGE-2: {score['rouge2'].fmeasure:.3f}")print(f" ROUGE-L: {score['rougeL'].fmeasure:.3f}")
- Finally, we calculate and print the average ROUGE scores across
avg_rouge1 = sum([score['rouge1'].fmeasurefor score in rouge_scores]) / len(rouge_scores)avg_rouge2 = sum([score['rouge2'].fmeasurefor score in rouge_scores]) / len(rouge_scores)avg_rougeL = sum([score['rougeL'].fmeasurefor score in rouge_scores]) / len(rouge_scores)print(f"\nAverage ROUGE Scores:")print(f" Average ROUGE-1: {avg_rouge1:.3f}")print(f" Average ROUGE-2: {avg_rouge2:.3f}")print(f" Average ROUGE-L: {avg_rougeL:.3f}")
Challenges in evaluating RAG-specific relevance
Section titled “Challenges in evaluating RAG-specific relevance”Having explored several methods for evaluating the relevance of retrieved information, we now turn to outlining some of the key challenges involved in this evaluation process:
- Subjectivity: Relevance judgments can be subjective, especially when considering factors such as contextual utility and LLM compatibility
- Annotation cost: Human evaluation can be expensive and time-consuming, especially for large-scale evaluations
- Metric limitations: Automated metrics might not fully capture the nuances of RAG-specific relevance and might not always correlate well with human judgments
- Dynamic contexts: The relevance of a document can change depending on the other documents that are retrieved and the specific generation strategy used by the LLM
Next, let’s learn how to measure the impact of retrieval on LLM generation.
Measuring the impact of retrieval on LLM generation
Section titled “Measuring the impact of retrieval on LLM generation”In RAG systems, the quality of the generated response is heavily influenced by the information that’s retrieved. Good retrieval provides the necessary context and facts, while poor retrieval can lead to irrelevant or incorrect responses. Enhancing retrieval through better models and filtering improves overall performance, which is measured by precision, faithfulness, and user satisfaction.
Therefore, a crucial aspect of evaluation involves measuring the impact of retrieval on LLM generation. Let’s check out some of the key metrics and techniques.
Key metrics for evaluating retrieval impact
Section titled “Key metrics for evaluating retrieval impact”As mentioned, the quality of a response generated by an LLM is closely tied to the information it retrieves. Therefore, evaluating the impact of retrieval on the final response is crucial. This involves assessing how effectively the LLM leverages the retrieved context to generate answers that are accurate, relevant, and well-grounded. Let’s now examine some of the key metrics used in this evaluation:
- Groundedness/faithfulness:
Groundedness, also known as faithfulness, measures the extent to which the generated response is factually supported by the retrieved context. A grounded response should only contain information that can be inferred from the provided documents.
Some of the techniques for evaluating this metric are as follows:
- Human evaluation: Human annotators can directly assess the groundedness of each statement in the generated response by verifying whether it is supported by the retrieved context. This can involve binary judgments (grounded/not grounded) or more fine-grained ratings.
- Automated metrics:
- NLI: NLI models can be used to determine whether each sentence in the generated response is entailed by the retrieved context. We treat the concatenation of retrieved documents as the premise and each sentence in the response as the hypothesis. A high entailment score suggests that the sentence is grounded in the context. - QA-based: We can formulate questions based on the generated response and check whether a QA model can answer them correctly using the retrieved context as the source of information. A high answerability score indicates that the response is grounded. - Fact verification models: These models can be used to check whether each fact stated in the generated response is supported by the retrieved documents or external knowledge sources.
- Human evaluation: Human annotators can directly assess the groundedness of each statement in the generated response by verifying whether it is supported by the retrieved context. This can involve binary judgments (grounded/not grounded) or more fine-grained ratings.
- Answer relevance:
Answer relevance measures how well the generated response addresses the user’s query, given the retrieved context. Even if the retrieved context is imperfect, a good RAG system should still strive to provide a relevant and helpful answer.
Some of the techniques for evaluating this metric are as follows:
- Human evaluation: Human judges can assess the relevance of the generated response to the query while taking into account the limitations of the retrieved context. They can rate relevance on a Likert scale or provide comparative judgments (e.g., ranking multiple responses).
- Automated metrics:
- Query-answer similarity: We can measure the semantic similarity between the query and the generated response using embedding-based techniques (e.g., cosine similarity) or other similarity metrics. - Task-specific metrics: Depending on the specific application, we can use task-specific metrics. For example, in a QA scenario, we can measure the overlap between the generated answer and a gold-standard answer using metrics such as EM or F1 score. - Information retrieval metrics: We can treat the generated response as a retrieved document and evaluate its relevance to the query using traditional IR metrics such as precision, recall, or NDCG, assuming we have relevance judgments for query-answer pairs.
- Human evaluation: Human judges can assess the relevance of the generated response to the query while taking into account the limitations of the retrieved context. They can rate relevance on a Likert scale or provide comparative judgments (e.g., ranking multiple responses).
- Context utilization:
This aspect focuses on how effectively the LLM utilizes the retrieved context when generating the response. It goes beyond just measuring groundedness and assesses whether the LLM is integrating and synthesizing information from the context appropriately.
Some of the techniques for evaluating this metric are as follows:
- Human evaluation: Human annotators can assess the extent to which the LLM is using the retrieved context, identifying instances where the model is underutilizing or over-relying on the context
- Automated metrics:
- Attribution analysis: We can use techniques such as attention visualization or gradient-based attribution to identify which parts of the retrieved context the LLM is paying the most attention to during generation. - Context ablation: We can measure the change in the generated response when portions of the context are removed or modified. This can help with determining which parts of the context are most influential.
- Human evaluation: Human annotators can assess the extent to which the LLM is using the retrieved context, identifying instances where the model is underutilizing or over-relying on the context
As an example, let’s carry out a groundedness evaluation using an NLI model. For this example, we’ll use the Transformers library:
- We first run the following command, which installs the
transformerslibrary. This library provides tools for working with pre-trained transformer models such as NLI. We also import thepip install transformers torchfrom transformers import (AutoTokenizer, AutoModelForSequenceClassification)import torch - We then define sample data representing a query, a generated answer, and the
query = "What is the capital of France?"answer = "The capital of France is Paris. It is a global center for art, fashion, gastronomy, and culture."context = """Paris is the capital city of France. It is situated on the River Seine, in northern France.Paris has an area of 105 square kilometers and a population of over 2 million people.France is a country located in Western Europe."""
- We load a pre-trained NLI model and its corresponding tokenizer. Here, we’re using the
roberta-large-mnlimodel, which is a RoBERTa model that’s been fine-tuned on themodel_name = "roberta-large-mnli"tokenizer = AutoTokenizer.from_pretrained(model_name)model = \AutoModelForSequenceClassification.from_pretrained(model_name) - We then define a function,
calculate_claim_groundedness, that calculates the entailment score for a single claim (a sentence from the generated answer) givendef calculate_claim_groundedness(context, claim):"""Calculates the entailment score for a single claim given the context."""inputs = tokenizer(context, claim, truncation=True,return_tensors="pt")outputs = model(**inputs)probs = torch.softmax(outputs.logits, dim=1)entailment_prob = probs[0][2].item() # Assuming label 2 corresponds to entailmentreturn entailment_prob - We also define a function,
calculate_groundedness, to calculate the overall groundedness score for the entire generated answer. It splits the answer into sentences, calculates the entailment score for each sentence, and then averagesdef calculate_groundedness(context, answer):"""Calculates the overall groundedness score for the generated answer."""claims = answer.split(". ") # Simple sentence splittingif not claims:return 0claim_scores = []for claim in claims:if claim:score = calculate_claim_groundedness(context, claim)claim_scores.append(score)return (sum(claim_scores) / len(claim_scores)if claim_scoreselse 0) - Finally, we calculate and print the overall groundedness score for the
groundedness_score = calculate_groundedness(context, answer)print(f"Groundedness Score: {groundedness_score:.3f}")
Challenges in measuring the impact of retrieval
Section titled “Challenges in measuring the impact of retrieval”Now that we’ve seen an example, let’s take a look at some of the key challenges that are encountered in the evaluation process of RAG systems:
- Defining the ground truth: Determining the ground truth for groundedness and answer relevance can be challenging and subjective, especially when dealing with complex or nuanced queries
- Attributing errors: It can be difficult to determine whether an error in the generated response is due to poor retrieval, limitations of the LLM, or a combination of both
- Computational cost: Evaluating the impact of retrieval on generation can be computationally expensive, especially when using bigger LLMs or performing human evaluations
- Inter-annotator agreement: When using human evaluation, ensuring high inter-annotator agreement on subjective judgments such as groundedness and relevance can be difficult
While evaluating the individual components of a RAG system (retrieval and generation) is important, it is also crucial to assess the system’s overall performance in an end-to-end manner. Let’s see that next.
End-to-end evaluation of RAG systems in LLMs
Section titled “End-to-end evaluation of RAG systems in LLMs”While evaluating the individual components of a RAG system (retrieval and generation) is important, it is also crucial to assess the system’s overall performance in an end-to-end manner. End-to-end evaluation considers the entire RAG pipeline, from the initial user query to the final generated response, providing a holistic view of the system’s effectiveness.
Let’s take a look at some holistic metrics:
- Task success: For task-oriented RAG systems (e.g., QA, dialogue), we can measure the overall task success rate. This involves determining whether the generated response completes the intended task successfully.
Here are some techniques for evaluating this metric:
- Automated evaluation: For some tasks, we can automatically evaluate task success. For example, in QA, we can check whether the generated answer matches the gold-standard answer.
- Human evaluation: For more complex tasks, human evaluation might be necessary to judge whether the RAG system successfully achieved the task’s goal.
- Automated evaluation: For some tasks, we can automatically evaluate task success. For example, in QA, we can check whether the generated answer matches the gold-standard answer.
- Answer quality: This metric assesses the overall quality of the generated response while considering factors such as accuracy, relevance, fluency, coherence, and groundedness.
Here are some techniques for evaluating this metric:
- Human evaluation: Human judges can rate the overall quality of the generated response on a Likert scale or by using more detailed rubrics that consider multiple quality dimensions
- Automated metrics: While answer quality can be challenging to fully automate, some aspects of answer quality can be approximated using metrics such as the following:
- ROUGE/BLEU: Measures the overlap between the generated response and a reference answer (if available) - Perplexity: Measures how well the LLM predicts the generated response (lower perplexity is generally better) - Groundedness metrics (NLI, QA-based): Assesses the factual consistency of the response with the retrieved context - Relevance metrics: Measures the similarity between the query and the generated response
- Human evaluation: Human judges can rate the overall quality of the generated response on a Likert scale or by using more detailed rubrics that consider multiple quality dimensions
Now, let’s look at some ways we can evaluate RAG systems.
Evaluation strategies
Section titled “Evaluation strategies”Evaluation strategies for RAG systems can be broadly categorized into black-box evaluation, glass-box evaluation, component-wise evaluation, and ablation studies, each offering distinct insights into system performance.
In black-box evaluation, the entire RAG system is treated as a single unit. Evaluators provide input queries and assess only the final generated responses without analyzing the intermediate retrieval or generation steps. This approach is particularly useful for measuring overall system performance and comparing different RAG implementations without having to delve into their internal mechanisms.
Glass-box evaluation, in contrast, involves a detailed examination of the internal workings of the RAG system. This method analyzes the retrieved context, the LLM’s attention patterns, and the intermediate generation steps. By dissecting these elements, glass-box evaluation helps identify system strengths and weaknesses, pinpoint error sources, and provide insights for targeted improvements.
A more granular approach is component-wise evaluation, which assesses the retrieval and generation components separately. Retrieval performance is typically measured using metrics such as Recall@k and NDCG, while the quality of the generated text is evaluated using metrics such as BLEU and ROUGE or through human judgment based on a fixed set of retrieved documents. This method is particularly effective in isolating and diagnosing performance issues within individual components.
Finally, ablation studies offer a systematic way to measure the impact of different components on overall system effectiveness. By removing or modifying specific parts of the RAG system – such as testing performance with and without retrieval or swapping different retrieval and generation models – researchers can better understand how each component contributes to the system’s functionality and overall success.
Challenges in end-to-end evaluation
Section titled “Challenges in end-to-end evaluation”Evaluating RAG systems holistically presents several challenges, particularly when assessing complex interactions between retrieval and generation components. Some of these challenges are as follows:
- Defining the ground truth: For open-ended tasks or tasks that involve generating complex responses, defining the ground truth can be difficult or even impossible
- Attributing errors: When the system generates an incorrect or low-quality response, it can be challenging to determine whether the error originated in the retrieval or generation component
- Computational cost: End-to-end evaluation can be computationally expensive, especially when using bigger LLMs or performing human evaluations on a large scale
- Reproducibility: Ensuring reproducibility can be difficult due to the complex interactions between the retrieval and generation components and the potential use of non-deterministic retrieval mechanisms or stochastic decoding strategies during generation, which may lead to variations in outputs across runs even with the same inputs.
Next, let’s shift focus to the role of human evaluation in assessing LLM-based RAG systems, which complements automated metrics by capturing nuanced aspects such as relevance, coherence, and factual accuracy.
Human evaluation techniques for LLM-based RAG
Section titled “Human evaluation techniques for LLM-based RAG”While automated metrics provide valuable insights, human evaluation remains the gold standard for assessing the overall quality and effectiveness of RAG systems. Human judgment is particularly crucial for evaluating aspects that are difficult to capture with automated metrics, such as the nuanced relevance of retrieved information, the coherence and fluency of generated text, and the overall helpfulness of the response in addressing the user’s need.
Human evaluators can assess various aspects of RAG system performance:
- Relevance: How relevant is the generated response to the user’s query? Does it address the specific information needed expressed in the query?
- Groundedness/faithfulness: Is the generated response factually supported by the retrieved context? Does it avoid hallucinating or contradicting the provided information?
- Coherence and fluency: Is the generated response well-structured, easy to understand, and written in grammatically correct and natural-sounding language?
- Helpfulness: Does the response provide a useful and satisfactory answer to the user’s query, considering the limitations of the retrieved context?
- Context utilization: How effectively does the system utilize the retrieved context in generating the response? Does it integrate and synthesize information from multiple sources appropriately?
- Attribution: Does the system provide clear citations or links to the sources in the retrieved context that support the generated claims?
Several methods can be used for the human evaluation of RAG systems:
- Rating scales (Likert scales): Annotators rate different aspects of the generated response (e.g., relevance, groundedness, fluency) on a numerical scale, such as 1 to 5, where 1 represents poor quality and 5 represents excellent quality:
- Advantages: Simple to implement and easy to collect and aggregate data
- Disadvantages: Can be subjective, susceptible to annotator bias, and may not capture nuanced differences
- Advantages: Simple to implement and easy to collect and aggregate data
- Comparative evaluation (ranking/best–worst scaling): Annotators are presented with multiple RAG system outputs for the same query and asked to rank them based on their overall quality or specific criteria.
- Best–worst scaling: A specific form of comparative evaluation where annotators choose the best and worst options from a set of outputs:
- Advantages: More reliable than absolute ratings and captures relative differences between systems effectively
- Disadvantages: More complex to implement than rating scales and requires more effort from annotators
- Advantages: More reliable than absolute ratings and captures relative differences between systems effectively
- Task-based evaluation: Annotators are asked to complete a specific task using the RAG system, such as finding an answer to a question, writing a summary, or engaging in a conversation. The quality of the RAG system is assessed based on the annotators’ ability to complete the task successfully and their satisfaction with the system’s performance:
- Advantages: More realistic and user-centered and provides a direct measure of the system’s utility
- Disadvantages: More complex to design and implement and can be time-consuming and expensive
- Advantages: More realistic and user-centered and provides a direct measure of the system’s utility
- Free-form feedback: Annotators provide open-ended feedback on the strengths and weaknesses of the RAG system’s output:
- Advantages: Captures detailed insights and suggestions for improvement and can uncover unexpected issues
- Disadvantages: More difficult to analyze and quantify and can be subjective and inconsistent
- Advantages: Captures detailed insights and suggestions for improvement and can uncover unexpected issues
Best practices for human evaluation
Section titled “Best practices for human evaluation”To ensure the reliability and fairness of human evaluation, consider the following best practices:
- Clear guidelines: Provide annotators with clear and detailed guidelines that define the evaluation criteria and annotation procedures
- Training and calibration: Train annotators on the task and calibrate their judgments using example annotations
- Inter-annotator agreement: Measure inter-annotator agreement (e.g., using Cohen’s Kappa or Fleiss’ Kappa) to ensure the reliability of the annotations
- Pilot studies: Conduct pilot studies to refine the evaluation protocol and identify potential issues before launching a large-scale evaluation
- Multiple annotators: Use multiple annotators for each item to mitigate individual biases and improve the robustness of the evaluation
- Diverse annotator pool: Recruit a diverse pool of annotators to capture a wider range of perspectives and reduce potential biases
- Quality control: Implement mechanisms for identifying and correcting errors or inconsistencies in the annotations
Challenges in human evaluation
Section titled “Challenges in human evaluation”Evaluating the performance of RAG systems built on LLMs poses a unique set of challenges. Here, we outline the key obstacles encountered in conducting reliable, consistent, and meaningful human evaluations of such systems:
- Cost and time: Human evaluation can be expensive and time-consuming, especially for large-scale evaluations
- Subjectivity: Human judgments can be subjective and influenced by individual preferences and biases
- Annotator training and expertise: Ensuring that annotators are properly trained and have the necessary expertise to assess RAG system performance can be challenging
- Reproducibility: Replicating human evaluations can be difficult due to the inherent variability in human judgments
In the next section, we’ll explore the role of standardized benchmarks and datasets in evaluating RAG systems, highlighting key benchmarks, evaluation criteria, and challenges.
Benchmarks and datasets for RAG evaluation
Section titled “Benchmarks and datasets for RAG evaluation”Standardized benchmarks and datasets play a crucial role in driving progress in RAG research and development. They provide a common ground for evaluating and comparing different RAG systems, facilitating the process of identifying best practices and tracking advancements over time.
Let’s look at some key benchmarks and datasets:
- Knowledge Intensive Language Tasks (KILT): A comprehensive benchmark for evaluating knowledge-intensive language tasks, including QA, fact-checking, dialogue, and entity linking:
- Data source: Based on Wikipedia, with a unified format for all tasks
- Strengths: Provides a diverse set of tasks, allows both retrieval and generation to be evaluated, and includes a standardized evaluation framework
- Limitations: Primarily based on Wikipedia, which might not reflect the diversity of real-world knowledge sources
- Data source: Based on Wikipedia, with a unified format for all tasks
- Natural Questions (NQ): A large-scale QA dataset collected from real user queries that is sent to the Google search engine:
- Data source: Contains pairs of questions and Wikipedia pages that contain the answer
- Strengths: Realistic queries, large scale, and includes both short and long answer annotations
- Limitations: Since it primarily focuses on factoid questions, it might not be suitable for evaluating more complex reasoning or generation tasks
- Data source: Contains pairs of questions and Wikipedia pages that contain the answer
- TriviaQA: A challenging QA dataset containing question-answer-evidence triples:
- Data source: Collected from trivia enthusiasts, it includes both web and Wikipedia evidence documents
- Strengths: More difficult than NQ; it requires reading and understanding multiple evidence documents
- Limitations: Primarily focused on factoid questions, the writing style of trivia questions might not be representative of real-world user queries
- Data source: Collected from trivia enthusiasts, it includes both web and Wikipedia evidence documents
- Explain Like I’m Five (ELI5): A dataset of questions and answers from the Reddit forum Explain Like I’m Five, where users ask for simplified explanations of complex topics:
- Data source: Collected from Reddit, it includes questions and answers on a wide range of topics
- Strengths: Focuses on long-form, explanatory answers, making it suitable for evaluating the generation capabilities of RAG systems
- Limitations: The quality and accuracy of answers can vary and might require careful filtering or annotation
- Data source: Collected from Reddit, it includes questions and answers on a wide range of topics
- ASQA: The first long-form QA dataset that unifies ambiguous questions:
- Data source: The dataset is built from scratch by combining multiple ambiguous questions
- Strengths: Can help evaluate long-form QA tasks
- Limitations: Building a high-quality dataset from scratch can be challenging
- Data source: The dataset is built from scratch by combining multiple ambiguous questions
- Microsoft Machine Reading Comprehension (MS MARCO): A large-scale dataset for machine reading comprehension and QA:
- Data source: Contains real anonymized user queries that are sent to the Bing search engine, along with human-generated answers and relevant passages.
- Strengths: It provides a large-scale, diverse set of queries and answers that includes both passage-level and full-document annotations
- Limitations: Primarily focused on extractive QA, it might not be ideal for evaluating the generation capabilities of RAG systems
- Data source: Contains real anonymized user queries that are sent to the Bing search engine, along with human-generated answers and relevant passages.
- Stanford Question Answering Dataset (SQuAD): A widely used dataset for reading comprehension consisting of questions posed by crowdworkers on a set of Wikipedia articles:
- Data source: Contains question-paragraph-answer triples, where the answer is a span of text in the paragraph
- Strengths: A large-scale, well-established benchmark for reading comprehension
- Limitations: Primarily focused on extractive QA, it might not be suitable for evaluating the generation capabilities of RAG systems
- Data source: Contains question-paragraph-answer triples, where the answer is a span of text in the paragraph
As an example, let’s illustrate how to use the KILT dataset to evaluate a RAG system. We’ll use the KILT library in Python to do so:
- Run the following code to install the kilt library and import the
pip install kilt==0.5.5from kilt import kilt_utils as utilsfrom kilt import retrievalfrom kilt.eval import answer_evaluation, provenance_evaluation
- Next, download a specific KILT task, such as the Wizard of Wikipedia (
# Download the WoW datasetutils.download_dataset("wow")
- Then, load the downloaded dataset
# Load the datasetwow_data = utils.load_dataset("wow", split="test")
- Define a dummy RAG function that simulates the behavior of a RAG retrieval component. For demonstration purposes, it simply returns a fixed set of Wikipedia pages for each query. In a real-world scenario, you would replace this with your actual RAG
class DummyRetriever(retrieval.base.Retriever):def __init__(self, k=1):super().__init__(num_return_docs=k)self.k = k# retrieve some Wikipedia pages (or the entire dataset)# based on the querydef retrieve(self, query, start_paragraph_id=None):# Dummy retrieval: return the same set of pages for each querydummy_pages = [{"wikipedia_id": "534366","start_paragraph_id": 1,"score": self.k,"text": "Paris is the capital of France."},{"wikipedia_id": "21854","start_paragraph_id": 1,"score": self.k-1,"text": "The Mona Lisa was painted by Leonardo da Vinci."},{"wikipedia_id": "37267","start_paragraph_id": 1,"score": self.k-2,"text": "Mount Everest is the highest mountain in the world."}]return dummy_pages[:self.k]# Example usageretriever = DummyRetriever(k=2)
- Define a dummy RAG generation function that simulates the behavior of a RAG generation component. For demonstration purposes, it simply returns a fixed answer for each query. In a real-world scenario, you would replace this with your actual LLM-based
def dummy_generate(query, retrieved_pages):"""Simulates RAG generation by returninga fixed answer for each query."""if "capital of France" in query:return "Paris"elif "Mona Lisa" in query:return "Leonardo da Vinci"elif "highest mountain" in query:return "Mount Everest"else:return "I don't know."
- Run the dummy RAG pipeline on the dataset, using the dummy retrieval and generation functions, and collect the
predictions = []for element in wow_data[:10]:query = element["input"]retrieved_pages = retriever.retrieve(query)# Add provenance information to the elementelement["output"] = [{"provenance": retrieved_pages}]generated_answer = dummy_generate(query, retrieved_pages)# Add the generated answer to the elementelement["output"][0]["answer"] = generated_answerpredictions.append(element)
- Finally, evaluate the generated predictions using the KILT evaluation functions. Both the retrieval performance (using
provenance_evaluation) and the answer quality (usinganswer_evaluation)kilt_scores = {}kilt_scores["provenance_MAP@k"] = \provenance_evaluation.get_map_at_k(predictions, verbose=False)kilt_scores["answer_EM"] = answer_evaluation.get_exact_match(predictions, verbose=False)kilt_scores["answer_F1"] = answer_evaluation.get_f1(predictions, verbose=False)kilt_scores["answer_ROUGE-L"] = answer_evaluation.get_rouge_l(predictions, verbose=False)print(kilt_scores)
This code provides a basic example of how to use the KILT framework to evaluate a RAG system. In a real-world scenario, you would replace the dummy retrieval and generation functions with your actual RAG implementation and use a larger portion of the dataset for evaluation. You can adapt this example to other KILT tasks by downloading and loading the corresponding datasets.
Here are a few things to consider when choosing benchmarks and datasets:
- Task alignment: Select benchmarks and datasets that align with the specific task you are evaluating (e.g., QA, dialogue, summarization)
- Knowledge domain: Consider the knowledge domain covered by the benchmark. Some benchmarks are based on general knowledge (e.g., Wikipedia), while others focus on specific domains (e.g., scientific literature, medical records)
- Retrieval setting: Choose benchmarks that are appropriate for the retrieval setting you are using (e.g., open-domain retrieval, closed-domain retrieval, passage retrieval, document retrieval)
- Generation requirements: Consider the type of generation required by the task (e.g., extractive versus abstractive, short versus long answers)
- Dataset size and quality: Ensure that the dataset is large enough to provide statistically significant results and that the data is of high quality (e.g., accurate annotations and well-formed questions)
- Evaluation metrics: Check what evaluation metrics are used by the benchmark and whether they are appropriate for your specific evaluation goals
Summary
Section titled “Summary”In this chapter, we discussed a wide range of metrics for evaluating both retrieval quality and generation performance, including traditional information retrieval metrics such as Recall@k, Precision@k, MRR, and NDCG, as well as more RAG-specific metrics such as groundedness, faithfulness, and answer relevance. We explored various techniques for measuring these metrics, including automated methods based on NLI and QA models, and human evaluation approaches using rating scales, comparative judgments, and task-based assessments.
We emphasized the crucial role of human evaluation in capturing the nuanced aspects of RAG performance that are difficult to assess with automated metrics alone. We also discussed best practices for designing and conducting human evaluations, such as providing clear guidelines, training annotators, measuring inter-annotator agreement, and conducting pilot studies. We need to keep in mind that tradeoffs between automated and human evaluation will be important in real-world deployments.
Furthermore, we explored widely used benchmarks and datasets for RAG evaluation, including KILT, NQ, TriviaQA, ELI5, ASQA, MS MARCO, and SQuAD, highlighting their strengths and limitations and providing guidance on selecting appropriate benchmarks for different tasks and domains.
As we conclude, it is clear that evaluating RAG systems is a complex and evolving field. The development of more sophisticated evaluation metrics, the creation of more diverse and challenging benchmarks, and the refinement of human evaluation methodologies will continue to be crucial for driving progress in RAG research and development.
In the next chapter, we’ll explore agentic patterns in LLMs, focusing on how LLMs can perform tasks involving reasoning, planning, and decision-making autonomously using advanced retrieval and generation techniques.
Get This Book's PDF Version and Exclusive Extras
|