@ai-sdk/google-vertex 5.0.4 → 5.0.6

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.
@@ -38,6 +38,15 @@ import type { GoogleVertexSpeechModelId } from './google-vertex-speech-model-opt
38
38
  const EXPRESS_MODE_BASE_URL =
39
39
  'https://aiplatform.googleapis.com/v1/publishers/google';
40
40
 
41
+ // Tuned models are served from a deployed endpoint and addressed by their
42
+ // `endpoints/{id}` resource, which replaces `publishers/google/models/{id}`.
43
+ // https://cloud.google.com/vertex-ai/generative-ai/docs/deploy/overview
44
+ const ENDPOINT_MODEL_PREFIX = 'endpoints/';
45
+
46
+ function isEndpointModelId(modelId: string): boolean {
47
+ return modelId.startsWith(ENDPOINT_MODEL_PREFIX);
48
+ }
49
+
41
50
  // set `x-goog-api-key` header to API key for express mode
42
51
  function createExpressModeFetch(
43
52
  apiKey: string,
@@ -186,7 +195,11 @@ export function createGoogleVertex(
186
195
  description: 'Google Vertex location',
187
196
  });
188
197
 
189
- const loadBaseURL = () => {
198
+ // Tuned models are addressed via their deployed endpoint
199
+ // `.../locations/{region}/endpoints/{id}` instead of the base-model
200
+ // `.../publishers/google/models/{id}` path, so they omit the
201
+ // `/publishers/google` suffix from the base URL.
202
+ const loadBaseURL = ({ endpoint = false }: { endpoint?: boolean } = {}) => {
190
203
  if (apiKey) {
191
204
  return withoutTrailingSlash(options.baseURL) ?? EXPRESS_MODE_BASE_URL;
192
205
  }
@@ -206,11 +219,16 @@ export function createGoogleVertex(
206
219
 
207
220
  return (
208
221
  withoutTrailingSlash(options.baseURL) ??
209
- `https://${getHost()}/v1beta1/projects/${project}/locations/${region}/publishers/google`
222
+ `https://${getHost()}/v1beta1/projects/${project}/locations/${region}${
223
+ endpoint ? '' : '/publishers/google'
224
+ }`
210
225
  );
211
226
  };
212
227
 
213
- const createConfig = (name: string): GoogleVertexConfig => {
228
+ const createConfig = (
229
+ name: string,
230
+ { endpoint = false }: { endpoint?: boolean } = {},
231
+ ): GoogleVertexConfig => {
214
232
  const getHeaders = async () => {
215
233
  const originalHeaders = await resolve(options.headers ?? {});
216
234
  return withUserAgentSuffix(
@@ -225,13 +243,21 @@ export function createGoogleVertex(
225
243
  fetch: apiKey
226
244
  ? createExpressModeFetch(apiKey, options.fetch)
227
245
  : options.fetch,
228
- baseURL: loadBaseURL(),
246
+ baseURL: loadBaseURL({ endpoint }),
229
247
  };
230
248
  };
231
249
 
232
250
  const createChatModel = (modelId: GoogleVertexModelId) => {
251
+ const endpoint = isEndpointModelId(modelId);
252
+
253
+ if (endpoint && apiKey) {
254
+ throw new Error(
255
+ 'Google Vertex tuned models do not support Express Mode API keys. Use standard Google Cloud credentials instead.',
256
+ );
257
+ }
258
+
233
259
  return new GoogleLanguageModel(modelId, {
234
- ...createConfig('chat'),
260
+ ...createConfig('chat', { endpoint }),
235
261
  generateId: options.generateId ?? generateId,
236
262
  supportedUrls: () => ({
237
263
  '*': [