Launch Week 02 wrapped — explore all five launches

Contextual Relevancy

Contextual Relevancy is a single-turn metric used to evaluate a RAG retriever

Overview

The contextual relevancy metric is a single-turn RAG metric that uses LLM-as-a-judge to evaluate whether all retrieved context is relevant to the input query.

Required Parameters

These are the parameters you must supply in your test case to run evaluations for contextual relevancy metric:

inputstringrequired

The input query you supply to your RAG application.

expected_outputstringrequired

The expected output your RAG application has to generate for a given input.

retrieval_contextlist of stringrequired

The retrieved context your retriever outputs for a given input sorted by their rank.

How Is It Calculated?

The contextual relevancy metric first extracts independent statements from all retrieved context using an LLM, then uses the same LLM to determine how many of those statements are relevant to the input query.


\text{Contextual Relevancy} = \frac{\text{Number of Relevant Statements}}{\text{Total Number of Statements}}

The final score is the proportion of relevant statements in retrieval context.

Create Locally

You can create the ContextualRelevancyMetric in deepeval as follows:

from deepeval.metrics import ContextualRelevancyMetric

metric = ContextualRelevancyMetric()

Here's a list of parameters you can configure when creating a ContextualRelevancyMetric:

thresholdnumberdefault: 0.5

A float to represent the minimum passing threshold.

modelstring | Objectdefault: gpt-4.1

A string specifying which of OpenAI's GPT models to use OR any custom LLM model of type DeepEvalBaseLLM.

include_reasonbooleandefault: true

A boolean to enable the inclusion a reason for its evaluation score.

async_modebooleandefault: true

A boolean to enable concurrent execution within the measure() method.

strict_modebooleandefault: false

A boolean to enforce a binary metric score: 0 for perfection, 1 otherwise.

verbose_modebooleandefault: false

A boolean to print the intermediate steps used to calculate the metric score.

evaluation_templateContextualRelevancyTemplatedefault: deepeval's template

An instance of ContextualRelevancyTemplate object, which allows you to override the default prompts used to compute the ContextualRelevancyMetric score.

Create Remotely

For users not using deepeval python, or want to run evals remotely on Confident AI, you can use the contextual relevancy metric by adding it to a single-turn metric collection. This will allow you to use contextual relevancy metric for:

  • Single-turn E2E testing
  • Single-turn component-level testing
  • Online and offline evals for traces and spans
Built byConfident AI