@ai-sdk/google-vertex 5.0.0-beta.10 → 5.0.0-beta.108
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 +930 -8
- package/README.md +65 -2
- package/dist/anthropic/edge/index.d.ts +18 -151
- package/dist/anthropic/edge/index.js +88 -70
- package/dist/anthropic/edge/index.js.map +1 -1
- package/dist/anthropic/index.d.ts +18 -151
- package/dist/anthropic/index.js +88 -81
- package/dist/anthropic/index.js.map +1 -1
- package/dist/edge/index.d.ts +58 -29
- package/dist/edge/index.js +581 -278
- package/dist/edge/index.js.map +1 -1
- package/dist/index.d.ts +70 -32
- package/dist/index.js +590 -296
- package/dist/index.js.map +1 -1
- package/dist/maas/edge/index.d.ts +76 -0
- package/dist/maas/edge/index.js +209 -0
- package/dist/maas/edge/index.js.map +1 -0
- package/dist/maas/index.d.ts +60 -0
- package/dist/maas/index.js +109 -0
- package/dist/maas/index.js.map +1 -0
- package/dist/xai/edge/index.d.ts +92 -0
- package/dist/xai/edge/index.js +259 -0
- package/dist/xai/edge/index.js.map +1 -0
- package/dist/xai/index.d.ts +76 -0
- package/dist/xai/index.js +159 -0
- package/dist/xai/index.js.map +1 -0
- package/docs/16-google-vertex.mdx +640 -102
- package/maas/edge.d.ts +1 -0
- package/maas/index.d.ts +1 -0
- package/package.json +49 -24
- package/src/anthropic/edge/google-vertex-anthropic-provider-edge.ts +17 -13
- package/src/anthropic/edge/index.ts +6 -2
- package/src/anthropic/{google-vertex-anthropic-messages-options.ts → google-vertex-anthropic-options.ts} +4 -1
- package/src/anthropic/google-vertex-anthropic-provider-node.ts +18 -13
- package/src/anthropic/google-vertex-anthropic-provider.ts +68 -19
- package/src/anthropic/index.ts +6 -2
- package/src/edge/google-vertex-provider-edge.ts +10 -12
- package/src/edge/index.ts +8 -1
- package/src/google-vertex-auth-google-auth-library.ts +13 -26
- package/src/google-vertex-config.ts +2 -2
- package/src/{google-vertex-embedding-options.ts → google-vertex-embedding-model-options.ts} +1 -0
- package/src/google-vertex-embedding-model.ts +35 -10
- package/src/google-vertex-image-model-options.ts +74 -0
- package/src/google-vertex-image-model.ts +106 -133
- package/src/google-vertex-options.ts +1 -1
- package/src/google-vertex-provider-base.ts +311 -0
- package/src/google-vertex-provider.ts +43 -233
- package/src/google-vertex-speech-model-options.ts +11 -0
- package/src/google-vertex-transcription-model-options.ts +46 -0
- package/src/google-vertex-transcription-model.ts +231 -0
- package/src/google-vertex-video-model-options.ts +49 -0
- package/src/google-vertex-video-model.ts +39 -75
- package/src/index.ts +20 -5
- package/src/maas/edge/google-vertex-maas-provider-edge.ts +64 -0
- package/src/maas/edge/index.ts +13 -0
- package/src/maas/google-vertex-maas-options.ts +15 -0
- package/src/maas/google-vertex-maas-provider-node.ts +65 -0
- package/src/maas/google-vertex-maas-provider.ts +122 -0
- package/src/maas/index.ts +13 -0
- package/src/xai/edge/google-vertex-xai-provider-edge.ts +61 -0
- package/src/xai/edge/index.ts +9 -0
- package/src/xai/google-vertex-xai-options.ts +7 -0
- package/src/xai/google-vertex-xai-provider-node.ts +62 -0
- package/src/xai/google-vertex-xai-provider.ts +212 -0
- package/src/xai/index.ts +9 -0
- package/xai/edge.d.ts +1 -0
- package/xai/index.d.ts +1 -0
- package/dist/anthropic/edge/index.d.mts +0 -231
- package/dist/anthropic/edge/index.mjs +0 -259
- package/dist/anthropic/edge/index.mjs.map +0 -1
- package/dist/anthropic/index.d.mts +0 -215
- package/dist/anthropic/index.mjs +0 -164
- package/dist/anthropic/index.mjs.map +0 -1
- package/dist/edge/index.d.mts +0 -160
- package/dist/edge/index.mjs +0 -1049
- package/dist/edge/index.mjs.map +0 -1
- package/dist/index.d.mts +0 -219
- package/dist/index.mjs +0 -960
- package/dist/index.mjs.map +0 -1
- package/src/google-vertex-provider-node.ts +0 -49
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
import {
|
|
2
|
+
GoogleLanguageModel,
|
|
3
|
+
GoogleSpeechModel,
|
|
4
|
+
} from '@ai-sdk/google/internal';
|
|
5
|
+
import type {
|
|
6
|
+
Experimental_VideoModelV4,
|
|
7
|
+
ImageModelV4,
|
|
8
|
+
LanguageModelV4,
|
|
9
|
+
ProviderV4,
|
|
10
|
+
SpeechModelV4,
|
|
11
|
+
TranscriptionModelV4,
|
|
12
|
+
} from '@ai-sdk/provider';
|
|
13
|
+
import {
|
|
14
|
+
generateId,
|
|
15
|
+
loadOptionalSetting,
|
|
16
|
+
loadSetting,
|
|
17
|
+
normalizeHeaders,
|
|
18
|
+
resolve,
|
|
19
|
+
withoutTrailingSlash,
|
|
20
|
+
withUserAgentSuffix,
|
|
21
|
+
type FetchFunction,
|
|
22
|
+
type Resolvable,
|
|
23
|
+
} from '@ai-sdk/provider-utils';
|
|
24
|
+
import { VERSION } from './version';
|
|
25
|
+
import type { GoogleVertexConfig } from './google-vertex-config';
|
|
26
|
+
import { GoogleVertexEmbeddingModel } from './google-vertex-embedding-model';
|
|
27
|
+
import type { GoogleVertexEmbeddingModelId } from './google-vertex-embedding-model-options';
|
|
28
|
+
import { GoogleVertexImageModel } from './google-vertex-image-model';
|
|
29
|
+
import type { GoogleVertexImageModelId } from './google-vertex-image-settings';
|
|
30
|
+
import type { GoogleVertexModelId } from './google-vertex-options';
|
|
31
|
+
import { googleVertexTools } from './google-vertex-tools';
|
|
32
|
+
import { GoogleVertexTranscriptionModel } from './google-vertex-transcription-model';
|
|
33
|
+
import type { GoogleVertexTranscriptionModelId } from './google-vertex-transcription-model-options';
|
|
34
|
+
import { GoogleVertexVideoModel } from './google-vertex-video-model';
|
|
35
|
+
import type { GoogleVertexVideoModelId } from './google-vertex-video-settings';
|
|
36
|
+
import type { GoogleVertexSpeechModelId } from './google-vertex-speech-model-options';
|
|
37
|
+
|
|
38
|
+
const EXPRESS_MODE_BASE_URL =
|
|
39
|
+
'https://aiplatform.googleapis.com/v1/publishers/google';
|
|
40
|
+
|
|
41
|
+
// set `x-goog-api-key` header to API key for express mode
|
|
42
|
+
function createExpressModeFetch(
|
|
43
|
+
apiKey: string,
|
|
44
|
+
customFetch?: FetchFunction,
|
|
45
|
+
): FetchFunction {
|
|
46
|
+
return async (url, init) => {
|
|
47
|
+
const modifiedInit: RequestInit = {
|
|
48
|
+
...init,
|
|
49
|
+
headers: {
|
|
50
|
+
...(init?.headers ? normalizeHeaders(init.headers) : {}),
|
|
51
|
+
'x-goog-api-key': apiKey,
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
return (customFetch ?? fetch)(url.toString(), modifiedInit);
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface GoogleVertexProvider extends ProviderV4 {
|
|
59
|
+
/**
|
|
60
|
+
* Creates a model for text generation.
|
|
61
|
+
*/
|
|
62
|
+
(modelId: GoogleVertexModelId): LanguageModelV4;
|
|
63
|
+
|
|
64
|
+
languageModel: (modelId: GoogleVertexModelId) => LanguageModelV4;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Creates a model for image generation.
|
|
68
|
+
*/
|
|
69
|
+
image(modelId: GoogleVertexImageModelId): ImageModelV4;
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Creates a model for image generation.
|
|
73
|
+
*/
|
|
74
|
+
imageModel(modelId: GoogleVertexImageModelId): ImageModelV4;
|
|
75
|
+
|
|
76
|
+
tools: typeof googleVertexTools;
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* @deprecated Use `embeddingModel` instead.
|
|
80
|
+
*/
|
|
81
|
+
textEmbeddingModel(
|
|
82
|
+
modelId: GoogleVertexEmbeddingModelId,
|
|
83
|
+
): GoogleVertexEmbeddingModel;
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Creates a model for video generation.
|
|
87
|
+
*/
|
|
88
|
+
video(modelId: GoogleVertexVideoModelId): Experimental_VideoModelV4;
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Creates a model for video generation.
|
|
92
|
+
*/
|
|
93
|
+
videoModel(modelId: GoogleVertexVideoModelId): Experimental_VideoModelV4;
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Creates a model for speech generation (text-to-speech).
|
|
97
|
+
*/
|
|
98
|
+
speech(modelId: GoogleVertexSpeechModelId): SpeechModelV4;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Creates a model for speech generation (text-to-speech).
|
|
102
|
+
*/
|
|
103
|
+
speechModel(modelId: GoogleVertexSpeechModelId): SpeechModelV4;
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Creates a model for transcription (speech-to-text).
|
|
107
|
+
*/
|
|
108
|
+
transcription(
|
|
109
|
+
modelId: GoogleVertexTranscriptionModelId,
|
|
110
|
+
): TranscriptionModelV4;
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Creates a model for transcription (speech-to-text).
|
|
114
|
+
*/
|
|
115
|
+
transcriptionModel(
|
|
116
|
+
modelId: GoogleVertexTranscriptionModelId,
|
|
117
|
+
): TranscriptionModelV4;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export interface GoogleVertexProviderSettings {
|
|
121
|
+
/**
|
|
122
|
+
* Optional. The API key for the Google Cloud project. If provided, the
|
|
123
|
+
* provider will use express mode with API key authentication. Defaults to
|
|
124
|
+
* the value of the `GOOGLE_VERTEX_API_KEY` environment variable.
|
|
125
|
+
*/
|
|
126
|
+
apiKey?: string;
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Your Google Vertex location. Defaults to the environment variable `GOOGLE_VERTEX_LOCATION`.
|
|
130
|
+
*/
|
|
131
|
+
location?: string;
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Your Google Vertex project. Defaults to the environment variable `GOOGLE_VERTEX_PROJECT`.
|
|
135
|
+
*/
|
|
136
|
+
project?: string;
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Headers to use for requests. Can be:
|
|
140
|
+
* - A headers object
|
|
141
|
+
* - A Promise that resolves to a headers object
|
|
142
|
+
* - A function that returns a headers object
|
|
143
|
+
* - A function that returns a Promise of a headers object
|
|
144
|
+
*/
|
|
145
|
+
headers?: Resolvable<Record<string, string | undefined>>;
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Custom fetch implementation. You can use it as a middleware to intercept requests,
|
|
149
|
+
* or to provide a custom fetch implementation for e.g. testing.
|
|
150
|
+
*/
|
|
151
|
+
fetch?: FetchFunction;
|
|
152
|
+
|
|
153
|
+
// for testing
|
|
154
|
+
generateId?: () => string;
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Base URL for the Google Vertex API calls.
|
|
158
|
+
*/
|
|
159
|
+
baseURL?: string;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Create a Google Vertex AI provider instance.
|
|
164
|
+
*/
|
|
165
|
+
export function createGoogleVertex(
|
|
166
|
+
options: GoogleVertexProviderSettings = {},
|
|
167
|
+
): GoogleVertexProvider {
|
|
168
|
+
const apiKey = loadOptionalSetting({
|
|
169
|
+
settingValue: options.apiKey,
|
|
170
|
+
environmentVariableName: 'GOOGLE_VERTEX_API_KEY',
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
const loadGoogleVertexProject = () =>
|
|
174
|
+
loadSetting({
|
|
175
|
+
settingValue: options.project,
|
|
176
|
+
settingName: 'project',
|
|
177
|
+
environmentVariableName: 'GOOGLE_VERTEX_PROJECT',
|
|
178
|
+
description: 'Google Vertex project',
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
const loadGoogleVertexLocation = () =>
|
|
182
|
+
loadSetting({
|
|
183
|
+
settingValue: options.location,
|
|
184
|
+
settingName: 'location',
|
|
185
|
+
environmentVariableName: 'GOOGLE_VERTEX_LOCATION',
|
|
186
|
+
description: 'Google Vertex location',
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
const loadBaseURL = () => {
|
|
190
|
+
if (apiKey) {
|
|
191
|
+
return withoutTrailingSlash(options.baseURL) ?? EXPRESS_MODE_BASE_URL;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const region = loadGoogleVertexLocation();
|
|
195
|
+
const project = loadGoogleVertexProject();
|
|
196
|
+
|
|
197
|
+
const getHost = () => {
|
|
198
|
+
if (region === 'global') {
|
|
199
|
+
return 'aiplatform.googleapis.com';
|
|
200
|
+
} else if (region === 'eu' || region === 'us') {
|
|
201
|
+
return `aiplatform.${region}.rep.googleapis.com`;
|
|
202
|
+
} else {
|
|
203
|
+
return `${region}-aiplatform.googleapis.com`;
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
return (
|
|
208
|
+
withoutTrailingSlash(options.baseURL) ??
|
|
209
|
+
`https://${getHost()}/v1beta1/projects/${project}/locations/${region}/publishers/google`
|
|
210
|
+
);
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
const createConfig = (name: string): GoogleVertexConfig => {
|
|
214
|
+
const getHeaders = async () => {
|
|
215
|
+
const originalHeaders = await resolve(options.headers ?? {});
|
|
216
|
+
return withUserAgentSuffix(
|
|
217
|
+
originalHeaders,
|
|
218
|
+
`ai-sdk/google-vertex/${VERSION}`,
|
|
219
|
+
);
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
return {
|
|
223
|
+
provider: `google.vertex.${name}`,
|
|
224
|
+
headers: getHeaders,
|
|
225
|
+
fetch: apiKey
|
|
226
|
+
? createExpressModeFetch(apiKey, options.fetch)
|
|
227
|
+
: options.fetch,
|
|
228
|
+
baseURL: loadBaseURL(),
|
|
229
|
+
};
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
const createChatModel = (modelId: GoogleVertexModelId) => {
|
|
233
|
+
return new GoogleLanguageModel(modelId, {
|
|
234
|
+
...createConfig('chat'),
|
|
235
|
+
generateId: options.generateId ?? generateId,
|
|
236
|
+
supportedUrls: () => ({
|
|
237
|
+
'*': [
|
|
238
|
+
// HTTP URLs:
|
|
239
|
+
/^https?:\/\/.*$/,
|
|
240
|
+
// Google Cloud Storage URLs:
|
|
241
|
+
/^gs:\/\/.*$/,
|
|
242
|
+
],
|
|
243
|
+
}),
|
|
244
|
+
});
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
const createEmbeddingModel = (modelId: GoogleVertexEmbeddingModelId) =>
|
|
248
|
+
new GoogleVertexEmbeddingModel(modelId, createConfig('embedding'));
|
|
249
|
+
|
|
250
|
+
const createImageModel = (modelId: GoogleVertexImageModelId) =>
|
|
251
|
+
new GoogleVertexImageModel(modelId, {
|
|
252
|
+
...createConfig('image'),
|
|
253
|
+
generateId: options.generateId ?? generateId,
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
const createVideoModel = (modelId: GoogleVertexVideoModelId) =>
|
|
257
|
+
new GoogleVertexVideoModel(modelId, {
|
|
258
|
+
...createConfig('video'),
|
|
259
|
+
generateId: options.generateId ?? generateId,
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
const createSpeechModel = (modelId: GoogleVertexSpeechModelId) =>
|
|
263
|
+
new GoogleSpeechModel(modelId, createConfig('speech'));
|
|
264
|
+
|
|
265
|
+
// Cloud Speech-to-Text reuses the Vertex auth headers from createConfig, but
|
|
266
|
+
// targets the Speech-to-Text API.
|
|
267
|
+
const createTranscriptionModel = (
|
|
268
|
+
modelId: GoogleVertexTranscriptionModelId,
|
|
269
|
+
) => {
|
|
270
|
+
if (apiKey) {
|
|
271
|
+
throw new Error(
|
|
272
|
+
'Google Vertex transcription models do not support Express Mode API keys. Use standard Google Cloud credentials instead.',
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
const config = createConfig('transcription');
|
|
277
|
+
return new GoogleVertexTranscriptionModel(modelId, {
|
|
278
|
+
provider: config.provider,
|
|
279
|
+
headers: config.headers,
|
|
280
|
+
fetch: config.fetch,
|
|
281
|
+
project: loadGoogleVertexProject(),
|
|
282
|
+
location: loadGoogleVertexLocation(),
|
|
283
|
+
});
|
|
284
|
+
};
|
|
285
|
+
|
|
286
|
+
const provider = function (modelId: GoogleVertexModelId) {
|
|
287
|
+
if (new.target) {
|
|
288
|
+
throw new Error(
|
|
289
|
+
'The Google Vertex AI model function cannot be called with the new keyword.',
|
|
290
|
+
);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
return createChatModel(modelId);
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
provider.specificationVersion = 'v4' as const;
|
|
297
|
+
provider.languageModel = createChatModel;
|
|
298
|
+
provider.embeddingModel = createEmbeddingModel;
|
|
299
|
+
provider.textEmbeddingModel = createEmbeddingModel;
|
|
300
|
+
provider.image = createImageModel;
|
|
301
|
+
provider.imageModel = createImageModel;
|
|
302
|
+
provider.video = createVideoModel;
|
|
303
|
+
provider.videoModel = createVideoModel;
|
|
304
|
+
provider.speech = createSpeechModel;
|
|
305
|
+
provider.speechModel = createSpeechModel;
|
|
306
|
+
provider.transcription = createTranscriptionModel;
|
|
307
|
+
provider.transcriptionModel = createTranscriptionModel;
|
|
308
|
+
provider.tools = googleVertexTools;
|
|
309
|
+
|
|
310
|
+
return provider;
|
|
311
|
+
}
|
|
@@ -1,136 +1,24 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { loadOptionalSetting, resolve } from '@ai-sdk/provider-utils';
|
|
2
|
+
import type { GoogleAuthOptions } from 'google-auth-library';
|
|
3
|
+
import { createAuthTokenGenerator } from './google-vertex-auth-google-auth-library';
|
|
2
4
|
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
Resolvable,
|
|
16
|
-
withoutTrailingSlash,
|
|
17
|
-
withUserAgentSuffix,
|
|
18
|
-
} from '@ai-sdk/provider-utils';
|
|
19
|
-
import { VERSION } from './version';
|
|
20
|
-
import { GoogleVertexConfig } from './google-vertex-config';
|
|
21
|
-
import { GoogleVertexEmbeddingModel } from './google-vertex-embedding-model';
|
|
22
|
-
import { GoogleVertexEmbeddingModelId } from './google-vertex-embedding-options';
|
|
23
|
-
import { GoogleVertexImageModel } from './google-vertex-image-model';
|
|
24
|
-
import { GoogleVertexImageModelId } from './google-vertex-image-settings';
|
|
25
|
-
import { GoogleVertexModelId } from './google-vertex-options';
|
|
26
|
-
import { googleVertexTools } from './google-vertex-tools';
|
|
27
|
-
import { GoogleVertexVideoModel } from './google-vertex-video-model';
|
|
28
|
-
import { GoogleVertexVideoModelId } from './google-vertex-video-settings';
|
|
29
|
-
|
|
30
|
-
const EXPRESS_MODE_BASE_URL =
|
|
31
|
-
'https://aiplatform.googleapis.com/v1/publishers/google';
|
|
32
|
-
|
|
33
|
-
// set `x-goog-api-key` header to API key for express mode
|
|
34
|
-
function createExpressModeFetch(
|
|
35
|
-
apiKey: string,
|
|
36
|
-
customFetch?: FetchFunction,
|
|
37
|
-
): FetchFunction {
|
|
38
|
-
return async (url, init) => {
|
|
39
|
-
const modifiedInit: RequestInit = {
|
|
40
|
-
...init,
|
|
41
|
-
headers: {
|
|
42
|
-
...(init?.headers ? normalizeHeaders(init.headers) : {}),
|
|
43
|
-
'x-goog-api-key': apiKey,
|
|
44
|
-
},
|
|
45
|
-
};
|
|
46
|
-
return (customFetch ?? fetch)(url.toString(), modifiedInit);
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export interface GoogleVertexProvider extends ProviderV3 {
|
|
51
|
-
/**
|
|
52
|
-
* Creates a model for text generation.
|
|
53
|
-
*/
|
|
54
|
-
(modelId: GoogleVertexModelId): LanguageModelV3;
|
|
55
|
-
|
|
56
|
-
languageModel: (modelId: GoogleVertexModelId) => LanguageModelV3;
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Creates a model for image generation.
|
|
60
|
-
*/
|
|
61
|
-
image(modelId: GoogleVertexImageModelId): ImageModelV3;
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Creates a model for image generation.
|
|
65
|
-
*/
|
|
66
|
-
imageModel(modelId: GoogleVertexImageModelId): ImageModelV3;
|
|
67
|
-
|
|
68
|
-
tools: typeof googleVertexTools;
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* @deprecated Use `embeddingModel` instead.
|
|
72
|
-
*/
|
|
73
|
-
textEmbeddingModel(
|
|
74
|
-
modelId: GoogleVertexEmbeddingModelId,
|
|
75
|
-
): GoogleVertexEmbeddingModel;
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Creates a model for video generation.
|
|
79
|
-
*/
|
|
80
|
-
video(modelId: GoogleVertexVideoModelId): Experimental_VideoModelV3;
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Creates a model for video generation.
|
|
84
|
-
*/
|
|
85
|
-
videoModel(modelId: GoogleVertexVideoModelId): Experimental_VideoModelV3;
|
|
5
|
+
createGoogleVertex as createGoogleVertexOriginal,
|
|
6
|
+
type GoogleVertexProvider,
|
|
7
|
+
type GoogleVertexProviderSettings as GoogleVertexProviderSettingsOriginal,
|
|
8
|
+
} from './google-vertex-provider-base';
|
|
9
|
+
export interface GoogleVertexProviderSettings extends GoogleVertexProviderSettingsOriginal {
|
|
10
|
+
/**
|
|
11
|
+
* Optional. The Authentication options provided by google-auth-library.
|
|
12
|
+
* Complete list of authentication options is documented in the
|
|
13
|
+
* GoogleAuthOptions interface:
|
|
14
|
+
* https://github.com/googleapis/google-auth-library-nodejs/blob/main/src/auth/googleauth.ts.
|
|
15
|
+
*/
|
|
16
|
+
googleAuthOptions?: GoogleAuthOptions;
|
|
86
17
|
}
|
|
87
18
|
|
|
88
|
-
export
|
|
89
|
-
/**
|
|
90
|
-
* Optional. The API key for the Google Cloud project. If provided, the
|
|
91
|
-
* provider will use express mode with API key authentication. Defaults to
|
|
92
|
-
* the value of the `GOOGLE_VERTEX_API_KEY` environment variable.
|
|
93
|
-
*/
|
|
94
|
-
apiKey?: string;
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Your Google Vertex location. Defaults to the environment variable `GOOGLE_VERTEX_LOCATION`.
|
|
98
|
-
*/
|
|
99
|
-
location?: string;
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Your Google Vertex project. Defaults to the environment variable `GOOGLE_VERTEX_PROJECT`.
|
|
103
|
-
*/
|
|
104
|
-
project?: string;
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Headers to use for requests. Can be:
|
|
108
|
-
* - A headers object
|
|
109
|
-
* - A Promise that resolves to a headers object
|
|
110
|
-
* - A function that returns a headers object
|
|
111
|
-
* - A function that returns a Promise of a headers object
|
|
112
|
-
*/
|
|
113
|
-
headers?: Resolvable<Record<string, string | undefined>>;
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Custom fetch implementation. You can use it as a middleware to intercept requests,
|
|
117
|
-
* or to provide a custom fetch implementation for e.g. testing.
|
|
118
|
-
*/
|
|
119
|
-
fetch?: FetchFunction;
|
|
120
|
-
|
|
121
|
-
// for testing
|
|
122
|
-
generateId?: () => string;
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* Base URL for the Google Vertex API calls.
|
|
126
|
-
*/
|
|
127
|
-
baseURL?: string;
|
|
128
|
-
}
|
|
19
|
+
export type { GoogleVertexProvider };
|
|
129
20
|
|
|
130
|
-
|
|
131
|
-
* Create a Google Vertex AI provider instance.
|
|
132
|
-
*/
|
|
133
|
-
export function createVertex(
|
|
21
|
+
export function createGoogleVertex(
|
|
134
22
|
options: GoogleVertexProviderSettings = {},
|
|
135
23
|
): GoogleVertexProvider {
|
|
136
24
|
const apiKey = loadOptionalSetting({
|
|
@@ -138,108 +26,30 @@ export function createVertex(
|
|
|
138
26
|
environmentVariableName: 'GOOGLE_VERTEX_API_KEY',
|
|
139
27
|
});
|
|
140
28
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
const region = loadVertexLocation();
|
|
163
|
-
const project = loadVertexProject();
|
|
164
|
-
|
|
165
|
-
// For global region, use aiplatform.googleapis.com directly
|
|
166
|
-
// For other regions, use region-aiplatform.googleapis.com
|
|
167
|
-
const baseHost = `${region === 'global' ? '' : region + '-'}aiplatform.googleapis.com`;
|
|
168
|
-
|
|
169
|
-
return (
|
|
170
|
-
withoutTrailingSlash(options.baseURL) ??
|
|
171
|
-
`https://${baseHost}/v1beta1/projects/${project}/locations/${region}/publishers/google`
|
|
172
|
-
);
|
|
173
|
-
};
|
|
174
|
-
|
|
175
|
-
const createConfig = (name: string): GoogleVertexConfig => {
|
|
176
|
-
const getHeaders = async () => {
|
|
177
|
-
const originalHeaders = await resolve(options.headers ?? {});
|
|
178
|
-
return withUserAgentSuffix(
|
|
179
|
-
originalHeaders,
|
|
180
|
-
`ai-sdk/google-vertex/${VERSION}`,
|
|
181
|
-
);
|
|
182
|
-
};
|
|
183
|
-
|
|
184
|
-
return {
|
|
185
|
-
provider: `google.vertex.${name}`,
|
|
186
|
-
headers: getHeaders,
|
|
187
|
-
fetch: apiKey
|
|
188
|
-
? createExpressModeFetch(apiKey, options.fetch)
|
|
189
|
-
: options.fetch,
|
|
190
|
-
baseURL: loadBaseURL(),
|
|
191
|
-
};
|
|
192
|
-
};
|
|
193
|
-
|
|
194
|
-
const createChatModel = (modelId: GoogleVertexModelId) => {
|
|
195
|
-
return new GoogleGenerativeAILanguageModel(modelId, {
|
|
196
|
-
...createConfig('chat'),
|
|
197
|
-
generateId: options.generateId ?? generateId,
|
|
198
|
-
supportedUrls: () => ({
|
|
199
|
-
'*': [
|
|
200
|
-
// HTTP URLs:
|
|
201
|
-
/^https?:\/\/.*$/,
|
|
202
|
-
// Google Cloud Storage URLs:
|
|
203
|
-
/^gs:\/\/.*$/,
|
|
204
|
-
],
|
|
205
|
-
}),
|
|
206
|
-
});
|
|
207
|
-
};
|
|
208
|
-
|
|
209
|
-
const createEmbeddingModel = (modelId: GoogleVertexEmbeddingModelId) =>
|
|
210
|
-
new GoogleVertexEmbeddingModel(modelId, createConfig('embedding'));
|
|
211
|
-
|
|
212
|
-
const createImageModel = (modelId: GoogleVertexImageModelId) =>
|
|
213
|
-
new GoogleVertexImageModel(modelId, {
|
|
214
|
-
...createConfig('image'),
|
|
215
|
-
generateId: options.generateId ?? generateId,
|
|
216
|
-
});
|
|
217
|
-
|
|
218
|
-
const createVideoModel = (modelId: GoogleVertexVideoModelId) =>
|
|
219
|
-
new GoogleVertexVideoModel(modelId, {
|
|
220
|
-
...createConfig('video'),
|
|
221
|
-
generateId: options.generateId ?? generateId,
|
|
222
|
-
});
|
|
223
|
-
|
|
224
|
-
const provider = function (modelId: GoogleVertexModelId) {
|
|
225
|
-
if (new.target) {
|
|
226
|
-
throw new Error(
|
|
227
|
-
'The Google Vertex AI model function cannot be called with the new keyword.',
|
|
228
|
-
);
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
return createChatModel(modelId);
|
|
232
|
-
};
|
|
233
|
-
|
|
234
|
-
provider.specificationVersion = 'v3' as const;
|
|
235
|
-
provider.languageModel = createChatModel;
|
|
236
|
-
provider.embeddingModel = createEmbeddingModel;
|
|
237
|
-
provider.textEmbeddingModel = createEmbeddingModel;
|
|
238
|
-
provider.image = createImageModel;
|
|
239
|
-
provider.imageModel = createImageModel;
|
|
240
|
-
provider.video = createVideoModel;
|
|
241
|
-
provider.videoModel = createVideoModel;
|
|
242
|
-
provider.tools = googleVertexTools;
|
|
243
|
-
|
|
244
|
-
return provider;
|
|
29
|
+
if (apiKey) {
|
|
30
|
+
return createGoogleVertexOriginal(options);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const googleAuthOptions =
|
|
34
|
+
options.project == null
|
|
35
|
+
? options.googleAuthOptions
|
|
36
|
+
: {
|
|
37
|
+
projectId: options.project,
|
|
38
|
+
...options.googleAuthOptions,
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const generateAuthToken = createAuthTokenGenerator(googleAuthOptions);
|
|
42
|
+
|
|
43
|
+
return createGoogleVertexOriginal({
|
|
44
|
+
...options,
|
|
45
|
+
headers: async () => ({
|
|
46
|
+
Authorization: `Bearer ${await generateAuthToken()}`,
|
|
47
|
+
...(await resolve(options.headers)),
|
|
48
|
+
}),
|
|
49
|
+
});
|
|
245
50
|
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Default Google Vertex AI provider instance.
|
|
54
|
+
*/
|
|
55
|
+
export const googleVertex = createGoogleVertex();
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { GoogleSpeechModelOptions } from '@ai-sdk/google';
|
|
2
|
+
|
|
3
|
+
// https://docs.cloud.google.com/text-to-speech/docs/gemini-tts
|
|
4
|
+
export type GoogleVertexSpeechModelId =
|
|
5
|
+
| 'gemini-2.5-flash-tts'
|
|
6
|
+
| 'gemini-2.5-pro-tts'
|
|
7
|
+
| 'gemini-2.5-flash-lite-preview-tts'
|
|
8
|
+
| 'gemini-3.1-flash-tts-preview'
|
|
9
|
+
| (string & {});
|
|
10
|
+
|
|
11
|
+
export type GoogleVertexSpeechModelOptions = GoogleSpeechModelOptions;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { z } from 'zod/v4';
|
|
2
|
+
|
|
3
|
+
// Google Cloud Speech-to-Text transcription models (Speech-to-Text v2).
|
|
4
|
+
// https://docs.cloud.google.com/speech-to-text/docs/transcription-model
|
|
5
|
+
export type GoogleVertexTranscriptionModelId =
|
|
6
|
+
| 'chirp_2'
|
|
7
|
+
| 'chirp_3'
|
|
8
|
+
| 'telephony'
|
|
9
|
+
| (string & {});
|
|
10
|
+
|
|
11
|
+
export const googleVertexTranscriptionProviderOptionsSchema = z.object({
|
|
12
|
+
/**
|
|
13
|
+
* BCP-47 language codes to recognize (e.g. `['en-US']`), or `['auto']` to let
|
|
14
|
+
* Chirp auto-detect the spoken language. Defaults to `['auto']`. For
|
|
15
|
+
* `telephony`, pass a supported explicit language code.
|
|
16
|
+
*/
|
|
17
|
+
languageCodes: z.array(z.string()).optional(),
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Whether to add punctuation to the transcript. Defaults to `true`.
|
|
21
|
+
*/
|
|
22
|
+
enableAutomaticPunctuation: z.boolean().optional(),
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Whether to include word-level timestamps. Defaults to `true` so the
|
|
26
|
+
* transcription result can include segments.
|
|
27
|
+
*
|
|
28
|
+
* Enabling word-level timestamps can reduce transcription quality and speed
|
|
29
|
+
* for Chirp models.
|
|
30
|
+
*/
|
|
31
|
+
enableWordTimeOffsets: z.boolean().optional(),
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The Cloud Speech-to-Text region for the request (e.g. `'us'`, `'eu'`,
|
|
35
|
+
* `'us-central1'`). Defaults to the provider `location`.
|
|
36
|
+
*
|
|
37
|
+
* Note: Speech-to-Text regions differ from Vertex AI regions. Chirp is only
|
|
38
|
+
* available in specific Speech-to-Text regions and is not available in the
|
|
39
|
+
* `global` location.
|
|
40
|
+
*/
|
|
41
|
+
region: z.string().optional(),
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
export type GoogleVertexTranscriptionModelOptions = z.infer<
|
|
45
|
+
typeof googleVertexTranscriptionProviderOptionsSchema
|
|
46
|
+
>;
|