-
-
Notifications
You must be signed in to change notification settings - Fork 526
Expand file tree
/
Copy pathhallucination.py
More file actions
77 lines (68 loc) 路 2.57 KB
/
Copy pathhallucination.py
File metadata and controls
77 lines (68 loc) 路 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from typing import override
from giskard.agents.workflow import TemplateReference
from giskard.core import provide_not_none
from pydantic import Field
from ..core import Trace
from ..core.check import Check
from ..core.extraction import JSONPathStr, provided_or_resolve
from .base import BaseLLMCheck
@Check.register("hallucination")
class Hallucination[InputType, OutputType, TraceType: Trace]( # pyright: ignore[reportMissingTypeArgument]
BaseLLMCheck[InputType, OutputType, TraceType]
):
"""LLM-based check that detects fabricated facts in an answer.
The check can evaluate an answer with or without a provided reference
context. When context is provided, it is used as evidence for detecting
fabricated facts, invented details, fake citations, and unsupported claims.
Attributes
----------
answer : str | None
The answer text to evaluate.
answer_key : str
JSONPath expression to extract the answer from the trace
(default: "trace.last.outputs").
context : str | list[str] | None
Optional reference context for the answer.
context_key : str | None
Optional JSONPath expression to extract context from the trace.
"""
answer: str | None = Field(
default=None, description="Input source for the answer to evaluate"
)
answer_key: JSONPathStr = Field(
default="trace.last.outputs",
description="Key to extract the answer from the trace",
)
context: str | list[str] | None = Field(
default=None, description="Optional reference context for the answer"
)
context_key: JSONPathStr | None = Field(
default=None,
description="Optional key to extract reference context from the trace",
)
@override
def get_prompt(self) -> TemplateReference:
return TemplateReference(
template_name="giskard.checks::judges/hallucination.j2"
)
@override
async def get_inputs(self, trace: Trace[InputType, OutputType]) -> dict[str, str]:
inputs = {
"answer": str(
provided_or_resolve(
trace,
key=self.answer_key,
value=provide_not_none(self.answer),
)
),
"context": "",
}
if self.context is not None or self.context_key is not None:
inputs["context"] = str(
provided_or_resolve(
trace,
key=self.context_key,
value=provide_not_none(self.context),
)
)
return inputs