@arizeai/phoenix-evals 2.5.0 → 2.7.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/CHANGELOG.md +16 -0
- package/dist/esm/decision/decideClassification.d.ts +20 -0
- package/dist/esm/decision/decideClassification.d.ts.map +1 -0
- package/dist/esm/decision/decideClassification.js +72 -0
- package/dist/esm/decision/decideClassification.js.map +1 -0
- package/dist/esm/decision/decisionPrompt.d.ts +8 -0
- package/dist/esm/decision/decisionPrompt.d.ts.map +1 -0
- package/dist/esm/decision/decisionPrompt.js +66 -0
- package/dist/esm/decision/decisionPrompt.js.map +1 -0
- package/dist/esm/llm/ClassificationEvaluator.d.ts +2 -1
- package/dist/esm/llm/ClassificationEvaluator.d.ts.map +1 -1
- package/dist/esm/llm/ClassificationEvaluator.js.map +1 -1
- package/dist/esm/llm/generateClassification.d.ts +6 -0
- package/dist/esm/llm/generateClassification.d.ts.map +1 -1
- package/dist/esm/llm/generateClassification.js +21 -7
- package/dist/esm/llm/generateClassification.js.map +1 -1
- package/dist/esm/telemetry/index.d.ts +14 -0
- package/dist/esm/telemetry/index.d.ts.map +1 -1
- package/dist/esm/telemetry/index.js +15 -0
- package/dist/esm/telemetry/index.js.map +1 -1
- package/dist/esm/tsconfig.esm.tsbuildinfo +1 -1
- package/dist/esm/types/evals.d.ts +16 -2
- package/dist/esm/types/evals.d.ts.map +1 -1
- package/dist/esm/utils/index.d.ts +1 -0
- package/dist/esm/utils/index.d.ts.map +1 -1
- package/dist/esm/utils/index.js +1 -0
- package/dist/esm/utils/index.js.map +1 -1
- package/dist/esm/utils/isDecisionModel.d.ts +18 -0
- package/dist/esm/utils/isDecisionModel.d.ts.map +1 -0
- package/dist/esm/utils/isDecisionModel.js +13 -0
- package/dist/esm/utils/isDecisionModel.js.map +1 -0
- package/dist/src/decision/decideClassification.d.ts +20 -0
- package/dist/src/decision/decideClassification.d.ts.map +1 -0
- package/dist/src/decision/decideClassification.js +71 -0
- package/dist/src/decision/decideClassification.js.map +1 -0
- package/dist/src/decision/decisionPrompt.d.ts +8 -0
- package/dist/src/decision/decisionPrompt.d.ts.map +1 -0
- package/dist/src/decision/decisionPrompt.js +69 -0
- package/dist/src/decision/decisionPrompt.js.map +1 -0
- package/dist/src/llm/ClassificationEvaluator.d.ts +2 -1
- package/dist/src/llm/ClassificationEvaluator.d.ts.map +1 -1
- package/dist/src/llm/ClassificationEvaluator.js.map +1 -1
- package/dist/src/llm/generateClassification.d.ts +6 -0
- package/dist/src/llm/generateClassification.d.ts.map +1 -1
- package/dist/src/llm/generateClassification.js +20 -7
- package/dist/src/llm/generateClassification.js.map +1 -1
- package/dist/src/telemetry/index.d.ts +14 -0
- package/dist/src/telemetry/index.d.ts.map +1 -1
- package/dist/src/telemetry/index.js +17 -0
- package/dist/src/telemetry/index.js.map +1 -1
- package/dist/src/types/evals.d.ts +16 -2
- package/dist/src/types/evals.d.ts.map +1 -1
- package/dist/src/utils/index.d.ts +1 -0
- package/dist/src/utils/index.d.ts.map +1 -1
- package/dist/src/utils/index.js +1 -0
- package/dist/src/utils/index.js.map +1 -1
- package/dist/src/utils/isDecisionModel.d.ts +18 -0
- package/dist/src/utils/isDecisionModel.d.ts.map +1 -0
- package/dist/src/utils/isDecisionModel.js +16 -0
- package/dist/src/utils/isDecisionModel.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/docs/classification.mdx +43 -1
- package/package.json +13 -12
- package/src/decision/decideClassification.ts +85 -0
- package/src/decision/decisionPrompt.ts +70 -0
- package/src/llm/ClassificationEvaluator.ts +2 -1
- package/src/llm/generateClassification.ts +22 -7
- package/src/telemetry/index.ts +21 -0
- package/src/types/evals.ts +17 -3
- package/src/utils/index.ts +1 -0
- package/src/utils/isDecisionModel.ts +29 -0
package/docs/classification.mdx
CHANGED
|
@@ -25,9 +25,49 @@ const result = await classify({
|
|
|
25
25
|
});
|
|
26
26
|
```
|
|
27
27
|
|
|
28
|
+
## Decision Models
|
|
29
|
+
|
|
30
|
+
Any classification evaluator, including every built-in, can run on an AI SDK decision model such as OpenAI's `gpt-6-luna` ([Decisions API](https://developers.openai.com/api/docs/guides/decisions)) or TypeSafe's Jev. Decision models pick one of the labels directly and return a probability for each, instead of generating text. They are much faster and cheaper than a chat model judge. With OpenAI, use `@ai-sdk/openai` 4.0.89 or later.
|
|
31
|
+
|
|
32
|
+
```ts
|
|
33
|
+
import { openai } from "@ai-sdk/openai";
|
|
34
|
+
import { createCorrectnessEvaluator } from "@arizeai/phoenix-evals";
|
|
35
|
+
|
|
36
|
+
const correctness = createCorrectnessEvaluator({
|
|
37
|
+
model: openai.decisionModel("gpt-6-luna"),
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const result = await correctness.evaluate({
|
|
41
|
+
input: "What is the capital of Australia?",
|
|
42
|
+
output: "Sydney.",
|
|
43
|
+
});
|
|
44
|
+
// {
|
|
45
|
+
// label: "incorrect",
|
|
46
|
+
// score: 0,
|
|
47
|
+
// metadata: {
|
|
48
|
+
// probabilities: { correct: 0.02, incorrect: 0.98 },
|
|
49
|
+
// confidence: 0.96,
|
|
50
|
+
// modelId: "gpt-6-luna",
|
|
51
|
+
// },
|
|
52
|
+
// }
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
How it differs from a language model:
|
|
56
|
+
|
|
57
|
+
- **No explanation.** Decision models don't generate text, so `explanation` is always undefined.
|
|
58
|
+
- **Probabilities in metadata.** `metadata.probabilities` maps each label to its probability. `metadata.confidence` is included when the provider reports one (OpenAI and TypeSafe do).
|
|
59
|
+
- **Prompt shape.** The template is rendered the same way as for a language model, then sent as one plain-text state. Message roles are dropped and only text content is sent.
|
|
60
|
+
- **Choices.** Each provider sets its own limits on how many choices a question can have. A classification outside them fails with the provider's error.
|
|
61
|
+
- **Refusals.** A decision model may refuse to classify, for example when asked to infer sensitive attributes rejected by the chosen model. On a refusal, the evaluator throws the AI SDK's `Experimental_DecisionRefusalError`. In `runExperiment`, it's recorded as that evaluation's error.
|
|
62
|
+
- **Plain model IDs.** A string model ID is always treated as a language model. Use the provider's `decisionModel()` to get a decision model.
|
|
63
|
+
|
|
64
|
+
<Warning>
|
|
65
|
+
Decision models can't work through a problem step by step. They do well on judgments you can read off the text, such as relevance, faithfulness, tool selection, and toxicity. They're unreliable for checking answers that need a calculation, such as unit conversions or arithmetic, and their probabilities can be confidently wrong on those. Use a language model for those evaluations.
|
|
66
|
+
</Warning>
|
|
67
|
+
|
|
28
68
|
## Lower-Level API
|
|
29
69
|
|
|
30
|
-
Use `generateClassification` directly when you already have a rendered prompt and only need structured label generation.
|
|
70
|
+
Use `generateClassification` directly when you already have a rendered prompt and only need structured label generation. It also accepts a decision model, in which case the rendered prompt is sent as one plain-text state.
|
|
31
71
|
|
|
32
72
|
<section className="hidden" data-agent-context="source-map" aria-label="Source map">
|
|
33
73
|
<h2>Source Map</h2>
|
|
@@ -35,6 +75,8 @@ Use `generateClassification` directly when you already have a rendered prompt an
|
|
|
35
75
|
<li><code>src/llm/createClassifierFn.ts</code></li>
|
|
36
76
|
<li><code>src/llm/createClassificationEvaluator.ts</code></li>
|
|
37
77
|
<li><code>src/llm/generateClassification.ts</code></li>
|
|
78
|
+
<li><code>src/decision/decideClassification.ts</code></li>
|
|
79
|
+
<li><code>src/decision/decisionPrompt.ts</code></li>
|
|
38
80
|
<li><code>src/types/evals.ts</code></li>
|
|
39
81
|
</ul>
|
|
40
82
|
</section>
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@arizeai/phoenix-evals",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.7.0",
|
|
4
4
|
"description": "A library for running evaluations for AI use cases",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"arize",
|
|
@@ -66,26 +66,27 @@
|
|
|
66
66
|
}
|
|
67
67
|
},
|
|
68
68
|
"dependencies": {
|
|
69
|
-
"@ai-sdk/otel": "
|
|
70
|
-
"@arizeai/openinference-core": "^2.
|
|
69
|
+
"@ai-sdk/otel": "1.0.137",
|
|
70
|
+
"@arizeai/openinference-core": "^2.8.0",
|
|
71
71
|
"@opentelemetry/api": "^1.9.1",
|
|
72
|
-
"ai": "
|
|
72
|
+
"ai": "7.0.137",
|
|
73
73
|
"jsonpath-plus": "^10.4.0",
|
|
74
74
|
"mustache": "^4.2.0",
|
|
75
|
-
"zod": "^4.
|
|
75
|
+
"zod": "^4.6.7"
|
|
76
76
|
},
|
|
77
77
|
"devDependencies": {
|
|
78
|
-
"@ai-sdk/google": "^4.0.
|
|
79
|
-
"@ai-sdk/openai": "^4.0.
|
|
80
|
-
"@
|
|
81
|
-
"@
|
|
78
|
+
"@ai-sdk/google": "^4.0.99",
|
|
79
|
+
"@ai-sdk/openai": "^4.0.96",
|
|
80
|
+
"@ai-sdk/typesafe-ai": "^3.0.21",
|
|
81
|
+
"@arizeai/openinference-instrumentation-openai": "^4.4.1",
|
|
82
|
+
"@opentelemetry/sdk-trace-node": "^2.12.0",
|
|
82
83
|
"@types/mustache": "^4.2.6",
|
|
83
|
-
"@types/node": "^26.
|
|
84
|
+
"@types/node": "^26.6.5",
|
|
84
85
|
"msw": "^2.15.0",
|
|
85
86
|
"nock": "^14.0.17",
|
|
86
|
-
"tsx": "^4.23.
|
|
87
|
+
"tsx": "^4.23.15",
|
|
87
88
|
"typedoc": "^0.28.20",
|
|
88
|
-
"vitest": "^5.0.
|
|
89
|
+
"vitest": "^5.0.5"
|
|
89
90
|
},
|
|
90
91
|
"engines": {
|
|
91
92
|
"node": ">=22.12"
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The only place phoenix-evals calls the AI SDK's experimental decision API.
|
|
3
|
+
* `experimental_decide` and the decision model spec may change in AI SDK
|
|
4
|
+
* patch releases, so keep everything that depends on their shape in here.
|
|
5
|
+
*/
|
|
6
|
+
import { experimental_decide } from "ai";
|
|
7
|
+
|
|
8
|
+
import { getTelemetryOptions } from "../telemetry";
|
|
9
|
+
import type { ClassificationResult } from "../types/evals";
|
|
10
|
+
import type { TelemetryConfig } from "../types/otel";
|
|
11
|
+
import type { WithPrompt } from "../types/prompts";
|
|
12
|
+
import type { DecisionModel } from "../utils/isDecisionModel";
|
|
13
|
+
import { promptToDecisionState } from "./decisionPrompt";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The ID of the single choice question asked per classification.
|
|
17
|
+
*/
|
|
18
|
+
const QUESTION_ID = "label";
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The question's instructions. The rendered prompt, rubric included, is sent
|
|
22
|
+
* as the state.
|
|
23
|
+
*/
|
|
24
|
+
const INSTRUCTIONS =
|
|
25
|
+
"Read the prompt in the state and answer it by selecting exactly one of the choices.";
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Classify with an AI SDK decision model by asking one choice question whose
|
|
29
|
+
* options are the labels. Decision models don't generate text, so the
|
|
30
|
+
* result has no explanation. Label probabilities, the provider's confidence
|
|
31
|
+
* (when it reports one) and the resolved model ID go in the metadata.
|
|
32
|
+
*
|
|
33
|
+
* Choice limits and refusals are left to the AI SDK and the provider: a
|
|
34
|
+
* refusal throws the AI SDK's `Experimental_DecisionRefusalError`.
|
|
35
|
+
*/
|
|
36
|
+
export async function decideClassification(args: {
|
|
37
|
+
model: DecisionModel;
|
|
38
|
+
labels: readonly string[];
|
|
39
|
+
prompt: WithPrompt;
|
|
40
|
+
telemetry?: TelemetryConfig;
|
|
41
|
+
}): Promise<ClassificationResult> {
|
|
42
|
+
const { model, labels, prompt, telemetry } = args;
|
|
43
|
+
const result = await experimental_decide({
|
|
44
|
+
model,
|
|
45
|
+
state: promptToDecisionState(prompt),
|
|
46
|
+
questions: {
|
|
47
|
+
[QUESTION_ID]: {
|
|
48
|
+
type: "choice",
|
|
49
|
+
instructions: INSTRUCTIONS,
|
|
50
|
+
criteria: Object.fromEntries(labels.map((label) => [label, null])),
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
telemetry: getTelemetryOptions({
|
|
54
|
+
functionId: "decideClassification",
|
|
55
|
+
telemetry,
|
|
56
|
+
}),
|
|
57
|
+
});
|
|
58
|
+
const answer = result.answers[QUESTION_ID];
|
|
59
|
+
const confidence = getConfidence(result.providerMetadata);
|
|
60
|
+
return {
|
|
61
|
+
label: answer.choice,
|
|
62
|
+
metadata: {
|
|
63
|
+
probabilities: answer.probabilities,
|
|
64
|
+
...(confidence !== undefined ? { confidence } : {}),
|
|
65
|
+
modelId: result.response.modelId,
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The decision model spec has no confidence field, but providers that
|
|
72
|
+
* report one (OpenAI, TypeSafe) put it in their provider metadata as
|
|
73
|
+
* `confidence[questionId]`.
|
|
74
|
+
*/
|
|
75
|
+
function getConfidence(providerMetadata: unknown): number | undefined {
|
|
76
|
+
if (typeof providerMetadata !== "object" || providerMetadata === null) {
|
|
77
|
+
return undefined;
|
|
78
|
+
}
|
|
79
|
+
for (const metadata of Object.values(providerMetadata)) {
|
|
80
|
+
const confidence = (metadata as { confidence?: Record<string, unknown> })
|
|
81
|
+
?.confidence?.[QUESTION_ID];
|
|
82
|
+
if (typeof confidence === "number") return confidence;
|
|
83
|
+
}
|
|
84
|
+
return undefined;
|
|
85
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { ModelMessage } from "ai";
|
|
2
|
+
|
|
3
|
+
import type { WithPrompt } from "../types/prompts";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Join a rendered AI SDK prompt into the plain-text state a decision model
|
|
7
|
+
* judges. Decision models have no roles, so system text, instructions and
|
|
8
|
+
* messages are joined in order.
|
|
9
|
+
*/
|
|
10
|
+
export function promptToDecisionState(prompt: WithPrompt): string {
|
|
11
|
+
const parts: string[] = [];
|
|
12
|
+
const { instructions, system, messages, prompt: promptContent } = prompt;
|
|
13
|
+
for (const value of [instructions, system]) {
|
|
14
|
+
if (value !== undefined) parts.push(instructionsToText(value));
|
|
15
|
+
}
|
|
16
|
+
if (typeof promptContent === "string") {
|
|
17
|
+
parts.push(promptContent);
|
|
18
|
+
} else if (Array.isArray(promptContent)) {
|
|
19
|
+
parts.push(messagesToText(promptContent));
|
|
20
|
+
}
|
|
21
|
+
if (messages !== undefined) parts.push(messagesToText(messages));
|
|
22
|
+
return parts.filter((part) => part.length > 0).join("\n\n");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function messagesToText(messages: readonly ModelMessage[]): string {
|
|
26
|
+
return messages
|
|
27
|
+
.map((message) => contentToText(message.content))
|
|
28
|
+
.filter((text) => text.length > 0)
|
|
29
|
+
.join("\n\n");
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function instructionsToText(value: unknown): string {
|
|
33
|
+
if (typeof value === "string") return value;
|
|
34
|
+
if (Array.isArray(value)) {
|
|
35
|
+
return value
|
|
36
|
+
.map((item) =>
|
|
37
|
+
typeof item === "object" && item !== null && "content" in item
|
|
38
|
+
? contentToText(item.content)
|
|
39
|
+
: ""
|
|
40
|
+
)
|
|
41
|
+
.filter((text) => text.length > 0)
|
|
42
|
+
.join("\n\n");
|
|
43
|
+
}
|
|
44
|
+
if (typeof value === "object" && value !== null && "content" in value) {
|
|
45
|
+
return contentToText(value.content);
|
|
46
|
+
}
|
|
47
|
+
return "";
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Keep only the text of a message. Decision models judge text here; image
|
|
52
|
+
* and file parts are not forwarded.
|
|
53
|
+
*/
|
|
54
|
+
function contentToText(content: unknown): string {
|
|
55
|
+
if (typeof content === "string") return content;
|
|
56
|
+
if (!Array.isArray(content)) return "";
|
|
57
|
+
return content
|
|
58
|
+
.map((part) =>
|
|
59
|
+
typeof part === "object" &&
|
|
60
|
+
part !== null &&
|
|
61
|
+
"type" in part &&
|
|
62
|
+
part.type === "text" &&
|
|
63
|
+
"text" in part &&
|
|
64
|
+
typeof part.text === "string"
|
|
65
|
+
? part.text
|
|
66
|
+
: ""
|
|
67
|
+
)
|
|
68
|
+
.filter((text) => text.length > 0)
|
|
69
|
+
.join("\n");
|
|
70
|
+
}
|
|
@@ -9,6 +9,7 @@ import type {
|
|
|
9
9
|
WithPromptTemplate,
|
|
10
10
|
} from "../types";
|
|
11
11
|
import type { ObjectMapping } from "../types/data";
|
|
12
|
+
import type { DecisionModel } from "../utils/isDecisionModel";
|
|
12
13
|
import { remapObject } from "../utils/objectMappingUtils";
|
|
13
14
|
import { createClassifierFn } from "./createClassifierFn";
|
|
14
15
|
import { LLMEvaluator } from "./LLMEvaluator";
|
|
@@ -29,7 +30,7 @@ export class ClassificationEvaluator<RecordType extends Record<string, unknown>>
|
|
|
29
30
|
/**
|
|
30
31
|
* The model to use for classification
|
|
31
32
|
*/
|
|
32
|
-
readonly model: LanguageModel;
|
|
33
|
+
readonly model: LanguageModel | DecisionModel;
|
|
33
34
|
/**
|
|
34
35
|
* The choices to classify the example into
|
|
35
36
|
*/
|
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import { generateObject } from "ai";
|
|
2
2
|
import { z } from "zod";
|
|
3
3
|
|
|
4
|
-
import {
|
|
4
|
+
import { decideClassification } from "../decision/decideClassification";
|
|
5
|
+
import { getTelemetryOptions } from "../telemetry";
|
|
5
6
|
import type { ClassificationResult, WithLLM } from "../types/evals";
|
|
6
7
|
import type { WithTelemetry } from "../types/otel";
|
|
7
8
|
import type { WithPrompt } from "../types/prompts";
|
|
9
|
+
import { isDecisionModel } from "../utils/isDecisionModel";
|
|
10
|
+
|
|
8
11
|
export type ClassifyArgs = WithLLM &
|
|
9
12
|
WithTelemetry &
|
|
10
13
|
WithPrompt & {
|
|
@@ -23,6 +26,12 @@ export type ClassifyArgs = WithLLM &
|
|
|
23
26
|
};
|
|
24
27
|
/**
|
|
25
28
|
* A function that leverages an llm to perform a classification
|
|
29
|
+
*
|
|
30
|
+
* When given an AI SDK decision model (e.g. `openai.decisionModel("gpt-6-luna")`)
|
|
31
|
+
* the classification is routed through `experimental_decide` as a single
|
|
32
|
+
* choice question, with the prompt sent as plain text. Decision models cannot
|
|
33
|
+
* generate text, so no explanation is returned in that case, and
|
|
34
|
+
* `schemaName` / `schemaDescription` are ignored.
|
|
26
35
|
*/
|
|
27
36
|
export async function generateClassification(
|
|
28
37
|
args: ClassifyArgs
|
|
@@ -30,11 +39,14 @@ export async function generateClassification(
|
|
|
30
39
|
const { labels, model, schemaName, schemaDescription, telemetry, ...prompt } =
|
|
31
40
|
args;
|
|
32
41
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
42
|
+
if (isDecisionModel(model)) {
|
|
43
|
+
return decideClassification({
|
|
44
|
+
model,
|
|
45
|
+
labels,
|
|
46
|
+
prompt,
|
|
47
|
+
telemetry,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
38
50
|
|
|
39
51
|
const result = await generateObject({
|
|
40
52
|
model,
|
|
@@ -44,7 +56,10 @@ export async function generateClassification(
|
|
|
44
56
|
explanation: z.string(), // We place the explanation in hopes it uses reasoning to explain the label.
|
|
45
57
|
label: z.enum(labels),
|
|
46
58
|
}),
|
|
47
|
-
telemetry:
|
|
59
|
+
telemetry: getTelemetryOptions({
|
|
60
|
+
functionId: "generateClassification",
|
|
61
|
+
telemetry,
|
|
62
|
+
}),
|
|
48
63
|
// AI SDK 7 rejects system messages inside `messages` by default; keep
|
|
49
64
|
// accepting them since prompt templates may include system messages.
|
|
50
65
|
allowSystemInMessages: true,
|
package/src/telemetry/index.ts
CHANGED
|
@@ -2,6 +2,8 @@ import { OpenTelemetry } from "@ai-sdk/otel";
|
|
|
2
2
|
import { trace, type Tracer } from "@opentelemetry/api";
|
|
3
3
|
import type { Telemetry } from "ai";
|
|
4
4
|
|
|
5
|
+
import type { TelemetryConfig } from "../types/otel";
|
|
6
|
+
|
|
5
7
|
const DEFAULT_TRACER_NAME = "phoenix-evals";
|
|
6
8
|
|
|
7
9
|
/**
|
|
@@ -55,6 +57,25 @@ export function getTelemetryIntegrations(
|
|
|
55
57
|
usage: true,
|
|
56
58
|
providerMetadata: true,
|
|
57
59
|
schema: true,
|
|
60
|
+
// Record the state, questions and answers of decision model calls.
|
|
61
|
+
experimental_decision: true,
|
|
58
62
|
}),
|
|
59
63
|
];
|
|
60
64
|
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Builds the AI SDK `telemetry` call option for an evaluator call. Telemetry
|
|
68
|
+
* is on unless explicitly disabled, and spans go to the configured tracer or
|
|
69
|
+
* the default Phoenix tracer.
|
|
70
|
+
*/
|
|
71
|
+
export function getTelemetryOptions(args: {
|
|
72
|
+
functionId: string;
|
|
73
|
+
telemetry?: TelemetryConfig;
|
|
74
|
+
}) {
|
|
75
|
+
const { functionId, telemetry } = args;
|
|
76
|
+
return {
|
|
77
|
+
isEnabled: telemetry?.isEnabled ?? true,
|
|
78
|
+
functionId,
|
|
79
|
+
integrations: getTelemetryIntegrations(telemetry?.tracer ?? tracer),
|
|
80
|
+
};
|
|
81
|
+
}
|
package/src/types/evals.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { LanguageModel } from "ai";
|
|
2
2
|
|
|
3
|
+
import type { DecisionModel } from "../utils/isDecisionModel";
|
|
3
4
|
import type { ObjectMapping } from "./data";
|
|
4
5
|
import type { WithTelemetry } from "./otel";
|
|
5
6
|
import type { PromptTemplate } from "./templating";
|
|
@@ -15,7 +16,14 @@ export interface ExampleRecord<OutputType, InputType> {
|
|
|
15
16
|
}
|
|
16
17
|
|
|
17
18
|
export interface WithLLM {
|
|
18
|
-
|
|
19
|
+
/**
|
|
20
|
+
* Either a regular AI SDK {@link LanguageModel}, or an AI SDK
|
|
21
|
+
* {@link DecisionModel} (e.g. `openai.decisionModel("gpt-6-luna")`) which
|
|
22
|
+
* classifies without generating text and therefore cannot produce an
|
|
23
|
+
* explanation. With a decision model, label probabilities are returned in
|
|
24
|
+
* the result's `metadata.probabilities`.
|
|
25
|
+
*/
|
|
26
|
+
model: LanguageModel | DecisionModel;
|
|
19
27
|
}
|
|
20
28
|
|
|
21
29
|
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
|
|
@@ -40,6 +48,11 @@ export interface EvaluationResult {
|
|
|
40
48
|
* @example "The model correctly identified the sentiment of the text."
|
|
41
49
|
*/
|
|
42
50
|
explanation?: string;
|
|
51
|
+
/**
|
|
52
|
+
* Additional information about how the result was produced, e.g. the
|
|
53
|
+
* label probabilities reported by a decision model.
|
|
54
|
+
*/
|
|
55
|
+
metadata?: Record<string, unknown>;
|
|
43
56
|
}
|
|
44
57
|
|
|
45
58
|
/**
|
|
@@ -48,6 +61,7 @@ export interface EvaluationResult {
|
|
|
48
61
|
export interface ClassificationResult {
|
|
49
62
|
label: string;
|
|
50
63
|
explanation?: string;
|
|
64
|
+
metadata?: Record<string, unknown>;
|
|
51
65
|
}
|
|
52
66
|
|
|
53
67
|
/**
|
|
@@ -68,9 +82,9 @@ export type ClassificationChoicesMap = Record<string, number>;
|
|
|
68
82
|
*/
|
|
69
83
|
export interface CreateClassifierArgs extends WithTelemetry {
|
|
70
84
|
/*
|
|
71
|
-
* The
|
|
85
|
+
* The model to use for classification / evaluation
|
|
72
86
|
*/
|
|
73
|
-
model: LanguageModel;
|
|
87
|
+
model: LanguageModel | DecisionModel;
|
|
74
88
|
/**
|
|
75
89
|
* The choices to classify the example into.
|
|
76
90
|
* e.g. { "correct": 1, "incorrect": 0 }
|
package/src/utils/index.ts
CHANGED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { Experimental_DecisionModel, LanguageModel } from "ai";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* An AI SDK decision model instance, e.g. `openai.decisionModel("gpt-6-luna")`
|
|
5
|
+
* or TypeSafe's Jev. Unlike a {@link LanguageModel}, a decision model only
|
|
6
|
+
* answers typed questions (here, which label applies) and cannot generate
|
|
7
|
+
* free-form text, so classifications made with it have no explanation.
|
|
8
|
+
*
|
|
9
|
+
* Gateway string IDs are excluded: a plain string is treated as a language
|
|
10
|
+
* model. Wrap the ID with the provider's `decisionModel()` instead.
|
|
11
|
+
*/
|
|
12
|
+
export type DecisionModel = Exclude<Experimental_DecisionModel, string>;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Type guard that distinguishes an AI SDK decision model from a language model.
|
|
16
|
+
* Decision models expose `doDecide` (or `doEvaluate` for providers written
|
|
17
|
+
* against the older evaluation model spec) instead of `doGenerate`.
|
|
18
|
+
*/
|
|
19
|
+
export function isDecisionModel(
|
|
20
|
+
model: LanguageModel | DecisionModel
|
|
21
|
+
): model is DecisionModel {
|
|
22
|
+
if (typeof model !== "object" || model === null) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
return (
|
|
26
|
+
("doDecide" in model && typeof model.doDecide === "function") ||
|
|
27
|
+
("doEvaluate" in model && typeof model.doEvaluate === "function")
|
|
28
|
+
);
|
|
29
|
+
}
|