@ai-sdk/google-vertex 1.0.3 → 2.0.0
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 +20 -0
- package/README.md +74 -8
- package/dist/index.d.mts +25 -35
- package/dist/index.d.ts +25 -35
- package/dist/index.js +69 -638
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +65 -640
- package/dist/index.mjs.map +1 -1
- package/edge/dist/index.d.mts +73 -0
- package/edge/dist/index.d.ts +73 -0
- package/edge/dist/index.js +298 -0
- package/edge/dist/index.js.map +1 -0
- package/edge/dist/index.mjs +280 -0
- package/edge/dist/index.mjs.map +1 -0
- package/package.json +12 -6
package/dist/index.mjs
CHANGED
@@ -1,618 +1,29 @@
|
|
1
|
-
// src/google-vertex-
|
2
|
-
import {
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
} from "@google-cloud/vertexai";
|
13
|
-
|
14
|
-
// src/convert-json-schema-to-openapi-schema.ts
|
15
|
-
function convertJSONSchemaToOpenAPISchema(jsonSchema) {
|
16
|
-
if (typeof jsonSchema === "boolean") {
|
17
|
-
return { type: "boolean", properties: {} };
|
18
|
-
}
|
19
|
-
const {
|
20
|
-
type,
|
21
|
-
description,
|
22
|
-
required,
|
23
|
-
properties,
|
24
|
-
items,
|
25
|
-
allOf,
|
26
|
-
anyOf,
|
27
|
-
oneOf,
|
28
|
-
format,
|
29
|
-
const: constValue,
|
30
|
-
minLength
|
31
|
-
} = jsonSchema;
|
32
|
-
const result = {};
|
33
|
-
if (description)
|
34
|
-
result.description = description;
|
35
|
-
if (required)
|
36
|
-
result.required = required;
|
37
|
-
if (format)
|
38
|
-
result.format = format;
|
39
|
-
if (constValue !== void 0) {
|
40
|
-
result.enum = [constValue];
|
41
|
-
}
|
42
|
-
if (type) {
|
43
|
-
if (Array.isArray(type)) {
|
44
|
-
if (type.includes("null")) {
|
45
|
-
result.type = type.filter((t) => t !== "null")[0];
|
46
|
-
result.nullable = true;
|
47
|
-
} else {
|
48
|
-
result.type = type;
|
49
|
-
}
|
50
|
-
} else if (type === "null") {
|
51
|
-
result.type = "null";
|
52
|
-
} else {
|
53
|
-
result.type = type;
|
54
|
-
}
|
55
|
-
}
|
56
|
-
if (properties) {
|
57
|
-
result.properties = Object.entries(properties).reduce(
|
58
|
-
(acc, [key, value]) => {
|
59
|
-
acc[key] = convertJSONSchemaToOpenAPISchema(value);
|
60
|
-
return acc;
|
61
|
-
},
|
62
|
-
{}
|
63
|
-
);
|
64
|
-
}
|
65
|
-
if (items) {
|
66
|
-
result.items = Array.isArray(items) ? items.map(convertJSONSchemaToOpenAPISchema) : convertJSONSchemaToOpenAPISchema(items);
|
67
|
-
}
|
68
|
-
if (allOf) {
|
69
|
-
result.allOf = allOf.map(convertJSONSchemaToOpenAPISchema);
|
70
|
-
}
|
71
|
-
if (anyOf) {
|
72
|
-
result.anyOf = anyOf.map(convertJSONSchemaToOpenAPISchema);
|
73
|
-
}
|
74
|
-
if (oneOf) {
|
75
|
-
result.oneOf = oneOf.map(convertJSONSchemaToOpenAPISchema);
|
1
|
+
// src/google-vertex-auth-google-auth-library.ts
|
2
|
+
import { GoogleAuth } from "google-auth-library";
|
3
|
+
var authInstance = null;
|
4
|
+
var authOptions = null;
|
5
|
+
function getAuth(options) {
|
6
|
+
if (!authInstance || options !== authOptions) {
|
7
|
+
authInstance = new GoogleAuth({
|
8
|
+
scopes: ["https://www.googleapis.com/auth/cloud-platform"],
|
9
|
+
...options
|
10
|
+
});
|
11
|
+
authOptions = options;
|
76
12
|
}
|
77
|
-
|
78
|
-
result.minLength = minLength;
|
79
|
-
return result;
|
13
|
+
return authInstance;
|
80
14
|
}
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
import { convertUint8ArrayToBase64 } from "@ai-sdk/provider-utils";
|
87
|
-
function convertToGoogleVertexContentRequest(prompt) {
|
88
|
-
var _a, _b;
|
89
|
-
const systemInstructionParts = [];
|
90
|
-
const contents = [];
|
91
|
-
let systemMessagesAllowed = true;
|
92
|
-
for (const { role, content } of prompt) {
|
93
|
-
switch (role) {
|
94
|
-
case "system": {
|
95
|
-
if (!systemMessagesAllowed) {
|
96
|
-
throw new UnsupportedFunctionalityError({
|
97
|
-
functionality: "system messages after first user message"
|
98
|
-
});
|
99
|
-
}
|
100
|
-
systemInstructionParts.push({ text: content });
|
101
|
-
break;
|
102
|
-
}
|
103
|
-
case "user": {
|
104
|
-
systemMessagesAllowed = false;
|
105
|
-
const parts = [];
|
106
|
-
for (const part of content) {
|
107
|
-
switch (part.type) {
|
108
|
-
case "text": {
|
109
|
-
parts.push({ text: part.text });
|
110
|
-
break;
|
111
|
-
}
|
112
|
-
case "image": {
|
113
|
-
parts.push(
|
114
|
-
part.image instanceof URL ? {
|
115
|
-
fileData: {
|
116
|
-
mimeType: (_a = part.mimeType) != null ? _a : "image/jpeg",
|
117
|
-
fileUri: part.image.toString()
|
118
|
-
}
|
119
|
-
} : {
|
120
|
-
inlineData: {
|
121
|
-
mimeType: (_b = part.mimeType) != null ? _b : "image/jpeg",
|
122
|
-
data: convertUint8ArrayToBase64(part.image)
|
123
|
-
}
|
124
|
-
}
|
125
|
-
);
|
126
|
-
break;
|
127
|
-
}
|
128
|
-
case "file": {
|
129
|
-
parts.push(
|
130
|
-
part.data instanceof URL ? {
|
131
|
-
fileData: {
|
132
|
-
mimeType: part.mimeType,
|
133
|
-
fileUri: part.data.toString()
|
134
|
-
}
|
135
|
-
} : {
|
136
|
-
inlineData: {
|
137
|
-
mimeType: part.mimeType,
|
138
|
-
data: part.data
|
139
|
-
}
|
140
|
-
}
|
141
|
-
);
|
142
|
-
break;
|
143
|
-
}
|
144
|
-
default: {
|
145
|
-
const _exhaustiveCheck = part;
|
146
|
-
throw new UnsupportedFunctionalityError({
|
147
|
-
functionality: `prompt part: ${_exhaustiveCheck}`
|
148
|
-
});
|
149
|
-
}
|
150
|
-
}
|
151
|
-
}
|
152
|
-
contents.push({ role: "user", parts });
|
153
|
-
break;
|
154
|
-
}
|
155
|
-
case "assistant": {
|
156
|
-
systemMessagesAllowed = false;
|
157
|
-
contents.push({
|
158
|
-
role: "assistant",
|
159
|
-
parts: content.filter((part) => part.type !== "text" || part.text.length > 0).map((part) => {
|
160
|
-
switch (part.type) {
|
161
|
-
case "text": {
|
162
|
-
return { text: part.text };
|
163
|
-
}
|
164
|
-
case "tool-call": {
|
165
|
-
return {
|
166
|
-
functionCall: {
|
167
|
-
name: part.toolName,
|
168
|
-
args: part.args
|
169
|
-
}
|
170
|
-
};
|
171
|
-
}
|
172
|
-
default: {
|
173
|
-
const _exhaustiveCheck = part;
|
174
|
-
throw new UnsupportedFunctionalityError({
|
175
|
-
functionality: `prompt part: ${_exhaustiveCheck}`
|
176
|
-
});
|
177
|
-
}
|
178
|
-
}
|
179
|
-
})
|
180
|
-
});
|
181
|
-
break;
|
182
|
-
}
|
183
|
-
case "tool": {
|
184
|
-
systemMessagesAllowed = false;
|
185
|
-
contents.push({
|
186
|
-
role: "user",
|
187
|
-
parts: content.map((part) => ({
|
188
|
-
functionResponse: {
|
189
|
-
name: part.toolName,
|
190
|
-
response: part.result
|
191
|
-
}
|
192
|
-
}))
|
193
|
-
});
|
194
|
-
break;
|
195
|
-
}
|
196
|
-
default: {
|
197
|
-
const _exhaustiveCheck = role;
|
198
|
-
throw new UnsupportedFunctionalityError({
|
199
|
-
functionality: `role: ${_exhaustiveCheck}`
|
200
|
-
});
|
201
|
-
}
|
202
|
-
}
|
203
|
-
}
|
204
|
-
return {
|
205
|
-
systemInstruction: systemInstructionParts.length > 0 ? { role: "system", parts: systemInstructionParts } : void 0,
|
206
|
-
contents
|
207
|
-
};
|
15
|
+
async function generateAuthToken(options) {
|
16
|
+
const auth = getAuth(options || {});
|
17
|
+
const client = await auth.getClient();
|
18
|
+
const token = await client.getAccessToken();
|
19
|
+
return (token == null ? void 0 : token.token) || null;
|
208
20
|
}
|
209
21
|
|
210
|
-
// src/google-vertex-
|
211
|
-
import {
|
212
|
-
UnsupportedFunctionalityError as UnsupportedFunctionalityError2
|
213
|
-
} from "@ai-sdk/provider";
|
22
|
+
// src/google-vertex-provider.ts
|
214
23
|
import {
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
useSearchGrounding,
|
219
|
-
mode
|
220
|
-
}) {
|
221
|
-
var _a, _b;
|
222
|
-
const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
|
223
|
-
const toolWarnings = [];
|
224
|
-
const vertexTools = [];
|
225
|
-
if (tools != null) {
|
226
|
-
const functionDeclarations = [];
|
227
|
-
for (const tool of tools) {
|
228
|
-
if (tool.type === "provider-defined") {
|
229
|
-
toolWarnings.push({ type: "unsupported-tool", tool });
|
230
|
-
} else {
|
231
|
-
functionDeclarations.push({
|
232
|
-
name: tool.name,
|
233
|
-
description: (_b = tool.description) != null ? _b : "",
|
234
|
-
parameters: convertJSONSchemaToOpenAPISchema(
|
235
|
-
tool.parameters
|
236
|
-
)
|
237
|
-
});
|
238
|
-
}
|
239
|
-
}
|
240
|
-
vertexTools.push({ functionDeclarations });
|
241
|
-
}
|
242
|
-
if (useSearchGrounding) {
|
243
|
-
vertexTools.push({ googleSearchRetrieval: {} });
|
244
|
-
}
|
245
|
-
const finalTools = vertexTools.length > 0 ? vertexTools : void 0;
|
246
|
-
const toolChoice = mode.toolChoice;
|
247
|
-
if (toolChoice == null) {
|
248
|
-
return {
|
249
|
-
tools: finalTools,
|
250
|
-
toolConfig: void 0,
|
251
|
-
toolWarnings
|
252
|
-
};
|
253
|
-
}
|
254
|
-
const type = toolChoice.type;
|
255
|
-
switch (type) {
|
256
|
-
case "auto":
|
257
|
-
return {
|
258
|
-
tools: finalTools,
|
259
|
-
toolConfig: {
|
260
|
-
functionCallingConfig: { mode: FunctionCallingMode.AUTO }
|
261
|
-
},
|
262
|
-
toolWarnings
|
263
|
-
};
|
264
|
-
case "none":
|
265
|
-
return {
|
266
|
-
tools: finalTools,
|
267
|
-
toolConfig: {
|
268
|
-
functionCallingConfig: { mode: FunctionCallingMode.NONE }
|
269
|
-
},
|
270
|
-
toolWarnings
|
271
|
-
};
|
272
|
-
case "required":
|
273
|
-
return {
|
274
|
-
tools: finalTools,
|
275
|
-
toolConfig: {
|
276
|
-
functionCallingConfig: { mode: FunctionCallingMode.ANY }
|
277
|
-
},
|
278
|
-
toolWarnings
|
279
|
-
};
|
280
|
-
case "tool":
|
281
|
-
return {
|
282
|
-
tools: finalTools,
|
283
|
-
toolConfig: {
|
284
|
-
functionCallingConfig: {
|
285
|
-
mode: FunctionCallingMode.ANY,
|
286
|
-
allowedFunctionNames: [toolChoice.toolName]
|
287
|
-
}
|
288
|
-
},
|
289
|
-
toolWarnings
|
290
|
-
};
|
291
|
-
default: {
|
292
|
-
const _exhaustiveCheck = type;
|
293
|
-
throw new UnsupportedFunctionalityError2({
|
294
|
-
functionality: `Unsupported tool choice type: ${_exhaustiveCheck}`
|
295
|
-
});
|
296
|
-
}
|
297
|
-
}
|
298
|
-
}
|
299
|
-
|
300
|
-
// src/map-google-vertex-finish-reason.ts
|
301
|
-
function mapGoogleVertexFinishReason({
|
302
|
-
finishReason,
|
303
|
-
hasToolCalls
|
304
|
-
}) {
|
305
|
-
switch (finishReason) {
|
306
|
-
case "STOP":
|
307
|
-
return hasToolCalls ? "tool-calls" : "stop";
|
308
|
-
case "MAX_TOKENS":
|
309
|
-
return "length";
|
310
|
-
case "BLOCKLIST":
|
311
|
-
case "PROHIBITED_CONTENT":
|
312
|
-
case "SPII":
|
313
|
-
case "RECITATION":
|
314
|
-
case "SAFETY":
|
315
|
-
return "content-filter";
|
316
|
-
case "FINISH_REASON_UNSPECIFIED":
|
317
|
-
case "OTHER":
|
318
|
-
return "other";
|
319
|
-
default:
|
320
|
-
return "unknown";
|
321
|
-
}
|
322
|
-
}
|
323
|
-
|
324
|
-
// src/google-vertex-language-model.ts
|
325
|
-
var GoogleVertexLanguageModel = class {
|
326
|
-
constructor(modelId, settings, config) {
|
327
|
-
this.specificationVersion = "v1";
|
328
|
-
this.provider = "google-vertex";
|
329
|
-
this.defaultObjectGenerationMode = "json";
|
330
|
-
this.supportsImageUrls = false;
|
331
|
-
this.modelId = modelId;
|
332
|
-
this.settings = settings;
|
333
|
-
this.config = config;
|
334
|
-
}
|
335
|
-
get supportsObjectGeneration() {
|
336
|
-
return this.settings.structuredOutputs !== false;
|
337
|
-
}
|
338
|
-
async getArgs({
|
339
|
-
mode,
|
340
|
-
prompt,
|
341
|
-
maxTokens,
|
342
|
-
temperature,
|
343
|
-
topP,
|
344
|
-
topK,
|
345
|
-
frequencyPenalty,
|
346
|
-
presencePenalty,
|
347
|
-
stopSequences,
|
348
|
-
responseFormat,
|
349
|
-
seed,
|
350
|
-
headers
|
351
|
-
}) {
|
352
|
-
var _a, _b;
|
353
|
-
const warnings = [];
|
354
|
-
if (presencePenalty != null) {
|
355
|
-
warnings.push({
|
356
|
-
type: "unsupported-setting",
|
357
|
-
setting: "presencePenalty"
|
358
|
-
});
|
359
|
-
}
|
360
|
-
if (seed != null) {
|
361
|
-
warnings.push({
|
362
|
-
type: "unsupported-setting",
|
363
|
-
setting: "seed"
|
364
|
-
});
|
365
|
-
}
|
366
|
-
if (headers != null) {
|
367
|
-
warnings.push({
|
368
|
-
type: "unsupported-setting",
|
369
|
-
setting: "headers"
|
370
|
-
});
|
371
|
-
}
|
372
|
-
const generationConfig = {
|
373
|
-
// standardized settings:
|
374
|
-
maxOutputTokens: maxTokens,
|
375
|
-
frequencyPenalty,
|
376
|
-
temperature,
|
377
|
-
topK,
|
378
|
-
topP,
|
379
|
-
stopSequences,
|
380
|
-
// response format:
|
381
|
-
responseMimeType: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? "application/json" : void 0,
|
382
|
-
responseSchema: (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && // Google Vertex does not support all OpenAPI Schema features,
|
383
|
-
// so this is needed as an escape hatch:
|
384
|
-
this.supportsObjectGeneration ? convertJSONSchemaToOpenAPISchema(
|
385
|
-
responseFormat.schema
|
386
|
-
) : void 0
|
387
|
-
};
|
388
|
-
const type = mode.type;
|
389
|
-
switch (type) {
|
390
|
-
case "regular": {
|
391
|
-
const { tools, toolConfig, toolWarnings } = prepareTools({
|
392
|
-
mode,
|
393
|
-
useSearchGrounding: (_a = this.settings.useSearchGrounding) != null ? _a : false
|
394
|
-
});
|
395
|
-
const configuration = {
|
396
|
-
model: this.modelId,
|
397
|
-
generationConfig,
|
398
|
-
tools,
|
399
|
-
toolConfig,
|
400
|
-
safetySettings: this.settings.safetySettings
|
401
|
-
};
|
402
|
-
return {
|
403
|
-
model: this.config.vertexAI.getGenerativeModel(configuration),
|
404
|
-
contentRequest: convertToGoogleVertexContentRequest(prompt),
|
405
|
-
warnings: [...warnings, ...toolWarnings]
|
406
|
-
};
|
407
|
-
}
|
408
|
-
case "object-json": {
|
409
|
-
return {
|
410
|
-
model: this.config.vertexAI.getGenerativeModel({
|
411
|
-
model: this.modelId,
|
412
|
-
generationConfig: {
|
413
|
-
...generationConfig,
|
414
|
-
responseMimeType: "application/json",
|
415
|
-
responseSchema: mode.schema != null && // Google Vertex does not support all OpenAPI Schema features,
|
416
|
-
// so this is needed as an escape hatch:
|
417
|
-
this.supportsObjectGeneration ? convertJSONSchemaToOpenAPISchema(
|
418
|
-
mode.schema
|
419
|
-
) : void 0
|
420
|
-
},
|
421
|
-
safetySettings: this.settings.safetySettings
|
422
|
-
}),
|
423
|
-
contentRequest: convertToGoogleVertexContentRequest(prompt),
|
424
|
-
warnings
|
425
|
-
};
|
426
|
-
}
|
427
|
-
case "object-tool": {
|
428
|
-
const configuration = {
|
429
|
-
model: this.modelId,
|
430
|
-
generationConfig,
|
431
|
-
tools: [
|
432
|
-
{
|
433
|
-
functionDeclarations: [
|
434
|
-
{
|
435
|
-
name: mode.tool.name,
|
436
|
-
description: (_b = mode.tool.description) != null ? _b : "",
|
437
|
-
parameters: convertJSONSchemaToOpenAPISchema(
|
438
|
-
mode.tool.parameters
|
439
|
-
)
|
440
|
-
}
|
441
|
-
]
|
442
|
-
}
|
443
|
-
],
|
444
|
-
toolConfig: {
|
445
|
-
functionCallingConfig: { mode: FunctionCallingMode2.ANY }
|
446
|
-
},
|
447
|
-
safetySettings: this.settings.safetySettings
|
448
|
-
};
|
449
|
-
return {
|
450
|
-
model: this.config.vertexAI.getGenerativeModel(configuration),
|
451
|
-
contentRequest: convertToGoogleVertexContentRequest(prompt),
|
452
|
-
warnings
|
453
|
-
};
|
454
|
-
}
|
455
|
-
default: {
|
456
|
-
const _exhaustiveCheck = type;
|
457
|
-
throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
|
458
|
-
}
|
459
|
-
}
|
460
|
-
}
|
461
|
-
supportsUrl(url) {
|
462
|
-
return url.protocol === "gs:";
|
463
|
-
}
|
464
|
-
async doGenerate(options) {
|
465
|
-
var _a, _b, _c;
|
466
|
-
const { model, contentRequest, warnings } = await this.getArgs(options);
|
467
|
-
const { response } = await model.generateContent(contentRequest);
|
468
|
-
const firstCandidate = (_a = response.candidates) == null ? void 0 : _a[0];
|
469
|
-
if (firstCandidate == null) {
|
470
|
-
throw new NoContentGeneratedError({ message: "No candidates returned" });
|
471
|
-
}
|
472
|
-
const parts = firstCandidate.content.parts;
|
473
|
-
const usageMetadata = response.usageMetadata;
|
474
|
-
const toolCalls = getToolCallsFromParts({
|
475
|
-
parts,
|
476
|
-
generateId: this.config.generateId
|
477
|
-
});
|
478
|
-
return {
|
479
|
-
text: getTextFromParts(parts),
|
480
|
-
toolCalls,
|
481
|
-
finishReason: mapGoogleVertexFinishReason({
|
482
|
-
finishReason: firstCandidate.finishReason,
|
483
|
-
hasToolCalls: toolCalls != null && toolCalls.length > 0
|
484
|
-
}),
|
485
|
-
usage: {
|
486
|
-
promptTokens: (_b = usageMetadata == null ? void 0 : usageMetadata.promptTokenCount) != null ? _b : NaN,
|
487
|
-
completionTokens: (_c = usageMetadata == null ? void 0 : usageMetadata.candidatesTokenCount) != null ? _c : NaN
|
488
|
-
},
|
489
|
-
rawCall: {
|
490
|
-
rawPrompt: contentRequest,
|
491
|
-
rawSettings: {}
|
492
|
-
},
|
493
|
-
providerMetadata: this.settings.useSearchGrounding ? {
|
494
|
-
vertex: {
|
495
|
-
groundingMetadata: firstCandidate.groundingMetadata
|
496
|
-
}
|
497
|
-
} : void 0,
|
498
|
-
warnings
|
499
|
-
};
|
500
|
-
}
|
501
|
-
async doStream(options) {
|
502
|
-
const { model, contentRequest, warnings } = await this.getArgs(options);
|
503
|
-
const { stream } = await model.generateContentStream(contentRequest);
|
504
|
-
let finishReason = "unknown";
|
505
|
-
let usage = {
|
506
|
-
promptTokens: Number.NaN,
|
507
|
-
completionTokens: Number.NaN
|
508
|
-
};
|
509
|
-
const generateId2 = this.config.generateId;
|
510
|
-
let hasToolCalls = false;
|
511
|
-
let providerMetadata;
|
512
|
-
return {
|
513
|
-
stream: convertAsyncIteratorToReadableStream(stream).pipeThrough(
|
514
|
-
new TransformStream(
|
515
|
-
{
|
516
|
-
transform(chunk, controller) {
|
517
|
-
var _a, _b, _c;
|
518
|
-
const usageMetadata = chunk.usageMetadata;
|
519
|
-
if (usageMetadata != null) {
|
520
|
-
usage = {
|
521
|
-
promptTokens: (_a = usageMetadata.promptTokenCount) != null ? _a : NaN,
|
522
|
-
completionTokens: (_b = usageMetadata.candidatesTokenCount) != null ? _b : NaN
|
523
|
-
};
|
524
|
-
}
|
525
|
-
const candidate = (_c = chunk.candidates) == null ? void 0 : _c[0];
|
526
|
-
if (candidate == null) {
|
527
|
-
return;
|
528
|
-
}
|
529
|
-
if (candidate.finishReason != null) {
|
530
|
-
finishReason = mapGoogleVertexFinishReason({
|
531
|
-
finishReason: candidate.finishReason,
|
532
|
-
hasToolCalls
|
533
|
-
});
|
534
|
-
}
|
535
|
-
if (candidate.groundingMetadata != null) {
|
536
|
-
providerMetadata = {
|
537
|
-
vertex: {
|
538
|
-
groundingMetadata: candidate.groundingMetadata
|
539
|
-
}
|
540
|
-
};
|
541
|
-
}
|
542
|
-
const content = candidate.content;
|
543
|
-
const deltaText = getTextFromParts(content.parts);
|
544
|
-
if (deltaText != null) {
|
545
|
-
controller.enqueue({
|
546
|
-
type: "text-delta",
|
547
|
-
textDelta: deltaText
|
548
|
-
});
|
549
|
-
}
|
550
|
-
const toolCallDeltas = getToolCallsFromParts({
|
551
|
-
parts: content.parts,
|
552
|
-
generateId: generateId2
|
553
|
-
});
|
554
|
-
if (toolCallDeltas != null) {
|
555
|
-
for (const toolCall of toolCallDeltas) {
|
556
|
-
controller.enqueue({
|
557
|
-
type: "tool-call-delta",
|
558
|
-
toolCallType: "function",
|
559
|
-
toolCallId: toolCall.toolCallId,
|
560
|
-
toolName: toolCall.toolName,
|
561
|
-
argsTextDelta: toolCall.args
|
562
|
-
});
|
563
|
-
controller.enqueue({
|
564
|
-
type: "tool-call",
|
565
|
-
toolCallType: "function",
|
566
|
-
toolCallId: toolCall.toolCallId,
|
567
|
-
toolName: toolCall.toolName,
|
568
|
-
args: toolCall.args
|
569
|
-
});
|
570
|
-
hasToolCalls = true;
|
571
|
-
}
|
572
|
-
}
|
573
|
-
},
|
574
|
-
flush(controller) {
|
575
|
-
controller.enqueue({
|
576
|
-
type: "finish",
|
577
|
-
finishReason,
|
578
|
-
usage,
|
579
|
-
providerMetadata
|
580
|
-
});
|
581
|
-
}
|
582
|
-
}
|
583
|
-
)
|
584
|
-
),
|
585
|
-
rawCall: {
|
586
|
-
rawPrompt: contentRequest,
|
587
|
-
rawSettings: {}
|
588
|
-
},
|
589
|
-
warnings
|
590
|
-
};
|
591
|
-
}
|
592
|
-
};
|
593
|
-
function getToolCallsFromParts({
|
594
|
-
parts,
|
595
|
-
generateId: generateId2
|
596
|
-
}) {
|
597
|
-
if (parts == null) {
|
598
|
-
return void 0;
|
599
|
-
}
|
600
|
-
return parts.flatMap(
|
601
|
-
(part) => part.functionCall == null ? [] : {
|
602
|
-
toolCallType: "function",
|
603
|
-
toolCallId: generateId2(),
|
604
|
-
toolName: part.functionCall.name,
|
605
|
-
args: JSON.stringify(part.functionCall.args)
|
606
|
-
}
|
607
|
-
);
|
608
|
-
}
|
609
|
-
function getTextFromParts(parts) {
|
610
|
-
if (parts == null) {
|
611
|
-
return void 0;
|
612
|
-
}
|
613
|
-
const textParts = parts.filter((part) => "text" in part);
|
614
|
-
return textParts.length === 0 ? void 0 : textParts.map((part) => part.text).join("");
|
615
|
-
}
|
24
|
+
generateId,
|
25
|
+
loadSetting
|
26
|
+
} from "@ai-sdk/provider-utils";
|
616
27
|
|
617
28
|
// src/google-vertex-embedding-model.ts
|
618
29
|
import {
|
@@ -621,24 +32,27 @@ import {
|
|
621
32
|
import {
|
622
33
|
combineHeaders,
|
623
34
|
createJsonResponseHandler,
|
624
|
-
postJsonToApi
|
35
|
+
postJsonToApi,
|
36
|
+
resolve
|
625
37
|
} from "@ai-sdk/provider-utils";
|
626
38
|
import { z as z2 } from "zod";
|
627
39
|
|
628
|
-
// src/google-error.ts
|
40
|
+
// src/google-vertex-error.ts
|
629
41
|
import { createJsonErrorResponseHandler } from "@ai-sdk/provider-utils";
|
630
42
|
import { z } from "zod";
|
631
|
-
var
|
43
|
+
var googleVertexErrorDataSchema = z.object({
|
632
44
|
error: z.object({
|
633
45
|
code: z.number().nullable(),
|
634
46
|
message: z.string(),
|
635
47
|
status: z.string()
|
636
48
|
})
|
637
49
|
});
|
638
|
-
var
|
639
|
-
|
640
|
-
|
641
|
-
|
50
|
+
var googleVertexFailedResponseHandler = createJsonErrorResponseHandler(
|
51
|
+
{
|
52
|
+
errorSchema: googleVertexErrorDataSchema,
|
53
|
+
errorToMessage: (data) => data.error.message
|
54
|
+
}
|
55
|
+
);
|
642
56
|
|
643
57
|
// src/google-vertex-embedding-model.ts
|
644
58
|
var GoogleVertexEmbeddingModel = class {
|
@@ -670,19 +84,20 @@ var GoogleVertexEmbeddingModel = class {
|
|
670
84
|
values
|
671
85
|
});
|
672
86
|
}
|
87
|
+
const mergedHeaders = combineHeaders(
|
88
|
+
await resolve(this.config.headers),
|
89
|
+
headers
|
90
|
+
);
|
673
91
|
const { responseHeaders, value: response } = await postJsonToApi({
|
674
92
|
url: `https://${this.config.region}-aiplatform.googleapis.com/v1/projects/${this.config.project}/locations/${this.config.region}/publishers/google/models/${this.modelId}:predict`,
|
675
|
-
headers:
|
676
|
-
{ Authorization: `Bearer ${await this.config.generateAuthToken()}` },
|
677
|
-
headers
|
678
|
-
),
|
93
|
+
headers: mergedHeaders,
|
679
94
|
body: {
|
680
95
|
instances: values.map((value) => ({ content: value })),
|
681
96
|
parameters: {
|
682
97
|
outputDimensionality: this.settings.outputDimensionality
|
683
98
|
}
|
684
99
|
},
|
685
|
-
failedResponseHandler:
|
100
|
+
failedResponseHandler: googleVertexFailedResponseHandler,
|
686
101
|
successfulResponseHandler: createJsonResponseHandler(
|
687
102
|
googleVertexTextEmbeddingResponseSchema
|
688
103
|
),
|
@@ -716,6 +131,7 @@ var googleVertexTextEmbeddingResponseSchema = z2.object({
|
|
716
131
|
});
|
717
132
|
|
718
133
|
// src/google-vertex-provider.ts
|
134
|
+
import { GoogleGenerativeAILanguageModel } from "@ai-sdk/google/internal";
|
719
135
|
function createVertex(options = {}) {
|
720
136
|
const loadVertexProject = () => loadSetting({
|
721
137
|
settingValue: options.project,
|
@@ -729,29 +145,25 @@ function createVertex(options = {}) {
|
|
729
145
|
environmentVariableName: "GOOGLE_VERTEX_LOCATION",
|
730
146
|
description: "Google Vertex location"
|
731
147
|
});
|
732
|
-
const createVertexAI = () => {
|
733
|
-
var _a, _b;
|
734
|
-
const config = {
|
735
|
-
project: loadVertexProject(),
|
736
|
-
location: loadVertexLocation(),
|
737
|
-
googleAuthOptions: options.googleAuthOptions
|
738
|
-
};
|
739
|
-
return (_b = (_a = options.createVertexAI) == null ? void 0 : _a.call(options, config)) != null ? _b : new VertexAI2(config);
|
740
|
-
};
|
741
148
|
const createChatModel = (modelId, settings = {}) => {
|
742
|
-
var _a;
|
743
|
-
|
744
|
-
|
745
|
-
|
149
|
+
var _a, _b;
|
150
|
+
const region = loadVertexLocation();
|
151
|
+
const project = loadVertexProject();
|
152
|
+
return new GoogleGenerativeAILanguageModel(modelId, settings, {
|
153
|
+
provider: `google.vertex.chat`,
|
154
|
+
baseURL: `https://${region}-aiplatform.googleapis.com/v1/projects/${project}/locations/${region}/publishers/google`,
|
155
|
+
headers: (_a = options.headers) != null ? _a : {},
|
156
|
+
generateId: (_b = options.generateId) != null ? _b : generateId,
|
157
|
+
fetch: options.fetch
|
746
158
|
});
|
747
159
|
};
|
748
160
|
const createEmbeddingModel = (modelId, settings = {}) => {
|
749
|
-
|
161
|
+
var _a;
|
750
162
|
return new GoogleVertexEmbeddingModel(modelId, settings, {
|
751
|
-
provider:
|
163
|
+
provider: `google.vertex.embedding`,
|
752
164
|
region: loadVertexLocation(),
|
753
165
|
project: loadVertexProject(),
|
754
|
-
|
166
|
+
headers: (_a = options.headers) != null ? _a : {}
|
755
167
|
});
|
756
168
|
};
|
757
169
|
const provider = function(modelId, settings) {
|
@@ -766,9 +178,22 @@ function createVertex(options = {}) {
|
|
766
178
|
provider.textEmbeddingModel = createEmbeddingModel;
|
767
179
|
return provider;
|
768
180
|
}
|
769
|
-
|
181
|
+
|
182
|
+
// src/google-vertex-provider-node.ts
|
183
|
+
function createVertex2(options = {}) {
|
184
|
+
var _a;
|
185
|
+
return createVertex({
|
186
|
+
...options,
|
187
|
+
headers: (_a = options.headers) != null ? _a : async () => ({
|
188
|
+
Authorization: `Bearer ${await generateAuthToken(
|
189
|
+
options.googleAuthOptions
|
190
|
+
)}`
|
191
|
+
})
|
192
|
+
});
|
193
|
+
}
|
194
|
+
var vertex = createVertex2();
|
770
195
|
export {
|
771
|
-
createVertex,
|
196
|
+
createVertex2 as createVertex,
|
772
197
|
vertex
|
773
198
|
};
|
774
199
|
//# sourceMappingURL=index.mjs.map
|