@arizeai/phoenix-evals 2.6.0 → 2.7.1
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/helpers/toEvaluationResult.d.ts +3 -3
- package/dist/esm/helpers/toEvaluationResult.d.ts.map +1 -1
- package/dist/esm/helpers/toEvaluationResult.js +22 -3
- package/dist/esm/helpers/toEvaluationResult.js.map +1 -1
- package/dist/esm/llm/ClassificationEvaluator.d.ts +2 -2
- 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 +5 -5
- package/dist/esm/llm/generateClassification.d.ts.map +1 -1
- package/dist/esm/llm/generateClassification.js +20 -71
- 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 +8 -6
- 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/helpers/toEvaluationResult.d.ts +3 -3
- package/dist/src/helpers/toEvaluationResult.d.ts.map +1 -1
- package/dist/src/helpers/toEvaluationResult.js +22 -3
- package/dist/src/helpers/toEvaluationResult.js.map +1 -1
- package/dist/src/llm/ClassificationEvaluator.d.ts +2 -2
- package/dist/src/llm/ClassificationEvaluator.d.ts.map +1 -1
- package/dist/src/llm/generateClassification.d.ts +5 -5
- package/dist/src/llm/generateClassification.d.ts.map +1 -1
- package/dist/src/llm/generateClassification.js +18 -70
- 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 +8 -6
- 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 +14 -14
- package/src/decision/decideClassification.ts +85 -0
- package/src/decision/decisionPrompt.ts +70 -0
- package/src/helpers/toEvaluationResult.ts +27 -3
- package/src/llm/ClassificationEvaluator.ts +2 -2
- package/src/llm/generateClassification.ts +20 -82
- package/src/telemetry/index.ts +21 -0
- package/src/types/evals.ts +8 -6
- package/src/utils/index.ts +1 -0
- package/src/utils/isDecisionModel.ts +29 -0
- package/dist/esm/utils/isEvaluationModel.d.ts +0 -13
- package/dist/esm/utils/isEvaluationModel.d.ts.map +0 -1
- package/dist/esm/utils/isEvaluationModel.js +0 -11
- package/dist/esm/utils/isEvaluationModel.js.map +0 -1
- package/dist/src/utils/isEvaluationModel.d.ts +0 -13
- package/dist/src/utils/isEvaluationModel.d.ts.map +0 -1
- package/dist/src/utils/isEvaluationModel.js +0 -14
- package/dist/src/utils/isEvaluationModel.js.map +0 -1
- package/src/utils/isEvaluationModel.ts +0 -23
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { LanguageModel } from "ai";
|
|
2
|
-
import type {
|
|
2
|
+
import type { DecisionModel } from "../utils/isDecisionModel.js";
|
|
3
3
|
import type { ObjectMapping } from "./data.js";
|
|
4
4
|
import type { WithTelemetry } from "./otel.js";
|
|
5
5
|
import type { PromptTemplate } from "./templating.js";
|
|
@@ -15,10 +15,12 @@ export interface ExampleRecord<OutputType, InputType> {
|
|
|
15
15
|
export interface WithLLM {
|
|
16
16
|
/**
|
|
17
17
|
* Either a regular AI SDK {@link LanguageModel}, or an AI SDK
|
|
18
|
-
* {@link
|
|
19
|
-
* generating text and therefore cannot produce an
|
|
18
|
+
* {@link DecisionModel} (e.g. `openai.decisionModel("gpt-6-luna")`) which
|
|
19
|
+
* classifies without generating text and therefore cannot produce an
|
|
20
|
+
* explanation. With a decision model, label probabilities are returned in
|
|
21
|
+
* the result's `metadata.probabilities`.
|
|
20
22
|
*/
|
|
21
|
-
model: LanguageModel |
|
|
23
|
+
model: LanguageModel | DecisionModel;
|
|
22
24
|
}
|
|
23
25
|
export interface LLMEvaluationArgs extends WithLLM {
|
|
24
26
|
}
|
|
@@ -43,7 +45,7 @@ export interface EvaluationResult {
|
|
|
43
45
|
explanation?: string;
|
|
44
46
|
/**
|
|
45
47
|
* Additional information about how the result was produced, e.g. the
|
|
46
|
-
* label probabilities reported by
|
|
48
|
+
* label probabilities reported by a decision model.
|
|
47
49
|
*/
|
|
48
50
|
metadata?: Record<string, unknown>;
|
|
49
51
|
}
|
|
@@ -70,7 +72,7 @@ export type ClassificationChoicesMap = Record<string, number>;
|
|
|
70
72
|
* The arguments for creating a classification-based evaluator
|
|
71
73
|
*/
|
|
72
74
|
export interface CreateClassifierArgs extends WithTelemetry {
|
|
73
|
-
model: LanguageModel |
|
|
75
|
+
model: LanguageModel | DecisionModel;
|
|
74
76
|
/**
|
|
75
77
|
* The choices to classify the example into.
|
|
76
78
|
* e.g. { "correct": 1, "incorrect": 0 }
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"evals.d.ts","sourceRoot":"","sources":["../../../src/types/evals.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,IAAI,CAAC;AAExC,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"evals.d.ts","sourceRoot":"","sources":["../../../src/types/evals.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,IAAI,CAAC;AAExC,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAC9D,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AAC5C,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AAC5C,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAEnD;;GAEG;AACH,MAAM,WAAW,aAAa,CAAC,UAAU,EAAE,SAAS;IAClD,MAAM,EAAE,UAAU,CAAC;IACnB,QAAQ,CAAC,EAAE,UAAU,CAAC;IACtB,KAAK,CAAC,EAAE,SAAS,CAAC;IAClB,CAAC,GAAG,EAAE,MAAM,GAAG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
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,mBAAmB,CAAC"}
|
package/dist/esm/utils/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/utils/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,mBAAmB,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Experimental_DecisionModel, LanguageModel } from "ai";
|
|
2
|
+
/**
|
|
3
|
+
* An AI SDK decision model instance, e.g. `openai.decisionModel("gpt-6-luna")`
|
|
4
|
+
* or TypeSafe's Jev. Unlike a {@link LanguageModel}, a decision model only
|
|
5
|
+
* answers typed questions (here, which label applies) and cannot generate
|
|
6
|
+
* free-form text, so classifications made with it have no explanation.
|
|
7
|
+
*
|
|
8
|
+
* Gateway string IDs are excluded: a plain string is treated as a language
|
|
9
|
+
* model. Wrap the ID with the provider's `decisionModel()` instead.
|
|
10
|
+
*/
|
|
11
|
+
export type DecisionModel = Exclude<Experimental_DecisionModel, string>;
|
|
12
|
+
/**
|
|
13
|
+
* Type guard that distinguishes an AI SDK decision model from a language model.
|
|
14
|
+
* Decision models expose `doDecide` (or `doEvaluate` for providers written
|
|
15
|
+
* against the older evaluation model spec) instead of `doGenerate`.
|
|
16
|
+
*/
|
|
17
|
+
export declare function isDecisionModel(model: LanguageModel | DecisionModel): model is DecisionModel;
|
|
18
|
+
//# sourceMappingURL=isDecisionModel.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isDecisionModel.d.ts","sourceRoot":"","sources":["../../../src/utils/isDecisionModel.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,0BAA0B,EAAE,aAAa,EAAE,MAAM,IAAI,CAAC;AAEpE;;;;;;;;GAQG;AACH,MAAM,MAAM,aAAa,GAAG,OAAO,CAAC,0BAA0B,EAAE,MAAM,CAAC,CAAC;AAExE;;;;GAIG;AACH,wBAAgB,eAAe,CAC7B,KAAK,EAAE,aAAa,GAAG,aAAa,GACnC,KAAK,IAAI,aAAa,CAQxB"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type guard that distinguishes an AI SDK decision model from a language model.
|
|
3
|
+
* Decision models expose `doDecide` (or `doEvaluate` for providers written
|
|
4
|
+
* against the older evaluation model spec) instead of `doGenerate`.
|
|
5
|
+
*/
|
|
6
|
+
export function isDecisionModel(model) {
|
|
7
|
+
if (typeof model !== "object" || model === null) {
|
|
8
|
+
return false;
|
|
9
|
+
}
|
|
10
|
+
return (("doDecide" in model && typeof model.doDecide === "function") ||
|
|
11
|
+
("doEvaluate" in model && typeof model.doEvaluate === "function"));
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=isDecisionModel.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isDecisionModel.js","sourceRoot":"","sources":["../../../src/utils/isDecisionModel.ts"],"names":[],"mappings":"AAaA;;;;GAIG;AACH,MAAM,UAAU,eAAe,CAC7B,KAAoC;IAEpC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;QAChD,OAAO,KAAK,CAAC;IACf,CAAC;IACD,OAAO,CACL,CAAC,UAAU,IAAI,KAAK,IAAI,OAAO,KAAK,CAAC,QAAQ,KAAK,UAAU,CAAC;QAC7D,CAAC,YAAY,IAAI,KAAK,IAAI,OAAO,KAAK,CAAC,UAAU,KAAK,UAAU,CAAC,CAClE,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { ClassificationResult } from "../types/evals";
|
|
2
|
+
import type { TelemetryConfig } from "../types/otel";
|
|
3
|
+
import type { WithPrompt } from "../types/prompts";
|
|
4
|
+
import type { DecisionModel } from "../utils/isDecisionModel";
|
|
5
|
+
/**
|
|
6
|
+
* Classify with an AI SDK decision model by asking one choice question whose
|
|
7
|
+
* options are the labels. Decision models don't generate text, so the
|
|
8
|
+
* result has no explanation. Label probabilities, the provider's confidence
|
|
9
|
+
* (when it reports one) and the resolved model ID go in the metadata.
|
|
10
|
+
*
|
|
11
|
+
* Choice limits and refusals are left to the AI SDK and the provider: a
|
|
12
|
+
* refusal throws the AI SDK's `Experimental_DecisionRefusalError`.
|
|
13
|
+
*/
|
|
14
|
+
export declare function decideClassification(args: {
|
|
15
|
+
model: DecisionModel;
|
|
16
|
+
labels: readonly string[];
|
|
17
|
+
prompt: WithPrompt;
|
|
18
|
+
telemetry?: TelemetryConfig;
|
|
19
|
+
}): Promise<ClassificationResult>;
|
|
20
|
+
//# sourceMappingURL=decideClassification.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decideClassification.d.ts","sourceRoot":"","sources":["../../../src/decision/decideClassification.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AAC3D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AACrD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AACnD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AAe9D;;;;;;;;GAQG;AACH,wBAAsB,oBAAoB,CAAC,IAAI,EAAE;IAC/C,KAAK,EAAE,aAAa,CAAC;IACrB,MAAM,EAAE,SAAS,MAAM,EAAE,CAAC;IAC1B,MAAM,EAAE,UAAU,CAAC;IACnB,SAAS,CAAC,EAAE,eAAe,CAAC;CAC7B,GAAG,OAAO,CAAC,oBAAoB,CAAC,CA2BhC"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.decideClassification = decideClassification;
|
|
4
|
+
/**
|
|
5
|
+
* The only place phoenix-evals calls the AI SDK's experimental decision API.
|
|
6
|
+
* `experimental_decide` and the decision model spec may change in AI SDK
|
|
7
|
+
* patch releases, so keep everything that depends on their shape in here.
|
|
8
|
+
*/
|
|
9
|
+
const ai_1 = require("ai");
|
|
10
|
+
const telemetry_1 = require("../telemetry");
|
|
11
|
+
const decisionPrompt_1 = require("./decisionPrompt");
|
|
12
|
+
/**
|
|
13
|
+
* The ID of the single choice question asked per classification.
|
|
14
|
+
*/
|
|
15
|
+
const QUESTION_ID = "label";
|
|
16
|
+
/**
|
|
17
|
+
* The question's instructions. The rendered prompt, rubric included, is sent
|
|
18
|
+
* as the state.
|
|
19
|
+
*/
|
|
20
|
+
const INSTRUCTIONS = "Read the prompt in the state and answer it by selecting exactly one of the choices.";
|
|
21
|
+
/**
|
|
22
|
+
* Classify with an AI SDK decision model by asking one choice question whose
|
|
23
|
+
* options are the labels. Decision models don't generate text, so the
|
|
24
|
+
* result has no explanation. Label probabilities, the provider's confidence
|
|
25
|
+
* (when it reports one) and the resolved model ID go in the metadata.
|
|
26
|
+
*
|
|
27
|
+
* Choice limits and refusals are left to the AI SDK and the provider: a
|
|
28
|
+
* refusal throws the AI SDK's `Experimental_DecisionRefusalError`.
|
|
29
|
+
*/
|
|
30
|
+
async function decideClassification(args) {
|
|
31
|
+
const { model, labels, prompt, telemetry } = args;
|
|
32
|
+
const result = await (0, ai_1.experimental_decide)({
|
|
33
|
+
model,
|
|
34
|
+
state: (0, decisionPrompt_1.promptToDecisionState)(prompt),
|
|
35
|
+
questions: {
|
|
36
|
+
[QUESTION_ID]: {
|
|
37
|
+
type: "choice",
|
|
38
|
+
instructions: INSTRUCTIONS,
|
|
39
|
+
criteria: Object.fromEntries(labels.map((label) => [label, null])),
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
telemetry: (0, telemetry_1.getTelemetryOptions)({
|
|
43
|
+
functionId: "decideClassification",
|
|
44
|
+
telemetry,
|
|
45
|
+
}),
|
|
46
|
+
});
|
|
47
|
+
const answer = result.answers[QUESTION_ID];
|
|
48
|
+
const confidence = getConfidence(result.providerMetadata);
|
|
49
|
+
return {
|
|
50
|
+
label: answer.choice,
|
|
51
|
+
metadata: Object.assign(Object.assign({ probabilities: answer.probabilities }, (confidence !== undefined ? { confidence } : {})), { modelId: result.response.modelId }),
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* The decision model spec has no confidence field, but providers that
|
|
56
|
+
* report one (OpenAI, TypeSafe) put it in their provider metadata as
|
|
57
|
+
* `confidence[questionId]`.
|
|
58
|
+
*/
|
|
59
|
+
function getConfidence(providerMetadata) {
|
|
60
|
+
var _a;
|
|
61
|
+
if (typeof providerMetadata !== "object" || providerMetadata === null) {
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
for (const metadata of Object.values(providerMetadata)) {
|
|
65
|
+
const confidence = (_a = metadata === null || metadata === void 0 ? void 0 : metadata.confidence) === null || _a === void 0 ? void 0 : _a[QUESTION_ID];
|
|
66
|
+
if (typeof confidence === "number")
|
|
67
|
+
return confidence;
|
|
68
|
+
}
|
|
69
|
+
return undefined;
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=decideClassification.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decideClassification.js","sourceRoot":"","sources":["../../../src/decision/decideClassification.ts"],"names":[],"mappings":";;;AAAA;;;;GAIG;AACH,2BAAyC;AAEzC,4CAAmD;AAKnD,qDAAyD;AAEzD;;GAEG;AACH,MAAM,WAAW,GAAG,OAAO,CAAC;AAE5B;;;GAGG;AACH,MAAM,YAAY,GAChB,qFAAqF,CAAC;AAExF;;;;;;;;GAQG;AACI,KAAK,+BAA+B,IAK1C;IACC,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE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AU,0CAAG,WAAW,CAAC,CAAC;QAC9B,IAAI,OAAO,UAAU,KAAK,QAAQ;YAAE,OAAO,UAAU,CAAC;IACxD,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { WithPrompt } from "../types/prompts";
|
|
2
|
+
/**
|
|
3
|
+
* Join a rendered AI SDK prompt into the plain-text state a decision model
|
|
4
|
+
* judges. Decision models have no roles, so system text, instructions and
|
|
5
|
+
* messages are joined in order.
|
|
6
|
+
*/
|
|
7
|
+
export declare function promptToDecisionState(prompt: WithPrompt): string;
|
|
8
|
+
//# sourceMappingURL=decisionPrompt.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decisionPrompt.d.ts","sourceRoot":"","sources":["../../../src/decision/decisionPrompt.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAEnD;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,UAAU,GAAG,MAAM,CAahE"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.promptToDecisionState = promptToDecisionState;
|
|
4
|
+
/**
|
|
5
|
+
* Join a rendered AI SDK prompt into the plain-text state a decision model
|
|
6
|
+
* judges. Decision models have no roles, so system text, instructions and
|
|
7
|
+
* messages are joined in order.
|
|
8
|
+
*/
|
|
9
|
+
function promptToDecisionState(prompt) {
|
|
10
|
+
const parts = [];
|
|
11
|
+
const { instructions, system, messages, prompt: promptContent } = prompt;
|
|
12
|
+
for (const value of [instructions, system]) {
|
|
13
|
+
if (value !== undefined)
|
|
14
|
+
parts.push(instructionsToText(value));
|
|
15
|
+
}
|
|
16
|
+
if (typeof promptContent === "string") {
|
|
17
|
+
parts.push(promptContent);
|
|
18
|
+
}
|
|
19
|
+
else if (Array.isArray(promptContent)) {
|
|
20
|
+
parts.push(messagesToText(promptContent));
|
|
21
|
+
}
|
|
22
|
+
if (messages !== undefined)
|
|
23
|
+
parts.push(messagesToText(messages));
|
|
24
|
+
return parts.filter((part) => part.length > 0).join("\n\n");
|
|
25
|
+
}
|
|
26
|
+
function messagesToText(messages) {
|
|
27
|
+
return messages
|
|
28
|
+
.map((message) => contentToText(message.content))
|
|
29
|
+
.filter((text) => text.length > 0)
|
|
30
|
+
.join("\n\n");
|
|
31
|
+
}
|
|
32
|
+
function instructionsToText(value) {
|
|
33
|
+
if (typeof value === "string")
|
|
34
|
+
return value;
|
|
35
|
+
if (Array.isArray(value)) {
|
|
36
|
+
return value
|
|
37
|
+
.map((item) => typeof item === "object" && item !== null && "content" in item
|
|
38
|
+
? contentToText(item.content)
|
|
39
|
+
: "")
|
|
40
|
+
.filter((text) => text.length > 0)
|
|
41
|
+
.join("\n\n");
|
|
42
|
+
}
|
|
43
|
+
if (typeof value === "object" && value !== null && "content" in value) {
|
|
44
|
+
return contentToText(value.content);
|
|
45
|
+
}
|
|
46
|
+
return "";
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Keep only the text of a message. Decision models judge text here; image
|
|
50
|
+
* and file parts are not forwarded.
|
|
51
|
+
*/
|
|
52
|
+
function contentToText(content) {
|
|
53
|
+
if (typeof content === "string")
|
|
54
|
+
return content;
|
|
55
|
+
if (!Array.isArray(content))
|
|
56
|
+
return "";
|
|
57
|
+
return content
|
|
58
|
+
.map((part) => typeof part === "object" &&
|
|
59
|
+
part !== null &&
|
|
60
|
+
"type" in part &&
|
|
61
|
+
part.type === "text" &&
|
|
62
|
+
"text" in part &&
|
|
63
|
+
typeof part.text === "string"
|
|
64
|
+
? part.text
|
|
65
|
+
: "")
|
|
66
|
+
.filter((text) => text.length > 0)
|
|
67
|
+
.join("\n");
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=decisionPrompt.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1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AM,GAAG,CAAC,CAAC;SACjC,IAAI,CAAC,IAAI,CAAC,CAAC;AAChB,CAAC"}
|
|
@@ -8,7 +8,7 @@ import type { EvaluationResult } from "../types";
|
|
|
8
8
|
*
|
|
9
9
|
* - **Numbers**: Converted to `{ score: number }`
|
|
10
10
|
* - **Strings**: Converted to `{ label: string }`
|
|
11
|
-
* - **Objects**: Extracts `score`, `label`, and `
|
|
11
|
+
* - **Objects**: Extracts `score`, `label`, `explanation`, and `metadata` properties if present
|
|
12
12
|
* - **Other types**: Returns an empty `EvaluationResult` object
|
|
13
13
|
*
|
|
14
14
|
* This is particularly useful when creating evaluators from functions that may
|
|
@@ -17,7 +17,7 @@ import type { EvaluationResult } from "../types";
|
|
|
17
17
|
* @param result - The value to convert to an EvaluationResult. Can be:
|
|
18
18
|
* - A number (converted to score)
|
|
19
19
|
* - A string (converted to label)
|
|
20
|
-
* - An object with optional `score`, `label`, and/or `
|
|
20
|
+
* - An object with optional `score`, `label`, `explanation`, and/or `metadata` properties
|
|
21
21
|
* - Any other value (returns empty object)
|
|
22
22
|
*
|
|
23
23
|
* @returns An {@link EvaluationResult} object with extracted properties
|
|
@@ -37,7 +37,7 @@ import type { EvaluationResult } from "../types";
|
|
|
37
37
|
* ```
|
|
38
38
|
*
|
|
39
39
|
* @example
|
|
40
|
-
* Convert an object with
|
|
40
|
+
* Convert an object with multiple properties:
|
|
41
41
|
* ```typescript
|
|
42
42
|
* const result = toEvaluationResult({
|
|
43
43
|
* score: 0.9,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"toEvaluationResult.d.ts","sourceRoot":"","sources":["../../../src/helpers/toEvaluationResult.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"toEvaluationResult.d.ts","sourceRoot":"","sources":["../../../src/helpers/toEvaluationResult.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AA4EjD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+DG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,OAAO,GAAG,gBAAgB,CA4BpE"}
|
|
@@ -43,6 +43,22 @@ function resultHasExplanation(result) {
|
|
|
43
43
|
"explanation" in result &&
|
|
44
44
|
typeof result.explanation === "string");
|
|
45
45
|
}
|
|
46
|
+
/**
|
|
47
|
+
* Type guard to check if a value has a metadata property.
|
|
48
|
+
*
|
|
49
|
+
* @param result - The value to check
|
|
50
|
+
* @returns True if the value is an object with a metadata record
|
|
51
|
+
*
|
|
52
|
+
* @internal
|
|
53
|
+
*/
|
|
54
|
+
function resultHasMetadata(result) {
|
|
55
|
+
return (typeof result === "object" &&
|
|
56
|
+
result !== null &&
|
|
57
|
+
"metadata" in result &&
|
|
58
|
+
typeof result.metadata === "object" &&
|
|
59
|
+
result.metadata !== null &&
|
|
60
|
+
!Array.isArray(result.metadata));
|
|
61
|
+
}
|
|
46
62
|
/**
|
|
47
63
|
* Converts an unknown value to an {@link EvaluationResult}.
|
|
48
64
|
*
|
|
@@ -52,7 +68,7 @@ function resultHasExplanation(result) {
|
|
|
52
68
|
*
|
|
53
69
|
* - **Numbers**: Converted to `{ score: number }`
|
|
54
70
|
* - **Strings**: Converted to `{ label: string }`
|
|
55
|
-
* - **Objects**: Extracts `score`, `label`, and `
|
|
71
|
+
* - **Objects**: Extracts `score`, `label`, `explanation`, and `metadata` properties if present
|
|
56
72
|
* - **Other types**: Returns an empty `EvaluationResult` object
|
|
57
73
|
*
|
|
58
74
|
* This is particularly useful when creating evaluators from functions that may
|
|
@@ -61,7 +77,7 @@ function resultHasExplanation(result) {
|
|
|
61
77
|
* @param result - The value to convert to an EvaluationResult. Can be:
|
|
62
78
|
* - A number (converted to score)
|
|
63
79
|
* - A string (converted to label)
|
|
64
|
-
* - An object with optional `score`, `label`, and/or `
|
|
80
|
+
* - An object with optional `score`, `label`, `explanation`, and/or `metadata` properties
|
|
65
81
|
* - Any other value (returns empty object)
|
|
66
82
|
*
|
|
67
83
|
* @returns An {@link EvaluationResult} object with extracted properties
|
|
@@ -81,7 +97,7 @@ function resultHasExplanation(result) {
|
|
|
81
97
|
* ```
|
|
82
98
|
*
|
|
83
99
|
* @example
|
|
84
|
-
* Convert an object with
|
|
100
|
+
* Convert an object with multiple properties:
|
|
85
101
|
* ```typescript
|
|
86
102
|
* const result = toEvaluationResult({
|
|
87
103
|
* score: 0.9,
|
|
@@ -129,6 +145,9 @@ function toEvaluationResult(result) {
|
|
|
129
145
|
if (resultHasExplanation(result)) {
|
|
130
146
|
evaluationResult.explanation = result.explanation;
|
|
131
147
|
}
|
|
148
|
+
if (resultHasMetadata(result)) {
|
|
149
|
+
evaluationResult.metadata = result.metadata;
|
|
150
|
+
}
|
|
132
151
|
return evaluationResult;
|
|
133
152
|
}
|
|
134
153
|
return {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"toEvaluationResult.js","sourceRoot":"","sources":["../../../src/helpers/toEvaluationResult.ts"],"names":[],"mappings":";;;AAEA;;;;;;;GAOG;AACH,SAAS,cAAc,CAAC,MAAe;IACrC,OAAO,CACL,OAAO,MAAM,KAAK,QAAQ;QAC1B,MAAM,KAAK,IAAI;QACf,OAAO,IAAI,MAAM;QACjB,OAAO,MAAM,CAAC,KAAK,KAAK,QAAQ,CACjC,CAAC;AACJ,CAAC;AAED;;;;;;;GAOG;AACH,SAAS,cAAc,CAAC,MAAe;IACrC,OAAO,CACL,OAAO,MAAM,KAAK,QAAQ;QAC1B,MAAM,KAAK,IAAI;QACf,OAAO,IAAI,MAAM;QACjB,OAAO,MAAM,CAAC,KAAK,KAAK,QAAQ,CACjC,CAAC;AACJ,CAAC;AAED;;;;;;;GAOG;AACH,SAAS,oBAAoB,CAC3B,MAAe;IAEf,OAAO,CACL,OAAO,MAAM,KAAK,QAAQ;QAC1B,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AE,CAAC;YAC3B,gBAAgB,CAAC,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC;QACxC,CAAC;QACD,IAAI,cAAc,CAAC,MAAM,CAAC,EAAE,CAAC;YAC3B,gBAAgB,CAAC,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC;QACxC,CAAC;QACD,IAAI,oBAAoB,CAAC,MAAM,CAAC,EAAE,CAAC;YACjC,gBAAgB,CAAC,WAAW,GAAG,MAAM,CAAC,WAAW,CAAC;QACpD,CAAC;QACD,IAAI,iBAAiB,CAAC,MAAM,CAAC,EAAE,CAAC;YAC9B,gBAAgB,CAAC,QAAQ,GAAG,MAAM,CAAC,QAAQ,CAAC;QAC9C,CAAC;QACD,OAAO,gBAAgB,CAAC;IAC1B,CAAC;IACD,OAAO,EAAE,CAAC;AACZ,CAAC"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { LanguageModel } from "ai";
|
|
2
2
|
import type { ClassificationChoicesMap, CreateClassificationEvaluatorArgs, EvaluatorFn, PromptTemplate, WithPromptTemplate } from "../types";
|
|
3
3
|
import type { ObjectMapping } from "../types/data";
|
|
4
|
-
import type {
|
|
4
|
+
import type { DecisionModel } from "../utils/isDecisionModel";
|
|
5
5
|
import { LLMEvaluator } from "./LLMEvaluator";
|
|
6
6
|
/**
|
|
7
7
|
* An LLM evaluator that performs evaluation via classification
|
|
@@ -16,7 +16,7 @@ export declare class ClassificationEvaluator<RecordType extends Record<string, u
|
|
|
16
16
|
/**
|
|
17
17
|
* The model to use for classification
|
|
18
18
|
*/
|
|
19
|
-
readonly model: LanguageModel |
|
|
19
|
+
readonly model: LanguageModel | DecisionModel;
|
|
20
20
|
/**
|
|
21
21
|
* The choices to classify the example into
|
|
22
22
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ClassificationEvaluator.d.ts","sourceRoot":"","sources":["../../../src/llm/ClassificationEvaluator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa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ACd,YAAY,EAAE,aAAa,CAAC,UAAU,CAAC,GACtC,uBAAuB,CAAC,UAAU,CAAC,CAKrC;CACF"}
|
|
@@ -18,11 +18,11 @@ export type ClassifyArgs = WithLLM & WithTelemetry & WithPrompt & {
|
|
|
18
18
|
/**
|
|
19
19
|
* A function that leverages an llm to perform a classification
|
|
20
20
|
*
|
|
21
|
-
* When given an AI SDK
|
|
22
|
-
* classification is routed through `
|
|
23
|
-
* choice question
|
|
24
|
-
* is returned in that case
|
|
25
|
-
*
|
|
21
|
+
* When given an AI SDK decision model (e.g. `openai.decisionModel("gpt-6-luna")`)
|
|
22
|
+
* the classification is routed through `experimental_decide` as a single
|
|
23
|
+
* choice question, with the prompt sent as plain text. Decision models cannot
|
|
24
|
+
* generate text, so no explanation is returned in that case, and
|
|
25
|
+
* `schemaName` / `schemaDescription` are ignored.
|
|
26
26
|
*/
|
|
27
27
|
export declare function generateClassification(args: ClassifyArgs): Promise<ClassificationResult>;
|
|
28
28
|
//# sourceMappingURL=generateClassification.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generateClassification.d.ts","sourceRoot":"","sources":["../../../src/llm/generateClassification.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"generateClassification.d.ts","sourceRoot":"","sources":["../../../src/llm/generateClassification.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,oBAAoB,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AACpE,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AACnD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAGnD,MAAM,MAAM,YAAY,GAAG,OAAO,GAChC,aAAa,GACb,UAAU,GAAG;IACX;;OAEG;IACH,MAAM,EAAE,CAAC,MAAM,EAAE,GAAG,MAAM,EAAE,CAAC,CAAC;IAC9B;;OAEG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB;;OAEG;IACH,iBAAiB,CAAC,EAAE,MAAM,CAAC;CAC5B,CAAC;AACJ;;;;;;;;GAQG;AACH,wBAAsB,sBAAsB,CAC1C,IAAI,EAAE,YAAY,GACjB,OAAO,CAAC,oBAAoB,CAAC,CAkC/B"}
|
|
@@ -14,34 +14,37 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
14
14
|
exports.generateClassification = generateClassification;
|
|
15
15
|
const ai_1 = require("ai");
|
|
16
16
|
const zod_1 = require("zod");
|
|
17
|
+
const decideClassification_1 = require("../decision/decideClassification");
|
|
17
18
|
const telemetry_1 = require("../telemetry");
|
|
18
|
-
const
|
|
19
|
+
const isDecisionModel_1 = require("../utils/isDecisionModel");
|
|
19
20
|
/**
|
|
20
21
|
* A function that leverages an llm to perform a classification
|
|
21
22
|
*
|
|
22
|
-
* When given an AI SDK
|
|
23
|
-
* classification is routed through `
|
|
24
|
-
* choice question
|
|
25
|
-
* is returned in that case
|
|
26
|
-
*
|
|
23
|
+
* When given an AI SDK decision model (e.g. `openai.decisionModel("gpt-6-luna")`)
|
|
24
|
+
* the classification is routed through `experimental_decide` as a single
|
|
25
|
+
* choice question, with the prompt sent as plain text. Decision models cannot
|
|
26
|
+
* generate text, so no explanation is returned in that case, and
|
|
27
|
+
* `schemaName` / `schemaDescription` are ignored.
|
|
27
28
|
*/
|
|
28
29
|
async function generateClassification(args) {
|
|
29
|
-
var _a, _b;
|
|
30
30
|
const { labels, model, schemaName, schemaDescription, telemetry } = args, prompt = __rest(args, ["labels", "model", "schemaName", "schemaDescription", "telemetry"]);
|
|
31
|
-
if ((0,
|
|
32
|
-
return
|
|
31
|
+
if ((0, isDecisionModel_1.isDecisionModel)(model)) {
|
|
32
|
+
return (0, decideClassification_1.decideClassification)({
|
|
33
|
+
model,
|
|
34
|
+
labels,
|
|
35
|
+
prompt,
|
|
36
|
+
telemetry,
|
|
37
|
+
});
|
|
33
38
|
}
|
|
34
|
-
const telemetryOptions = {
|
|
35
|
-
isEnabled: (_a = telemetry === null || telemetry === void 0 ? void 0 : telemetry.isEnabled) !== null && _a !== void 0 ? _a : true,
|
|
36
|
-
functionId: "generateClassification",
|
|
37
|
-
integrations: (0, telemetry_1.getTelemetryIntegrations)((_b = telemetry === null || telemetry === void 0 ? void 0 : telemetry.tracer) !== null && _b !== void 0 ? _b : telemetry_1.tracer),
|
|
38
|
-
};
|
|
39
39
|
const result = await (0, ai_1.generateObject)(Object.assign({ model,
|
|
40
40
|
schemaName,
|
|
41
41
|
schemaDescription, schema: zod_1.z.object({
|
|
42
42
|
explanation: zod_1.z.string(), // We place the explanation in hopes it uses reasoning to explain the label.
|
|
43
43
|
label: zod_1.z.enum(labels),
|
|
44
|
-
}), telemetry:
|
|
44
|
+
}), telemetry: (0, telemetry_1.getTelemetryOptions)({
|
|
45
|
+
functionId: "generateClassification",
|
|
46
|
+
telemetry,
|
|
47
|
+
}),
|
|
45
48
|
// AI SDK 7 rejects system messages inside `messages` by default; keep
|
|
46
49
|
// accepting them since prompt templates may include system messages.
|
|
47
50
|
allowSystemInMessages: true }, prompt));
|
|
@@ -50,59 +53,4 @@ async function generateClassification(args) {
|
|
|
50
53
|
explanation: result.object.explanation,
|
|
51
54
|
};
|
|
52
55
|
}
|
|
53
|
-
/**
|
|
54
|
-
* Classify using an AI SDK evaluation model. The rendered prompt becomes the
|
|
55
|
-
* shared evaluation state and the labels become the options of one choice
|
|
56
|
-
* question. The model's label probabilities and resolved model id are
|
|
57
|
-
* returned as metadata.
|
|
58
|
-
*/
|
|
59
|
-
async function evaluateClassification(args) {
|
|
60
|
-
const { model, labels, prompt } = args;
|
|
61
|
-
const result = await (0, ai_1.experimental_evaluate)({
|
|
62
|
-
model,
|
|
63
|
-
state: toEvaluationState(prompt),
|
|
64
|
-
questions: {
|
|
65
|
-
label: {
|
|
66
|
-
type: "choice",
|
|
67
|
-
instructions: "Read the prompt in the state and answer it by selecting exactly one of the choices.",
|
|
68
|
-
criteria: Object.fromEntries(labels.map((label) => [label, null])),
|
|
69
|
-
},
|
|
70
|
-
},
|
|
71
|
-
});
|
|
72
|
-
const answer = result.answers.label;
|
|
73
|
-
return {
|
|
74
|
-
label: answer.choice,
|
|
75
|
-
metadata: {
|
|
76
|
-
probabilities: answer.probabilities,
|
|
77
|
-
modelId: result.response.modelId,
|
|
78
|
-
},
|
|
79
|
-
};
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* Convert an AI SDK prompt into JSON-compatible evaluation state.
|
|
83
|
-
* A bare text prompt is passed through as a string; anything else
|
|
84
|
-
* (instructions, system messages, message arrays) is serialized to JSON.
|
|
85
|
-
*/
|
|
86
|
-
function toEvaluationState(prompt) {
|
|
87
|
-
const { instructions, system, messages, prompt: promptContent } = prompt;
|
|
88
|
-
if (typeof promptContent === "string" &&
|
|
89
|
-
instructions === undefined &&
|
|
90
|
-
system === undefined &&
|
|
91
|
-
messages === undefined) {
|
|
92
|
-
return promptContent;
|
|
93
|
-
}
|
|
94
|
-
const state = {};
|
|
95
|
-
if (instructions !== undefined)
|
|
96
|
-
state.instructions = instructions;
|
|
97
|
-
if (system !== undefined)
|
|
98
|
-
state.system = system;
|
|
99
|
-
if (promptContent !== undefined)
|
|
100
|
-
state.prompt = promptContent;
|
|
101
|
-
if (messages !== undefined)
|
|
102
|
-
state.messages = messages;
|
|
103
|
-
// Round-trip through JSON so the state only contains JSON values
|
|
104
|
-
// (strips `undefined`, turns Date/URL into strings). Binary file parts
|
|
105
|
-
// are not supported by evaluation models and are not stripped here.
|
|
106
|
-
return JSON.parse(JSON.stringify(state));
|
|
107
|
-
}
|
|
108
56
|
//# sourceMappingURL=generateClassification.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generateClassification.js","sourceRoot":"","sources":["../../../src/llm/generateClassification.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"generateClassification.js","sourceRoot":"","sources":["../../../src/llm/generateClassification.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,2BAAoC;AACpC,6BAAwB;AAExB,2EAAwE;AACxE,4CAAmD;AAInD,8DAA2D;AAkB3D;;;;;;;;GAQG;AACI,KAAK,iCACV,IAAkB;IAElB,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,UAAU,EAAE,iBAAiB,EAAE,SAAS,KAC7D,IAAI,EAD8D,MAAM,UACxE,IAAI,EADA,mEAAsE,CACtE,CAAC;IAEP,IAAI,IAAA,iCAAe,EAAC,KAAK,CAAC,EAAE,CAAC;QAC3B,OAAO,IAAA,2CAAoB,EAAC;YAC1B,KAAK;YACL,MAAM;YACN,MAAM;YACN,SAAS;SACV,CAAC,CAAC;IACL,CAAC;IAED,MAAM,MAAM,GAAG,MAAM,IAAA,mBAAc,kBACjC,KAAK;QACL,UAAU;QACV,iBAAiB,EACjB,MAAM,EAAE,OAAC,CAAC,MAAM,CAAC;YACf,WAAW,EAAE,OAAC,CAAC,MAAM,EAAE,EAAE,4EAA4E;YACrG,KAAK,EAAE,OAAC,CAAC,IAAI,CAAC,MAAM,CAAC;SACtB,CAAC,EACF,SAAS,EAAE,IAAA,+BAAmB,EAAC;YAC7B,UAAU,EAAE,wBAAwB;YACpC,SAAS;SACV,CAAC;QACF,sEAAsE;QACtE,qEAAqE;QACrE,qBAAqB,EAAE,IAAI,IACxB,MAAM,EACT,CAAC;IACH,OAAO;QACL,KAAK,EAAE,MAAM,CAAC,MAAM,CAAC,KAAK;QAC1B,WAAW,EAAE,MAAM,CAAC,MAAM,CAAC,WAAW;KACvC,CAAC;AACJ,CAAC"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { type Tracer } from "@opentelemetry/api";
|
|
2
2
|
import type { Telemetry } from "ai";
|
|
3
|
+
import type { TelemetryConfig } from "../types/otel";
|
|
3
4
|
/**
|
|
4
5
|
* Returns a lazy tracer that resolves from `trace.getTracer()` on every call,
|
|
5
6
|
* so evaluator spans follow whichever provider is currently mounted as global.
|
|
@@ -18,4 +19,17 @@ export declare const tracer: Tracer;
|
|
|
18
19
|
* used as-is so the call is not traced twice.
|
|
19
20
|
*/
|
|
20
21
|
export declare function getTelemetryIntegrations(integrationTracer: Tracer): Telemetry[];
|
|
22
|
+
/**
|
|
23
|
+
* Builds the AI SDK `telemetry` call option for an evaluator call. Telemetry
|
|
24
|
+
* is on unless explicitly disabled, and spans go to the configured tracer or
|
|
25
|
+
* the default Phoenix tracer.
|
|
26
|
+
*/
|
|
27
|
+
export declare function getTelemetryOptions(args: {
|
|
28
|
+
functionId: string;
|
|
29
|
+
telemetry?: TelemetryConfig;
|
|
30
|
+
}): {
|
|
31
|
+
isEnabled: boolean;
|
|
32
|
+
functionId: string;
|
|
33
|
+
integrations: Telemetry[];
|
|
34
|
+
};
|
|
21
35
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/telemetry/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAS,KAAK,MAAM,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,IAAI,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/telemetry/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAS,KAAK,MAAM,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,IAAI,CAAC;AAEpC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAIrD;;;;;;GAMG;AACH,wBAAgB,SAAS,CAAC,IAAI,GAAE,MAA4B,GAAG,MAAM,CAUpE;AAED,eAAO,MAAM,MAAM,QAAc,CAAC;AAElC;;;;;;;GAOG;AACH,wBAAgB,wBAAwB,CACtC,iBAAiB,EAAE,MAAM,GACxB,SAAS,EAAE,CAwBb;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE;IACxC,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,CAAC,EAAE,eAAe,CAAC;CAC7B;;;;EAOA"}
|