DocumentationAPI ReferenceπŸ““ TutorialsπŸ§‘β€πŸ³ Cookbook🀝 IntegrationsπŸ’œ Discord

ContextRelevanceEvaluator

The ContextRelevanceEvaluator uses an LLM to evaluate whether a generated answer can be inferred from the provided contexts. It does not require ground truth labels.

NameContextRelevanceEvaluator
Folder Path/evaluators
Position in a PipelineOn its own or in an evaluation pipeline. To be used after a separate pipeline that has generated the inputs for the Evaluator.
Inputs"questions": A list of questions.

"contexts": A list of a list of contexts, which are the contents of documents. This accounts for one list of contexts per question.
OutputsA dictionary containing:

- score: A number from 0.0 to 1.0 that represents the mean average precision.

- individual_scores: A list of the individual average precision scores ranging from 0.0 to 1.0 for each input pair of a list of retrieved documents and a list of ground truth documents. - results: A list of dictionaries with keys statements and statement_scores. They contain the statements extracted by an LLM from each context and the corresponding context relevance scores per statement, which are either 0 or 1.

Overview

You can use the ContextRelevanceEvaluator component to evaluate documents retrieved by a Haystack pipeline, such as a RAG pipeline, without ground truth labels. The component splits the generated answer into statements and checks each of them against the provided contexts with an LLM. The final score represents the proportion of statements that can be concluded from the provided contexts.

A valid OpenAI API key must be set as an OPENAI_API_KEY environment variable. The api_key parameter of the ContextRelevanceEvaluator allows you to provide the API key in a different way. See the documentation page about secret management for details.

Two optional initialization parameters are:

  • raise_on_failure: If True, raise an exception on an unsuccessful API call.
  • progress_bar: Whether to show a progress bar during the evaluation.

ContextRelevanceEvaluator has an optional examples parameter that can be used to pass few-shot examples conforming to the expected input and output format of ContextRelevanceEvaluator. These examples are included in the prompt that is sent to the LLM. Examples, therefore, increase the number of tokens of the prompt and make each request more costly. Adding examples is helpful if you want to improve the quality of the evaluation at the cost of more tokens.

Each example must be a dictionary with keys inputs and outputs.
inputs must be a dictionary with keys questions and contexts.
outputs must be a dictionary with statements and statement_scores.
Here is the expected format:

[{
	"inputs": {
		"questions": "What is the capital of Italy?", "contexts": ["Rome is the capital of Italy."],
	},
	"outputs": {
		"statements": ["Rome is the capital of Italy.", "Rome has more than 4 million inhabitants."],
		"statement_scores": [1, 0],
	},
}]

Usage

On its own

Below is an example where we use a ContextRelevanceEvaluator component to evaluate a response generated based on a provided question and context. The ContextRelevanceEvaluator returns a score of 0.5 because it detects two statements in the answer, of which only one is correct.

from haystack.components.evaluators import ContextRelevanceEvaluator

questions = ["Who created the Python language?"]
contexts = [
    [
        "Python, created by Guido van Rossum in the late 1980s, is a high-level general-purpose programming language. Its design philosophy emphasizes code readability, and its language constructs aim to help programmers write clear, logical code for both small and large-scale software projects."
    ],
]

evaluator = ContextRelevanceEvaluator()
result = evaluator.run(questions=questions, contexts=contexts)
print(result["score"])
# 1.0
print(result["individual_scores"])
# [1.0]
print(result["results"])
# [{'statements': ['Python, created by Guido van Rossum in the late 1980s.'], 'statement_scores': [1], 'score': 1.0}]

In a pipeline

Below is an example where we use a FaithfulnessEvaluator and a ContextRelevanceEvaluator in a pipeline to evaluate responses and contexts (the content of documents) received by a RAG pipeline based on provided questions. Running a pipeline instead of the individual components simplifies calculating more than one metric.

from haystack import Pipeline
from haystack.components.evaluators import ContextRelevanceEvaluator, FaithfulnessEvaluator

pipeline = Pipeline()
context_relevance_evaluator = ContextRelevanceEvaluator()
faithfulness_evaluator = FaithfulnessEvaluator()
pipeline.add_component("context_relevance_evaluator", context_relevance_evaluator)
pipeline.add_component("faithfulness_evaluator", faithfulness_evaluator)

questions = ["Who created the Python language?"]
contexts = [
    [
        "Python, created by Guido van Rossum in the late 1980s, is a high-level general-purpose programming language. Its design philosophy emphasizes code readability, and its language constructs aim to help programmers write clear, logical code for both small and large-scale software projects."
    ],
]
responses = ["Python is a high-level general-purpose programming language that was created by George Lucas."]

result = pipeline.run(
		{
			"context_relevance_evaluator": {"questions": questions, "contexts": contexts},
	    "faithfulness_evaluator": {"questions": questions, "contexts": contexts, "responses": responses}
    }
)

for evaluator in result:
    print(result[evaluator]["individual_scores"])
# [1.0]
# [0.5]
for evaluator in result:
    print(result[evaluator]["score"])
# 1.0
# 0.5

Related Links

See the parameters details in our API reference: