@ai-sdk/google-vertex 5.0.0-beta.62 → 5.0.0-beta.63
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 +18 -0
- package/dist/anthropic/edge/index.d.ts +10 -5
- package/dist/anthropic/edge/index.js +15 -13
- package/dist/anthropic/edge/index.js.map +1 -1
- package/dist/anthropic/index.d.ts +10 -5
- package/dist/anthropic/index.js +14 -12
- package/dist/anthropic/index.js.map +1 -1
- package/dist/edge/index.d.ts +3 -3
- package/dist/edge/index.js +190 -153
- package/dist/edge/index.js.map +1 -1
- package/dist/index.d.ts +7 -7
- package/dist/index.js +192 -155
- package/dist/index.js.map +1 -1
- package/dist/maas/edge/index.d.ts +3 -3
- package/dist/maas/edge/index.js +9 -7
- package/dist/maas/edge/index.js.map +1 -1
- package/dist/maas/index.d.ts +3 -3
- package/dist/maas/index.js +8 -6
- package/dist/maas/index.js.map +1 -1
- package/docs/16-google-vertex.mdx +75 -75
- package/package.json +5 -5
- package/src/anthropic/edge/google-vertex-anthropic-provider-edge.ts +13 -7
- package/src/anthropic/edge/index.ts +6 -2
- package/src/anthropic/google-vertex-anthropic-provider-node.ts +13 -7
- package/src/anthropic/google-vertex-anthropic-provider.ts +5 -5
- package/src/anthropic/index.ts +6 -2
- package/src/edge/google-vertex-provider-edge.ts +6 -6
- package/src/edge/index.ts +8 -1
- package/src/google-vertex-embedding-model.ts +10 -2
- package/src/google-vertex-image-model-options.ts +74 -0
- package/src/google-vertex-image-model.ts +42 -102
- package/src/google-vertex-provider-base.ts +245 -0
- package/src/google-vertex-provider.ts +35 -233
- package/src/google-vertex-video-model-options.ts +49 -0
- package/src/google-vertex-video-model.ts +30 -66
- package/src/index.ts +12 -5
- package/src/maas/edge/google-vertex-maas-provider-edge.ts +3 -3
- package/src/maas/edge/index.ts +6 -2
- package/src/maas/google-vertex-maas-provider-node.ts +3 -3
- package/src/maas/google-vertex-maas-provider.ts +1 -1
- package/src/maas/index.ts +6 -2
- package/src/google-vertex-provider-node.ts +0 -47
- /package/src/{google-vertex-embedding-options.ts → google-vertex-embedding-model-options.ts} +0 -0
|
@@ -19,7 +19,7 @@ import type { GoogleVertexAnthropicModelId } from './google-vertex-anthropic-opt
|
|
|
19
19
|
* Tools supported by Google Vertex Anthropic.
|
|
20
20
|
* This is a subset of the full Anthropic tools - only these are recognized by the Vertex API.
|
|
21
21
|
*/
|
|
22
|
-
export const
|
|
22
|
+
export const googleVertexAnthropicTools = {
|
|
23
23
|
/**
|
|
24
24
|
* The bash tool enables Claude to execute shell commands in a persistent bash session,
|
|
25
25
|
* allowing system operations, script execution, and command-line automation.
|
|
@@ -120,7 +120,7 @@ export interface GoogleVertexAnthropicProvider extends ProviderV4 {
|
|
|
120
120
|
* computer_20241022, webSearch_20250305, toolSearchRegex_20251119,
|
|
121
121
|
* toolSearchBm25_20251119
|
|
122
122
|
*/
|
|
123
|
-
tools: typeof
|
|
123
|
+
tools: typeof googleVertexAnthropicTools;
|
|
124
124
|
|
|
125
125
|
/**
|
|
126
126
|
* @deprecated Use `embeddingModel` instead.
|
|
@@ -160,7 +160,7 @@ export interface GoogleVertexAnthropicProviderSettings {
|
|
|
160
160
|
/**
|
|
161
161
|
* Create a Google Vertex Anthropic provider instance.
|
|
162
162
|
*/
|
|
163
|
-
export function
|
|
163
|
+
export function createGoogleVertexAnthropic(
|
|
164
164
|
options: GoogleVertexAnthropicProviderSettings = {},
|
|
165
165
|
): GoogleVertexAnthropicProvider {
|
|
166
166
|
const getBaseURL = () => {
|
|
@@ -181,7 +181,7 @@ export function createVertexAnthropic(
|
|
|
181
181
|
|
|
182
182
|
const createChatModel = (modelId: GoogleVertexAnthropicModelId) =>
|
|
183
183
|
new AnthropicLanguageModel(modelId, {
|
|
184
|
-
provider: '
|
|
184
|
+
provider: 'googleVertex.anthropic.messages',
|
|
185
185
|
baseURL: getBaseURL(),
|
|
186
186
|
headers: options.headers ?? {},
|
|
187
187
|
fetch: options.fetch,
|
|
@@ -229,7 +229,7 @@ export function createVertexAnthropic(
|
|
|
229
229
|
throw new NoSuchModelError({ modelId, modelType: 'imageModel' });
|
|
230
230
|
};
|
|
231
231
|
|
|
232
|
-
provider.tools =
|
|
232
|
+
provider.tools = googleVertexAnthropicTools;
|
|
233
233
|
|
|
234
234
|
return provider;
|
|
235
235
|
}
|
package/src/anthropic/index.ts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
export {
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
googleVertexAnthropic,
|
|
3
|
+
/** @deprecated Use `googleVertexAnthropic` instead. */
|
|
4
|
+
googleVertexAnthropic as vertexAnthropic,
|
|
5
|
+
createGoogleVertexAnthropic,
|
|
6
|
+
/** @deprecated Use `createGoogleVertexAnthropic` instead. */
|
|
7
|
+
createGoogleVertexAnthropic as createVertexAnthropic,
|
|
4
8
|
} from './google-vertex-anthropic-provider-node';
|
|
5
9
|
export type {
|
|
6
10
|
GoogleVertexAnthropicProvider,
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { loadOptionalSetting, resolve } from '@ai-sdk/provider-utils';
|
|
2
2
|
import {
|
|
3
|
-
|
|
3
|
+
createGoogleVertex as createGoogleVertexOriginal,
|
|
4
4
|
type GoogleVertexProvider,
|
|
5
5
|
type GoogleVertexProviderSettings as GoogleVertexProviderSettingsOriginal,
|
|
6
|
-
} from '../google-vertex-provider';
|
|
6
|
+
} from '../google-vertex-provider-base';
|
|
7
7
|
import {
|
|
8
8
|
generateAuthToken,
|
|
9
9
|
type GoogleCredentials,
|
|
@@ -19,7 +19,7 @@ export interface GoogleVertexProviderSettings extends GoogleVertexProviderSettin
|
|
|
19
19
|
googleCredentials?: GoogleCredentials;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
export function
|
|
22
|
+
export function createGoogleVertex(
|
|
23
23
|
options: GoogleVertexProviderSettings = {},
|
|
24
24
|
): GoogleVertexProvider {
|
|
25
25
|
const apiKey = loadOptionalSetting({
|
|
@@ -28,10 +28,10 @@ export function createVertex(
|
|
|
28
28
|
});
|
|
29
29
|
|
|
30
30
|
if (apiKey) {
|
|
31
|
-
return
|
|
31
|
+
return createGoogleVertexOriginal(options);
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
-
return
|
|
34
|
+
return createGoogleVertexOriginal({
|
|
35
35
|
...options,
|
|
36
36
|
headers: async () => ({
|
|
37
37
|
Authorization: `Bearer ${await generateAuthToken(
|
|
@@ -45,4 +45,4 @@ export function createVertex(
|
|
|
45
45
|
/**
|
|
46
46
|
* Default Google Vertex AI provider instance.
|
|
47
47
|
*/
|
|
48
|
-
export const
|
|
48
|
+
export const googleVertex = createGoogleVertex();
|
package/src/edge/index.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export {
|
|
2
|
+
createGoogleVertex,
|
|
3
|
+
/** @deprecated Use `createGoogleVertex` instead. */
|
|
4
|
+
createGoogleVertex as createVertex,
|
|
5
|
+
googleVertex,
|
|
6
|
+
/** @deprecated Use `googleVertex` instead. */
|
|
7
|
+
googleVertex as vertex,
|
|
8
|
+
} from './google-vertex-provider-edge';
|
|
2
9
|
export type {
|
|
3
10
|
GoogleVertexProviderSettings,
|
|
4
11
|
GoogleVertexProvider,
|
|
@@ -17,7 +17,7 @@ import { googleVertexFailedResponseHandler } from './google-vertex-error';
|
|
|
17
17
|
import {
|
|
18
18
|
googleVertexEmbeddingModelOptions,
|
|
19
19
|
type GoogleVertexEmbeddingModelId,
|
|
20
|
-
} from './google-vertex-embedding-options';
|
|
20
|
+
} from './google-vertex-embedding-model-options';
|
|
21
21
|
import type { GoogleVertexConfig } from './google-vertex-config';
|
|
22
22
|
|
|
23
23
|
export class GoogleVertexEmbeddingModel implements EmbeddingModelV4 {
|
|
@@ -63,11 +63,19 @@ export class GoogleVertexEmbeddingModel implements EmbeddingModelV4 {
|
|
|
63
63
|
Awaited<ReturnType<EmbeddingModelV4['doEmbed']>>
|
|
64
64
|
> {
|
|
65
65
|
let googleOptions = await parseProviderOptions({
|
|
66
|
-
provider: '
|
|
66
|
+
provider: 'googleVertex',
|
|
67
67
|
providerOptions,
|
|
68
68
|
schema: googleVertexEmbeddingModelOptions,
|
|
69
69
|
});
|
|
70
70
|
|
|
71
|
+
if (googleOptions == null) {
|
|
72
|
+
googleOptions = await parseProviderOptions({
|
|
73
|
+
provider: 'vertex',
|
|
74
|
+
providerOptions,
|
|
75
|
+
schema: googleVertexEmbeddingModelOptions,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
71
79
|
if (googleOptions == null) {
|
|
72
80
|
googleOptions = await parseProviderOptions({
|
|
73
81
|
provider: 'google',
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { z } from 'zod/v4';
|
|
2
|
+
|
|
3
|
+
export const googleVertexImageModelOptionsSchema = z.object({
|
|
4
|
+
negativePrompt: z.string().nullish(),
|
|
5
|
+
personGeneration: z
|
|
6
|
+
.enum(['dont_allow', 'allow_adult', 'allow_all'])
|
|
7
|
+
.nullish(),
|
|
8
|
+
safetySetting: z
|
|
9
|
+
.enum([
|
|
10
|
+
'block_low_and_above',
|
|
11
|
+
'block_medium_and_above',
|
|
12
|
+
'block_only_high',
|
|
13
|
+
'block_none',
|
|
14
|
+
])
|
|
15
|
+
.nullish(),
|
|
16
|
+
addWatermark: z.boolean().nullish(),
|
|
17
|
+
storageUri: z.string().nullish(),
|
|
18
|
+
sampleImageSize: z.enum(['1K', '2K']).nullish(),
|
|
19
|
+
/**
|
|
20
|
+
* Configuration for image editing operations
|
|
21
|
+
*/
|
|
22
|
+
edit: z
|
|
23
|
+
.object({
|
|
24
|
+
/**
|
|
25
|
+
* An integer that represents the number of sampling steps.
|
|
26
|
+
* A higher value offers better image quality, a lower value offers better latency.
|
|
27
|
+
* Try 35 steps to start. If the quality doesn't meet your requirements,
|
|
28
|
+
* increase the value towards an upper limit of 75.
|
|
29
|
+
*/
|
|
30
|
+
baseSteps: z.number().nullish(),
|
|
31
|
+
|
|
32
|
+
// Edit mode options
|
|
33
|
+
// https://cloud.google.com/vertex-ai/generative-ai/docs/image/edit-insert-objects
|
|
34
|
+
mode: z
|
|
35
|
+
.enum([
|
|
36
|
+
'EDIT_MODE_INPAINT_INSERTION',
|
|
37
|
+
'EDIT_MODE_INPAINT_REMOVAL',
|
|
38
|
+
'EDIT_MODE_OUTPAINT',
|
|
39
|
+
'EDIT_MODE_CONTROLLED_EDITING',
|
|
40
|
+
'EDIT_MODE_PRODUCT_IMAGE',
|
|
41
|
+
'EDIT_MODE_BGSWAP',
|
|
42
|
+
])
|
|
43
|
+
.nullish(),
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The mask mode to use.
|
|
47
|
+
* - `MASK_MODE_DEFAULT` - Default value for mask mode.
|
|
48
|
+
* - `MASK_MODE_USER_PROVIDED` - User provided mask. No segmentation needed.
|
|
49
|
+
* - `MASK_MODE_DETECTION_BOX` - Mask from detected bounding boxes.
|
|
50
|
+
* - `MASK_MODE_CLOTHING_AREA` - Masks from segmenting the clothing area with open-vocab segmentation.
|
|
51
|
+
* - `MASK_MODE_PARSED_PERSON` - Masks from segmenting the person body and clothing using the person-parsing model.
|
|
52
|
+
*/
|
|
53
|
+
maskMode: z
|
|
54
|
+
.enum([
|
|
55
|
+
'MASK_MODE_DEFAULT',
|
|
56
|
+
'MASK_MODE_USER_PROVIDED',
|
|
57
|
+
'MASK_MODE_DETECTION_BOX',
|
|
58
|
+
'MASK_MODE_CLOTHING_AREA',
|
|
59
|
+
'MASK_MODE_PARSED_PERSON',
|
|
60
|
+
])
|
|
61
|
+
.nullish(),
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Optional. A float value between 0 and 1, inclusive, that represents the
|
|
65
|
+
* percentage of the image width to grow the mask by. Using dilation helps
|
|
66
|
+
* compensate for imprecise masks. We recommend a value of 0.01.
|
|
67
|
+
*/
|
|
68
|
+
maskDilation: z.number().nullish(),
|
|
69
|
+
})
|
|
70
|
+
.nullish(),
|
|
71
|
+
});
|
|
72
|
+
export type GoogleVertexImageModelOptions = z.infer<
|
|
73
|
+
typeof googleVertexImageModelOptionsSchema
|
|
74
|
+
>;
|
|
@@ -22,6 +22,7 @@ import {
|
|
|
22
22
|
} from '@ai-sdk/provider-utils';
|
|
23
23
|
import { z } from 'zod/v4';
|
|
24
24
|
import { googleVertexFailedResponseHandler } from './google-vertex-error';
|
|
25
|
+
import { googleVertexImageModelOptionsSchema } from './google-vertex-image-model-options';
|
|
25
26
|
import type { GoogleVertexImageModelId } from './google-vertex-image-settings';
|
|
26
27
|
|
|
27
28
|
interface GoogleVertexImageModelConfig {
|
|
@@ -103,14 +104,20 @@ export class GoogleVertexImageModel implements ImageModelV4 {
|
|
|
103
104
|
});
|
|
104
105
|
}
|
|
105
106
|
|
|
106
|
-
const
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
107
|
+
const googleVertexImageOptions =
|
|
108
|
+
(await parseProviderOptions({
|
|
109
|
+
provider: 'googleVertex',
|
|
110
|
+
providerOptions,
|
|
111
|
+
schema: googleVertexImageModelOptionsSchema,
|
|
112
|
+
})) ??
|
|
113
|
+
(await parseProviderOptions({
|
|
114
|
+
provider: 'vertex',
|
|
115
|
+
providerOptions,
|
|
116
|
+
schema: googleVertexImageModelOptionsSchema,
|
|
117
|
+
}));
|
|
111
118
|
|
|
112
119
|
// Extract edit-specific options from provider options
|
|
113
|
-
const { edit, ...otherOptions } =
|
|
120
|
+
const { edit, ...otherOptions } = googleVertexImageOptions ?? {};
|
|
114
121
|
const { mode: editMode, baseSteps, maskMode, maskDilation } = edit ?? {};
|
|
115
122
|
|
|
116
123
|
// Build the request body based on whether we're editing or generating
|
|
@@ -188,7 +195,7 @@ export class GoogleVertexImageModel implements ImageModelV4 {
|
|
|
188
195
|
body,
|
|
189
196
|
failedResponseHandler: googleVertexFailedResponseHandler,
|
|
190
197
|
successfulResponseHandler: createJsonResponseHandler(
|
|
191
|
-
|
|
198
|
+
googleVertexImageResponseSchema,
|
|
192
199
|
),
|
|
193
200
|
abortSignal,
|
|
194
201
|
fetch: this.config.fetch,
|
|
@@ -205,8 +212,8 @@ export class GoogleVertexImageModel implements ImageModelV4 {
|
|
|
205
212
|
modelId: this.modelId,
|
|
206
213
|
headers: responseHeaders,
|
|
207
214
|
},
|
|
208
|
-
providerMetadata: {
|
|
209
|
-
|
|
215
|
+
providerMetadata: (() => {
|
|
216
|
+
const payload = {
|
|
210
217
|
images:
|
|
211
218
|
response.predictions?.map(prediction => {
|
|
212
219
|
const {
|
|
@@ -216,8 +223,9 @@ export class GoogleVertexImageModel implements ImageModelV4 {
|
|
|
216
223
|
|
|
217
224
|
return { ...(revisedPrompt != null && { revisedPrompt }) };
|
|
218
225
|
}) ?? [],
|
|
219
|
-
}
|
|
220
|
-
|
|
226
|
+
};
|
|
227
|
+
return { googleVertex: payload, vertex: payload };
|
|
228
|
+
})(),
|
|
221
229
|
};
|
|
222
230
|
}
|
|
223
231
|
|
|
@@ -312,24 +320,27 @@ export class GoogleVertexImageModel implements ImageModelV4 {
|
|
|
312
320
|
}),
|
|
313
321
|
});
|
|
314
322
|
|
|
323
|
+
const userVertexOptions = (providerOptions?.googleVertex ??
|
|
324
|
+
providerOptions?.vertex) as
|
|
325
|
+
| Omit<GoogleLanguageModelOptions, 'responseModalities' | 'imageConfig'>
|
|
326
|
+
| undefined;
|
|
327
|
+
const innerVertexOptions: GoogleLanguageModelOptions = {
|
|
328
|
+
responseModalities: ['IMAGE'],
|
|
329
|
+
imageConfig: aspectRatio
|
|
330
|
+
? {
|
|
331
|
+
aspectRatio: aspectRatio as NonNullable<
|
|
332
|
+
GoogleLanguageModelOptions['imageConfig']
|
|
333
|
+
>['aspectRatio'],
|
|
334
|
+
}
|
|
335
|
+
: undefined,
|
|
336
|
+
...(userVertexOptions ?? {}),
|
|
337
|
+
};
|
|
315
338
|
const result = await languageModel.doGenerate({
|
|
316
339
|
prompt: languageModelPrompt,
|
|
317
340
|
seed,
|
|
318
341
|
providerOptions: {
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
imageConfig: aspectRatio
|
|
322
|
-
? {
|
|
323
|
-
aspectRatio: aspectRatio as NonNullable<
|
|
324
|
-
GoogleLanguageModelOptions['imageConfig']
|
|
325
|
-
>['aspectRatio'],
|
|
326
|
-
}
|
|
327
|
-
: undefined,
|
|
328
|
-
...((providerOptions?.vertex as Omit<
|
|
329
|
-
GoogleLanguageModelOptions,
|
|
330
|
-
'responseModalities' | 'imageConfig'
|
|
331
|
-
>) ?? {}),
|
|
332
|
-
} satisfies GoogleLanguageModelOptions,
|
|
342
|
+
googleVertex: innerVertexOptions,
|
|
343
|
+
vertex: innerVertexOptions,
|
|
333
344
|
},
|
|
334
345
|
headers,
|
|
335
346
|
abortSignal,
|
|
@@ -348,13 +359,15 @@ export class GoogleVertexImageModel implements ImageModelV4 {
|
|
|
348
359
|
}
|
|
349
360
|
}
|
|
350
361
|
|
|
362
|
+
const geminiPayload = {
|
|
363
|
+
images: images.map(() => ({})),
|
|
364
|
+
};
|
|
351
365
|
return {
|
|
352
366
|
images,
|
|
353
367
|
warnings,
|
|
354
368
|
providerMetadata: {
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
},
|
|
369
|
+
googleVertex: geminiPayload,
|
|
370
|
+
vertex: geminiPayload,
|
|
358
371
|
},
|
|
359
372
|
response: {
|
|
360
373
|
timestamp: currentDate,
|
|
@@ -380,7 +393,7 @@ function isGeminiModel(modelId: string): boolean {
|
|
|
380
393
|
|
|
381
394
|
// minimal version of the schema, focussed on what is needed for the implementation
|
|
382
395
|
// this approach limits breakages when the API changes and increases efficiency
|
|
383
|
-
const
|
|
396
|
+
const googleVertexImageResponseSchema = z.object({
|
|
384
397
|
predictions: z
|
|
385
398
|
.array(
|
|
386
399
|
z.object({
|
|
@@ -392,79 +405,6 @@ const vertexImageResponseSchema = z.object({
|
|
|
392
405
|
.nullish(),
|
|
393
406
|
});
|
|
394
407
|
|
|
395
|
-
const googleVertexImageModelOptionsSchema = z.object({
|
|
396
|
-
negativePrompt: z.string().nullish(),
|
|
397
|
-
personGeneration: z
|
|
398
|
-
.enum(['dont_allow', 'allow_adult', 'allow_all'])
|
|
399
|
-
.nullish(),
|
|
400
|
-
safetySetting: z
|
|
401
|
-
.enum([
|
|
402
|
-
'block_low_and_above',
|
|
403
|
-
'block_medium_and_above',
|
|
404
|
-
'block_only_high',
|
|
405
|
-
'block_none',
|
|
406
|
-
])
|
|
407
|
-
.nullish(),
|
|
408
|
-
addWatermark: z.boolean().nullish(),
|
|
409
|
-
storageUri: z.string().nullish(),
|
|
410
|
-
sampleImageSize: z.enum(['1K', '2K']).nullish(),
|
|
411
|
-
/**
|
|
412
|
-
* Configuration for image editing operations
|
|
413
|
-
*/
|
|
414
|
-
edit: z
|
|
415
|
-
.object({
|
|
416
|
-
/**
|
|
417
|
-
* An integer that represents the number of sampling steps.
|
|
418
|
-
* A higher value offers better image quality, a lower value offers better latency.
|
|
419
|
-
* Try 35 steps to start. If the quality doesn't meet your requirements,
|
|
420
|
-
* increase the value towards an upper limit of 75.
|
|
421
|
-
*/
|
|
422
|
-
baseSteps: z.number().nullish(),
|
|
423
|
-
|
|
424
|
-
// Edit mode options
|
|
425
|
-
// https://cloud.google.com/vertex-ai/generative-ai/docs/image/edit-insert-objects
|
|
426
|
-
mode: z
|
|
427
|
-
.enum([
|
|
428
|
-
'EDIT_MODE_INPAINT_INSERTION',
|
|
429
|
-
'EDIT_MODE_INPAINT_REMOVAL',
|
|
430
|
-
'EDIT_MODE_OUTPAINT',
|
|
431
|
-
'EDIT_MODE_CONTROLLED_EDITING',
|
|
432
|
-
'EDIT_MODE_PRODUCT_IMAGE',
|
|
433
|
-
'EDIT_MODE_BGSWAP',
|
|
434
|
-
])
|
|
435
|
-
.nullish(),
|
|
436
|
-
|
|
437
|
-
/**
|
|
438
|
-
* The mask mode to use.
|
|
439
|
-
* - `MASK_MODE_DEFAULT` - Default value for mask mode.
|
|
440
|
-
* - `MASK_MODE_USER_PROVIDED` - User provided mask. No segmentation needed.
|
|
441
|
-
* - `MASK_MODE_DETECTION_BOX` - Mask from detected bounding boxes.
|
|
442
|
-
* - `MASK_MODE_CLOTHING_AREA` - Masks from segmenting the clothing area with open-vocab segmentation.
|
|
443
|
-
* - `MASK_MODE_PARSED_PERSON` - Masks from segmenting the person body and clothing using the person-parsing model.
|
|
444
|
-
*/
|
|
445
|
-
maskMode: z
|
|
446
|
-
.enum([
|
|
447
|
-
'MASK_MODE_DEFAULT',
|
|
448
|
-
'MASK_MODE_USER_PROVIDED',
|
|
449
|
-
'MASK_MODE_DETECTION_BOX',
|
|
450
|
-
'MASK_MODE_CLOTHING_AREA',
|
|
451
|
-
'MASK_MODE_PARSED_PERSON',
|
|
452
|
-
])
|
|
453
|
-
.nullish(),
|
|
454
|
-
|
|
455
|
-
/**
|
|
456
|
-
* Optional. A float value between 0 and 1, inclusive, that represents the
|
|
457
|
-
* percentage of the image width to grow the mask by. Using dilation helps
|
|
458
|
-
* compensate for imprecise masks. We recommend a value of 0.01.
|
|
459
|
-
*/
|
|
460
|
-
maskDilation: z.number().nullish(),
|
|
461
|
-
})
|
|
462
|
-
.nullish(),
|
|
463
|
-
});
|
|
464
|
-
export type GoogleVertexImageModelOptions = z.infer<
|
|
465
|
-
typeof googleVertexImageModelOptionsSchema
|
|
466
|
-
>;
|
|
467
|
-
|
|
468
408
|
/**
|
|
469
409
|
* Helper to convert ImageModelV4File data to base64 string
|
|
470
410
|
*/
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import { GoogleLanguageModel } from '@ai-sdk/google/internal';
|
|
2
|
+
import type {
|
|
3
|
+
Experimental_VideoModelV4,
|
|
4
|
+
ImageModelV4,
|
|
5
|
+
LanguageModelV4,
|
|
6
|
+
ProviderV4,
|
|
7
|
+
} from '@ai-sdk/provider';
|
|
8
|
+
import {
|
|
9
|
+
generateId,
|
|
10
|
+
loadOptionalSetting,
|
|
11
|
+
loadSetting,
|
|
12
|
+
normalizeHeaders,
|
|
13
|
+
resolve,
|
|
14
|
+
withoutTrailingSlash,
|
|
15
|
+
withUserAgentSuffix,
|
|
16
|
+
type FetchFunction,
|
|
17
|
+
type Resolvable,
|
|
18
|
+
} from '@ai-sdk/provider-utils';
|
|
19
|
+
import { VERSION } from './version';
|
|
20
|
+
import type { GoogleVertexConfig } from './google-vertex-config';
|
|
21
|
+
import { GoogleVertexEmbeddingModel } from './google-vertex-embedding-model';
|
|
22
|
+
import type { GoogleVertexEmbeddingModelId } from './google-vertex-embedding-model-options';
|
|
23
|
+
import { GoogleVertexImageModel } from './google-vertex-image-model';
|
|
24
|
+
import type { GoogleVertexImageModelId } from './google-vertex-image-settings';
|
|
25
|
+
import type { GoogleVertexModelId } from './google-vertex-options';
|
|
26
|
+
import { googleVertexTools } from './google-vertex-tools';
|
|
27
|
+
import { GoogleVertexVideoModel } from './google-vertex-video-model';
|
|
28
|
+
import type { 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 ProviderV4 {
|
|
51
|
+
/**
|
|
52
|
+
* Creates a model for text generation.
|
|
53
|
+
*/
|
|
54
|
+
(modelId: GoogleVertexModelId): LanguageModelV4;
|
|
55
|
+
|
|
56
|
+
languageModel: (modelId: GoogleVertexModelId) => LanguageModelV4;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Creates a model for image generation.
|
|
60
|
+
*/
|
|
61
|
+
image(modelId: GoogleVertexImageModelId): ImageModelV4;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Creates a model for image generation.
|
|
65
|
+
*/
|
|
66
|
+
imageModel(modelId: GoogleVertexImageModelId): ImageModelV4;
|
|
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_VideoModelV4;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Creates a model for video generation.
|
|
84
|
+
*/
|
|
85
|
+
videoModel(modelId: GoogleVertexVideoModelId): Experimental_VideoModelV4;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export interface GoogleVertexProviderSettings {
|
|
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
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Create a Google Vertex AI provider instance.
|
|
132
|
+
*/
|
|
133
|
+
export function createGoogleVertex(
|
|
134
|
+
options: GoogleVertexProviderSettings = {},
|
|
135
|
+
): GoogleVertexProvider {
|
|
136
|
+
const apiKey = loadOptionalSetting({
|
|
137
|
+
settingValue: options.apiKey,
|
|
138
|
+
environmentVariableName: 'GOOGLE_VERTEX_API_KEY',
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
const loadGoogleVertexProject = () =>
|
|
142
|
+
loadSetting({
|
|
143
|
+
settingValue: options.project,
|
|
144
|
+
settingName: 'project',
|
|
145
|
+
environmentVariableName: 'GOOGLE_VERTEX_PROJECT',
|
|
146
|
+
description: 'Google Vertex project',
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
const loadGoogleVertexLocation = () =>
|
|
150
|
+
loadSetting({
|
|
151
|
+
settingValue: options.location,
|
|
152
|
+
settingName: 'location',
|
|
153
|
+
environmentVariableName: 'GOOGLE_VERTEX_LOCATION',
|
|
154
|
+
description: 'Google Vertex location',
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
const loadBaseURL = () => {
|
|
158
|
+
if (apiKey) {
|
|
159
|
+
return withoutTrailingSlash(options.baseURL) ?? EXPRESS_MODE_BASE_URL;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const region = loadGoogleVertexLocation();
|
|
163
|
+
const project = loadGoogleVertexProject();
|
|
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 GoogleLanguageModel(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 = 'v4' 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;
|
|
245
|
+
}
|