Please note, this video is best viewed with 1080p quality selected. You can use the player bar to navigate to chapters within the video.
Chapters
0:00 Introduction
0:49 Evaluation Options
2:45 Techniques and Tools
5:13 Generating Metrics
11:19 Acting on Your Results
15:23 Feedback and Iteration
16:23 Learn More
Resources
- Check out the Atlas Vector Search Quick Start page.
- Follow along with a real world example of evaluating a RAG Application in this video, in this blog, and on GitHub.
- For more on selecting an embedding model, check out this blog.
- Check out the ragas-wikiqa dataset on Hugging Face.
- Explore the Ragas Getting Started page.
Video Transcript (English)
Hi, and welcome to MongoDB Learning Bytes. I'm Apoorva Joshi, a senior AI developer advocate at MongoDB. If you've ever built a generative AI application, you understand that evaluating your results is crucial for ensuring your application's effectiveness and reliability. This means using appropriate metrics and techniques to assess the performance of your application on its intended functionality.
In this Learning Byte, we'll discuss some common methods used to evaluate GenAI apps with an emphasis on RAG systems. This includes different evaluation metrics and how to interpret them, as well as some options for improving performance based on the results.
While traditional machine learning models have well-defined metrics such as mean squared error, precision, and recall, evaluation methods for LLM applications are still emerging. This is largely due to the fact that the outputs are not easily verifiable because of multiple moving parts or complexity of natural language outputs.
While there is no prescribed way to evaluate LLM applications today, some guiding principles are emerging that allow us to generate metrics for objective evaluation. Let's take a look at a few methods, each with its own pros and cons. The most obvious option to evaluate GenAI outputs is human evaluation. Going this route usually leads to more reliable results than other methods, but it does not scale well.
After all, one of the key advantages of LLMs is that they can access, utilize, and generate vast amounts of content. So it is often difficult for humans to work through all of the outputs. To avoid a human bottleneck, an alternative approach employs an LLM to evaluate the results from another LLM. This is typically done by using a more sophisticated and often more expensive LLM to review output from the primary LLMs used in the application.
As mentioned, this method scales well but also increases costs and probably should be checked for accuracy on its own outputs. This type of evaluation is what we'll explore in this Learning Byte. There are other specialized methods available, too, like training specific models for evaluation. This method can yield the most reliable results, and it also scales well.
However, it does require a large data set for model training and data science expertise to train and manage the models.
Before we begin calculating metrics, let's walk through the general steps for evaluating your RAG system effectively. Since RAG applications have a lot of components that can be adjusted, we recommend focusing on individual components and evaluating them in turn, then assessing their performance as a whole. RAG systems typically have two main components, retrieval and generation.
We'll start with evaluating the retrieval component of our RAG app by experimenting with different embedding models. There are additional variables that you can experiment with in order to improve retrieval performance, but we will discuss these options towards the end of the learning pipe. For now, we'll focus on the various embedding models offered by vendors, which are always evolving. By using different models to embed the same data, we'll be able to see how the embedding models impact retrieval performance.
Once we select an embedding model, we will move on to testing the LLMs that generate responses. For this step, we will have the LLMs generate answers to questions in our data set and then compare them to ground truth answers, which are factually correct statements that represent an ideal response. Finally, we'll use the winning embedding model and LLM together to assess the app's performance. This round of testing will focus on the overall performance of the app relative to the prompt and the ground truth provided.
By evaluating each component of the app, then evaluating the overall performance of the applications. With those components working together, we will have metrics to help us diagnose and improve our application. This allows us to objectively evaluate performance and measure the effectiveness of any changes made. For instance, if we notice that there is an issue with performance on a particular query, we use the metrics to determine if the retriever is pulling the wrong context for the topic if the LLM struggles with that topic or if the context we provided is insufficient.
This method can also be very helpful in addressing hallucinations, which are one of the most common issues with GenAI content. This term refers to situations where AI generates coherent but inaccurate information. Hallucinations can erode trust in the application and are often difficult to identify and address.
Now that we've discussed the steps that we will use to evaluate our app's performance, we can shift our focus to collecting evaluation metrics.
Let's briefly talk about the tool we will use. Ragas is an open-source framework for evaluating RAG applications that include support for metrics such as context precision and recall, and integrates well with frameworks like LongChain and LlamaIndex. This Learning Byte focuses on using Ragas for evaluation, but alternative frameworks are available. Next up, we'll need some data to run through the application.
We recommend curating a test data set based on your best guess of the type of queries users will make to your application. Even a data set of 40 to 50 samples can work well to get started. This data set should consist of at least two fields, the query and the ground truth, which is the ideal answer we would like from the LLM. The ragas-wikiqa data set on Hugging Face is a good example of this format and is linked below. This data set features a question field, a correct answer field, which serves as the ground truth, and a context field that mimics the information that will form the knowledge base of your RAG app. In reality, you'll have to curate the information that forms the knowledge base for your RAG app on your own.
To prepare for evaluation, chunk embed and ingest the context into MongoDB Atlas to build the knowledge base for the RAG app. Repeat this for each embedding model to be tested and save the embeddings from each model separately in their own collection for simplicity. To evaluate the retrieval component of our RAG app, we embed each question in our test data set and retrieve the most relevant documents from MongoDB using semantic search. The original queries and ground truth answers from the test data set and the retrieve context are passed to Ragas to score performance.
Ragas uses an LLM behind the scenes to calculate the context precision and context recall on our test data set. This process is repeated for each embedding model we want to evaluate. Context precision represents the ability of the retriever to order retrieved items by relevance to the ground truth. Context recall represents how aligned the context retrieved is to the ground truth.
You can think of precision as, how well did the retrieval rank the returned content and recall as whether it found the best matches? Both of these metrics range from 0 to 1, with 1 representing the best performance. When reviewing the scores for each metric, you won't always have one model score highest on both metrics. In those cases, you'll need to consider the goal of your application and interpret the metrics as they apply to your app to decide on the most appropriate model.
This will come down to whether you prefer the retriever find the best chunks or do a better job of ranking the retrieved context in order for the LLM to weigh their importance. After selecting the best model for this data set, we'll use it for all subsequent steps. Now we are ready to test the generation performance of the LLMs we are evaluating. In this phase, we will first use the best performing embedding model from the previous step to retrieve the most relevant context for each query in our data set.
But this time we will fill out a prompt template that we can pass to the LLM. The prompt template consists of the query from the test data set and the retrieved context. This is repeated for each LLM we want to test. The LLM results are sent to Ragas along with the queries and ground truth answers from the data set and the retrieved context.
Ragas then scores each LLM on answer relevancy and faithfulness. Scores range from 0 to 1, with 1 representing better performance. These metrics will help us get an idea of how well each LLM uses the retrieved context and how well it responds to the original query. In other words, faithfulness measures how close the answer generated is to the context that the LLM was provided, and answer relevancy indicates the quality of the response with respect to the original prompt.
Low faithfulness scores indicate that the model's answers did not match the context provided, which may indicate hallucination. Answer relevancy indicates how well the LLM responded to the prompt, accounting for things like redundancy and answering the prompt fully. Once we have selected the best LLM for the application, we can perform one last test using the best performing embedding model and LLM to gauge the overall performance of our RAG app. Once again, we retrieve the appropriate context from the knowledge base and fill out a prompt template for the LLM.
The generated answers are combined with the query, ground truth, and retrieved context values and send to Ragas. However, this time Ragas scores performance on the answer similarity and answer correctness metrics. Answer semantic similarity compares the app's answers to the ground truth provided, looking at the meaning specifically. Answer correctness also compares the answer to the ground truth but focuses on accuracy.
You can think of answer similarity as how well the LLM generated answer matches the ground truth in meaning. Answer correctness represents how well the generated answer matches the ground truth and how factually correct it is. Again, values range from 0 to 1, and 1 indicates better performance. While these metrics seem similar, they are calculated very differently and can highlight areas to investigate if the app performs poorly on one and not the other.
As you go through the process, keep in mind that there are many variables that can be adjusted to impact performance. However, many of the changes made will also impact the performance of other components within the system. So it's often a trade-off that requires you to determine which option provides the best balance in your app.
As you experiment with different settings, some of the options available to adjust the performance of the retriever will include, using a different chunking method while ingesting the data, reranking the retrieved chunks, adding metadata to chunk content, rewriting the user query, and switching the similarity function for finding matches. While they don't impact the retrieval quality, you may also want to consider the storage and inference costs as well as embedding latency when choosing your embedding model. Caching can also be a useful addition to your system to provide verified answers and reduce latency. Finally, consider using a hybrid search approach to improve retrieval quality, especially for data sets that benefit from keyword searches, like ones containing acronyms, for example.
When evaluating the generation component, keep the embedding model consistent throughout the testing to ensure differences observed can be attributed correctly. Other options for experimentation include consolidated metrics, such as weighted summations, and customizing the prompts sent to the LLMs for evaluation. As we arrive at the final application tests, you can also use data exploration and visualizations to help identify additional areas for improvement. Heat maps are a great way to highlight strong performance on certain topics and opportunities for further investigation.
This testing will often lead to a revisiting of the previous testing steps and experimentation. Additionally, new models are evolving quickly, so it's a good idea to reevaluate performance periodically. Be mindful of the limitations of your system and think through your testing procedures to account for the way LLMs work. A good example of this is the intern test, which involves reviewing the query and the retrieved content, then asking if an intern could succeed if provided the same materials.
If not, try to supplement the content available or break the task into separate tasks if possible. When using LLM as a judge, some additional considerations include, using pairwise comparisons to pick from two options versus allowing for open-ended responses; asking the LLM for an explanation in the prompt; and limiting the length of the inputs to prevent bias towards longer prompts. Up to this point, we have focused on metrics and changing settings, but it's important to also take a step back and consider qualitative performance of your RAG apps. This includes adding guardrails for things like bias and toxicity. You can use content moderation AIs to help avoid problematic scenarios.
There are also factual inconsistency guardrails available. They can be used to help combat hallucinations by including prompt engineering that requires the LLM to explain its reasoning, then having the factual inconsistency guardrails check the summary of reasoning. Potential issues are then filtered or regenerated by the LLM. Outside of using Ragas, there are other tools that can be helpful when evaluating a RAG app.
You can learn more about these via the resources linked below. LangSmith and Arize Phoenix are general purpose evaluation and observability tools that can be helpful in gathering feedback from users. Resources like the Massive Text Embedding Benchmark and LMSYS leaderboards on Hugging Face are great for seeing how different models perform on different tasks. There are loads of new tools and even benchmarks coming out, but these should help you get started.
As your GenAI apps get used, collecting feedback is an invaluable way of finding issues, making improvements, and maintaining performance over time. How you collect this feedback is up to you. But as mentioned, there are specialized tools like LangSmith and Arize Phoenix that can help you collect this information. You can also store evaluation results in Atlas, build monitoring dashboards using Atlas Charts, and set up alerts for performance degradations.
No matter how the feedback is collected or which methods you are using to evaluate app performance, it's important to have a human in the loop periodically. Reviewing user data, spot checking performance, and updating ground truth are all important activities that need to be done periodically. This Learning Byte has covered a lot of ground, but hopefully it's highlighted the importance of evaluating your GenAI apps and provided some ideas on how to go about doing so.
If you'd like to learn more, there are additional resources linked below.
Thanks for watching, and keep Mongoing.