@ai-sdk/gateway 3.0.135 → 3.0.136
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.mts +25 -5
- package/dist/index.d.ts +25 -5
- package/dist/index.js +265 -21
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +315 -58
- package/dist/index.mjs.map +1 -1
- package/docs/00-ai-gateway.mdx +34 -0
- package/package.json +1 -1
- package/src/gateway-language-model-settings.ts +1 -0
- package/src/gateway-model-entry.ts +2 -0
- package/src/gateway-provider.ts +50 -0
- package/src/gateway-speech-model-settings.ts +5 -0
- package/src/gateway-speech-model.ts +138 -0
- package/src/gateway-transcription-model-settings.ts +6 -0
- package/src/gateway-transcription-model.ts +148 -0
- package/src/gateway-video-model-settings.ts +1 -0
- package/src/index.ts +2 -0
package/docs/00-ai-gateway.mdx
CHANGED
|
@@ -204,6 +204,40 @@ console.log(ranking);
|
|
|
204
204
|
|
|
205
205
|
Reranking models are useful for improving search results in retrieval-augmented generation (RAG) pipelines by re-scoring candidate documents after an initial retrieval step.
|
|
206
206
|
|
|
207
|
+
## Speech Models
|
|
208
|
+
|
|
209
|
+
You can create speech models using the `speechModel` method on the provider instance:
|
|
210
|
+
|
|
211
|
+
```ts
|
|
212
|
+
import { experimental_generateSpeech as generateSpeech } from "ai";
|
|
213
|
+
import { gateway } from "@ai-sdk/gateway";
|
|
214
|
+
|
|
215
|
+
const { audio } = await generateSpeech({
|
|
216
|
+
model: gateway.speechModel("openai/tts-1"),
|
|
217
|
+
text: "Hello, world!",
|
|
218
|
+
voice: "alloy",
|
|
219
|
+
});
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
Speech models are useful for generating audio from text using the same Gateway authentication, routing, and observability as other Gateway model types.
|
|
223
|
+
|
|
224
|
+
## Transcription Models
|
|
225
|
+
|
|
226
|
+
You can create transcription models using the `transcriptionModel` method on the provider instance:
|
|
227
|
+
|
|
228
|
+
```ts
|
|
229
|
+
import { experimental_transcribe as transcribe } from "ai";
|
|
230
|
+
import { gateway } from "@ai-sdk/gateway";
|
|
231
|
+
import { readFile } from "fs/promises";
|
|
232
|
+
|
|
233
|
+
const { text } = await transcribe({
|
|
234
|
+
model: gateway.transcriptionModel("openai/gpt-4o-transcribe"),
|
|
235
|
+
audio: await readFile("audio.mp3"),
|
|
236
|
+
});
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
Transcription models are useful for converting audio to text through the AI Gateway.
|
|
240
|
+
|
|
207
241
|
## Available Models
|
|
208
242
|
|
|
209
243
|
The AI Gateway supports models from OpenAI, Anthropic, Google, Meta, xAI, Mistral, DeepSeek, Amazon Bedrock, Cohere, Perplexity, Alibaba, and other providers.
|
package/package.json
CHANGED
package/src/gateway-provider.ts
CHANGED
|
@@ -29,9 +29,13 @@ import { GatewayEmbeddingModel } from './gateway-embedding-model';
|
|
|
29
29
|
import { GatewayImageModel } from './gateway-image-model';
|
|
30
30
|
import { GatewayVideoModel } from './gateway-video-model';
|
|
31
31
|
import { GatewayRerankingModel } from './gateway-reranking-model';
|
|
32
|
+
import { GatewaySpeechModel } from './gateway-speech-model';
|
|
33
|
+
import { GatewayTranscriptionModel } from './gateway-transcription-model';
|
|
32
34
|
import type { GatewayEmbeddingModelId } from './gateway-embedding-model-settings';
|
|
33
35
|
import type { GatewayImageModelId } from './gateway-image-model-settings';
|
|
34
36
|
import type { GatewayRerankingModelId } from './gateway-reranking-model-settings';
|
|
37
|
+
import type { GatewaySpeechModelId } from './gateway-speech-model-settings';
|
|
38
|
+
import type { GatewayTranscriptionModelId } from './gateway-transcription-model-settings';
|
|
35
39
|
import type { GatewayVideoModelId } from './gateway-video-model-settings';
|
|
36
40
|
import { gatewayTools } from './gateway-tools';
|
|
37
41
|
import { getVercelOidcToken, getVercelRequestId } from './vercel-environment';
|
|
@@ -41,6 +45,8 @@ import type {
|
|
|
41
45
|
EmbeddingModelV3,
|
|
42
46
|
ImageModelV3,
|
|
43
47
|
RerankingModelV3,
|
|
48
|
+
SpeechModelV3,
|
|
49
|
+
TranscriptionModelV3,
|
|
44
50
|
Experimental_VideoModelV3,
|
|
45
51
|
ProviderV3,
|
|
46
52
|
} from '@ai-sdk/provider';
|
|
@@ -130,6 +136,28 @@ export interface GatewayProvider extends ProviderV3 {
|
|
|
130
136
|
*/
|
|
131
137
|
rerankingModel(modelId: GatewayRerankingModelId): RerankingModelV3;
|
|
132
138
|
|
|
139
|
+
/**
|
|
140
|
+
* Creates a model for text-to-speech generation.
|
|
141
|
+
*/
|
|
142
|
+
speech(modelId: GatewaySpeechModelId): SpeechModelV3;
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Creates a model for text-to-speech generation.
|
|
146
|
+
*/
|
|
147
|
+
speechModel(modelId: GatewaySpeechModelId): SpeechModelV3;
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Creates a model for audio transcription.
|
|
151
|
+
*/
|
|
152
|
+
transcription(modelId: GatewayTranscriptionModelId): TranscriptionModelV3;
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Creates a model for audio transcription.
|
|
156
|
+
*/
|
|
157
|
+
transcriptionModel(
|
|
158
|
+
modelId: GatewayTranscriptionModelId,
|
|
159
|
+
): TranscriptionModelV3;
|
|
160
|
+
|
|
133
161
|
/**
|
|
134
162
|
* Gateway-specific tools executed server-side.
|
|
135
163
|
*/
|
|
@@ -378,6 +406,28 @@ export function createGatewayProvider(
|
|
|
378
406
|
};
|
|
379
407
|
provider.rerankingModel = createRerankingModel;
|
|
380
408
|
provider.reranking = createRerankingModel;
|
|
409
|
+
const createSpeechModel = (modelId: GatewaySpeechModelId) => {
|
|
410
|
+
return new GatewaySpeechModel(modelId, {
|
|
411
|
+
provider: 'gateway',
|
|
412
|
+
baseURL,
|
|
413
|
+
headers: getHeaders,
|
|
414
|
+
fetch: options.fetch,
|
|
415
|
+
o11yHeaders: createO11yHeaders(),
|
|
416
|
+
});
|
|
417
|
+
};
|
|
418
|
+
provider.speechModel = createSpeechModel;
|
|
419
|
+
provider.speech = createSpeechModel;
|
|
420
|
+
const createTranscriptionModel = (modelId: GatewayTranscriptionModelId) => {
|
|
421
|
+
return new GatewayTranscriptionModel(modelId, {
|
|
422
|
+
provider: 'gateway',
|
|
423
|
+
baseURL,
|
|
424
|
+
headers: getHeaders,
|
|
425
|
+
fetch: options.fetch,
|
|
426
|
+
o11yHeaders: createO11yHeaders(),
|
|
427
|
+
});
|
|
428
|
+
};
|
|
429
|
+
provider.transcriptionModel = createTranscriptionModel;
|
|
430
|
+
provider.transcription = createTranscriptionModel;
|
|
381
431
|
provider.chat = provider.languageModel;
|
|
382
432
|
provider.embedding = provider.embeddingModel;
|
|
383
433
|
provider.image = provider.imageModel;
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
SharedV3ProviderMetadata,
|
|
3
|
+
SharedV3Warning,
|
|
4
|
+
SpeechModelV3,
|
|
5
|
+
} from '@ai-sdk/provider';
|
|
6
|
+
import {
|
|
7
|
+
combineHeaders,
|
|
8
|
+
createJsonErrorResponseHandler,
|
|
9
|
+
createJsonResponseHandler,
|
|
10
|
+
postJsonToApi,
|
|
11
|
+
resolve,
|
|
12
|
+
type Resolvable,
|
|
13
|
+
} from '@ai-sdk/provider-utils';
|
|
14
|
+
import { z } from 'zod/v4';
|
|
15
|
+
import { asGatewayError } from './errors';
|
|
16
|
+
import { parseAuthMethod } from './errors/parse-auth-method';
|
|
17
|
+
import type { GatewayConfig } from './gateway-config';
|
|
18
|
+
|
|
19
|
+
export class GatewaySpeechModel implements SpeechModelV3 {
|
|
20
|
+
readonly specificationVersion = 'v3' as const;
|
|
21
|
+
|
|
22
|
+
constructor(
|
|
23
|
+
readonly modelId: string,
|
|
24
|
+
private readonly config: GatewayConfig & {
|
|
25
|
+
provider: string;
|
|
26
|
+
o11yHeaders: Resolvable<Record<string, string>>;
|
|
27
|
+
},
|
|
28
|
+
) {}
|
|
29
|
+
|
|
30
|
+
get provider(): string {
|
|
31
|
+
return this.config.provider;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async doGenerate({
|
|
35
|
+
text,
|
|
36
|
+
voice,
|
|
37
|
+
outputFormat,
|
|
38
|
+
instructions,
|
|
39
|
+
speed,
|
|
40
|
+
language,
|
|
41
|
+
providerOptions,
|
|
42
|
+
headers,
|
|
43
|
+
abortSignal,
|
|
44
|
+
}: Parameters<SpeechModelV3['doGenerate']>[0]): Promise<
|
|
45
|
+
Awaited<ReturnType<SpeechModelV3['doGenerate']>>
|
|
46
|
+
> {
|
|
47
|
+
const resolvedHeaders = await resolve(this.config.headers());
|
|
48
|
+
try {
|
|
49
|
+
const {
|
|
50
|
+
responseHeaders,
|
|
51
|
+
value: responseBody,
|
|
52
|
+
rawValue,
|
|
53
|
+
} = await postJsonToApi({
|
|
54
|
+
url: this.getUrl(),
|
|
55
|
+
headers: combineHeaders(
|
|
56
|
+
resolvedHeaders,
|
|
57
|
+
headers ?? {},
|
|
58
|
+
this.getModelConfigHeaders(),
|
|
59
|
+
await resolve(this.config.o11yHeaders),
|
|
60
|
+
),
|
|
61
|
+
body: {
|
|
62
|
+
text,
|
|
63
|
+
...(voice && { voice }),
|
|
64
|
+
...(outputFormat && { outputFormat }),
|
|
65
|
+
...(instructions && { instructions }),
|
|
66
|
+
...(speed != null && { speed }),
|
|
67
|
+
...(language && { language }),
|
|
68
|
+
...(providerOptions && { providerOptions }),
|
|
69
|
+
},
|
|
70
|
+
successfulResponseHandler: createJsonResponseHandler(
|
|
71
|
+
gatewaySpeechResponseSchema,
|
|
72
|
+
),
|
|
73
|
+
failedResponseHandler: createJsonErrorResponseHandler({
|
|
74
|
+
errorSchema: z.any(),
|
|
75
|
+
errorToMessage: data => data,
|
|
76
|
+
}),
|
|
77
|
+
...(abortSignal && { abortSignal }),
|
|
78
|
+
fetch: this.config.fetch,
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
return {
|
|
82
|
+
audio: responseBody.audio,
|
|
83
|
+
warnings: (responseBody.warnings ?? []) as Array<SharedV3Warning>,
|
|
84
|
+
providerMetadata:
|
|
85
|
+
responseBody.providerMetadata as SharedV3ProviderMetadata,
|
|
86
|
+
response: {
|
|
87
|
+
timestamp: new Date(),
|
|
88
|
+
modelId: this.modelId,
|
|
89
|
+
headers: responseHeaders,
|
|
90
|
+
body: rawValue,
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
} catch (error) {
|
|
94
|
+
throw await asGatewayError(
|
|
95
|
+
error,
|
|
96
|
+
await parseAuthMethod(resolvedHeaders ?? {}),
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
private getUrl() {
|
|
102
|
+
return `${this.config.baseURL}/speech-model`;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
private getModelConfigHeaders() {
|
|
106
|
+
return {
|
|
107
|
+
'ai-speech-model-specification-version': '3',
|
|
108
|
+
'ai-model-id': this.modelId,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const providerMetadataEntrySchema = z.object({}).catchall(z.unknown());
|
|
114
|
+
|
|
115
|
+
const gatewaySpeechWarningSchema = z.discriminatedUnion('type', [
|
|
116
|
+
z.object({
|
|
117
|
+
type: z.literal('unsupported'),
|
|
118
|
+
feature: z.string(),
|
|
119
|
+
details: z.string().optional(),
|
|
120
|
+
}),
|
|
121
|
+
z.object({
|
|
122
|
+
type: z.literal('compatibility'),
|
|
123
|
+
feature: z.string(),
|
|
124
|
+
details: z.string().optional(),
|
|
125
|
+
}),
|
|
126
|
+
z.object({
|
|
127
|
+
type: z.literal('other'),
|
|
128
|
+
message: z.string(),
|
|
129
|
+
}),
|
|
130
|
+
]);
|
|
131
|
+
|
|
132
|
+
const gatewaySpeechResponseSchema = z.object({
|
|
133
|
+
audio: z.string(),
|
|
134
|
+
warnings: z.array(gatewaySpeechWarningSchema).optional(),
|
|
135
|
+
providerMetadata: z
|
|
136
|
+
.record(z.string(), providerMetadataEntrySchema)
|
|
137
|
+
.optional(),
|
|
138
|
+
});
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
SharedV3ProviderMetadata,
|
|
3
|
+
SharedV3Warning,
|
|
4
|
+
TranscriptionModelV3,
|
|
5
|
+
} from '@ai-sdk/provider';
|
|
6
|
+
import {
|
|
7
|
+
combineHeaders,
|
|
8
|
+
convertUint8ArrayToBase64,
|
|
9
|
+
createJsonErrorResponseHandler,
|
|
10
|
+
createJsonResponseHandler,
|
|
11
|
+
postJsonToApi,
|
|
12
|
+
resolve,
|
|
13
|
+
type Resolvable,
|
|
14
|
+
} from '@ai-sdk/provider-utils';
|
|
15
|
+
import { z } from 'zod/v4';
|
|
16
|
+
import { asGatewayError } from './errors';
|
|
17
|
+
import { parseAuthMethod } from './errors/parse-auth-method';
|
|
18
|
+
import type { GatewayConfig } from './gateway-config';
|
|
19
|
+
|
|
20
|
+
export class GatewayTranscriptionModel implements TranscriptionModelV3 {
|
|
21
|
+
readonly specificationVersion = 'v3' as const;
|
|
22
|
+
|
|
23
|
+
constructor(
|
|
24
|
+
readonly modelId: string,
|
|
25
|
+
private readonly config: GatewayConfig & {
|
|
26
|
+
provider: string;
|
|
27
|
+
o11yHeaders: Resolvable<Record<string, string>>;
|
|
28
|
+
},
|
|
29
|
+
) {}
|
|
30
|
+
|
|
31
|
+
get provider(): string {
|
|
32
|
+
return this.config.provider;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async doGenerate({
|
|
36
|
+
audio,
|
|
37
|
+
mediaType,
|
|
38
|
+
providerOptions,
|
|
39
|
+
headers,
|
|
40
|
+
abortSignal,
|
|
41
|
+
}: Parameters<TranscriptionModelV3['doGenerate']>[0]): Promise<
|
|
42
|
+
Awaited<ReturnType<TranscriptionModelV3['doGenerate']>>
|
|
43
|
+
> {
|
|
44
|
+
const resolvedHeaders = await resolve(this.config.headers());
|
|
45
|
+
try {
|
|
46
|
+
const {
|
|
47
|
+
responseHeaders,
|
|
48
|
+
value: responseBody,
|
|
49
|
+
rawValue,
|
|
50
|
+
} = await postJsonToApi({
|
|
51
|
+
url: this.getUrl(),
|
|
52
|
+
headers: combineHeaders(
|
|
53
|
+
resolvedHeaders,
|
|
54
|
+
headers ?? {},
|
|
55
|
+
this.getModelConfigHeaders(),
|
|
56
|
+
await resolve(this.config.o11yHeaders),
|
|
57
|
+
),
|
|
58
|
+
body: {
|
|
59
|
+
audio:
|
|
60
|
+
audio instanceof Uint8Array
|
|
61
|
+
? convertUint8ArrayToBase64(audio)
|
|
62
|
+
: audio,
|
|
63
|
+
mediaType,
|
|
64
|
+
...(providerOptions && { providerOptions }),
|
|
65
|
+
},
|
|
66
|
+
successfulResponseHandler: createJsonResponseHandler(
|
|
67
|
+
gatewayTranscriptionResponseSchema,
|
|
68
|
+
),
|
|
69
|
+
failedResponseHandler: createJsonErrorResponseHandler({
|
|
70
|
+
errorSchema: z.any(),
|
|
71
|
+
errorToMessage: data => data,
|
|
72
|
+
}),
|
|
73
|
+
...(abortSignal && { abortSignal }),
|
|
74
|
+
fetch: this.config.fetch,
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
return {
|
|
78
|
+
text: responseBody.text,
|
|
79
|
+
segments: responseBody.segments ?? [],
|
|
80
|
+
language: responseBody.language ?? undefined,
|
|
81
|
+
durationInSeconds: responseBody.durationInSeconds ?? undefined,
|
|
82
|
+
warnings: (responseBody.warnings ?? []) as Array<SharedV3Warning>,
|
|
83
|
+
providerMetadata:
|
|
84
|
+
responseBody.providerMetadata as SharedV3ProviderMetadata,
|
|
85
|
+
response: {
|
|
86
|
+
timestamp: new Date(),
|
|
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}/transcription-model`;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
private getModelConfigHeaders() {
|
|
105
|
+
return {
|
|
106
|
+
'ai-transcription-model-specification-version': '3',
|
|
107
|
+
'ai-model-id': this.modelId,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const providerMetadataEntrySchema = z.object({}).catchall(z.unknown());
|
|
113
|
+
|
|
114
|
+
const gatewayTranscriptionWarningSchema = z.discriminatedUnion('type', [
|
|
115
|
+
z.object({
|
|
116
|
+
type: z.literal('unsupported'),
|
|
117
|
+
feature: z.string(),
|
|
118
|
+
details: z.string().optional(),
|
|
119
|
+
}),
|
|
120
|
+
z.object({
|
|
121
|
+
type: z.literal('compatibility'),
|
|
122
|
+
feature: z.string(),
|
|
123
|
+
details: z.string().optional(),
|
|
124
|
+
}),
|
|
125
|
+
z.object({
|
|
126
|
+
type: z.literal('other'),
|
|
127
|
+
message: z.string(),
|
|
128
|
+
}),
|
|
129
|
+
]);
|
|
130
|
+
|
|
131
|
+
const gatewayTranscriptionResponseSchema = z.object({
|
|
132
|
+
text: z.string(),
|
|
133
|
+
segments: z
|
|
134
|
+
.array(
|
|
135
|
+
z.object({
|
|
136
|
+
text: z.string(),
|
|
137
|
+
startSecond: z.number(),
|
|
138
|
+
endSecond: z.number(),
|
|
139
|
+
}),
|
|
140
|
+
)
|
|
141
|
+
.optional(),
|
|
142
|
+
language: z.string().nullish(),
|
|
143
|
+
durationInSeconds: z.number().nullish(),
|
|
144
|
+
warnings: z.array(gatewayTranscriptionWarningSchema).optional(),
|
|
145
|
+
providerMetadata: z
|
|
146
|
+
.record(z.string(), providerMetadataEntrySchema)
|
|
147
|
+
.optional(),
|
|
148
|
+
});
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
export type { GatewayModelId } from './gateway-language-model-settings';
|
|
2
2
|
export type { GatewayRerankingModelId } from './gateway-reranking-model-settings';
|
|
3
|
+
export type { GatewaySpeechModelId } from './gateway-speech-model-settings';
|
|
4
|
+
export type { GatewayTranscriptionModelId } from './gateway-transcription-model-settings';
|
|
3
5
|
export type { GatewayVideoModelId } from './gateway-video-model-settings';
|
|
4
6
|
export type {
|
|
5
7
|
GatewayLanguageModelEntry,
|