@ai-sdk/gateway 4.0.84 → 4.0.85
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 +7 -0
- package/dist/index.d.ts +13 -3
- package/dist/index.js +230 -73
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/gateway-evaluation-model-settings.ts +1 -0
- package/src/gateway-evaluation-model.ts +173 -0
- package/src/gateway-model-entry.ts +1 -0
- package/src/gateway-provider.ts +26 -0
- package/src/index.ts +1 -0
package/package.json
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type GatewayEvaluationModelId = 'typesafe-ai/jev-latest' | (string & {});
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Experimental_EvaluationModelV4 as EvaluationModelV4,
|
|
3
|
+
SharedV4ProviderMetadata,
|
|
4
|
+
} from '@ai-sdk/provider';
|
|
5
|
+
import {
|
|
6
|
+
combineHeaders,
|
|
7
|
+
createJsonErrorResponseHandler,
|
|
8
|
+
createJsonResponseHandler,
|
|
9
|
+
getErrorMessage,
|
|
10
|
+
lazySchema,
|
|
11
|
+
postJsonToApi,
|
|
12
|
+
resolve,
|
|
13
|
+
zodSchema,
|
|
14
|
+
type Resolvable,
|
|
15
|
+
} from '@ai-sdk/provider-utils';
|
|
16
|
+
import { z } from './zod';
|
|
17
|
+
import { asGatewayError } from './errors';
|
|
18
|
+
import { parseAuthMethod } from './errors/parse-auth-method';
|
|
19
|
+
import type { GatewayConfig } from './gateway-config';
|
|
20
|
+
|
|
21
|
+
export class GatewayEvaluationModel implements EvaluationModelV4 {
|
|
22
|
+
readonly specificationVersion = 'v4';
|
|
23
|
+
|
|
24
|
+
readonly supportedQuestionTypes = ['choice', 'score', 'boolean'] as const;
|
|
25
|
+
|
|
26
|
+
constructor(
|
|
27
|
+
readonly modelId: string,
|
|
28
|
+
private readonly config: GatewayConfig & {
|
|
29
|
+
provider: string;
|
|
30
|
+
o11yHeaders: Resolvable<Record<string, string>>;
|
|
31
|
+
},
|
|
32
|
+
) {}
|
|
33
|
+
|
|
34
|
+
get provider(): string {
|
|
35
|
+
return this.config.provider;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async doEvaluate({
|
|
39
|
+
state,
|
|
40
|
+
questions,
|
|
41
|
+
headers,
|
|
42
|
+
abortSignal,
|
|
43
|
+
providerOptions,
|
|
44
|
+
}: Parameters<EvaluationModelV4['doEvaluate']>[0]): Promise<
|
|
45
|
+
Awaited<ReturnType<EvaluationModelV4['doEvaluate']>>
|
|
46
|
+
> {
|
|
47
|
+
const resolvedHeaders = this.config.headers
|
|
48
|
+
? await resolve(this.config.headers)
|
|
49
|
+
: undefined;
|
|
50
|
+
try {
|
|
51
|
+
const {
|
|
52
|
+
responseHeaders,
|
|
53
|
+
value: responseBody,
|
|
54
|
+
rawValue,
|
|
55
|
+
} = await postJsonToApi({
|
|
56
|
+
url: this.getUrl(),
|
|
57
|
+
headers: combineHeaders(
|
|
58
|
+
resolvedHeaders,
|
|
59
|
+
headers ?? {},
|
|
60
|
+
this.getModelConfigHeaders(),
|
|
61
|
+
await resolve(this.config.o11yHeaders),
|
|
62
|
+
),
|
|
63
|
+
body: {
|
|
64
|
+
state,
|
|
65
|
+
questions,
|
|
66
|
+
...(providerOptions ? { providerOptions } : {}),
|
|
67
|
+
},
|
|
68
|
+
successfulResponseHandler: createJsonResponseHandler(
|
|
69
|
+
gatewayEvaluationResponseSchema,
|
|
70
|
+
),
|
|
71
|
+
failedResponseHandler: createJsonErrorResponseHandler({
|
|
72
|
+
errorSchema: z.any(),
|
|
73
|
+
errorToMessage: data => getErrorMessage(data) ?? 'unknown error',
|
|
74
|
+
}),
|
|
75
|
+
...(abortSignal && { abortSignal }),
|
|
76
|
+
fetch: this.config.fetch,
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
return {
|
|
80
|
+
answers: responseBody.answers,
|
|
81
|
+
...(responseBody.rounding ? { rounding: responseBody.rounding } : {}),
|
|
82
|
+
...(responseBody.usage ? { usage: responseBody.usage } : {}),
|
|
83
|
+
warnings: responseBody.warnings ?? [],
|
|
84
|
+
providerMetadata:
|
|
85
|
+
responseBody.providerMetadata as unknown as SharedV4ProviderMetadata,
|
|
86
|
+
response: {
|
|
87
|
+
modelId: this.modelId,
|
|
88
|
+
headers: responseHeaders,
|
|
89
|
+
body: rawValue,
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
} catch (error) {
|
|
93
|
+
throw await asGatewayError(
|
|
94
|
+
error,
|
|
95
|
+
await parseAuthMethod(resolvedHeaders ?? {}),
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
private getUrl() {
|
|
101
|
+
return `${this.config.baseURL}/evaluation-model`;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
private getModelConfigHeaders() {
|
|
105
|
+
return {
|
|
106
|
+
'ai-evaluation-model-specification-version': '4',
|
|
107
|
+
'ai-model-id': this.modelId,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const gatewayEvaluationAnswerSchema = z.discriminatedUnion('type', [
|
|
113
|
+
z.object({
|
|
114
|
+
type: z.literal('choice'),
|
|
115
|
+
choice: z.string(),
|
|
116
|
+
probabilities: z.record(z.string(), z.number()).optional(),
|
|
117
|
+
}),
|
|
118
|
+
z.object({
|
|
119
|
+
type: z.literal('score'),
|
|
120
|
+
score: z.number(),
|
|
121
|
+
probabilities: z.record(z.string(), z.number()).optional(),
|
|
122
|
+
}),
|
|
123
|
+
z.object({
|
|
124
|
+
type: z.literal('boolean'),
|
|
125
|
+
probability: z.number(),
|
|
126
|
+
}),
|
|
127
|
+
]);
|
|
128
|
+
|
|
129
|
+
const gatewayEvaluationWarningSchema = z.discriminatedUnion('type', [
|
|
130
|
+
z.object({
|
|
131
|
+
type: z.literal('unsupported'),
|
|
132
|
+
feature: z.string(),
|
|
133
|
+
details: z.string().optional(),
|
|
134
|
+
}),
|
|
135
|
+
z.object({
|
|
136
|
+
type: z.literal('compatibility'),
|
|
137
|
+
feature: z.string(),
|
|
138
|
+
details: z.string().optional(),
|
|
139
|
+
}),
|
|
140
|
+
z.object({
|
|
141
|
+
type: z.literal('deprecated'),
|
|
142
|
+
setting: z.string(),
|
|
143
|
+
message: z.string(),
|
|
144
|
+
}),
|
|
145
|
+
z.object({
|
|
146
|
+
type: z.literal('other'),
|
|
147
|
+
message: z.string(),
|
|
148
|
+
}),
|
|
149
|
+
]);
|
|
150
|
+
|
|
151
|
+
const gatewayEvaluationResponseSchema = lazySchema(() =>
|
|
152
|
+
zodSchema(
|
|
153
|
+
z.object({
|
|
154
|
+
answers: z.record(z.string(), gatewayEvaluationAnswerSchema),
|
|
155
|
+
rounding: z
|
|
156
|
+
.object({
|
|
157
|
+
probabilityDecimals: z.number().optional(),
|
|
158
|
+
scoreDecimals: z.number().optional(),
|
|
159
|
+
})
|
|
160
|
+
.optional(),
|
|
161
|
+
usage: z
|
|
162
|
+
.object({
|
|
163
|
+
inputTokens: z.number().optional(),
|
|
164
|
+
outputTokens: z.number().optional(),
|
|
165
|
+
})
|
|
166
|
+
.optional(),
|
|
167
|
+
warnings: z.array(gatewayEvaluationWarningSchema).optional(),
|
|
168
|
+
providerMetadata: z
|
|
169
|
+
.record(z.string(), z.record(z.string(), z.unknown()))
|
|
170
|
+
.optional(),
|
|
171
|
+
}),
|
|
172
|
+
),
|
|
173
|
+
);
|
package/src/gateway-provider.ts
CHANGED
|
@@ -36,6 +36,7 @@ import { GatewayLanguageModel } from './gateway-language-model';
|
|
|
36
36
|
import { GatewayEmbeddingModel } from './gateway-embedding-model';
|
|
37
37
|
import { GatewayImageModel } from './gateway-image-model';
|
|
38
38
|
import { GatewayVideoModel } from './gateway-video-model';
|
|
39
|
+
import { GatewayEvaluationModel } from './gateway-evaluation-model';
|
|
39
40
|
import { GatewayRerankingModel } from './gateway-reranking-model';
|
|
40
41
|
import { GatewaySpeechModel } from './gateway-speech-model';
|
|
41
42
|
import {
|
|
@@ -44,6 +45,7 @@ import {
|
|
|
44
45
|
} from './gateway-transcription-model';
|
|
45
46
|
import { GatewayRealtimeModel } from './gateway-realtime-model';
|
|
46
47
|
import type { GatewayEmbeddingModelId } from './gateway-embedding-model-settings';
|
|
48
|
+
import type { GatewayEvaluationModelId } from './gateway-evaluation-model-settings';
|
|
47
49
|
import type { GatewayImageModelId } from './gateway-image-model-settings';
|
|
48
50
|
import type { GatewayRerankingModelId } from './gateway-reranking-model-settings';
|
|
49
51
|
import type { GatewaySpeechModelId } from './gateway-speech-model-settings';
|
|
@@ -61,6 +63,7 @@ import type {
|
|
|
61
63
|
SpeechModelV4,
|
|
62
64
|
TranscriptionModelV4,
|
|
63
65
|
Experimental_VideoModelV4,
|
|
66
|
+
Experimental_EvaluationModelV4,
|
|
64
67
|
Experimental_RealtimeFactoryV4 as RealtimeFactoryV4,
|
|
65
68
|
Experimental_RealtimeFactoryV4GetTokenOptions as RealtimeFactoryV4GetTokenOptions,
|
|
66
69
|
ProviderV4,
|
|
@@ -155,6 +158,18 @@ export interface GatewayProvider extends ProviderV4 {
|
|
|
155
158
|
*/
|
|
156
159
|
rerankingModel(modelId: GatewayRerankingModelId): RerankingModelV4;
|
|
157
160
|
|
|
161
|
+
/**
|
|
162
|
+
* Creates a model for evaluating state against questions.
|
|
163
|
+
*/
|
|
164
|
+
evaluation(modelId: GatewayEvaluationModelId): Experimental_EvaluationModelV4;
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Creates a model for evaluating state against questions.
|
|
168
|
+
*/
|
|
169
|
+
evaluationModel(
|
|
170
|
+
modelId: GatewayEvaluationModelId,
|
|
171
|
+
): Experimental_EvaluationModelV4;
|
|
172
|
+
|
|
158
173
|
/**
|
|
159
174
|
* Creates a model for text-to-speech generation.
|
|
160
175
|
*/
|
|
@@ -568,6 +583,17 @@ export function createGateway(
|
|
|
568
583
|
};
|
|
569
584
|
provider.rerankingModel = createRerankingModel;
|
|
570
585
|
provider.reranking = createRerankingModel;
|
|
586
|
+
const createEvaluationModel = (modelId: GatewayEvaluationModelId) => {
|
|
587
|
+
return new GatewayEvaluationModel(modelId, {
|
|
588
|
+
provider: 'gateway',
|
|
589
|
+
baseURL,
|
|
590
|
+
headers: getHeaders,
|
|
591
|
+
fetch: options.fetch,
|
|
592
|
+
o11yHeaders: createO11yHeaders(),
|
|
593
|
+
});
|
|
594
|
+
};
|
|
595
|
+
provider.evaluationModel = createEvaluationModel;
|
|
596
|
+
provider.evaluation = createEvaluationModel;
|
|
571
597
|
const createSpeechModel = (modelId: GatewaySpeechModelId) => {
|
|
572
598
|
return new GatewaySpeechModel(modelId, {
|
|
573
599
|
provider: 'gateway',
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export type { GatewayEmbeddingModelId } from './gateway-embedding-model-settings';
|
|
2
|
+
export type { GatewayEvaluationModelId } from './gateway-evaluation-model-settings';
|
|
2
3
|
export type { GatewayImageModelId } from './gateway-image-model-settings';
|
|
3
4
|
export type { GatewayModelId } from './gateway-language-model-settings';
|
|
4
5
|
export {
|