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