@ai-sdk/google 2.0.0-canary.2 → 2.0.0-canary.20
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 +184 -0
- package/README.md +2 -2
- package/dist/index.d.mts +107 -65
- package/dist/index.d.ts +107 -65
- package/dist/index.js +363 -280
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +368 -282
- package/dist/index.mjs.map +1 -1
- package/{internal/dist → dist/internal}/index.d.mts +14 -80
- package/{internal/dist → dist/internal}/index.d.ts +14 -80
- package/{internal/dist → dist/internal}/index.js +298 -253
- package/dist/internal/index.js.map +1 -0
- package/{internal/dist → dist/internal}/index.mjs +301 -254
- package/dist/internal/index.mjs.map +1 -0
- package/internal.d.ts +1 -0
- package/package.json +20 -19
- package/internal/dist/index.js.map +0 -1
- package/internal/dist/index.mjs.map +0 -1
package/dist/index.mjs
CHANGED
@@ -15,10 +15,11 @@ import {
|
|
15
15
|
import {
|
16
16
|
combineHeaders,
|
17
17
|
createJsonResponseHandler,
|
18
|
+
parseProviderOptions,
|
18
19
|
postJsonToApi,
|
19
20
|
resolve
|
20
21
|
} from "@ai-sdk/provider-utils";
|
21
|
-
import { z as
|
22
|
+
import { z as z3 } from "zod";
|
22
23
|
|
23
24
|
// src/google-error.ts
|
24
25
|
import { createJsonErrorResponseHandler } from "@ai-sdk/provider-utils";
|
@@ -35,28 +36,61 @@ var googleFailedResponseHandler = createJsonErrorResponseHandler({
|
|
35
36
|
errorToMessage: (data) => data.error.message
|
36
37
|
});
|
37
38
|
|
39
|
+
// src/google-generative-ai-embedding-options.ts
|
40
|
+
import { z as z2 } from "zod";
|
41
|
+
var googleGenerativeAIEmbeddingProviderOptions = z2.object({
|
42
|
+
/**
|
43
|
+
* Optional. Optional reduced dimension for the output embedding.
|
44
|
+
* If set, excessive values in the output embedding are truncated from the end.
|
45
|
+
*/
|
46
|
+
outputDimensionality: z2.number().optional(),
|
47
|
+
/**
|
48
|
+
* Optional. Specifies the task type for generating embeddings.
|
49
|
+
* Supported task types:
|
50
|
+
* - SEMANTIC_SIMILARITY: Optimized for text similarity.
|
51
|
+
* - CLASSIFICATION: Optimized for text classification.
|
52
|
+
* - CLUSTERING: Optimized for clustering texts based on similarity.
|
53
|
+
* - RETRIEVAL_DOCUMENT: Optimized for document retrieval.
|
54
|
+
* - RETRIEVAL_QUERY: Optimized for query-based retrieval.
|
55
|
+
* - QUESTION_ANSWERING: Optimized for answering questions.
|
56
|
+
* - FACT_VERIFICATION: Optimized for verifying factual information.
|
57
|
+
* - CODE_RETRIEVAL_QUERY: Optimized for retrieving code blocks based on natural language queries.
|
58
|
+
*/
|
59
|
+
taskType: z2.enum([
|
60
|
+
"SEMANTIC_SIMILARITY",
|
61
|
+
"CLASSIFICATION",
|
62
|
+
"CLUSTERING",
|
63
|
+
"RETRIEVAL_DOCUMENT",
|
64
|
+
"RETRIEVAL_QUERY",
|
65
|
+
"QUESTION_ANSWERING",
|
66
|
+
"FACT_VERIFICATION",
|
67
|
+
"CODE_RETRIEVAL_QUERY"
|
68
|
+
]).optional()
|
69
|
+
});
|
70
|
+
|
38
71
|
// src/google-generative-ai-embedding-model.ts
|
39
72
|
var GoogleGenerativeAIEmbeddingModel = class {
|
40
|
-
constructor(modelId,
|
41
|
-
this.specificationVersion = "
|
73
|
+
constructor(modelId, config) {
|
74
|
+
this.specificationVersion = "v2";
|
75
|
+
this.maxEmbeddingsPerCall = 2048;
|
76
|
+
this.supportsParallelCalls = true;
|
42
77
|
this.modelId = modelId;
|
43
|
-
this.settings = settings;
|
44
78
|
this.config = config;
|
45
79
|
}
|
46
80
|
get provider() {
|
47
81
|
return this.config.provider;
|
48
82
|
}
|
49
|
-
get maxEmbeddingsPerCall() {
|
50
|
-
return 2048;
|
51
|
-
}
|
52
|
-
get supportsParallelCalls() {
|
53
|
-
return true;
|
54
|
-
}
|
55
83
|
async doEmbed({
|
56
84
|
values,
|
57
85
|
headers,
|
58
|
-
abortSignal
|
86
|
+
abortSignal,
|
87
|
+
providerOptions
|
59
88
|
}) {
|
89
|
+
const googleOptions = await parseProviderOptions({
|
90
|
+
provider: "google",
|
91
|
+
providerOptions,
|
92
|
+
schema: googleGenerativeAIEmbeddingProviderOptions
|
93
|
+
});
|
60
94
|
if (values.length > this.maxEmbeddingsPerCall) {
|
61
95
|
throw new TooManyEmbeddingValuesForCallError({
|
62
96
|
provider: this.provider,
|
@@ -69,14 +103,19 @@ var GoogleGenerativeAIEmbeddingModel = class {
|
|
69
103
|
await resolve(this.config.headers),
|
70
104
|
headers
|
71
105
|
);
|
72
|
-
const {
|
106
|
+
const {
|
107
|
+
responseHeaders,
|
108
|
+
value: response,
|
109
|
+
rawValue
|
110
|
+
} = await postJsonToApi({
|
73
111
|
url: `${this.config.baseURL}/models/${this.modelId}:batchEmbedContents`,
|
74
112
|
headers: mergedHeaders,
|
75
113
|
body: {
|
76
114
|
requests: values.map((value) => ({
|
77
115
|
model: `models/${this.modelId}`,
|
78
116
|
content: { role: "user", parts: [{ text: value }] },
|
79
|
-
outputDimensionality:
|
117
|
+
outputDimensionality: googleOptions == null ? void 0 : googleOptions.outputDimensionality,
|
118
|
+
taskType: googleOptions == null ? void 0 : googleOptions.taskType
|
80
119
|
}))
|
81
120
|
},
|
82
121
|
failedResponseHandler: googleFailedResponseHandler,
|
@@ -89,12 +128,12 @@ var GoogleGenerativeAIEmbeddingModel = class {
|
|
89
128
|
return {
|
90
129
|
embeddings: response.embeddings.map((item) => item.values),
|
91
130
|
usage: void 0,
|
92
|
-
|
131
|
+
response: { headers: responseHeaders, body: rawValue }
|
93
132
|
};
|
94
133
|
}
|
95
134
|
};
|
96
|
-
var googleGenerativeAITextEmbeddingResponseSchema =
|
97
|
-
embeddings:
|
135
|
+
var googleGenerativeAITextEmbeddingResponseSchema = z3.object({
|
136
|
+
embeddings: z3.array(z3.object({ values: z3.array(z3.number()) }))
|
98
137
|
});
|
99
138
|
|
100
139
|
// src/google-generative-ai-language-model.ts
|
@@ -102,15 +141,15 @@ import {
|
|
102
141
|
combineHeaders as combineHeaders2,
|
103
142
|
createEventSourceResponseHandler,
|
104
143
|
createJsonResponseHandler as createJsonResponseHandler2,
|
105
|
-
parseProviderOptions,
|
144
|
+
parseProviderOptions as parseProviderOptions2,
|
106
145
|
postJsonToApi as postJsonToApi2,
|
107
146
|
resolve as resolve2
|
108
147
|
} from "@ai-sdk/provider-utils";
|
109
|
-
import { z as
|
148
|
+
import { z as z5 } from "zod";
|
110
149
|
|
111
150
|
// src/convert-json-schema-to-openapi-schema.ts
|
112
151
|
function convertJSONSchemaToOpenAPISchema(jsonSchema) {
|
113
|
-
if (isEmptyObjectSchema(jsonSchema)) {
|
152
|
+
if (jsonSchema == null || isEmptyObjectSchema(jsonSchema)) {
|
114
153
|
return void 0;
|
115
154
|
}
|
116
155
|
if (typeof jsonSchema === "boolean") {
|
@@ -209,9 +248,10 @@ function isEmptyObjectSchema(jsonSchema) {
|
|
209
248
|
import {
|
210
249
|
UnsupportedFunctionalityError
|
211
250
|
} from "@ai-sdk/provider";
|
212
|
-
import {
|
251
|
+
import {
|
252
|
+
convertToBase64
|
253
|
+
} from "@ai-sdk/provider-utils";
|
213
254
|
function convertToGoogleGenerativeAIMessages(prompt) {
|
214
|
-
var _a, _b;
|
215
255
|
const systemInstructionParts = [];
|
216
256
|
const contents = [];
|
217
257
|
let systemMessagesAllowed = true;
|
@@ -235,33 +275,18 @@ function convertToGoogleGenerativeAIMessages(prompt) {
|
|
235
275
|
parts.push({ text: part.text });
|
236
276
|
break;
|
237
277
|
}
|
238
|
-
case "image": {
|
239
|
-
parts.push(
|
240
|
-
part.image instanceof URL ? {
|
241
|
-
fileData: {
|
242
|
-
mimeType: (_a = part.mimeType) != null ? _a : "image/jpeg",
|
243
|
-
fileUri: part.image.toString()
|
244
|
-
}
|
245
|
-
} : {
|
246
|
-
inlineData: {
|
247
|
-
mimeType: (_b = part.mimeType) != null ? _b : "image/jpeg",
|
248
|
-
data: convertUint8ArrayToBase64(part.image)
|
249
|
-
}
|
250
|
-
}
|
251
|
-
);
|
252
|
-
break;
|
253
|
-
}
|
254
278
|
case "file": {
|
279
|
+
const mediaType = part.mediaType === "image/*" ? "image/jpeg" : part.mediaType;
|
255
280
|
parts.push(
|
256
281
|
part.data instanceof URL ? {
|
257
282
|
fileData: {
|
258
|
-
mimeType:
|
283
|
+
mimeType: mediaType,
|
259
284
|
fileUri: part.data.toString()
|
260
285
|
}
|
261
286
|
} : {
|
262
287
|
inlineData: {
|
263
|
-
mimeType:
|
264
|
-
data: part.data
|
288
|
+
mimeType: mediaType,
|
289
|
+
data: convertToBase64(part.data)
|
265
290
|
}
|
266
291
|
}
|
267
292
|
);
|
@@ -282,7 +307,7 @@ function convertToGoogleGenerativeAIMessages(prompt) {
|
|
282
307
|
return part.text.length === 0 ? void 0 : { text: part.text };
|
283
308
|
}
|
284
309
|
case "file": {
|
285
|
-
if (part.
|
310
|
+
if (part.mediaType !== "image/png") {
|
286
311
|
throw new UnsupportedFunctionalityError({
|
287
312
|
functionality: "Only PNG images are supported in assistant messages"
|
288
313
|
});
|
@@ -294,8 +319,8 @@ function convertToGoogleGenerativeAIMessages(prompt) {
|
|
294
319
|
}
|
295
320
|
return {
|
296
321
|
inlineData: {
|
297
|
-
mimeType: part.
|
298
|
-
data: part.data
|
322
|
+
mimeType: part.mediaType,
|
323
|
+
data: convertToBase64(part.data)
|
299
324
|
}
|
300
325
|
};
|
301
326
|
}
|
@@ -341,20 +366,112 @@ function getModelPath(modelId) {
|
|
341
366
|
return modelId.includes("/") ? modelId : `models/${modelId}`;
|
342
367
|
}
|
343
368
|
|
369
|
+
// src/google-generative-ai-options.ts
|
370
|
+
import { z as z4 } from "zod";
|
371
|
+
var dynamicRetrievalConfig = z4.object({
|
372
|
+
/**
|
373
|
+
* The mode of the predictor to be used in dynamic retrieval.
|
374
|
+
*/
|
375
|
+
mode: z4.enum(["MODE_UNSPECIFIED", "MODE_DYNAMIC"]).optional(),
|
376
|
+
/**
|
377
|
+
* The threshold to be used in dynamic retrieval. If not set, a system default
|
378
|
+
* value is used.
|
379
|
+
*/
|
380
|
+
dynamicThreshold: z4.number().optional()
|
381
|
+
});
|
382
|
+
var googleGenerativeAIProviderOptions = z4.object({
|
383
|
+
responseModalities: z4.array(z4.enum(["TEXT", "IMAGE"])).optional(),
|
384
|
+
thinkingConfig: z4.object({
|
385
|
+
thinkingBudget: z4.number().optional()
|
386
|
+
}).optional(),
|
387
|
+
/**
|
388
|
+
Optional.
|
389
|
+
The name of the cached content used as context to serve the prediction.
|
390
|
+
Format: cachedContents/{cachedContent}
|
391
|
+
*/
|
392
|
+
cachedContent: z4.string().optional(),
|
393
|
+
/**
|
394
|
+
* Optional. Enable structured output. Default is true.
|
395
|
+
*
|
396
|
+
* This is useful when the JSON Schema contains elements that are
|
397
|
+
* not supported by the OpenAPI schema version that
|
398
|
+
* Google Generative AI uses. You can use this to disable
|
399
|
+
* structured outputs if you need to.
|
400
|
+
*/
|
401
|
+
structuredOutputs: z4.boolean().optional(),
|
402
|
+
/**
|
403
|
+
Optional. A list of unique safety settings for blocking unsafe content.
|
404
|
+
*/
|
405
|
+
safetySettings: z4.array(
|
406
|
+
z4.object({
|
407
|
+
category: z4.enum([
|
408
|
+
"HARM_CATEGORY_UNSPECIFIED",
|
409
|
+
"HARM_CATEGORY_HATE_SPEECH",
|
410
|
+
"HARM_CATEGORY_DANGEROUS_CONTENT",
|
411
|
+
"HARM_CATEGORY_HARASSMENT",
|
412
|
+
"HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
413
|
+
"HARM_CATEGORY_CIVIC_INTEGRITY"
|
414
|
+
]),
|
415
|
+
threshold: z4.enum([
|
416
|
+
"HARM_BLOCK_THRESHOLD_UNSPECIFIED",
|
417
|
+
"BLOCK_LOW_AND_ABOVE",
|
418
|
+
"BLOCK_MEDIUM_AND_ABOVE",
|
419
|
+
"BLOCK_ONLY_HIGH",
|
420
|
+
"BLOCK_NONE",
|
421
|
+
"OFF"
|
422
|
+
])
|
423
|
+
})
|
424
|
+
).optional(),
|
425
|
+
threshold: z4.enum([
|
426
|
+
"HARM_BLOCK_THRESHOLD_UNSPECIFIED",
|
427
|
+
"BLOCK_LOW_AND_ABOVE",
|
428
|
+
"BLOCK_MEDIUM_AND_ABOVE",
|
429
|
+
"BLOCK_ONLY_HIGH",
|
430
|
+
"BLOCK_NONE",
|
431
|
+
"OFF"
|
432
|
+
]).optional(),
|
433
|
+
/**
|
434
|
+
* Optional. Enables timestamp understanding for audio-only files.
|
435
|
+
*
|
436
|
+
* https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/audio-understanding
|
437
|
+
*/
|
438
|
+
audioTimestamp: z4.boolean().optional(),
|
439
|
+
/**
|
440
|
+
Optional. When enabled, the model will use Google search to ground the response.
|
441
|
+
|
442
|
+
@see https://cloud.google.com/vertex-ai/generative-ai/docs/grounding/overview
|
443
|
+
*/
|
444
|
+
useSearchGrounding: z4.boolean().optional(),
|
445
|
+
/**
|
446
|
+
Optional. Specifies the dynamic retrieval configuration.
|
447
|
+
|
448
|
+
@note Dynamic retrieval is only compatible with Gemini 1.5 Flash.
|
449
|
+
|
450
|
+
@see https://cloud.google.com/vertex-ai/generative-ai/docs/multimodal/ground-with-google-search#dynamic-retrieval
|
451
|
+
*/
|
452
|
+
dynamicRetrievalConfig: dynamicRetrievalConfig.optional()
|
453
|
+
});
|
454
|
+
|
344
455
|
// src/google-prepare-tools.ts
|
345
456
|
import {
|
346
457
|
UnsupportedFunctionalityError as UnsupportedFunctionalityError2
|
347
458
|
} from "@ai-sdk/provider";
|
348
|
-
function prepareTools(
|
349
|
-
|
350
|
-
|
459
|
+
function prepareTools({
|
460
|
+
tools,
|
461
|
+
toolChoice,
|
462
|
+
useSearchGrounding,
|
463
|
+
dynamicRetrievalConfig: dynamicRetrievalConfig2,
|
464
|
+
modelId
|
465
|
+
}) {
|
466
|
+
var _a;
|
467
|
+
tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
|
351
468
|
const toolWarnings = [];
|
352
469
|
const isGemini2 = modelId.includes("gemini-2");
|
353
470
|
const supportsDynamicRetrieval = modelId.includes("gemini-1.5-flash") && !modelId.includes("-8b");
|
354
471
|
if (useSearchGrounding) {
|
355
472
|
return {
|
356
473
|
tools: isGemini2 ? { googleSearch: {} } : {
|
357
|
-
googleSearchRetrieval: !supportsDynamicRetrieval || !
|
474
|
+
googleSearchRetrieval: !supportsDynamicRetrieval || !dynamicRetrievalConfig2 ? {} : { dynamicRetrievalConfig: dynamicRetrievalConfig2 }
|
358
475
|
},
|
359
476
|
toolConfig: void 0,
|
360
477
|
toolWarnings
|
@@ -370,12 +487,11 @@ function prepareTools(mode, useSearchGrounding, dynamicRetrievalConfig, modelId)
|
|
370
487
|
} else {
|
371
488
|
functionDeclarations.push({
|
372
489
|
name: tool.name,
|
373
|
-
description: (
|
490
|
+
description: (_a = tool.description) != null ? _a : "",
|
374
491
|
parameters: convertJSONSchemaToOpenAPISchema(tool.parameters)
|
375
492
|
});
|
376
493
|
}
|
377
494
|
}
|
378
|
-
const toolChoice = mode.toolChoice;
|
379
495
|
if (toolChoice == null) {
|
380
496
|
return {
|
381
497
|
tools: { functionDeclarations },
|
@@ -417,7 +533,7 @@ function prepareTools(mode, useSearchGrounding, dynamicRetrievalConfig, modelId)
|
|
417
533
|
default: {
|
418
534
|
const _exhaustiveCheck = type;
|
419
535
|
throw new UnsupportedFunctionalityError2({
|
420
|
-
functionality: `
|
536
|
+
functionality: `tool choice type: ${_exhaustiveCheck}`
|
421
537
|
});
|
422
538
|
}
|
423
539
|
}
|
@@ -452,25 +568,21 @@ function mapGoogleGenerativeAIFinishReason({
|
|
452
568
|
|
453
569
|
// src/google-generative-ai-language-model.ts
|
454
570
|
var GoogleGenerativeAILanguageModel = class {
|
455
|
-
constructor(modelId,
|
571
|
+
constructor(modelId, config) {
|
456
572
|
this.specificationVersion = "v2";
|
457
|
-
this.defaultObjectGenerationMode = "json";
|
458
|
-
this.supportsImageUrls = false;
|
459
573
|
this.modelId = modelId;
|
460
|
-
this.settings = settings;
|
461
574
|
this.config = config;
|
462
575
|
}
|
463
|
-
get supportsStructuredOutputs() {
|
464
|
-
var _a;
|
465
|
-
return (_a = this.settings.structuredOutputs) != null ? _a : true;
|
466
|
-
}
|
467
576
|
get provider() {
|
468
577
|
return this.config.provider;
|
469
578
|
}
|
579
|
+
get supportedUrls() {
|
580
|
+
var _a, _b, _c;
|
581
|
+
return (_c = (_b = (_a = this.config).supportedUrls) == null ? void 0 : _b.call(_a)) != null ? _c : {};
|
582
|
+
}
|
470
583
|
async getArgs({
|
471
|
-
mode,
|
472
584
|
prompt,
|
473
|
-
|
585
|
+
maxOutputTokens,
|
474
586
|
temperature,
|
475
587
|
topP,
|
476
588
|
topK,
|
@@ -479,111 +591,66 @@ var GoogleGenerativeAILanguageModel = class {
|
|
479
591
|
stopSequences,
|
480
592
|
responseFormat,
|
481
593
|
seed,
|
482
|
-
|
594
|
+
tools,
|
595
|
+
toolChoice,
|
596
|
+
providerOptions
|
483
597
|
}) {
|
484
598
|
var _a, _b;
|
485
|
-
const type = mode.type;
|
486
599
|
const warnings = [];
|
487
|
-
const googleOptions =
|
600
|
+
const googleOptions = await parseProviderOptions2({
|
488
601
|
provider: "google",
|
489
|
-
providerOptions
|
490
|
-
schema:
|
602
|
+
providerOptions,
|
603
|
+
schema: googleGenerativeAIProviderOptions
|
491
604
|
});
|
492
|
-
const generationConfig = {
|
493
|
-
// standardized settings:
|
494
|
-
maxOutputTokens: maxTokens,
|
495
|
-
temperature,
|
496
|
-
topK,
|
497
|
-
topP,
|
498
|
-
frequencyPenalty,
|
499
|
-
presencePenalty,
|
500
|
-
stopSequences,
|
501
|
-
seed,
|
502
|
-
// response format:
|
503
|
-
responseMimeType: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? "application/json" : void 0,
|
504
|
-
responseSchema: (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && // Google GenAI does not support all OpenAPI Schema features,
|
505
|
-
// so this is needed as an escape hatch:
|
506
|
-
this.supportsStructuredOutputs ? convertJSONSchemaToOpenAPISchema(responseFormat.schema) : void 0,
|
507
|
-
...this.settings.audioTimestamp && {
|
508
|
-
audioTimestamp: this.settings.audioTimestamp
|
509
|
-
},
|
510
|
-
// provider options:
|
511
|
-
responseModalities: googleOptions == null ? void 0 : googleOptions.responseModalities
|
512
|
-
};
|
513
605
|
const { contents, systemInstruction } = convertToGoogleGenerativeAIMessages(prompt);
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
contents,
|
546
|
-
systemInstruction,
|
547
|
-
safetySettings: this.settings.safetySettings,
|
548
|
-
cachedContent: this.settings.cachedContent
|
549
|
-
},
|
550
|
-
warnings
|
551
|
-
};
|
552
|
-
}
|
553
|
-
case "object-tool": {
|
554
|
-
return {
|
555
|
-
args: {
|
556
|
-
generationConfig,
|
557
|
-
contents,
|
558
|
-
tools: {
|
559
|
-
functionDeclarations: [
|
560
|
-
{
|
561
|
-
name: mode.tool.name,
|
562
|
-
description: (_b = mode.tool.description) != null ? _b : "",
|
563
|
-
parameters: convertJSONSchemaToOpenAPISchema(
|
564
|
-
mode.tool.parameters
|
565
|
-
)
|
566
|
-
}
|
567
|
-
]
|
568
|
-
},
|
569
|
-
toolConfig: { functionCallingConfig: { mode: "ANY" } },
|
570
|
-
safetySettings: this.settings.safetySettings,
|
571
|
-
cachedContent: this.settings.cachedContent
|
606
|
+
const {
|
607
|
+
tools: googleTools,
|
608
|
+
toolConfig: googleToolConfig,
|
609
|
+
toolWarnings
|
610
|
+
} = prepareTools({
|
611
|
+
tools,
|
612
|
+
toolChoice,
|
613
|
+
useSearchGrounding: (_a = googleOptions == null ? void 0 : googleOptions.useSearchGrounding) != null ? _a : false,
|
614
|
+
dynamicRetrievalConfig: googleOptions == null ? void 0 : googleOptions.dynamicRetrievalConfig,
|
615
|
+
modelId: this.modelId
|
616
|
+
});
|
617
|
+
return {
|
618
|
+
args: {
|
619
|
+
generationConfig: {
|
620
|
+
// standardized settings:
|
621
|
+
maxOutputTokens,
|
622
|
+
temperature,
|
623
|
+
topK,
|
624
|
+
topP,
|
625
|
+
frequencyPenalty,
|
626
|
+
presencePenalty,
|
627
|
+
stopSequences,
|
628
|
+
seed,
|
629
|
+
// response format:
|
630
|
+
responseMimeType: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? "application/json" : void 0,
|
631
|
+
responseSchema: (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && // Google GenAI does not support all OpenAPI Schema features,
|
632
|
+
// so this is needed as an escape hatch:
|
633
|
+
// TODO convert into provider option
|
634
|
+
((_b = googleOptions == null ? void 0 : googleOptions.structuredOutputs) != null ? _b : true) ? convertJSONSchemaToOpenAPISchema(responseFormat.schema) : void 0,
|
635
|
+
...(googleOptions == null ? void 0 : googleOptions.audioTimestamp) && {
|
636
|
+
audioTimestamp: googleOptions.audioTimestamp
|
572
637
|
},
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
|
583
|
-
|
638
|
+
// provider options:
|
639
|
+
responseModalities: googleOptions == null ? void 0 : googleOptions.responseModalities,
|
640
|
+
thinkingConfig: googleOptions == null ? void 0 : googleOptions.thinkingConfig
|
641
|
+
},
|
642
|
+
contents,
|
643
|
+
systemInstruction,
|
644
|
+
safetySettings: googleOptions == null ? void 0 : googleOptions.safetySettings,
|
645
|
+
tools: googleTools,
|
646
|
+
toolConfig: googleToolConfig,
|
647
|
+
cachedContent: googleOptions == null ? void 0 : googleOptions.cachedContent
|
648
|
+
},
|
649
|
+
warnings: [...warnings, ...toolWarnings]
|
650
|
+
};
|
584
651
|
}
|
585
652
|
async doGenerate(options) {
|
586
|
-
var _a, _b, _c, _d, _e;
|
653
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
587
654
|
const { args, warnings } = await this.getArgs(options);
|
588
655
|
const body = JSON.stringify(args);
|
589
656
|
const mergedHeaders = combineHeaders2(
|
@@ -605,43 +672,62 @@ var GoogleGenerativeAILanguageModel = class {
|
|
605
672
|
abortSignal: options.abortSignal,
|
606
673
|
fetch: this.config.fetch
|
607
674
|
});
|
608
|
-
const { contents: rawPrompt, ...rawSettings } = args;
|
609
675
|
const candidate = response.candidates[0];
|
610
|
-
const
|
611
|
-
const
|
612
|
-
|
676
|
+
const content = [];
|
677
|
+
const parts = candidate.content == null || typeof candidate.content !== "object" || !("parts" in candidate.content) ? [] : (_a = candidate.content.parts) != null ? _a : [];
|
678
|
+
for (const part of parts) {
|
679
|
+
if ("text" in part && part.text.length > 0) {
|
680
|
+
content.push({ type: "text", text: part.text });
|
681
|
+
} else if ("functionCall" in part) {
|
682
|
+
content.push({
|
683
|
+
type: "tool-call",
|
684
|
+
toolCallType: "function",
|
685
|
+
toolCallId: this.config.generateId(),
|
686
|
+
toolName: part.functionCall.name,
|
687
|
+
args: JSON.stringify(part.functionCall.args)
|
688
|
+
});
|
689
|
+
} else if ("inlineData" in part) {
|
690
|
+
content.push({
|
691
|
+
type: "file",
|
692
|
+
data: part.inlineData.data,
|
693
|
+
mediaType: part.inlineData.mimeType
|
694
|
+
});
|
695
|
+
}
|
696
|
+
}
|
697
|
+
const sources = (_b = extractSources({
|
698
|
+
groundingMetadata: candidate.groundingMetadata,
|
613
699
|
generateId: this.config.generateId
|
614
|
-
});
|
700
|
+
})) != null ? _b : [];
|
701
|
+
for (const source of sources) {
|
702
|
+
content.push(source);
|
703
|
+
}
|
615
704
|
const usageMetadata = response.usageMetadata;
|
616
705
|
return {
|
617
|
-
|
618
|
-
files: (_a = getInlineDataParts(parts)) == null ? void 0 : _a.map((part) => ({
|
619
|
-
data: part.inlineData.data,
|
620
|
-
mimeType: part.inlineData.mimeType
|
621
|
-
})),
|
622
|
-
toolCalls,
|
706
|
+
content,
|
623
707
|
finishReason: mapGoogleGenerativeAIFinishReason({
|
624
708
|
finishReason: candidate.finishReason,
|
625
|
-
hasToolCalls:
|
709
|
+
hasToolCalls: content.some((part) => part.type === "tool-call")
|
626
710
|
}),
|
627
711
|
usage: {
|
628
|
-
|
629
|
-
|
712
|
+
inputTokens: (_c = usageMetadata == null ? void 0 : usageMetadata.promptTokenCount) != null ? _c : void 0,
|
713
|
+
outputTokens: (_d = usageMetadata == null ? void 0 : usageMetadata.candidatesTokenCount) != null ? _d : void 0,
|
714
|
+
totalTokens: (_e = usageMetadata == null ? void 0 : usageMetadata.totalTokenCount) != null ? _e : void 0,
|
715
|
+
reasoningTokens: (_f = usageMetadata == null ? void 0 : usageMetadata.thoughtsTokenCount) != null ? _f : void 0,
|
716
|
+
cachedInputTokens: (_g = usageMetadata == null ? void 0 : usageMetadata.cachedContentTokenCount) != null ? _g : void 0
|
630
717
|
},
|
631
|
-
rawCall: { rawPrompt, rawSettings },
|
632
|
-
rawResponse: { headers: responseHeaders, body: rawResponse },
|
633
718
|
warnings,
|
634
719
|
providerMetadata: {
|
635
720
|
google: {
|
636
|
-
groundingMetadata: (
|
637
|
-
safetyRatings: (
|
721
|
+
groundingMetadata: (_h = candidate.groundingMetadata) != null ? _h : null,
|
722
|
+
safetyRatings: (_i = candidate.safetyRatings) != null ? _i : null
|
638
723
|
}
|
639
724
|
},
|
640
|
-
|
641
|
-
|
642
|
-
|
643
|
-
|
644
|
-
|
725
|
+
request: { body },
|
726
|
+
response: {
|
727
|
+
// TODO timestamp, model id, id
|
728
|
+
headers: responseHeaders,
|
729
|
+
body: rawResponse
|
730
|
+
}
|
645
731
|
};
|
646
732
|
}
|
647
733
|
async doStream(options) {
|
@@ -662,11 +748,11 @@ var GoogleGenerativeAILanguageModel = class {
|
|
662
748
|
abortSignal: options.abortSignal,
|
663
749
|
fetch: this.config.fetch
|
664
750
|
});
|
665
|
-
const { contents: rawPrompt, ...rawSettings } = args;
|
666
751
|
let finishReason = "unknown";
|
667
|
-
|
668
|
-
|
669
|
-
|
752
|
+
const usage = {
|
753
|
+
inputTokens: void 0,
|
754
|
+
outputTokens: void 0,
|
755
|
+
totalTokens: void 0
|
670
756
|
};
|
671
757
|
let providerMetadata = void 0;
|
672
758
|
const generateId2 = this.config.generateId;
|
@@ -674,8 +760,11 @@ var GoogleGenerativeAILanguageModel = class {
|
|
674
760
|
return {
|
675
761
|
stream: response.pipeThrough(
|
676
762
|
new TransformStream({
|
763
|
+
start(controller) {
|
764
|
+
controller.enqueue({ type: "stream-start", warnings });
|
765
|
+
},
|
677
766
|
transform(chunk, controller) {
|
678
|
-
var _a, _b, _c, _d, _e, _f;
|
767
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _i;
|
679
768
|
if (!chunk.success) {
|
680
769
|
controller.enqueue({ type: "error", error: chunk.error });
|
681
770
|
return;
|
@@ -683,12 +772,13 @@ var GoogleGenerativeAILanguageModel = class {
|
|
683
772
|
const value = chunk.value;
|
684
773
|
const usageMetadata = value.usageMetadata;
|
685
774
|
if (usageMetadata != null) {
|
686
|
-
usage =
|
687
|
-
|
688
|
-
|
689
|
-
|
775
|
+
usage.inputTokens = (_a = usageMetadata.promptTokenCount) != null ? _a : void 0;
|
776
|
+
usage.outputTokens = (_b = usageMetadata.candidatesTokenCount) != null ? _b : void 0;
|
777
|
+
usage.totalTokens = (_c = usageMetadata.totalTokenCount) != null ? _c : void 0;
|
778
|
+
usage.reasoningTokens = (_d = usageMetadata.thoughtsTokenCount) != null ? _d : void 0;
|
779
|
+
usage.cachedInputTokens = (_e = usageMetadata.cachedContentTokenCount) != null ? _e : void 0;
|
690
780
|
}
|
691
|
-
const candidate = (
|
781
|
+
const candidate = (_f = value.candidates) == null ? void 0 : _f[0];
|
692
782
|
if (candidate == null) {
|
693
783
|
return;
|
694
784
|
}
|
@@ -696,17 +786,14 @@ var GoogleGenerativeAILanguageModel = class {
|
|
696
786
|
if (content != null) {
|
697
787
|
const deltaText = getTextFromParts(content.parts);
|
698
788
|
if (deltaText != null) {
|
699
|
-
controller.enqueue(
|
700
|
-
type: "text-delta",
|
701
|
-
textDelta: deltaText
|
702
|
-
});
|
789
|
+
controller.enqueue(deltaText);
|
703
790
|
}
|
704
791
|
const inlineDataParts = getInlineDataParts(content.parts);
|
705
792
|
if (inlineDataParts != null) {
|
706
793
|
for (const part of inlineDataParts) {
|
707
794
|
controller.enqueue({
|
708
795
|
type: "file",
|
709
|
-
|
796
|
+
mediaType: part.inlineData.mimeType,
|
710
797
|
data: part.inlineData.data
|
711
798
|
});
|
712
799
|
}
|
@@ -740,17 +827,17 @@ var GoogleGenerativeAILanguageModel = class {
|
|
740
827
|
finishReason: candidate.finishReason,
|
741
828
|
hasToolCalls
|
742
829
|
});
|
743
|
-
const sources = (
|
830
|
+
const sources = (_g = extractSources({
|
744
831
|
groundingMetadata: candidate.groundingMetadata,
|
745
832
|
generateId: generateId2
|
746
|
-
})) != null ?
|
833
|
+
})) != null ? _g : [];
|
747
834
|
for (const source of sources) {
|
748
|
-
controller.enqueue(
|
835
|
+
controller.enqueue(source);
|
749
836
|
}
|
750
837
|
providerMetadata = {
|
751
838
|
google: {
|
752
|
-
groundingMetadata: (
|
753
|
-
safetyRatings: (
|
839
|
+
groundingMetadata: (_h = candidate.groundingMetadata) != null ? _h : null,
|
840
|
+
safetyRatings: (_i = candidate.safetyRatings) != null ? _i : null
|
754
841
|
}
|
755
842
|
};
|
756
843
|
}
|
@@ -765,9 +852,7 @@ var GoogleGenerativeAILanguageModel = class {
|
|
765
852
|
}
|
766
853
|
})
|
767
854
|
),
|
768
|
-
|
769
|
-
rawResponse: { headers: responseHeaders },
|
770
|
-
warnings,
|
855
|
+
response: { headers: responseHeaders },
|
771
856
|
request: { body }
|
772
857
|
};
|
773
858
|
}
|
@@ -780,6 +865,7 @@ function getToolCallsFromParts({
|
|
780
865
|
(part) => "functionCall" in part
|
781
866
|
);
|
782
867
|
return functionCallParts == null || functionCallParts.length === 0 ? void 0 : functionCallParts.map((part) => ({
|
868
|
+
type: "tool-call",
|
783
869
|
toolCallType: "function",
|
784
870
|
toolCallId: generateId2(),
|
785
871
|
toolName: part.functionCall.name,
|
@@ -788,7 +874,10 @@ function getToolCallsFromParts({
|
|
788
874
|
}
|
789
875
|
function getTextFromParts(parts) {
|
790
876
|
const textParts = parts == null ? void 0 : parts.filter((part) => "text" in part);
|
791
|
-
return textParts == null || textParts.length === 0 ? void 0 :
|
877
|
+
return textParts == null || textParts.length === 0 ? void 0 : {
|
878
|
+
type: "text",
|
879
|
+
text: textParts.map((part) => part.text).join("")
|
880
|
+
};
|
792
881
|
}
|
793
882
|
function getInlineDataParts(parts) {
|
794
883
|
return parts == null ? void 0 : parts.filter(
|
@@ -803,110 +892,102 @@ function extractSources({
|
|
803
892
|
return (_a = groundingMetadata == null ? void 0 : groundingMetadata.groundingChunks) == null ? void 0 : _a.filter(
|
804
893
|
(chunk) => chunk.web != null
|
805
894
|
).map((chunk) => ({
|
895
|
+
type: "source",
|
806
896
|
sourceType: "url",
|
807
897
|
id: generateId2(),
|
808
898
|
url: chunk.web.uri,
|
809
899
|
title: chunk.web.title
|
810
900
|
}));
|
811
901
|
}
|
812
|
-
var contentSchema =
|
813
|
-
role:
|
814
|
-
parts:
|
815
|
-
|
816
|
-
|
817
|
-
text:
|
902
|
+
var contentSchema = z5.object({
|
903
|
+
role: z5.string(),
|
904
|
+
parts: z5.array(
|
905
|
+
z5.union([
|
906
|
+
z5.object({
|
907
|
+
text: z5.string()
|
818
908
|
}),
|
819
|
-
|
820
|
-
functionCall:
|
821
|
-
name:
|
822
|
-
args:
|
909
|
+
z5.object({
|
910
|
+
functionCall: z5.object({
|
911
|
+
name: z5.string(),
|
912
|
+
args: z5.unknown()
|
823
913
|
})
|
824
914
|
}),
|
825
|
-
|
826
|
-
inlineData:
|
827
|
-
mimeType:
|
828
|
-
data:
|
915
|
+
z5.object({
|
916
|
+
inlineData: z5.object({
|
917
|
+
mimeType: z5.string(),
|
918
|
+
data: z5.string()
|
829
919
|
})
|
830
920
|
})
|
831
921
|
])
|
832
922
|
).nullish()
|
833
923
|
});
|
834
|
-
var groundingChunkSchema =
|
835
|
-
web:
|
836
|
-
retrievedContext:
|
924
|
+
var groundingChunkSchema = z5.object({
|
925
|
+
web: z5.object({ uri: z5.string(), title: z5.string() }).nullish(),
|
926
|
+
retrievedContext: z5.object({ uri: z5.string(), title: z5.string() }).nullish()
|
837
927
|
});
|
838
|
-
var groundingMetadataSchema =
|
839
|
-
webSearchQueries:
|
840
|
-
retrievalQueries:
|
841
|
-
searchEntryPoint:
|
842
|
-
groundingChunks:
|
843
|
-
groundingSupports:
|
844
|
-
|
845
|
-
segment:
|
846
|
-
startIndex:
|
847
|
-
endIndex:
|
848
|
-
text:
|
928
|
+
var groundingMetadataSchema = z5.object({
|
929
|
+
webSearchQueries: z5.array(z5.string()).nullish(),
|
930
|
+
retrievalQueries: z5.array(z5.string()).nullish(),
|
931
|
+
searchEntryPoint: z5.object({ renderedContent: z5.string() }).nullish(),
|
932
|
+
groundingChunks: z5.array(groundingChunkSchema).nullish(),
|
933
|
+
groundingSupports: z5.array(
|
934
|
+
z5.object({
|
935
|
+
segment: z5.object({
|
936
|
+
startIndex: z5.number().nullish(),
|
937
|
+
endIndex: z5.number().nullish(),
|
938
|
+
text: z5.string().nullish()
|
849
939
|
}),
|
850
|
-
segment_text:
|
851
|
-
groundingChunkIndices:
|
852
|
-
supportChunkIndices:
|
853
|
-
confidenceScores:
|
854
|
-
confidenceScore:
|
940
|
+
segment_text: z5.string().nullish(),
|
941
|
+
groundingChunkIndices: z5.array(z5.number()).nullish(),
|
942
|
+
supportChunkIndices: z5.array(z5.number()).nullish(),
|
943
|
+
confidenceScores: z5.array(z5.number()).nullish(),
|
944
|
+
confidenceScore: z5.array(z5.number()).nullish()
|
855
945
|
})
|
856
946
|
).nullish(),
|
857
|
-
retrievalMetadata:
|
858
|
-
|
859
|
-
webDynamicRetrievalScore:
|
947
|
+
retrievalMetadata: z5.union([
|
948
|
+
z5.object({
|
949
|
+
webDynamicRetrievalScore: z5.number()
|
860
950
|
}),
|
861
|
-
|
951
|
+
z5.object({})
|
862
952
|
]).nullish()
|
863
953
|
});
|
864
|
-
var safetyRatingSchema =
|
865
|
-
category:
|
866
|
-
probability:
|
867
|
-
probabilityScore:
|
868
|
-
severity:
|
869
|
-
severityScore:
|
870
|
-
blocked:
|
954
|
+
var safetyRatingSchema = z5.object({
|
955
|
+
category: z5.string().nullish(),
|
956
|
+
probability: z5.string().nullish(),
|
957
|
+
probabilityScore: z5.number().nullish(),
|
958
|
+
severity: z5.string().nullish(),
|
959
|
+
severityScore: z5.number().nullish(),
|
960
|
+
blocked: z5.boolean().nullish()
|
961
|
+
});
|
962
|
+
var usageSchema = z5.object({
|
963
|
+
cachedContentTokenCount: z5.number().nullish(),
|
964
|
+
thoughtsTokenCount: z5.number().nullish(),
|
965
|
+
promptTokenCount: z5.number().nullish(),
|
966
|
+
candidatesTokenCount: z5.number().nullish(),
|
967
|
+
totalTokenCount: z5.number().nullish()
|
871
968
|
});
|
872
|
-
var responseSchema =
|
873
|
-
candidates:
|
874
|
-
|
875
|
-
content: contentSchema.nullish().or(
|
876
|
-
finishReason:
|
877
|
-
safetyRatings:
|
969
|
+
var responseSchema = z5.object({
|
970
|
+
candidates: z5.array(
|
971
|
+
z5.object({
|
972
|
+
content: contentSchema.nullish().or(z5.object({}).strict()),
|
973
|
+
finishReason: z5.string().nullish(),
|
974
|
+
safetyRatings: z5.array(safetyRatingSchema).nullish(),
|
878
975
|
groundingMetadata: groundingMetadataSchema.nullish()
|
879
976
|
})
|
880
977
|
),
|
881
|
-
usageMetadata:
|
882
|
-
promptTokenCount: z3.number().nullish(),
|
883
|
-
candidatesTokenCount: z3.number().nullish(),
|
884
|
-
totalTokenCount: z3.number().nullish()
|
885
|
-
}).nullish()
|
978
|
+
usageMetadata: usageSchema.nullish()
|
886
979
|
});
|
887
|
-
var chunkSchema =
|
888
|
-
candidates:
|
889
|
-
|
980
|
+
var chunkSchema = z5.object({
|
981
|
+
candidates: z5.array(
|
982
|
+
z5.object({
|
890
983
|
content: contentSchema.nullish(),
|
891
|
-
finishReason:
|
892
|
-
safetyRatings:
|
984
|
+
finishReason: z5.string().nullish(),
|
985
|
+
safetyRatings: z5.array(safetyRatingSchema).nullish(),
|
893
986
|
groundingMetadata: groundingMetadataSchema.nullish()
|
894
987
|
})
|
895
988
|
).nullish(),
|
896
|
-
usageMetadata:
|
897
|
-
promptTokenCount: z3.number().nullish(),
|
898
|
-
candidatesTokenCount: z3.number().nullish(),
|
899
|
-
totalTokenCount: z3.number().nullish()
|
900
|
-
}).nullish()
|
989
|
+
usageMetadata: usageSchema.nullish()
|
901
990
|
});
|
902
|
-
var googleGenerativeAIProviderOptionsSchema = z3.object({
|
903
|
-
responseModalities: z3.array(z3.enum(["TEXT", "IMAGE"])).nullish()
|
904
|
-
});
|
905
|
-
|
906
|
-
// src/google-supported-file-url.ts
|
907
|
-
function isSupportedFileUrl(url) {
|
908
|
-
return url.toString().startsWith("https://generativelanguage.googleapis.com/v1beta/files/");
|
909
|
-
}
|
910
991
|
|
911
992
|
// src/google-provider.ts
|
912
993
|
function createGoogleGenerativeAI(options = {}) {
|
@@ -920,30 +1001,35 @@ function createGoogleGenerativeAI(options = {}) {
|
|
920
1001
|
}),
|
921
1002
|
...options.headers
|
922
1003
|
});
|
923
|
-
const createChatModel = (modelId
|
1004
|
+
const createChatModel = (modelId) => {
|
924
1005
|
var _a2;
|
925
|
-
return new GoogleGenerativeAILanguageModel(modelId,
|
1006
|
+
return new GoogleGenerativeAILanguageModel(modelId, {
|
926
1007
|
provider: "google.generative-ai",
|
927
1008
|
baseURL,
|
928
1009
|
headers: getHeaders,
|
929
1010
|
generateId: (_a2 = options.generateId) != null ? _a2 : generateId,
|
930
|
-
|
1011
|
+
supportedUrls: () => ({
|
1012
|
+
"*": [
|
1013
|
+
// HTTP URLs:
|
1014
|
+
/^https?:\/\/.*$/
|
1015
|
+
]
|
1016
|
+
}),
|
931
1017
|
fetch: options.fetch
|
932
1018
|
});
|
933
1019
|
};
|
934
|
-
const createEmbeddingModel = (modelId
|
1020
|
+
const createEmbeddingModel = (modelId) => new GoogleGenerativeAIEmbeddingModel(modelId, {
|
935
1021
|
provider: "google.generative-ai",
|
936
1022
|
baseURL,
|
937
1023
|
headers: getHeaders,
|
938
1024
|
fetch: options.fetch
|
939
1025
|
});
|
940
|
-
const provider = function(modelId
|
1026
|
+
const provider = function(modelId) {
|
941
1027
|
if (new.target) {
|
942
1028
|
throw new Error(
|
943
1029
|
"The Google Generative AI model function cannot be called with the new keyword."
|
944
1030
|
);
|
945
1031
|
}
|
946
|
-
return createChatModel(modelId
|
1032
|
+
return createChatModel(modelId);
|
947
1033
|
};
|
948
1034
|
provider.languageModel = createChatModel;
|
949
1035
|
provider.chat = createChatModel;
|