Google Colab
colab.research.google.com
- Understanding Retrieval Augmented Generation (RAG).
- Building RAG (with the help of a framework such as LlamaIndex).
- Evaluating RAG with Phoenix Evals.
Retrieval Augmented Generation (RAG)
LLMs are trained on vast amounts of data, but these will not include your specific data (things like company knowledge bases and documentation). Retrieval-Augmented Generation (RAG) addresses this by dynamically incorporating your data as context during the generation process. This is done not by altering the training data of the LLMs but by allowing the model to access and utilize your data in real-time to provide more tailored and contextually relevant responses. In RAG, your data is loaded and prepared for queries. This process is called indexing. User queries act on this index, which filters your data down to the most relevant context. This context and your query then are sent to the LLM along with a prompt, and the LLM provides a response. RAG is a critical component for building applications such a chatbots or agents and you will want to know RAG techniques on how to get data into your application.
Stages within RAG
There are five key stages within RAG, which will in turn be a part of any larger RAG application.- Loading: This refers to getting your data from where it lives - whether it’s text files, PDFs, another website, a database or an API - into your pipeline.
- Indexing: This means creating a data structure that allows for querying the data. For LLMs this nearly always means creating vector embeddings, numerical representations of the meaning of your data, as well as numerous other metadata strategies to make it easy to accurately find contextually relevant data.
- Storing: Once your data is indexed, you will want to store your index, along with any other metadata, to avoid the need to re-index it.
- Querying: For any given indexing strategy there are many ways you can utilize LLMs and data structures to query, including sub-queries, multi-step queries, and hybrid strategies.
- Evaluation: A critical step in any pipeline is checking how effective it is relative to other strategies, or when you make changes. Evaluation provides objective measures on how accurate, faithful, and fast your responses to queries are.
Build a RAG system
Now that we have understood the stages of RAG, let’s build a pipeline. We will use LlamaIndex for RAG and Phoenix Evals for evaluation.Load Data and Build an Index
vector_index.as_query_engine(similarity_top_k=k).
Let’s check the text in each of these retrieved nodes.
Note that the traces have captured the documents that were retrieved by the query engine. This is nice because it means we can introspect the documents without having to keep track of them ourselves.
We have built a RAG pipeline and also have instrumented it using Phoenix Tracing. We now need to evaluate it’s performance. We can assess our RAG system/query engine using Phoenix’s LLM Evals. Let’s examine how to leverage these tools to quantify the quality of our retrieval-augmented generation system.
Evaluation
Evaluation should serve as the primary metric for assessing your RAG application. It determines whether the pipeline will produce accurate responses based on the data sources and range of queries. While it’s beneficial to examine individual queries and responses, this approach is impractical as the volume of edge-cases and failures increases. Instead, it’s more effective to establish a suite of metrics and automated evaluations. These tools can provide insights into overall system performance and can identify specific areas that may require scrutiny. In a RAG system, evaluation focuses on two critical aspects:- Retrieval Evaluation: To assess the accuracy and relevance of the documents that were retrieved
- Response Evaluation: Measure the appropriateness of the response generated by the system when the context was provided.
Generate Question Context Pairs
For the evaluation of a RAG system, it’s essential to have queries that can fetch the correct context and subsequently generate an appropriate response. For this tutorial, let’s use Phoenix’sLLM to help us create the question-context pairs.
First, let’s create a dataframe of all the document chunks that we have indexed.
Now that we have the document chunks, let’s prompt an LLM to generate us 3 questions per chunk. Note that you could manually solicit questions from your team or customers, but this is a quick and easy way to generate a large number of questions.
Retrieval Evaluation
We are now prepared to perform our retrieval evaluations. We will execute the queries we generated in the previous step and verify whether or not that the correct context is retrieved.
Let’s now use Phoenix’s LLM Evals to evaluate the relevance of the retrieved documents with regards to the query. Note, we’ve turned on
explanations which prompts the LLM to explain it’s reasoning. This can be useful for debugging and for figuring out potential corrective actions.
Observations
Let’s now take our results and aggregate them to get a sense of how well our RAG system is performing.Response Evaluation
The retrieval evaluations demonstrates that our RAG system is not perfect. However, it’s possible that the LLM is able to generate the correct response even when the context is incorrect. Let’s evaluate the responses generated by the LLM.Observations
Let’s now take our results and aggregate them to get a sense of how well the LLM is answering the questions given the context.0.91 and a Hallucinations score 0.05 signifies that the generated answers are correct ~91% of the time and that the responses contain hallucinations 5% of the time - there is room for improvement. This could be due to the retrieval strategy or the LLM itself. We will need to investigate further to determine the root cause.
Since we have evaluated our RAG system’s QA performance and Hallucinations performance, let’s send these evaluations to Phoenix for visualization.

