Hallucination
The hallucination metric allows you to check if the LLM response contains any hallucinated information. In order to check for hallucination, you will need to provide the LLM input, LLM output. If the context is provided, this will also be used to check for hallucinations.
How to use the Hallucination metric
You can use the Hallucination
metric as follows:
If you want to check for hallucinations based on context, you can also pass the context to the score
method:
Asynchronous scoring is also supported with the ascore
scoring method.
The hallucination score is either 0
or 1
. A score of 0
indicates that no hallucinations were detected, a score
of 1
indicates that hallucinations were detected.
Hallucination Prompt
Opik uses an LLM as a Judge to detect hallucinations, for this we have a prompt template that is used to generate the prompt for the LLM. By default, the gpt-4o
model is used to detect hallucinations but you can change this to any model supported by LiteLLM by setting the model
parameter. You can learn more about customizing models in the Customize models for LLM as a Judge metrics section.
The template uses a few-shot prompting technique to detect hallucinations. The template is as follows: