@arizeai/phoenix-evals 0.7.0 → 0.8.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/FAITHFULNESS_CLASSIFICATION_EVALUATOR_CONFIG.d.ts +3 -0
- package/dist/esm/__generated__/default_templates/FAITHFULNESS_CLASSIFICATION_EVALUATOR_CONFIG.d.ts.map +1 -0
- package/dist/esm/__generated__/default_templates/FAITHFULNESS_CLASSIFICATION_EVALUATOR_CONFIG.js +33 -0
- package/dist/esm/__generated__/default_templates/FAITHFULNESS_CLASSIFICATION_EVALUATOR_CONFIG.js.map +1 -0
- package/dist/esm/__generated__/default_templates/TOOL_INVOCATION_CLASSIFICATION_EVALUATOR_CONFIG.d.ts +3 -0
- package/dist/esm/__generated__/default_templates/TOOL_INVOCATION_CLASSIFICATION_EVALUATOR_CONFIG.d.ts.map +1 -0
- package/dist/esm/__generated__/default_templates/TOOL_INVOCATION_CLASSIFICATION_EVALUATOR_CONFIG.js +30 -0
- package/dist/esm/__generated__/default_templates/TOOL_INVOCATION_CLASSIFICATION_EVALUATOR_CONFIG.js.map +1 -0
- package/dist/esm/__generated__/default_templates/index.d.ts +2 -0
- package/dist/esm/__generated__/default_templates/index.d.ts.map +1 -1
- package/dist/esm/__generated__/default_templates/index.js +2 -0
- package/dist/esm/__generated__/default_templates/index.js.map +1 -1
- package/dist/esm/core/EvaluatorBase.d.ts.map +1 -1
- package/dist/esm/llm/createCorrectnessEvaluator.d.ts.map +1 -1
- package/dist/esm/llm/createDocumentRelevanceEvaluator.d.ts.map +1 -1
- package/dist/esm/llm/createDocumentRelevanceEvaluator.js.map +1 -1
- package/dist/esm/llm/createFaithfulnessEvaluator.d.ts +24 -0
- package/dist/esm/llm/createFaithfulnessEvaluator.d.ts.map +1 -0
- package/dist/esm/llm/createFaithfulnessEvaluator.js +19 -0
- package/dist/esm/llm/createFaithfulnessEvaluator.js.map +1 -0
- package/dist/esm/llm/createHallucinationEvaluator.d.ts +12 -0
- package/dist/esm/llm/createHallucinationEvaluator.d.ts.map +1 -1
- package/dist/esm/llm/createHallucinationEvaluator.js +17 -0
- package/dist/esm/llm/createHallucinationEvaluator.js.map +1 -1
- package/dist/esm/llm/createToolInvocationEvaluator.d.ts +74 -0
- package/dist/esm/llm/createToolInvocationEvaluator.d.ts.map +1 -0
- package/dist/esm/llm/createToolInvocationEvaluator.js +60 -0
- package/dist/esm/llm/createToolInvocationEvaluator.js.map +1 -0
- package/dist/esm/llm/index.d.ts +7 -5
- package/dist/esm/llm/index.d.ts.map +1 -1
- package/dist/esm/llm/index.js +7 -5
- package/dist/esm/llm/index.js.map +1 -1
- package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/dist/esm/types/evals.d.ts.map +1 -1
- package/dist/src/__generated__/default_templates/FAITHFULNESS_CLASSIFICATION_EVALUATOR_CONFIG.d.ts +3 -0
- package/dist/src/__generated__/default_templates/FAITHFULNESS_CLASSIFICATION_EVALUATOR_CONFIG.d.ts.map +1 -0
- package/dist/src/__generated__/default_templates/FAITHFULNESS_CLASSIFICATION_EVALUATOR_CONFIG.js +36 -0
- package/dist/src/__generated__/default_templates/FAITHFULNESS_CLASSIFICATION_EVALUATOR_CONFIG.js.map +1 -0
- package/dist/src/__generated__/default_templates/TOOL_INVOCATION_CLASSIFICATION_EVALUATOR_CONFIG.d.ts +3 -0
- package/dist/src/__generated__/default_templates/TOOL_INVOCATION_CLASSIFICATION_EVALUATOR_CONFIG.d.ts.map +1 -0
- package/dist/src/__generated__/default_templates/TOOL_INVOCATION_CLASSIFICATION_EVALUATOR_CONFIG.js +33 -0
- package/dist/src/__generated__/default_templates/TOOL_INVOCATION_CLASSIFICATION_EVALUATOR_CONFIG.js.map +1 -0
- package/dist/src/__generated__/default_templates/index.d.ts +2 -0
- package/dist/src/__generated__/default_templates/index.d.ts.map +1 -1
- package/dist/src/__generated__/default_templates/index.js +5 -1
- package/dist/src/__generated__/default_templates/index.js.map +1 -1
- package/dist/src/core/EvaluatorBase.d.ts.map +1 -1
- package/dist/src/llm/createCorrectnessEvaluator.d.ts.map +1 -1
- package/dist/src/llm/createDocumentRelevanceEvaluator.d.ts.map +1 -1
- package/dist/src/llm/createDocumentRelevanceEvaluator.js.map +1 -1
- package/dist/src/llm/createFaithfulnessEvaluator.d.ts +24 -0
- package/dist/src/llm/createFaithfulnessEvaluator.d.ts.map +1 -0
- package/dist/src/llm/createFaithfulnessEvaluator.js +30 -0
- package/dist/src/llm/createFaithfulnessEvaluator.js.map +1 -0
- package/dist/src/llm/createHallucinationEvaluator.d.ts +12 -0
- package/dist/src/llm/createHallucinationEvaluator.d.ts.map +1 -1
- package/dist/src/llm/createHallucinationEvaluator.js +17 -0
- package/dist/src/llm/createHallucinationEvaluator.js.map +1 -1
- package/dist/src/llm/createToolInvocationEvaluator.d.ts +74 -0
- package/dist/src/llm/createToolInvocationEvaluator.d.ts.map +1 -0
- package/dist/src/llm/createToolInvocationEvaluator.js +71 -0
- package/dist/src/llm/createToolInvocationEvaluator.js.map +1 -0
- package/dist/src/llm/index.d.ts +7 -5
- package/dist/src/llm/index.d.ts.map +1 -1
- package/dist/src/llm/index.js +7 -5
- package/dist/src/llm/index.js.map +1 -1
- package/dist/src/types/evals.d.ts.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/__generated__/default_templates/FAITHFULNESS_CLASSIFICATION_EVALUATOR_CONFIG.ts +35 -0
- package/src/__generated__/default_templates/TOOL_INVOCATION_CLASSIFICATION_EVALUATOR_CONFIG.ts +32 -0
- package/src/__generated__/default_templates/index.ts +2 -0
- package/src/core/EvaluatorBase.ts +3 -3
- package/src/llm/createCorrectnessEvaluator.ts +3 -3
- package/src/llm/createDocumentRelevanceEvaluator.ts +7 -11
- package/src/llm/createFaithfulnessEvaluator.ts +52 -0
- package/src/llm/createHallucinationEvaluator.ts +25 -3
- package/src/llm/createToolInvocationEvaluator.ts +103 -0
- package/src/llm/index.ts +7 -5
- package/src/types/evals.ts +5 -4
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// This file is generated. Do not edit by hand.
|
|
2
|
+
|
|
3
|
+
import type { ClassificationEvaluatorConfig } from "../types";
|
|
4
|
+
|
|
5
|
+
export const FAITHFULNESS_CLASSIFICATION_EVALUATOR_CONFIG: ClassificationEvaluatorConfig = {
|
|
6
|
+
name: "faithfulness",
|
|
7
|
+
description: "A specialized evaluator for detecting faithfulness in grounded LLM responses.",
|
|
8
|
+
optimizationDirection: "MAXIMIZE",
|
|
9
|
+
template: [
|
|
10
|
+
{
|
|
11
|
+
role: "user",
|
|
12
|
+
content: `
|
|
13
|
+
In this task, you will be presented with a query, some context and a response. The response is generated to the question based on the context. The response may contain false information. You must use the context to determine if the response to the question contains false information, if the response is unfaithful to the facts. Your objective is to determine whether the response text contains factual information and is faithful to the context. An 'unfaithful' response refers to a response that is not based on the context or assumes information that is not available in the context. Your response should be a single word: either 'faithful' or 'unfaithful', and it should not include any other text or characters. 'unfaithful' indicates that the response provides factually inaccurate information to the query based on the context. 'faithful' indicates that the response to the question is correct relative to the context, and does not contain made up information. Please read the query and context carefully before determining your response.
|
|
14
|
+
|
|
15
|
+
<data>
|
|
16
|
+
<query>
|
|
17
|
+
{{input}}
|
|
18
|
+
</query>
|
|
19
|
+
<context>
|
|
20
|
+
{{context}}
|
|
21
|
+
</context>
|
|
22
|
+
<response>
|
|
23
|
+
{{output}}
|
|
24
|
+
</response>
|
|
25
|
+
</data>
|
|
26
|
+
|
|
27
|
+
Is the response above faithful or unfaithful based on the query and context?
|
|
28
|
+
`,
|
|
29
|
+
},
|
|
30
|
+
],
|
|
31
|
+
choices: {
|
|
32
|
+
"faithful": 1,
|
|
33
|
+
"unfaithful": 0
|
|
34
|
+
},
|
|
35
|
+
};
|
package/src/__generated__/default_templates/TOOL_INVOCATION_CLASSIFICATION_EVALUATOR_CONFIG.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// This file is generated. Do not edit by hand.
|
|
2
|
+
|
|
3
|
+
import type { ClassificationEvaluatorConfig } from "../types";
|
|
4
|
+
|
|
5
|
+
export const TOOL_INVOCATION_CLASSIFICATION_EVALUATOR_CONFIG: ClassificationEvaluatorConfig = {
|
|
6
|
+
name: "tool_invocation",
|
|
7
|
+
description: "For determining if a tool was invoked correctly with proper arguments, formatting, and safe content. Requires conversation context, available tool schemas, and the LLM's tool invocation(s).",
|
|
8
|
+
optimizationDirection: "MAXIMIZE",
|
|
9
|
+
template: [
|
|
10
|
+
{
|
|
11
|
+
role: "user",
|
|
12
|
+
content: `
|
|
13
|
+
You are an impartial judge evaluating an LLM's tool-calling behavior, specifically whether the LLM invoked a tool (or tools) correctly with valid arguments and proper formatting.
|
|
14
|
+
Your task: Determine whether the LLM's tool invocation(s) were correct or incorrect based on: - The full conversation context (including all previous turns, not just the most recent message) - The available tool schemas - The LLM's tool invocation(s) with arguments
|
|
15
|
+
IMPORTANT - Tool Invocation vs. Tool Selection: - You are ONLY evaluating the tool invocation, not the tool selection. - If the tool selection is incorrect or not relevant to the user's query, but the tool invocation is correct, return "correct". - If the tool selection is correct but the tool invocation is incorrect, return "incorrect".
|
|
16
|
+
IMPORTANT - Multi-Tool Invocations: - The LLM may invoke MULTIPLE tools in a single response. This is valid and expected for complex requests. - When multiple tools are invoked, evaluate EACH tool invocation independently. - Return "correct" only if ALL tool invocations are correct. - Return "incorrect" if ANY tool invocation has an error.
|
|
17
|
+
IMPORTANT - Conversation Context: - Read the entire conversation history carefully, not just the final user message. - Argument values may need to be extracted from EARLIER turns in the conversation (e.g., user mentions a location, date, or quantity in a previous message). - The LLM should use context from the full conversation to populate argument values correctly.
|
|
18
|
+
Criteria Return "correct" only when ALL of the following are true for EVERY tool invocation: - JSON is properly structured (if applicable). - All required fields/parameters are present. - No hallucinated or nonexistent fields (all fields exist in the tool schema). - Argument values match the user's intent from the conversation context (correct types, realistic values). - No unsafe content (e.g., PII like SSNs, credit card numbers, passwords) in arguments.
|
|
19
|
+
Return "incorrect" if ANY of the following are true for ANY tool invocation: - The invocation contains hallucinated or nonexistent fields not in the schema. - Required fields/parameters are missing. - JSON is improperly formatted or malformed. - Argument values are incorrect, hallucinated, or do not match user intent from the conversation. - Arguments contain unsafe content (e.g., PII, sensitive data that should not be passed).
|
|
20
|
+
Before providing your final judgment, explain your reasoning and consider: - How many tools were invoked? Evaluate each one. - Does each tool invocation match the schema for that tool? - Are all required parameters provided with appropriate values for each invocation? - Are there any extra fields that don't exist in the schema? - Looking at the FULL conversation: do the argument values accurately reflect what the user requested across all messages? - Is there any unsafe or sensitive content in any of the arguments? - Check that you are not evaluating the tool selection, only the tool invocation.
|
|
21
|
+
<data> <context> {{input}} </context>
|
|
22
|
+
<available_tools> {{availableTools}} </available_tools>
|
|
23
|
+
<tool_invocation> {{toolSelection}} </tool_invocation> </data>
|
|
24
|
+
Given the above data, is the tool invocation correct or incorrect?
|
|
25
|
+
`,
|
|
26
|
+
},
|
|
27
|
+
],
|
|
28
|
+
choices: {
|
|
29
|
+
"correct": 1,
|
|
30
|
+
"incorrect": 0
|
|
31
|
+
},
|
|
32
|
+
};
|
|
@@ -2,5 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
export { CORRECTNESS_CLASSIFICATION_EVALUATOR_CONFIG } from "./CORRECTNESS_CLASSIFICATION_EVALUATOR_CONFIG";
|
|
4
4
|
export { DOCUMENT_RELEVANCE_CLASSIFICATION_EVALUATOR_CONFIG } from "./DOCUMENT_RELEVANCE_CLASSIFICATION_EVALUATOR_CONFIG";
|
|
5
|
+
export { FAITHFULNESS_CLASSIFICATION_EVALUATOR_CONFIG } from "./FAITHFULNESS_CLASSIFICATION_EVALUATOR_CONFIG";
|
|
5
6
|
export { HALLUCINATION_CLASSIFICATION_EVALUATOR_CONFIG } from "./HALLUCINATION_CLASSIFICATION_EVALUATOR_CONFIG";
|
|
7
|
+
export { TOOL_INVOCATION_CLASSIFICATION_EVALUATOR_CONFIG } from "./TOOL_INVOCATION_CLASSIFICATION_EVALUATOR_CONFIG";
|
|
6
8
|
export { TOOL_SELECTION_CLASSIFICATION_EVALUATOR_CONFIG } from "./TOOL_SELECTION_CLASSIFICATION_EVALUATOR_CONFIG";
|
|
@@ -11,9 +11,9 @@ import { ObjectMapping } from "../types/data";
|
|
|
11
11
|
/**
|
|
12
12
|
* Base class for all evaluators
|
|
13
13
|
*/
|
|
14
|
-
export abstract class EvaluatorBase<
|
|
15
|
-
|
|
16
|
-
{
|
|
14
|
+
export abstract class EvaluatorBase<
|
|
15
|
+
RecordType extends Record<string, unknown>,
|
|
16
|
+
> implements EvaluatorInterface<RecordType> {
|
|
17
17
|
readonly name: string;
|
|
18
18
|
readonly kind: EvaluationKind;
|
|
19
19
|
readonly optimizationDirection?: OptimizationDirection;
|
|
@@ -7,9 +7,9 @@ import { createClassificationEvaluator } from "./createClassificationEvaluator";
|
|
|
7
7
|
export interface CorrectnessEvaluatorArgs<
|
|
8
8
|
RecordType extends Record<string, unknown> = CorrectnessEvaluationRecord,
|
|
9
9
|
> extends Omit<
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
10
|
+
CreateClassificationEvaluatorArgs<RecordType>,
|
|
11
|
+
"promptTemplate" | "choices" | "optimizationDirection" | "name"
|
|
12
|
+
> {
|
|
13
13
|
optimizationDirection?: CreateClassificationEvaluatorArgs<RecordType>["optimizationDirection"];
|
|
14
14
|
name?: CreateClassificationEvaluatorArgs<RecordType>["name"];
|
|
15
15
|
choices?: CreateClassificationEvaluatorArgs<RecordType>["choices"];
|
|
@@ -5,14 +5,12 @@ import { ClassificationEvaluator } from "./ClassificationEvaluator";
|
|
|
5
5
|
import { createClassificationEvaluator } from "./createClassificationEvaluator";
|
|
6
6
|
|
|
7
7
|
export interface DocumentRelevanceEvaluatorArgs<
|
|
8
|
-
RecordType extends Record<
|
|
9
|
-
|
|
10
|
-
unknown
|
|
11
|
-
> = DocumentRelevanceEvaluationRecord,
|
|
8
|
+
RecordType extends Record<string, unknown> =
|
|
9
|
+
DocumentRelevanceEvaluationRecord,
|
|
12
10
|
> extends Omit<
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
11
|
+
CreateClassificationEvaluatorArgs<RecordType>,
|
|
12
|
+
"promptTemplate" | "choices" | "optimizationDirection" | "name"
|
|
13
|
+
> {
|
|
16
14
|
optimizationDirection?: CreateClassificationEvaluatorArgs<RecordType>["optimizationDirection"];
|
|
17
15
|
name?: CreateClassificationEvaluatorArgs<RecordType>["name"];
|
|
18
16
|
choices?: CreateClassificationEvaluatorArgs<RecordType>["choices"];
|
|
@@ -55,10 +53,8 @@ export interface DocumentRelevanceEvaluationRecord {
|
|
|
55
53
|
* ```
|
|
56
54
|
*/
|
|
57
55
|
export function createDocumentRelevanceEvaluator<
|
|
58
|
-
RecordType extends Record<
|
|
59
|
-
|
|
60
|
-
unknown
|
|
61
|
-
> = DocumentRelevanceEvaluationRecord,
|
|
56
|
+
RecordType extends Record<string, unknown> =
|
|
57
|
+
DocumentRelevanceEvaluationRecord,
|
|
62
58
|
>(
|
|
63
59
|
args: DocumentRelevanceEvaluatorArgs<RecordType>
|
|
64
60
|
): ClassificationEvaluator<RecordType> {
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { FAITHFULNESS_CLASSIFICATION_EVALUATOR_CONFIG } from "../__generated__/default_templates";
|
|
2
|
+
import { CreateClassificationEvaluatorArgs } from "../types/evals";
|
|
3
|
+
|
|
4
|
+
import { ClassificationEvaluator } from "./ClassificationEvaluator";
|
|
5
|
+
import { createClassificationEvaluator } from "./createClassificationEvaluator";
|
|
6
|
+
|
|
7
|
+
export interface FaithfulnessEvaluatorArgs<
|
|
8
|
+
RecordType extends Record<string, unknown> = FaithfulnessEvaluationRecord,
|
|
9
|
+
> extends Omit<
|
|
10
|
+
CreateClassificationEvaluatorArgs<RecordType>,
|
|
11
|
+
"promptTemplate" | "choices" | "optimizationDirection" | "name"
|
|
12
|
+
> {
|
|
13
|
+
optimizationDirection?: CreateClassificationEvaluatorArgs<RecordType>["optimizationDirection"];
|
|
14
|
+
name?: CreateClassificationEvaluatorArgs<RecordType>["name"];
|
|
15
|
+
choices?: CreateClassificationEvaluatorArgs<RecordType>["choices"];
|
|
16
|
+
promptTemplate?: CreateClassificationEvaluatorArgs<RecordType>["promptTemplate"];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* A record to be evaluated by the faithfulness evaluator.
|
|
21
|
+
*/
|
|
22
|
+
export type FaithfulnessEvaluationRecord = {
|
|
23
|
+
input: string;
|
|
24
|
+
output: string;
|
|
25
|
+
context?: string;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Creates a function that evaluates whether an answer is faithful or unfaithful based on a query and reference text.
|
|
29
|
+
*
|
|
30
|
+
* @param args - The arguments for creating the faithfulness evaluator.
|
|
31
|
+
* @returns A function that evaluates whether an answer is faithful or unfaithful based on a query and reference text.
|
|
32
|
+
*/
|
|
33
|
+
export function createFaithfulnessEvaluator<
|
|
34
|
+
RecordType extends Record<string, unknown> = FaithfulnessEvaluationRecord,
|
|
35
|
+
>(
|
|
36
|
+
args: FaithfulnessEvaluatorArgs<RecordType>
|
|
37
|
+
): ClassificationEvaluator<RecordType> {
|
|
38
|
+
const {
|
|
39
|
+
choices = FAITHFULNESS_CLASSIFICATION_EVALUATOR_CONFIG.choices,
|
|
40
|
+
promptTemplate = FAITHFULNESS_CLASSIFICATION_EVALUATOR_CONFIG.template,
|
|
41
|
+
optimizationDirection = FAITHFULNESS_CLASSIFICATION_EVALUATOR_CONFIG.optimizationDirection,
|
|
42
|
+
name = FAITHFULNESS_CLASSIFICATION_EVALUATOR_CONFIG.name,
|
|
43
|
+
...rest
|
|
44
|
+
} = args;
|
|
45
|
+
return createClassificationEvaluator<RecordType>({
|
|
46
|
+
...rest,
|
|
47
|
+
promptTemplate,
|
|
48
|
+
choices,
|
|
49
|
+
optimizationDirection,
|
|
50
|
+
name,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
@@ -1,3 +1,10 @@
|
|
|
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
|
+
|
|
1
8
|
import { HALLUCINATION_CLASSIFICATION_EVALUATOR_CONFIG } from "../__generated__/default_templates";
|
|
2
9
|
import { CreateClassificationEvaluatorArgs } from "../types/evals";
|
|
3
10
|
|
|
@@ -7,9 +14,9 @@ import { createClassificationEvaluator } from "./createClassificationEvaluator";
|
|
|
7
14
|
export interface HallucinationEvaluatorArgs<
|
|
8
15
|
RecordType extends Record<string, unknown> = HallucinationEvaluationRecord,
|
|
9
16
|
> extends Omit<
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
17
|
+
CreateClassificationEvaluatorArgs<RecordType>,
|
|
18
|
+
"promptTemplate" | "choices" | "optimizationDirection" | "name"
|
|
19
|
+
> {
|
|
13
20
|
optimizationDirection?: CreateClassificationEvaluatorArgs<RecordType>["optimizationDirection"];
|
|
14
21
|
name?: CreateClassificationEvaluatorArgs<RecordType>["name"];
|
|
15
22
|
choices?: CreateClassificationEvaluatorArgs<RecordType>["choices"];
|
|
@@ -24,9 +31,16 @@ export type HallucinationEvaluationRecord = {
|
|
|
24
31
|
output: string;
|
|
25
32
|
context?: string;
|
|
26
33
|
};
|
|
34
|
+
|
|
27
35
|
/**
|
|
36
|
+
* @deprecated Use createFaithfulnessEvaluator instead.
|
|
37
|
+
*
|
|
28
38
|
* Creates a function that evaluates whether an answer is factual or hallucinated based on a query and reference text.
|
|
29
39
|
*
|
|
40
|
+
* Note: This is deprecated. Please use createFaithfulnessEvaluator which:
|
|
41
|
+
* - Uses 'faithful'/'unfaithful' labels instead of 'factual'/'hallucinated'
|
|
42
|
+
* - Maximizes the score (1.0 for faithful, 0.0 for unfaithful)
|
|
43
|
+
*
|
|
30
44
|
* @param args - The arguments for creating the hallucination evaluator.
|
|
31
45
|
* @returns A function that evaluates whether an answer is factual or hallucinated based on a query and reference text.
|
|
32
46
|
*/
|
|
@@ -35,6 +49,14 @@ export function createHallucinationEvaluator<
|
|
|
35
49
|
>(
|
|
36
50
|
args: HallucinationEvaluatorArgs<RecordType>
|
|
37
51
|
): ClassificationEvaluator<RecordType> {
|
|
52
|
+
// eslint-disable-next-line no-console
|
|
53
|
+
console.warn(
|
|
54
|
+
"createHallucinationEvaluator is deprecated and will be removed in a future version. " +
|
|
55
|
+
"Please use createFaithfulnessEvaluator instead. The new evaluator uses " +
|
|
56
|
+
"'faithful'/'unfaithful' labels and maximizes score (1.0=faithful) instead of " +
|
|
57
|
+
"minimizing it (0.0=factual)."
|
|
58
|
+
);
|
|
59
|
+
|
|
38
60
|
const {
|
|
39
61
|
choices = HALLUCINATION_CLASSIFICATION_EVALUATOR_CONFIG.choices,
|
|
40
62
|
promptTemplate = HALLUCINATION_CLASSIFICATION_EVALUATOR_CONFIG.template,
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { TOOL_INVOCATION_CLASSIFICATION_EVALUATOR_CONFIG } from "../__generated__/default_templates";
|
|
2
|
+
import { CreateClassificationEvaluatorArgs } from "../types/evals";
|
|
3
|
+
|
|
4
|
+
import { ClassificationEvaluator } from "./ClassificationEvaluator";
|
|
5
|
+
import { createClassificationEvaluator } from "./createClassificationEvaluator";
|
|
6
|
+
|
|
7
|
+
export interface ToolInvocationEvaluatorArgs<
|
|
8
|
+
RecordType extends Record<string, unknown> = ToolInvocationEvaluationRecord,
|
|
9
|
+
> extends Omit<
|
|
10
|
+
CreateClassificationEvaluatorArgs<RecordType>,
|
|
11
|
+
"promptTemplate" | "choices" | "optimizationDirection" | "name"
|
|
12
|
+
> {
|
|
13
|
+
optimizationDirection?: CreateClassificationEvaluatorArgs<RecordType>["optimizationDirection"];
|
|
14
|
+
name?: CreateClassificationEvaluatorArgs<RecordType>["name"];
|
|
15
|
+
choices?: CreateClassificationEvaluatorArgs<RecordType>["choices"];
|
|
16
|
+
promptTemplate?: CreateClassificationEvaluatorArgs<RecordType>["promptTemplate"];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* A record to be evaluated by the tool invocation evaluator.
|
|
21
|
+
*/
|
|
22
|
+
export type ToolInvocationEvaluationRecord = {
|
|
23
|
+
/**
|
|
24
|
+
* The input query or conversation context.
|
|
25
|
+
*/
|
|
26
|
+
input: string;
|
|
27
|
+
/**
|
|
28
|
+
* The available tool schemas, either as JSON schema or human-readable format.
|
|
29
|
+
*/
|
|
30
|
+
availableTools: string;
|
|
31
|
+
/**
|
|
32
|
+
* The tool invocation(s) made by the LLM, including arguments.
|
|
33
|
+
*/
|
|
34
|
+
toolSelection: string;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Creates a tool invocation evaluator function.
|
|
39
|
+
*
|
|
40
|
+
* This function returns an evaluator that determines whether a tool was invoked
|
|
41
|
+
* correctly with proper arguments, formatting, and safe content.
|
|
42
|
+
*
|
|
43
|
+
* The evaluator checks for:
|
|
44
|
+
* - Properly structured JSON (if applicable)
|
|
45
|
+
* - All required fields/parameters present
|
|
46
|
+
* - No hallucinated or nonexistent fields
|
|
47
|
+
* - Argument values matching user query and schema expectations
|
|
48
|
+
* - No unsafe content (e.g., PII) in arguments
|
|
49
|
+
*
|
|
50
|
+
* @param args - The arguments for creating the tool invocation evaluator.
|
|
51
|
+
* @param args.model - The model to use for classification.
|
|
52
|
+
* @param args.choices - The possible classification choices (defaults to correct/incorrect).
|
|
53
|
+
* @param args.promptTemplate - The prompt template to use (defaults to TOOL_INVOCATION_TEMPLATE).
|
|
54
|
+
* @param args.telemetry - The telemetry to use for the evaluator.
|
|
55
|
+
*
|
|
56
|
+
* @returns An evaluator function that takes a {@link ToolInvocationEvaluationRecord} and returns
|
|
57
|
+
* a classification result indicating whether the tool invocation is correct or incorrect.
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```ts
|
|
61
|
+
* const evaluator = createToolInvocationEvaluator({ model: openai("gpt-4o-mini") });
|
|
62
|
+
*
|
|
63
|
+
* // Example with JSON schema format for available tools
|
|
64
|
+
* const result = await evaluator.evaluate({
|
|
65
|
+
* input: "User: Book a flight from NYC to LA for tomorrow",
|
|
66
|
+
* availableTools: JSON.stringify({
|
|
67
|
+
* name: "book_flight",
|
|
68
|
+
* description: "Book a flight between two cities",
|
|
69
|
+
* parameters: {
|
|
70
|
+
* type: "object",
|
|
71
|
+
* properties: {
|
|
72
|
+
* origin: { type: "string", description: "Departure city code" },
|
|
73
|
+
* destination: { type: "string", description: "Arrival city code" },
|
|
74
|
+
* date: { type: "string", description: "Flight date in YYYY-MM-DD" }
|
|
75
|
+
* },
|
|
76
|
+
* required: ["origin", "destination", "date"]
|
|
77
|
+
* }
|
|
78
|
+
* }),
|
|
79
|
+
* toolSelection: 'book_flight(origin="NYC", destination="LA", date="2024-01-15")'
|
|
80
|
+
* });
|
|
81
|
+
* console.log(result.label); // "correct" or "incorrect"
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
export function createToolInvocationEvaluator<
|
|
85
|
+
RecordType extends Record<string, unknown> = ToolInvocationEvaluationRecord,
|
|
86
|
+
>(
|
|
87
|
+
args: ToolInvocationEvaluatorArgs<RecordType>
|
|
88
|
+
): ClassificationEvaluator<RecordType> {
|
|
89
|
+
const {
|
|
90
|
+
choices = TOOL_INVOCATION_CLASSIFICATION_EVALUATOR_CONFIG.choices,
|
|
91
|
+
promptTemplate = TOOL_INVOCATION_CLASSIFICATION_EVALUATOR_CONFIG.template,
|
|
92
|
+
optimizationDirection = TOOL_INVOCATION_CLASSIFICATION_EVALUATOR_CONFIG.optimizationDirection,
|
|
93
|
+
name = TOOL_INVOCATION_CLASSIFICATION_EVALUATOR_CONFIG.name,
|
|
94
|
+
...rest
|
|
95
|
+
} = args;
|
|
96
|
+
return createClassificationEvaluator<RecordType>({
|
|
97
|
+
...rest,
|
|
98
|
+
promptTemplate,
|
|
99
|
+
choices,
|
|
100
|
+
optimizationDirection,
|
|
101
|
+
name,
|
|
102
|
+
});
|
|
103
|
+
}
|
package/src/llm/index.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
export * from "./
|
|
2
|
-
export * from "./createClassifierFn";
|
|
1
|
+
export * from "./ClassificationEvaluator";
|
|
3
2
|
export * from "./createClassificationEvaluator";
|
|
4
|
-
export * from "./
|
|
5
|
-
export * from "./createDocumentRelevanceEvaluator";
|
|
3
|
+
export * from "./createClassifierFn";
|
|
6
4
|
export * from "./createCorrectnessEvaluator";
|
|
7
|
-
export * from "./
|
|
5
|
+
export * from "./createDocumentRelevanceEvaluator";
|
|
6
|
+
export * from "./createFaithfulnessEvaluator";
|
|
7
|
+
export * from "./createHallucinationEvaluator"; // Deprecated: use createFaithfulnessEvaluator
|
|
8
|
+
export * from "./createToolInvocationEvaluator";
|
|
9
|
+
export * from "./generateClassification";
|
|
8
10
|
export * from "./LLMEvaluator";
|
package/src/types/evals.ts
CHANGED
|
@@ -110,8 +110,8 @@ export type CreateLLMEvaluatorArgs<RecordType extends Record<string, unknown>> =
|
|
|
110
110
|
|
|
111
111
|
export interface CreateClassificationEvaluatorArgs<
|
|
112
112
|
RecordType extends Record<string, unknown>,
|
|
113
|
-
>
|
|
114
|
-
|
|
113
|
+
>
|
|
114
|
+
extends CreateClassifierArgs, CreateLLMEvaluatorArgs<RecordType> {
|
|
115
115
|
/**
|
|
116
116
|
* The prompt template to use for classification
|
|
117
117
|
*/
|
|
@@ -156,8 +156,9 @@ interface EvaluatorDescription {
|
|
|
156
156
|
* The Base Evaluator interface
|
|
157
157
|
* This is the interface that all evaluators must implement
|
|
158
158
|
*/
|
|
159
|
-
export interface EvaluatorInterface<
|
|
160
|
-
extends
|
|
159
|
+
export interface EvaluatorInterface<
|
|
160
|
+
ExampleType extends Record<string, unknown>,
|
|
161
|
+
> extends EvaluatorDescription {
|
|
161
162
|
/**
|
|
162
163
|
* The function that evaluates the example
|
|
163
164
|
*/
|