DataPro is a weekly, expert-curated newsletter trusted by 120k+ global data professionals. Built by data practitioners, it blends first-hand industry experience with practical insights and peer-driven learning.Make sure to subscribe here so you never miss a key update in the data world. IntroductionBuilding a Retrieval-Augmented Generation (RAG) system is only the first step; ensuring that it retrieves the right information and generates accurate, context-grounded responses is equally important. RAG evaluation helps identify weaknesses across both the retriever and generator by measuring retrieval relevance, ranking quality, answer relevance, and faithfulness.In this article, you’ll explore the two core areas of RAG evaluation—retrieval evaluation and response evaluation—and understand how the RAG TRIAD provides a holistic framework for assessing query, context, and response quality. You’ll also learn how key retrieval metrics such as precision, recall, F1 score, Mean Reciprocal Rank (MRR), Mean Average Precision (MAP), and P@K can be used to evaluate and improve the effectiveness of RAG systems.Retrieval evaluation and response evaluationRetrieval evaluation and response evaluation are the two core components of the RAG system evaluation framework; let’s consider them in detail:Retrieval evaluation focuses on whether the model can retrieve accurate and relevant context during a query. It assesses whether the content found by the system from the knowledge base or external resources is closely related to the user’s query and can effectively support subsequent generation. Key metrics in retrieval evaluation include context relevance, retrieval precision, and recall. Retrieval evaluation often emphasizes assessing the quality of the embedding model (i.e., the core component of the retriever).Response evaluation, on the other hand, focuses on the quality of the answers generated by the model based on the retrieved content. Its goal is to ensure that the generated content is not only relevant to the user’s query but also faithful to the provided context information. Response evaluation typically looks at whether the generated answer is accurate, whether it contains “hallucinations”, and whether it is consistent with the context. Key metrics for response evaluation include answer relevance and faithfulness, which help teams better assess whether the generated answers meet expectations. Response evaluation often emphasizes assessing the level of the generative model (i.e., the core component of the generator).When an evaluation dataset is available, meaning some questions already have labeled standard answers and sources, for retrieval evaluation, more precise quantitative metrics such as precision and recall can be used; whereas for response evaluation, it is usually difficult to quantify answer quality with specific scores, and subjective factors play a relatively larger role.RAG TRIAD – holistic evaluationAt present, one evaluation approach is to assess the overall effectiveness of the Retrieval-Augmented Generation (RAG) process. This method is known as the RAG TRIAD framework.Figure 9.4: A flowchart showing relationships among query context and responseIn the RAG TRIAD framework, evaluation is focused on three core aspects: query, context, and response. These three aspects are measured by the following three key evaluation metrics:Contextual relevancy: An indicator for retrieval evaluation, mainly assessing whether the text blocks returned by the retriever are relevant to the user’s query and helpful for generating an ideal answer. This involves selecting hyperparameters such as block size, Top-K value, and embedding model. An efficient embedding model can ensure that the retrieved text blocks are semantically similar to the user’s query, while an appropriate combination of block size and Top-K value helps pick out the most critical information from the knowledge base.Faithfulness: A key metric in response evaluation, used to measure whether the generated answer is based on the provided context, i.e., whether the answer has a factual basis and does not contain “hallucinations.” This metric is often closely related to model selection. If the current model cannot generate accurate answers based on the retrieved context, it may be necessary to replace the model or fine-tune it.Answer relevancy: Also a response evaluation metric, it focuses on the degree to which the generated answer is relevant to the user’s query. As the model’s reasoning ability improves, this metric increasingly depends on the quality of the retrieval results rather than the capability of the model itself. If the score is low, it may be necessary to improve the retrieval process or optimize prompts to enhance the model’s ability to generate more relevant answers.The RAG TRIAD framework provides a reference-free evaluation system. Even without readymade evaluation datasets, it is still possible to evaluate the outputs of the RAG system from the above three aspects. Therefore, this evaluation system has a certain degree of generality and flexibility. Each metric corresponds to specific components or hyperparameter settings in the RAG process. If a RAG system scores highly in these three areas, it indicates that its components or hyperparameter settings are appropriate; otherwise, further adjustment and optimization are needed.This reference-free evaluation system saves the trouble of creating evaluation datasets, but at the same time, it also means that the evaluation metrics themselves have a certain degree of non-determinism. Therefore, the final performance of the RAG system should not only depend on these evaluation metrics but also require practical confirmation from business teams and end users.Retrieval evaluation metricsRetrieval evaluation is critical for RAG systems because it directly affects the quality of the context adopted by the generator, thereby indirectly impacting the relevance and accuracy of the final generated response.MTEB provides a comprehensive benchmarking platform for evaluating text embedding models, covering tasks such as text similarity, text classification, and retrieval. MTEB can effectively measure the performance of text embedding models across multiple tasks. Therefore, MTEB is regarded as a means to evaluate the embedding models used in the retrieval process and is suitable for local task evaluation.However, in practical RAG projects, retrieval evaluation is not limited to the direct assessment of the embedding model, but more broadly targets the entire retriever component, evaluating whether it can effectively identify and prioritize the most relevant documents. This process not only depends on the quality of the embedding model but is also influenced by factors such as chunking strategy and indexing strategy.Retrieval evaluation mainly uses metrics such as precision, recall, Mean Reciprocal Rank (MRR), and Mean Average Precision (MAP). Before introducing these metrics, it should be noted that the text chunks retrieved by the retriever are usually not singular but rather one or more (this is a configurable parameter). In addition, the context text chunks relevant to a specific query are often more than one, and all relevant text chunks should be listed in the evaluation dataset.PrecisionPrecision measures how many of the retrieved text blocks are relevant to the query, i.e., the proportion of relevant text blocks among all retrieved text blocks. Precision aims to answer the question: “Of all text blocks retrieved, how many are truly relevant?” The calculation formula for precision is as follows: Number of relevant text chunks retrievedPrecision = Total number of text chunks retrievedFigure 9.5: Formula for precisionSuppose a scientific literature information retrieval system returns 15 text blocks, of which 12 are directly related to the query, then the precision is 80%. High precision means that learners can obtain the needed information more quickly without being distracted by irrelevant text blocks.Tip from Lewis: “In the evaluation dataset, if the system can find the relevant ‘text chunks,’ it is considered a successful retrieval.However, depending on the task, we may also need to locate specific pages, paragraphs, sentences, or even the original document.”High precision also means that the retrieved content is more accurate, effectively reducing unnecessary information display. This metric is especially important in avoiding the presentation of irrelevant information, in fields such as medicine and law, high precision can effectively prevent the spread of misleading information.RecallRecall measures the comprehensiveness with which the system retrieves relevant text blocks, that is, the proportion of relevant text blocks retrieved by the system to all relevant text blocks in the database. Recall aims to answer the following question: Of all the relevant text blocks, how many did the system successfully retrieve?The formula for calculating recall is as follows: Number of relevant text chunks retrievedRecall = Total number of relevant text chunksFigure 9.6: The ratio of retrieved relevant documents to all relevant documentsAssume that in a legal document retrieval system, the database contains 50 documents relevant to the query topic, and the system successfully retrieves 40 of them. Then the recall is 80%. In scientific research, high recall can ensure that researchers do not miss key references.High recall is critical for avoiding the omission of crucial information. If recall is low, the model may generate incomplete answers or make mistakes due to missing key information. For example, in legal document retrieval, missing relevant information may affect the completeness of case analysis.F1 scoreIn a RAG system, improving precision often leads to a decrease in recall, and vice versa. Therefore, to achieve optimal retrieval performance, it is usually necessary to find a balance between precision and recall. This balance is typically quantified using the F1 score, which is the harmonic mean of precision and recall, used to find the optimal point suitable for the specific application requirements.The formula for calculating the F1 score is as follows:2 × (Precision × Recall) Precision + RecallFigure 9.7: Equation for F1 score in showing the harmonic mean of precision and recall valuesMean reciprocal rankMean Reciprocal Rank (MRR) is an evaluation metric for the efficiency of retrieval systems, with a special focus on the rank of the first relevant text chunk. MRR helps us gauge whether a RAG system can quickly return the first relevant text chunk, and its value has a direct impact on user experience. The higher the MRR value, the faster the system is able to find the first suitable answer.The formula for calculating MRR is as follows:Figure 9.8: Mathematical formula for MRR showing the average inverse rank over Q queriesWhere Q represents the total number of queries. rankq denotes the position of the first relevant text chunk for q query q. The formula requires taking the reciprocal of the ranking of the first relevant text chunk, which means that MRR only considers the position of the first relevant text chunk returned for each query. With this calculation method, the higher the rank of the text chunk, the greater the reciprocal of rankq, and thus the greater the MRR.MRR is particularly important in question-answering or information retrieval systems, as it focuses on the ability to quickly obtain the first relevant answer. For example, in a customer service Q&A system, if a user poses a question and the system can select and respond with an answer within the top 3 results, the MRR value will be relatively high, indicating that the system has stronger speed and accuracy in responding to user needs.Mean average precisionMean Average Precision (MAP) is a precision metric evaluated across multiple queries. It not only considers the precision of the retrieval results but also emphasizes the importance of document ranking. MAP evaluates retrieval effectiveness by calculating the precision at various ranks for each query, ensuring that important relevant documents are positioned higher, thereby optimizing the user search experience.The calculation formula for MAP is as follows: 𝑀𝑀𝑀𝑀𝑀𝑀 𝑀 q Figure 9.9: MAP equals the mean of Average Precision over Q queriesHere, Q denotes the total number of queries. Average Precision is the mean precision calculated for each query, taking into account the order of relevant documents.MAP is very useful in systems where the quality of rankings is important, such as search engines, and is often used in product recommendation systems on e-commerce platforms.P@KP@K measures the precision of the top K retrieved results, ensuring that as many relevant pieces of information as possible are included in the first few results displayed. Number of relevant documents in the top K search resultsP@K = Figure 9.10: Mathematical formula for P@KHere, the numerator represents the number of relevant documents among the top K results, and the K in the denominator is a fixed return count.P@K is especially useful in scenarios where users are particularly concerned with the top few results. For example, in a news search system, P@K can ensure that the first few news items retrieved are highly relevant to the query topic, thereby improving reading efficiency.ConclusionEvaluating a RAG system requires looking beyond the final generated answer. Retrieval quality determines whether the model receives useful context, while response evaluation measures whether that context is transformed into relevant and faithful answers. Frameworks such as the RAG TRIAD help connect these two stages by assessing contextual relevancy, faithfulness, and answer relevancy.Metrics such as precision, recall, F1 score, MRR, MAP, and P@K provide practical ways to measure retrieval performance from different perspectives, including relevance, coverage, and ranking quality. By selecting metrics that align with your application requirements and combining quantitative evaluation with feedback from business teams and end users, you can identify weaknesses in the RAG pipeline and make more informed decisions about retrieval strategies, model selection, prompts, and system configuration.This article is an excerpt from RAG from First Principles. To explore RAG in greater depth—from its foundational concepts to the design, implementation, evaluation, and optimization of retrieval-augmented generation systems—read the full book on Packt: Author BioJia Huang is a Lead Research Engineer at A*STAR (Agency for Science, Technology and Research), Singapore, where his work focuses on NLP, large language models, and applied AI engineering. With over twenty years of experience leading large-scale AI and data projects across government, finance, healthcare, and e-commerce, he brings an unusually practical lens to technically rigorous subjects. In recent years, his research has primarily focused on NLP pre-trained large models and FinTech applications. He is the author of six bestselling technical books, including Hands-on AI Agent Development for Large Model Applications selected as one of JD Best Books of 2024, and GPT: How Large Models Are Built, named CSDN's Most Influential IT Book of 2023. His online RAG engineering course has been completed by over 10,000 students.
Read more