Evaluate Relevance Classifications
Retrieval Relevance Evals
Arize provides tooling to evaluate LLM applications, including tools to determine the relevance or irrelevance of documents retrieved by retrieval-augmented generation (RAG) applications. This relevance is then used to measure the quality of each retrieval using ranking metrics such as precision@k. In order to determine whether each retrieved document is relevant or irrelevant to the corresponding query, our approach is straightforward: ask an LLM.
The purpose of this notebook is:
- to evaluate the performance of an LLM-assisted approach to relevance classification against information retrieval datasets with ground-truth relevance labels,
- to provide an experimental framework for users to iterate and improve on the default classification template.
Install Dependencies and Import Libraries
9391.10s - pydevd: Sending message related to process being replaced timed-out after 5 seconds
ℹ️ To enable async request submission in notebook environments like Jupyter or Google Colab, optionally use nest_asyncio. nest_asyncio globally patches asyncio to enable event loops to be re-entrant. This is not required for non-notebook environments.
Without nest_asyncio, eval submission can be much slower, depending on your organization's rate limits. Speed increases of about 5x are typical.
Download Benchmark Dataset
We'll evaluate the evaluation system consisting of an LLM model and settings in addition to an evaluation prompt template against benchmark datasets of queries and retrieved documents with ground-truth relevance labels. Currently supported datasets include:
- "wiki_qa-train"
- "ms_marco-v1.1-train"
Display Binary Relevance Classification Template
View the default template used to classify relevance. You can tweak this template and evaluate its performance relative to the default.
You are comparing a reference text to a question and trying to determine if the reference text
contains information relevant to answering the question. Here is the data:
[BEGIN DATA]
************
[Question]: {input}
************
[Reference text]: {reference}
************
[END DATA]
Compare the Question above to the Reference text. You must determine whether the Reference text
contains information that can answer the Question. Please focus on whether the very specific
question can be answered by the information in the Reference text.
Your response must be single word, either "relevant" or "unrelated",
and should not contain any text or characters aside from that word.
"unrelated" means that the reference text does not contain an answer to the Question.
"relevant" means the reference text contains an answer to the Question.
The template variables are:
- input: the question asked by a user
- reference: the text of the retrieved document
- output: a ground-truth relevance label
Configure the LLM
Configure your OpenAI API key.
Benchmark Dataset Sample
Sample size determines run time Recommend iterating small: 100 samples Then increasing to large test set
LLM Evals: Retrieval Relevance Classifications GPT-4
Run relevance against a subset of the data. Instantiate the LLM and set parameters.
The `model_name` field is deprecated. Use `model` instead. This will be removed in a future release.
"Hello! I'm working perfectly. How can I assist you today?"
Run Relevance Classifications
Run relevance classifications against a subset of the data.
llm_classify | | 0/100 (0.0%) | ⏳ 00:00<? | ?it/s
Evaluate Classifications
Evaluate the predictions against human-labeled ground-truth relevance labels.
precision recall f1-score support
relevant 0.77 0.87 0.82 47
unrelated 0.87 0.77 0.82 53
accuracy 0.82 100
macro avg 0.82 0.82 0.82 100
weighted avg 0.83 0.82 0.82 100
<Axes: title={'center': 'Confusion Matrix (Normalized)'}, xlabel='Predicted Classes', ylabel='Actual Classes'>Classifications with explanations
When evaluating a dataset for relevance, it can be useful to know why the LLM classified a document as relevant or irrelevant. The following code block runs llm_classify with explanations turned on so that we can inspect why the LLM made the classification it did. There is speed tradeoff since more tokens is being generated but it can be highly informative when troubleshooting.
Using prompt:
You are comparing a reference text to a question and trying to determine if the reference text
contains information relevant to answering the question. Here is the data:
[BEGIN DATA]
************
[Question]: {input}
************
[Reference text]: {reference}
************
[END DATA]
Compare the Question above to the Reference text. You must determine whether the Reference text
contains information that can help answer the Question. First, write out in a step by step manner
an EXPLANATION to show how to arrive at the correct answer. Avoid simply stating the correct answer
at the outset. Your response LABEL must be single word, either "relevant" or "unrelated", and
should not contain any text or characters aside from that word. "unrelated" means that the
reference text does not help answer to the Question. "relevant" means the reference text directly
answers the question.
Example response:
************
EXPLANATION: An explanation of your reasoning for why the label is "relevant" or "unrelated"
LABEL: "relevant" or "unrelated"
************
EXPLANATION:
OpenAI invocation parameters: {'model': 'gpt-4', 'temperature': 0.0, 'max_tokens': 256, 'frequency_penalty': 0, 'presence_penalty': 0, 'top_p': 1, 'n': 1, 'timeout': None}
llm_classify | | 0/5 (0.0%) | ⏳ 00:00<? | ?it/s
- Snapped 'relevant' to rail: relevant - Snapped 'relevant' to rail: relevant - Snapped 'unrelated' to rail: unrelated - Snapped 'relevant' to rail: relevant - Snapped 'unrelated' to rail: unrelated
LLM Evals: relevance Classifications GPT-3.5 Turbo
Run relevance against a subset of the data using GPT-3.5. GPT-3.5 can significantly speed up the classification process. However there are tradeoffs as we will see below.
The `model_name` field is deprecated. Use `model` instead. This will be removed in a future release.
llm_classify | | 0/100 (0.0%) | ⏳ 00:00<? | ?it/s
Exception in worker on attempt 1: raised APITimeoutError('Request timed out.')
Requeuing...
precision recall f1-score support
relevant 0.50 1.00 0.67 47
unrelated 1.00 0.11 0.20 53
accuracy 0.53 100
macro avg 0.75 0.56 0.44 100
weighted avg 0.77 0.53 0.42 100
<Axes: title={'center': 'Confusion Matrix (Normalized)'}, xlabel='Predicted Classes', ylabel='Actual Classes'>Preview: Running with GPT-4 Turbo
The `model_name` field is deprecated. Use `model` instead. This will be removed in a future release.
llm_classify | | 0/100 (0.0%) | ⏳ 00:00<? | ?it/s
precision recall f1-score support
relevant 0.75 0.94 0.83 47
unrelated 0.93 0.72 0.81 53
accuracy 0.82 100
macro avg 0.84 0.83 0.82 100
weighted avg 0.84 0.82 0.82 100
<Axes: title={'center': 'Confusion Matrix (Normalized)'}, xlabel='Predicted Classes', ylabel='Actual Classes'>