@arizeai/phoenix-evals 2.1.0 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/__generated__/default_templates/HALLUCINATION_CLASSIFICATION_EVALUATOR_CONFIG.d.ts.map +1 -1
- package/dist/esm/__generated__/default_templates/HALLUCINATION_CLASSIFICATION_EVALUATOR_CONFIG.js +48 -17
- package/dist/esm/__generated__/default_templates/HALLUCINATION_CLASSIFICATION_EVALUATOR_CONFIG.js.map +1 -1
- package/dist/esm/llm/createHallucinationEvaluator.d.ts +36 -19
- package/dist/esm/llm/createHallucinationEvaluator.d.ts.map +1 -1
- package/dist/esm/llm/createHallucinationEvaluator.js +24 -18
- package/dist/esm/llm/createHallucinationEvaluator.js.map +1 -1
- package/dist/esm/llm/index.js +1 -1
- package/dist/esm/llm/index.js.map +1 -1
- package/dist/esm/template/getTemplateVariables.js.map +1 -1
- package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/dist/src/__generated__/default_templates/HALLUCINATION_CLASSIFICATION_EVALUATOR_CONFIG.d.ts.map +1 -1
- package/dist/src/__generated__/default_templates/HALLUCINATION_CLASSIFICATION_EVALUATOR_CONFIG.js +48 -17
- package/dist/src/__generated__/default_templates/HALLUCINATION_CLASSIFICATION_EVALUATOR_CONFIG.js.map +1 -1
- package/dist/src/llm/createHallucinationEvaluator.d.ts +36 -19
- package/dist/src/llm/createHallucinationEvaluator.d.ts.map +1 -1
- package/dist/src/llm/createHallucinationEvaluator.js +24 -18
- package/dist/src/llm/createHallucinationEvaluator.js.map +1 -1
- package/dist/src/llm/index.js +1 -1
- package/dist/src/llm/index.js.map +1 -1
- package/dist/src/template/getTemplateVariables.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +6 -6
- package/src/__generated__/default_templates/HALLUCINATION_CLASSIFICATION_EVALUATOR_CONFIG.ts +48 -17
- package/src/llm/createHallucinationEvaluator.ts +36 -28
- package/src/llm/index.ts +1 -1
- package/src/template/getTemplateVariables.ts +4 -4
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@arizeai/phoenix-evals",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.0",
|
|
4
4
|
"description": "A library for running evaluations for AI use cases",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"arize",
|
|
@@ -66,19 +66,19 @@
|
|
|
66
66
|
}
|
|
67
67
|
},
|
|
68
68
|
"dependencies": {
|
|
69
|
-
"@ai-sdk/otel": "^1.0.
|
|
69
|
+
"@ai-sdk/otel": "^1.0.37",
|
|
70
70
|
"@arizeai/openinference-core": "^2.4.0",
|
|
71
71
|
"@opentelemetry/api": "^1.9.1",
|
|
72
|
-
"ai": "^7.0.
|
|
72
|
+
"ai": "^7.0.37",
|
|
73
73
|
"jsonpath-plus": "^10.4.0",
|
|
74
74
|
"mustache": "^4.2.0",
|
|
75
75
|
"zod": "^4.4.3"
|
|
76
76
|
},
|
|
77
77
|
"devDependencies": {
|
|
78
|
-
"@ai-sdk/google": "^4.0.
|
|
79
|
-
"@ai-sdk/openai": "^4.0.
|
|
78
|
+
"@ai-sdk/google": "^4.0.24",
|
|
79
|
+
"@ai-sdk/openai": "^4.0.20",
|
|
80
80
|
"@arizeai/openinference-instrumentation-openai": "^4.1.5",
|
|
81
|
-
"@opentelemetry/sdk-trace-node": "^2.
|
|
81
|
+
"@opentelemetry/sdk-trace-node": "^2.10.0",
|
|
82
82
|
"@types/mustache": "^4.2.6",
|
|
83
83
|
"@types/node": "^26.1.1",
|
|
84
84
|
"msw": "^2.15.0",
|
package/src/__generated__/default_templates/HALLUCINATION_CLASSIFICATION_EVALUATOR_CONFIG.ts
CHANGED
|
@@ -4,46 +4,77 @@ import type { ClassificationEvaluatorConfig } from "../types";
|
|
|
4
4
|
|
|
5
5
|
export const HALLUCINATION_CLASSIFICATION_EVALUATOR_CONFIG: ClassificationEvaluatorConfig = {
|
|
6
6
|
name: "hallucination",
|
|
7
|
-
description: "
|
|
7
|
+
description: "Detect whether an assistant's response contains hallucinations — claims not supported by the input it came from (prior turns, tool calls and results, and any content read, retrieved, or provided).",
|
|
8
8
|
optimizationDirection: "MINIMIZE",
|
|
9
9
|
template: [
|
|
10
10
|
{
|
|
11
11
|
role: "user",
|
|
12
12
|
content: `
|
|
13
|
-
|
|
13
|
+
You are an expert evaluator labeling whether an assistant's response is grounded in the input it came from.
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
The input is the complete record of what the assistant had available before it responded: the user's request, any earlier turns, any tool calls and the results they returned, and any content that was read, retrieved, or provided. It may appear as plain text, as structured data, or as a rendered transcript. Whatever form it takes, treat it as the full record of what the assistant knew and did, and as the only source of truth for claims about this situation. You are judging whether the response is GROUNDED in that record, not whether it is factually true about the world. The output is the response you are judging.
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
<rubric>
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
HALLUCINATED - The response contains at least one claim that the input does not support. This includes:
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
- Contradiction: the claim conflicts with something in the input.
|
|
22
|
+
- Invented specifics: names, numbers, prices, dates, quotes, identifiers, entities, or events that do not appear in the input and do not follow from it.
|
|
23
|
+
- Fabricated tool results: the response reports what a tool returned, but the input shows no such result — or shows a different one.
|
|
24
|
+
- Unsupported findings: the response presents findings, file contents, search results, sources, or a review of material that the input never shows being read, searched, or returned.
|
|
25
|
+
- Fabricated completed actions: the response states that an action has already been taken when the input shows no record of it happening.
|
|
26
|
+
- Misattribution: the response attributes a claim, source, or action to the input that never appears there.
|
|
22
27
|
|
|
23
|
-
|
|
28
|
+
GROUNDED - Every claim in the response is supported. The response:
|
|
29
|
+
|
|
30
|
+
- Restates or summarizes what is in the input, accurately.
|
|
31
|
+
- Draws conclusions that follow necessarily from the input.
|
|
32
|
+
- Uses only uncontroversial, widely-known general knowledge — common facts, definitions, arithmetic, units of time or measure — that does not contradict the input. This is the one kind of claim that need not appear in the input (see rule 4).
|
|
33
|
+
- Declines to answer, reports a failure, expresses uncertainty, or asks a clarifying question without asserting unsupported specifics.
|
|
34
|
+
|
|
35
|
+
</rubric>
|
|
36
|
+
|
|
37
|
+
Apply these rules when deciding:
|
|
38
|
+
|
|
39
|
+
1. Absence of evidence is enough. You do not need to find a contradiction. If the response asserts situation-specific facts that simply do not appear anywhere in the input, that is a hallucination — however plausible, fluent, or well-organized the response is. A confident report is not evidence that the work behind it was done.
|
|
40
|
+
|
|
41
|
+
2. Not everything in the input carries the same weight. User messages, tool results, and content that was read or provided are authoritative evidence. The assistant's own earlier statements are part of the record but are not independent evidence for a claim.
|
|
42
|
+
|
|
43
|
+
3. A failed tool is not a source. An error, timeout, empty result, or "not found" is evidence that the lookup failed. It is not evidence about the thing being looked up. A response that supplies the missing value anyway is hallucinating.
|
|
44
|
+
|
|
45
|
+
4. Apply the absence-of-evidence rule only to situation-specific claims. First distinguish claims about this particular situation — its entities, values, dates, events, sources, and what was returned or done — from ordinary general knowledge. Situation-specific claims must restate the input or follow from it necessarily; they are not supported if deriving them requires adding a typical value, default, cause, external lookup, or plausible completion. Ordinary general knowledge — definitions, arithmetic, common units and conventions, and widely known facts — is outside the grounding check and cannot by itself make a response hallucinated, as long as it does not contradict the input.
|
|
46
|
+
|
|
47
|
+
5. One unsupported claim is enough. If any claim in the response fails these tests, the response is hallucinated. Quote the specific claim in your explanation.
|
|
48
|
+
|
|
49
|
+
Out of scope — do not label a response hallucinated for any of these:
|
|
50
|
+
|
|
51
|
+
- Being unhelpful, incomplete, poorly written, off-topic, or badly formatted.
|
|
52
|
+
- Failing to do everything the user asked. Whether the assistant covered the full request is separate from whether what it said is supported.
|
|
53
|
+
- Stating an intention to do something in the future. "I'll open a ticket" is a plan, not a claim about what happened. Only claims that an action is already complete are checkable here.
|
|
54
|
+
- Content the input shows was shortened or omitted. Do not treat the absence of a claim from an explicitly elided region as evidence that the claim was fabricated. If the omitted region could plausibly contain the missing support, default to grounded. Otherwise, judge the claim against the visible input normally.
|
|
55
|
+
|
|
56
|
+
When you are genuinely unsure whether a claim is supported, label the response grounded.
|
|
24
57
|
|
|
25
58
|
<data>
|
|
26
59
|
|
|
27
|
-
<
|
|
60
|
+
<input>
|
|
28
61
|
{{input}}
|
|
29
|
-
</
|
|
30
|
-
|
|
31
|
-
<context>
|
|
32
|
-
{{context}}
|
|
33
|
-
</context>
|
|
62
|
+
</input>
|
|
34
63
|
|
|
35
|
-
<
|
|
64
|
+
<output>
|
|
36
65
|
{{output}}
|
|
37
|
-
</
|
|
66
|
+
</output>
|
|
38
67
|
|
|
39
68
|
</data>
|
|
40
69
|
|
|
41
|
-
|
|
70
|
+
Work through the claims in the output one at a time, checking each against the input, before you decide.
|
|
71
|
+
|
|
72
|
+
Is the output above grounded or hallucinated?
|
|
42
73
|
`,
|
|
43
74
|
},
|
|
44
75
|
],
|
|
45
76
|
choices: {
|
|
46
77
|
"hallucinated": 1,
|
|
47
|
-
"
|
|
78
|
+
"grounded": 0
|
|
48
79
|
},
|
|
49
80
|
};
|
|
@@ -1,10 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @deprecated This evaluator is maintained for backwards compatibility.
|
|
3
|
-
* Please use createFaithfulnessEvaluator instead, which uses updated terminology:
|
|
4
|
-
* - 'faithful'/'unfaithful' labels instead of 'factual'/'hallucinated'
|
|
5
|
-
* - Maximizes score (1.0=faithful) instead of minimizing it
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
1
|
import { HALLUCINATION_CLASSIFICATION_EVALUATOR_CONFIG } from "../__generated__/default_templates";
|
|
9
2
|
import type { CreateClassificationEvaluatorArgs } from "../types/evals";
|
|
10
3
|
import type { ClassificationEvaluator } from "./ClassificationEvaluator";
|
|
@@ -22,40 +15,55 @@ export interface HallucinationEvaluatorArgs<
|
|
|
22
15
|
promptTemplate?: CreateClassificationEvaluatorArgs<RecordType>["promptTemplate"];
|
|
23
16
|
}
|
|
24
17
|
|
|
25
|
-
/**
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
18
|
+
/** A conversation and the assistant response to evaluate for hallucination. */
|
|
19
|
+
export interface HallucinationEvaluationRecord {
|
|
20
|
+
/**
|
|
21
|
+
* The full conversation history the assistant had access to (prior turns,
|
|
22
|
+
* tool calls, and tool results); its last message is the user turn being
|
|
23
|
+
* answered. Treated as the source of truth.
|
|
24
|
+
*/
|
|
29
25
|
input: string;
|
|
26
|
+
/**
|
|
27
|
+
* The assistant's latest response to classify.
|
|
28
|
+
*/
|
|
30
29
|
output: string;
|
|
31
|
-
|
|
32
|
-
}
|
|
30
|
+
[key: string]: unknown;
|
|
31
|
+
}
|
|
33
32
|
|
|
34
33
|
/**
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
* Creates a function that evaluates whether an answer is factual or hallucinated based on a query and reference text.
|
|
34
|
+
* Creates a hallucination evaluator function.
|
|
38
35
|
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
36
|
+
* This function returns an evaluator that detects whether an assistant's latest
|
|
37
|
+
* response contains claims that are not grounded in — unsupported by, or
|
|
38
|
+
* contradicting — the conversation. Unlike the faithfulness evaluator, which
|
|
39
|
+
* grounds a response against a specific provided context (e.g. retrieved
|
|
40
|
+
* documents), this grounds it against the conversation itself.
|
|
42
41
|
*
|
|
43
42
|
* @param args - The arguments for creating the hallucination evaluator.
|
|
44
|
-
* @
|
|
43
|
+
* @param args.model - The model to use for classification.
|
|
44
|
+
* @param args.choices - The possible classification choices (defaults to hallucinated/grounded).
|
|
45
|
+
* @param args.promptTemplate - The prompt template to use (defaults to HALLUCINATION_CLASSIFICATION_EVALUATOR_CONFIG.template).
|
|
46
|
+
* @param args.telemetry - The telemetry to use for the evaluator.
|
|
47
|
+
*
|
|
48
|
+
* @returns An evaluator function that takes a {@link HallucinationEvaluationRecord} and returns a classification result
|
|
49
|
+
* indicating whether the response is grounded or hallucinated relative to the conversation.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```ts
|
|
53
|
+
* const evaluator = createHallucinationEvaluator({ model: openai("gpt-4o-mini") });
|
|
54
|
+
* const result = await evaluator.evaluate({
|
|
55
|
+
* input:
|
|
56
|
+
* "User: What's our refund window?\nTool (lookup_policy): Refunds: 30 days from delivery.\nAssistant: 30 days from delivery.\nUser: And for electronics?",
|
|
57
|
+
* output: "Electronics can be returned within 90 days.",
|
|
58
|
+
* });
|
|
59
|
+
* console.log(result.label); // "hallucinated"
|
|
60
|
+
* ```
|
|
45
61
|
*/
|
|
46
62
|
export function createHallucinationEvaluator<
|
|
47
63
|
RecordType extends Record<string, unknown> = HallucinationEvaluationRecord,
|
|
48
64
|
>(
|
|
49
65
|
args: HallucinationEvaluatorArgs<RecordType>
|
|
50
66
|
): ClassificationEvaluator<RecordType> {
|
|
51
|
-
// eslint-disable-next-line no-console
|
|
52
|
-
console.warn(
|
|
53
|
-
"createHallucinationEvaluator is deprecated and will be removed in a future version. " +
|
|
54
|
-
"Please use createFaithfulnessEvaluator instead. The new evaluator uses " +
|
|
55
|
-
"'faithful'/'unfaithful' labels and maximizes score (1.0=faithful) instead of " +
|
|
56
|
-
"minimizing it (0.0=factual)."
|
|
57
|
-
);
|
|
58
|
-
|
|
59
67
|
const {
|
|
60
68
|
choices = HALLUCINATION_CLASSIFICATION_EVALUATOR_CONFIG.choices,
|
|
61
69
|
promptTemplate = HALLUCINATION_CLASSIFICATION_EVALUATOR_CONFIG.template,
|
package/src/llm/index.ts
CHANGED
|
@@ -5,7 +5,7 @@ export * from "./createConcisenessEvaluator";
|
|
|
5
5
|
export * from "./createCorrectnessEvaluator";
|
|
6
6
|
export * from "./createDocumentRelevanceEvaluator";
|
|
7
7
|
export * from "./createFaithfulnessEvaluator";
|
|
8
|
-
export * from "./createHallucinationEvaluator";
|
|
8
|
+
export * from "./createHallucinationEvaluator";
|
|
9
9
|
export * from "./createRefusalEvaluator";
|
|
10
10
|
export * from "./createToolInvocationEvaluator";
|
|
11
11
|
export * from "./createToolResponseHandlingEvaluator";
|
|
@@ -15,12 +15,12 @@ export function getTemplateVariables(args: GetTemplateVariableArgs): string[] {
|
|
|
15
15
|
if (typeof template === "string") {
|
|
16
16
|
return getTemplateVariablesFromString(template);
|
|
17
17
|
}
|
|
18
|
-
return template.reduce((acc, message) => {
|
|
18
|
+
return template.reduce<string[]>((acc, message) => {
|
|
19
19
|
if (message.content !== undefined && typeof message.content === "string") {
|
|
20
20
|
return [...acc, ...getTemplateVariablesFromString(message.content)];
|
|
21
21
|
}
|
|
22
22
|
return acc;
|
|
23
|
-
}, []
|
|
23
|
+
}, []);
|
|
24
24
|
}
|
|
25
25
|
/**
|
|
26
26
|
* Parse out the template variables of a string template
|
|
@@ -29,11 +29,11 @@ export function getTemplateVariables(args: GetTemplateVariableArgs): string[] {
|
|
|
29
29
|
*/
|
|
30
30
|
function getTemplateVariablesFromString(template: string): string[] {
|
|
31
31
|
const templateSpans = Mustache.parse(template);
|
|
32
|
-
return templateSpans.reduce((acc, templateSpan) => {
|
|
32
|
+
return templateSpans.reduce<string[]>((acc, templateSpan) => {
|
|
33
33
|
const [spanType, value] = templateSpan;
|
|
34
34
|
if (spanType === "name" && typeof value === "string") {
|
|
35
35
|
acc = [...acc, value];
|
|
36
36
|
}
|
|
37
37
|
return acc;
|
|
38
|
-
}, []
|
|
38
|
+
}, []);
|
|
39
39
|
}
|