Reranking And Evaluation
Improve Quality of your LLM Application and RAG Relevance with AIMon
Overview
In this tutorial, we'll help you build a retrieval-augmented generation (RAG) chatbot that answers questions on the meeting bank dataset.
In this tutorial you will learn to:
- Build an LLM application that answers a user's query related to the meeting bank dataset.
- Define, measure and improve the quality of your LLM application.
- Improve the RAG relevance score significanly (as high as 97% although your mileage may vary)
Tech Stack
Vector Database
For this application, we will use Weaviate, which is an open-source, AI-native vector database. We will also use Weaviate's fast embedding computation service.
LLM Framework
LlamaIndex is an open-source data orchestration framework that simplifies building large language model (LLM) applications by facilitating the integration of private data with LLMs, enabling context-augmented generative AI applications through a Retrieval-Augmented Generation (RAG) pipeline. We will use LlamaIndex for this tutorial since it offers a good amount of flexibility and better lower level API abstractions.
LLM Output Quality Evaluation
AIMon offers proprietary Judge models for Hallucination, Context Quality issues, Instruction Adherence of LLMs, Retrieval Quality and other LLM reliability tasks. We will use AIMon to judge the quality of the LLM application.
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 366.6/366.6 kB 5.0 MB/s eta 0:00:00
Pre-requisites
- Signup for an AIMon account here.
Add this secret to the Colab Secrets (the "key" symbol on the left panel)
- AIMON_API_KEY
- Signup for a Weaviate account here, create a sandbox cluster, and set the following keys in the Colab secrets.
- WCD_URL (REST Endpoint of your Weaviate cluster)
- WCD_API_KEY (Admin API Key of your Weaviate cluster)
- Signup for an OpenAI account here and add the following key in Colab secrets:
- OPENAI_API_KEY
Required API keys
Dataset
We will use the MeetingBank dataset which is a benchmark dataset created from the city councils of 6 major U.S. cities to supplement existing datasets. It contains 1,366 meetings with over 3,579 hours of video, as well as transcripts, PDF documents of meeting minutes, agenda, and other metadata.
For this exercise, we have created a smaller dataset. It can be found here.
Folder '/content/meetingbank_train_split.hf' and its contents deleted successfully.
Downloading... From: https://drive.google.com/uc?id=1bs4kwwiD30DUeCjuqEdOeixCuI-3i9F5 To: /content/meetingbank_train_split.tar.gz 100% 1.87M/1.87M [00:00<00:00, 26.5MB/s] Downloading... From: https://drive.google.com/uc?id=1fkxaS8eltjfkzws5BRXpVXnxl2Qxwy5F To: /content/score_metrics_relevant_examples_2.csv 100% 163k/163k [00:00<00:00, 72.2MB/s]
Extracted to: /content/
Queries
Below are the 12 queries that we will run on the transcript above
['What was the key decision in the meeting?', , 'What are the next steps for the team?', , 'Summarize the meeting in 10 words.', , 'What were the main points of discussion?', , 'What decision was made regarding the project?', , 'What were the outcomes of the meeting?', , 'What was discussed in the meeting?', , 'What examples were discussed for project inspiration?', , 'What considerations were made for the project timeline?', , 'Who is responsible for completing the tasks?', , 'What were the decisions made in the meeting?', , 'What did the team decide about the project timeline?']
Metric Definition
This quality score metric will help us understand how good the LLM responses are for the set of queries above. To measure quality of our application, we will run a set of queries and aggregate the quality scores across all these queries.
LLM Application Quality Score is a combination of 3 individual quality metrics from AIMon:
- Hallucination Score (hall_score): checks if the generated text is grounded in the provided context. A score closer to 1.0 means that there is a strong indication of hallucination and a score closer to 0.0 means a lower indication of hallucination. Hence, we will use (1.0-hall_score) here when computing the final quality score.
- Instruction Adherence Score (ia_score): checks if all explicit instructions provided have been followed by the LLM. The higher the ia_score the better the adherence to instructions. The lower the score, the poorer the adherence to instructions.
- Retrieval Relevance Score (rr_score): checks if the retrieved documents are relevant to the query. A score closer to 100.0 means perfect relevance of document to query and a score closer to 0.0 means poor relevance of document to query.
quality_score = 0.35 * (1.0 - hall_score) + 0.35 * ia_score + 0.3 * rr_score
Setup AIMon
As mentioned previously, AIMon will be used to judge the quality of the LLM application. Documentation can be found here.
Instantiate the Weaviate client
Weaviate Cloud Connection Successful
Create a Weaviate collection
We will use OpenAI's text-embedding-3-small model to create embeddings for the documents. These embeddings are created using Weaviate's embedding service which offers a fast embedding computation option. Note that this option is not available in the open source version of Weaviate. To use the embedding service you need to have a cluster or a sandbox running in Weaviate cloud.
Successfully created collection: MeetingBankDataset.
Ingest data to the Weaviate Collection
This step takes about 30 seconds to ingest ~4000 documents.
Data ingested into 'MeetingBankDataset' successfully.
1. Use Weaviate Vector Database for document retrieval
There are two main components we need to be aware of: Ingestion and RAG based Q&A. The ingestion pipeline processes the transcripts from the Meeting Bank dataset and stores it in the Weaviate Vector database. The RAG Q&A pipeline processes a user query by first retrieving the relevant documents from the vector store. These documents are then be used as grounding documents for the LLM to generate its response. We leverage AIMon to calculate the quality score and continuously monitor the application for hallucination, , instruction adherence, context relevance. These are the same 3 metrics we used to define the quality score above.
We will use LlamaIndex's query embedding to embed the input queries. This option uses the same Open AI embedding model text-embedding-3-small that was used to ingest the documents above.
In the cells below, we will setup a LlamaIndex VectorStoreIndex, setup the LLM and build a LLamaIndex Query Engine that interfaces with the stored embeddings and the LLM to answer a user's questions.
We will use a Hybrid search strategy here. Hybrid search provides a balanced approach to using both the keyword based reverse index search and vector search. The alpha parameter determines how much weight should be given to keyword search bm25 v/s vector search (alpha = 0 -> bm25, alpha=1 -> vector search).
At this point, the query engine. Next, we setup AIMon to help us measure quality scores. We use the same @detect decorator that was created in the previous cells above. The only additional code in ask_and_validate here is to help AIMon interface with LLamaIndex's retrieved document "nodes".
Lets run through all the queries through the LlamaIndex query engine in the queries_df and compute the overall quality score using AIMon.
NOTE: This will take about 2 mins
Avg. Retrieval relevance score across chunks: 20.71450106313847 for query: What was the key decision in the meeting? Avg. Retrieval relevance score across chunks: 18.488070103829415 for query: What are the next steps for the team? Avg. Retrieval relevance score across chunks: 16.689701359327245 for query: Summarize the meeting in 10 words. Avg. Retrieval relevance score across chunks: 20.71252137903855 for query: What were the main points of discussion? Avg. Retrieval relevance score across chunks: 19.24149681146247 for query: What decision was made regarding the project? Avg. Retrieval relevance score across chunks: 19.950058762526908 for query: What were the outcomes of the meeting? Avg. Retrieval relevance score across chunks: 23.701073375683777 for query: What was discussed in the meeting? Avg. Retrieval relevance score across chunks: 17.11218430288355 for query: What examples were discussed for project inspiration? Avg. Retrieval relevance score across chunks: 18.740298928401984 for query: What considerations were made for the project timeline? Avg. Retrieval relevance score across chunks: 17.004480433007103 for query: Who is responsible for completing the tasks? Avg. Retrieval relevance score across chunks: 24.65773289350558 for query: What were the decisions made in the meeting? Avg. Retrieval relevance score across chunks: 18.779891341226502 for query: What did the team decide about the project timeline? Time elapsed: 119.2650191783905 seconds
Average Quality score for vector DB approach: 66.73888776885079
Average retrieval relevance score for vector DB approach: 19.649334229502628
2. Add Re-ranking to your retrieval
Now, we will add in AIMon's domain adaptable re-ranker using AIMon's LlamaIndex postprocessor re-rank integration.
As shown in the figure below, reranking helps bubble up the most relevant documents to the top by using a more advanced Query-Document matching function. The unique feature of AIMon's re-ranker is the ability to customize it per domain. Similar to how you would prompt engineer an LLM, you can customize reranking performance per domain using the task_definition field. This state-of-the-art reranker runs at ultra low sub second latency (for a ~2k context) and its performance ranks in the top 5 of the MTEB reranking leaderboard.
Let's run through the queries again and recompute the overall quality score to see if there is an improvement.
✨ AIMon's re-ranking should not add additional latency overhead to the total query reponse time since it actually reduces the amount of context documents that need to be sent to the LLM for generating a response making the operation efficient in terms of network I/O and LLM token processing cost (money and time).
NOTE: This step will take 2 mins
Avg. Retrieval relevance score across chunks: 36.252343849590574 for query: What was the key decision in the meeting? Avg. Retrieval relevance score across chunks: 39.172939741264656 for query: What are the next steps for the team? Avg. Retrieval relevance score across chunks: 43.1129893570283 for query: Summarize the meeting in 10 words. Avg. Retrieval relevance score across chunks: 31.55758507967228 for query: What were the main points of discussion? Avg. Retrieval relevance score across chunks: 38.262344369792345 for query: What decision was made regarding the project? Avg. Retrieval relevance score across chunks: 38.48223390749013 for query: What were the outcomes of the meeting? Avg. Retrieval relevance score across chunks: 36.24011982592492 for query: What was discussed in the meeting? Avg. Retrieval relevance score across chunks: 35.08738039950927 for query: What examples were discussed for project inspiration? Avg. Retrieval relevance score across chunks: 40.769517268595834 for query: What considerations were made for the project timeline? Avg. Retrieval relevance score across chunks: 41.782401862943885 for query: Who is responsible for completing the tasks? Avg. Retrieval relevance score across chunks: 42.25547877659466 for query: What were the decisions made in the meeting? Avg. Retrieval relevance score across chunks: 43.43217201856557 for query: What did the team decide about the project timeline? Time elapsed: 115.07113265991211 seconds
Notice the difference in average document relevance scores when using the reranker v/s when not using the reranker
Average Quality score for AIMon Re-ranking approach: 73.19234599475764
Average retrieval relevance score for AIMon Re-ranking approach: 38.8672922047477
Terminate the connection to the Weaviate client.
🎉 Quality Score improved!
Notice that the overall quality score across all queries improved after using AIMon's reranker.
In sum, as shown in the figure below, we demonstrated the following:
- Computing a quality score using a weighted combination of 3 different quality metrics: hallucination score, instruction adherence score and retrieval relevance score.
- Improved the quality score using AIMon's low-latency, domain adaptable re-ranker.
- We also showed how retrieval relevance improves significantly by adding in AIMon's re-ranker.
We encourage you to experiment with the different components shown in this notebook to further increase the quality score. One idea is to add your own definitions of quality using the instructions field in the instruction_adherence detector above. Another idea is to add another one of AIMon's checker models as part of the quality metric calculation.
Happy experimenting! Join our discord server for fun conversations on AI!
The above table summarizes our results. Your actual numbers will vary depending on various factors such as variations in quality of LLM responses, performance of the nearest neighbor search in the VectorDB etc.
In conclusion, as shown by the figure below, we evaluated quality score, RAG relevance and instruction following capabilities of your LLM application. We used AIMon's re-ranker to improve the overall quality of the application and the average relevance of documents retrieved from your RAG.