Contextual Recall
The contextual recall metric measures the quality of your RAG pipeline's retriever by evaluating the extent of which the retrieval_context aligns with the expected_output. deepeval's contextual recall metric is a self-explaining LLM-Eval, meaning it outputs a reason for its metric score.
Required Arguments
To use the ContextualRecallMetric, you'll have to provide the following arguments when creating an LLMTestCase:
inputactual_outputexpected_outputretrieval_context
Example
from deepeval import evaluate
from deepeval.metrics import ContextualRecallMetric
from deepeval.test_case import LLMTestCase
# Replace this with the actual output from your LLM application
actual_output = "We offer a 30-day full refund at no extra cost."
# Replace this with the expected output from your RAG generator
expected_output = "You are eligible for a 30 day full refund at no extra cost."
# Replace this with the actual retrieved context from your RAG pipeline
retrieval_context = ["All customers are eligible for a 30 day full refund at no extra cost."]
metric = ContextualRecallMetric(
    threshold=0.7,
    model="gpt-4",
    include_reason=True
)
test_case = LLMTestCase(
    input="What if these shoes don't fit?",
    actual_output=actual_output,
    expected_output=expected_output,
    retrieval_context=retrieval_context
)
metric.measure(test_case)
print(metric.score)
print(metric.reason)
# or evaluate test cases in bulk
evaluate([test_case], [metric])
There are three optional parameters when creating a ContextualRecallMetric:
- [Optional] 
threshold: a float representing the minimum passing threshold, defaulted to 0.5. - [Optional] 
model: a string specifying which of OpenAI's GPT models to use, OR any custom LLM model of typeDeepEvalBaseLLM. Defaulted to 'gpt-4-0125-preview'. - [Optional] 
include_reason: a boolean which when set toTrue, will include a reason for its evaluation score. Defaulted toTrue. 
How Is It Calculated?
The ContextualRecallMetric score is calculated according to the following equation:
The ContextualRecallMetric first uses an LLM to extract all statements made in the expected_output, before using the same LLM to classify whether each statement can be attributed to nodes in the retrieval_context.
We use the expected_output instead of the actual_output because we're measuring the quality of the RAG retriever for a given ideal output.
A higher contextual recall score represents a greater ability of the retrieval system to capture all relevant information from the total available relevant set within your knowledge base.