Hallucination
Halucination is a single-turn metric to determine if your LLM is hallucinating false information.
Overview
The hallucination metric is a single-turn safety metric that uses LLM-as-a-judge to assess whether your LLM's output is truthful and free from false or hallucinated information.
Required Parameters
These are the parameters you must supply in your test case to run evaluations for hallucination metric:
inputstringrequired
The input you supplied to your LLM application.
actual_outputstringrequired
The final output your LLM application generates.
contextlist of stringrequired
A list of strings containing context that can be used to answer the input. Usually strings of documents.
How Is It Calculated?
The hallucination metric uses an LLM to identify contradictions between the actual output and the provided context, treating the context as ground truth.
\text{Hallucination} = \frac{\text{Number of Contradicted Contexts}}{\text{Number of Contexts}}The final score is the proportion of contradicted contexts found in the actual output.
Create Locally
You can create the HallucinationMetric in deepeval as follows:
from deepeval.metrics import HallucinationMetric
metric = HallucinationMetric()Here's a list of parameters you can configure when creating a HallucinationMetric:
thresholdnumberdefault: 0.5
A float representing the maximum passing threshold.
Unlike other metrics, the threshold for the HallucinationMetric is a maximum instead of a minimum 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.
Create Remotely
For users not using deepeval python, or want to run evals remotely on Confident AI, you can use the hallucination metric by adding it to a single-turn metric collection. This will allow you to use hallucination metric for:
- Single-turn E2E testing
- Single-turn component-level testing
- Online and offline evals for traces and spans