@ai-sdk/google-vertex 5.0.0-beta.61 → 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 +33 -0
- package/dist/anthropic/edge/index.d.ts +14 -9
- package/dist/anthropic/edge/index.js +15 -13
- package/dist/anthropic/edge/index.js.map +1 -1
- package/dist/anthropic/index.d.ts +14 -9
- 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 +197 -157
- package/dist/edge/index.js.map +1 -1
- package/dist/index.d.ts +7 -7
- package/dist/index.js +199 -159
- package/dist/index.js.map +1 -1
- package/dist/maas/edge/index.d.ts +3 -3
- package/dist/maas/edge/index.js +12 -8
- package/dist/maas/edge/index.js.map +1 -1
- package/dist/maas/index.d.ts +3 -3
- package/dist/maas/index.js +11 -7
- package/dist/maas/index.js.map +1 -1
- package/docs/16-google-vertex.mdx +77 -77
- package/package.json +7 -7
- package/src/anthropic/edge/google-vertex-anthropic-provider-edge.ts +16 -11
- package/src/anthropic/edge/index.ts +6 -2
- package/src/anthropic/google-vertex-anthropic-provider-node.ts +16 -11
- package/src/anthropic/google-vertex-anthropic-provider.ts +10 -10
- package/src/anthropic/index.ts +6 -2
- package/src/edge/google-vertex-provider-edge.ts +9 -10
- package/src/edge/index.ts +8 -1
- package/src/google-vertex-auth-google-auth-library.ts +1 -2
- package/src/google-vertex-config.ts +1 -1
- package/src/google-vertex-embedding-model.ts +13 -5
- package/src/google-vertex-image-model-options.ts +74 -0
- package/src/google-vertex-image-model.ts +66 -113
- 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 +32 -68
- package/src/index.ts +12 -5
- package/src/maas/edge/google-vertex-maas-provider-edge.ts +7 -8
- package/src/maas/edge/index.ts +6 -2
- package/src/maas/google-vertex-maas-provider-node.ts +7 -8
- package/src/maas/google-vertex-maas-provider.ts +7 -5
- package/src/maas/index.ts +6 -2
- package/src/google-vertex-provider-node.ts +0 -48
- /package/src/{google-vertex-embedding-options.ts → google-vertex-embedding-model-options.ts} +0 -0
|
@@ -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
|
+
}
|
|
@@ -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 { generateAuthToken } 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 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;
|
|
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,22 @@ 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
|
-
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 = 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 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;
|
|
29
|
+
if (apiKey) {
|
|
30
|
+
return createGoogleVertexOriginal(options);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return createGoogleVertexOriginal({
|
|
34
|
+
...options,
|
|
35
|
+
headers: async () => ({
|
|
36
|
+
Authorization: `Bearer ${await generateAuthToken(
|
|
37
|
+
options.googleAuthOptions,
|
|
38
|
+
)}`,
|
|
39
|
+
...(await resolve(options.headers)),
|
|
40
|
+
}),
|
|
41
|
+
});
|
|
245
42
|
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Default Google Vertex AI provider instance.
|
|
46
|
+
*/
|
|
47
|
+
export const googleVertex = createGoogleVertex();
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { lazySchema, zodSchema } from '@ai-sdk/provider-utils';
|
|
2
|
+
import { z } from 'zod/v4';
|
|
3
|
+
|
|
4
|
+
export type GoogleVertexVideoModelOptions = {
|
|
5
|
+
// Polling configuration
|
|
6
|
+
pollIntervalMs?: number | null;
|
|
7
|
+
pollTimeoutMs?: number | null;
|
|
8
|
+
|
|
9
|
+
// Video generation options
|
|
10
|
+
personGeneration?: 'dont_allow' | 'allow_adult' | 'allow_all' | null;
|
|
11
|
+
negativePrompt?: string | null;
|
|
12
|
+
generateAudio?: boolean | null;
|
|
13
|
+
|
|
14
|
+
// Output configuration
|
|
15
|
+
gcsOutputDirectory?: string | null;
|
|
16
|
+
|
|
17
|
+
// Reference images (for style/asset reference)
|
|
18
|
+
referenceImages?: Array<{
|
|
19
|
+
bytesBase64Encoded?: string;
|
|
20
|
+
gcsUri?: string;
|
|
21
|
+
}> | null;
|
|
22
|
+
|
|
23
|
+
[key: string]: unknown; // For passthrough
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export const googleVertexVideoModelOptionsSchema = lazySchema(() =>
|
|
27
|
+
zodSchema(
|
|
28
|
+
z
|
|
29
|
+
.object({
|
|
30
|
+
pollIntervalMs: z.number().positive().nullish(),
|
|
31
|
+
pollTimeoutMs: z.number().positive().nullish(),
|
|
32
|
+
personGeneration: z
|
|
33
|
+
.enum(['dont_allow', 'allow_adult', 'allow_all'])
|
|
34
|
+
.nullish(),
|
|
35
|
+
negativePrompt: z.string().nullish(),
|
|
36
|
+
generateAudio: z.boolean().nullish(),
|
|
37
|
+
gcsOutputDirectory: z.string().nullish(),
|
|
38
|
+
referenceImages: z
|
|
39
|
+
.array(
|
|
40
|
+
z.object({
|
|
41
|
+
bytesBase64Encoded: z.string().nullish(),
|
|
42
|
+
gcsUri: z.string().nullish(),
|
|
43
|
+
}),
|
|
44
|
+
)
|
|
45
|
+
.nullish(),
|
|
46
|
+
})
|
|
47
|
+
.passthrough(),
|
|
48
|
+
),
|
|
49
|
+
);
|