@langchain/google-common 2.1.17 → 2.1.19
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 +18 -0
- package/dist/auth.cjs +1 -2
- package/dist/auth.cjs.map +1 -1
- package/dist/auth.d.cts.map +1 -1
- package/dist/auth.d.ts.map +1 -1
- package/dist/auth.js +1 -2
- package/dist/auth.js.map +1 -1
- package/dist/chat_models.cjs +24 -27
- package/dist/chat_models.cjs.map +1 -1
- package/dist/chat_models.d.cts.map +1 -1
- package/dist/chat_models.d.ts.map +1 -1
- package/dist/chat_models.js +7 -9
- package/dist/chat_models.js.map +1 -1
- package/dist/connection.cjs +16 -26
- package/dist/connection.cjs.map +1 -1
- package/dist/connection.d.cts.map +1 -1
- package/dist/connection.d.ts.map +1 -1
- package/dist/connection.js +13 -22
- package/dist/connection.js.map +1 -1
- package/dist/embeddings.cjs +12 -22
- package/dist/embeddings.cjs.map +1 -1
- package/dist/embeddings.d.cts +0 -1
- package/dist/embeddings.d.cts.map +1 -1
- package/dist/embeddings.d.ts +0 -1
- package/dist/embeddings.d.ts.map +1 -1
- package/dist/embeddings.js +7 -16
- package/dist/embeddings.js.map +1 -1
- package/dist/experimental/media.cjs +41 -70
- package/dist/experimental/media.cjs.map +1 -1
- package/dist/experimental/media.d.cts.map +1 -1
- package/dist/experimental/media.d.ts.map +1 -1
- package/dist/experimental/media.js +36 -65
- package/dist/experimental/media.js.map +1 -1
- package/dist/experimental/utils/media_core.cjs +22 -39
- package/dist/experimental/utils/media_core.cjs.map +1 -1
- package/dist/experimental/utils/media_core.d.cts.map +1 -1
- package/dist/experimental/utils/media_core.d.ts.map +1 -1
- package/dist/experimental/utils/media_core.js +16 -33
- package/dist/experimental/utils/media_core.js.map +1 -1
- package/dist/index.cjs +1 -0
- package/dist/llms.cjs +14 -19
- package/dist/llms.cjs.map +1 -1
- package/dist/llms.d.cts.map +1 -1
- package/dist/llms.d.ts.map +1 -1
- package/dist/llms.js +4 -8
- package/dist/llms.js.map +1 -1
- package/dist/output_parsers.cjs +12 -29
- package/dist/output_parsers.cjs.map +1 -1
- package/dist/output_parsers.d.cts.map +1 -1
- package/dist/output_parsers.d.ts.map +1 -1
- package/dist/output_parsers.js +10 -26
- package/dist/output_parsers.js.map +1 -1
- package/dist/profiles.cjs +100 -17
- package/dist/profiles.cjs.map +1 -1
- package/dist/profiles.js +100 -17
- package/dist/profiles.js.map +1 -1
- package/dist/types-anthropic.d.cts.map +1 -1
- package/dist/types-anthropic.d.ts.map +1 -1
- package/dist/types.cjs +1 -0
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +0 -1
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.ts +0 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/utils/anthropic.cjs +73 -113
- package/dist/utils/anthropic.cjs.map +1 -1
- package/dist/utils/anthropic.js +63 -102
- package/dist/utils/anthropic.js.map +1 -1
- package/dist/utils/common.cjs +10 -13
- package/dist/utils/common.cjs.map +1 -1
- package/dist/utils/common.d.cts.map +1 -1
- package/dist/utils/common.d.ts.map +1 -1
- package/dist/utils/common.js +6 -8
- package/dist/utils/common.js.map +1 -1
- package/dist/utils/failed_handler.cjs +1 -2
- package/dist/utils/failed_handler.cjs.map +1 -1
- package/dist/utils/failed_handler.d.cts.map +1 -1
- package/dist/utils/failed_handler.d.ts.map +1 -1
- package/dist/utils/failed_handler.js +1 -2
- package/dist/utils/failed_handler.js.map +1 -1
- package/dist/utils/gemini.cjs +89 -135
- package/dist/utils/gemini.cjs.map +1 -1
- package/dist/utils/gemini.d.cts.map +1 -1
- package/dist/utils/gemini.d.ts.map +1 -1
- package/dist/utils/gemini.js +64 -109
- package/dist/utils/gemini.js.map +1 -1
- package/dist/utils/index.cjs +1 -0
- package/dist/utils/palm.d.cts.map +1 -1
- package/dist/utils/palm.d.ts.map +1 -1
- package/dist/utils/safety.cjs.map +1 -1
- package/dist/utils/safety.d.cts.map +1 -1
- package/dist/utils/safety.d.ts.map +1 -1
- package/dist/utils/safety.js.map +1 -1
- package/dist/utils/stream.cjs +5 -9
- package/dist/utils/stream.cjs.map +1 -1
- package/dist/utils/stream.d.cts.map +1 -1
- package/dist/utils/stream.d.ts.map +1 -1
- package/dist/utils/stream.js +5 -9
- package/dist/utils/stream.js.map +1 -1
- package/dist/utils/zod_to_gemini_parameters.cjs +4 -7
- package/dist/utils/zod_to_gemini_parameters.cjs.map +1 -1
- package/dist/utils/zod_to_gemini_parameters.d.cts.map +1 -1
- package/dist/utils/zod_to_gemini_parameters.d.ts.map +1 -1
- package/dist/utils/zod_to_gemini_parameters.js +2 -4
- package/dist/utils/zod_to_gemini_parameters.js.map +1 -1
- package/package.json +5 -5
- package/dist/_virtual/rolldown_runtime.cjs +0 -25
package/dist/utils/gemini.cjs
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
const require_rolldown_runtime = require('../_virtual/rolldown_runtime.cjs');
|
|
2
1
|
const require_types = require('../types.cjs');
|
|
3
2
|
const require_safety = require('./safety.cjs');
|
|
4
3
|
const require_zod_to_gemini_parameters = require('./zod_to_gemini_parameters.cjs');
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
4
|
+
let _langchain_core_outputs = require("@langchain/core/outputs");
|
|
5
|
+
let _langchain_core_messages = require("@langchain/core/messages");
|
|
6
|
+
let _langchain_core_utils_stream = require("@langchain/core/utils/stream");
|
|
7
|
+
let _langchain_core_utils_function_calling = require("@langchain/core/utils/function_calling");
|
|
8
|
+
let uuid = require("uuid");
|
|
10
9
|
|
|
11
10
|
//#region src/utils/gemini.ts
|
|
12
11
|
var DefaultGeminiSafetyHandler = class {
|
|
@@ -19,14 +18,12 @@ var DefaultGeminiSafetyHandler = class {
|
|
|
19
18
|
this.errorFinish = settings?.errorFinish ?? this.errorFinish;
|
|
20
19
|
}
|
|
21
20
|
handleDataPromptFeedback(response, data) {
|
|
22
|
-
const
|
|
23
|
-
const blockReason = promptFeedback?.blockReason;
|
|
21
|
+
const blockReason = (data?.promptFeedback)?.blockReason;
|
|
24
22
|
if (blockReason) throw new require_safety.GoogleAISafetyError(response, `Prompt blocked: ${blockReason}`);
|
|
25
23
|
return data;
|
|
26
24
|
}
|
|
27
25
|
handleDataFinishReason(response, data) {
|
|
28
|
-
const
|
|
29
|
-
const finishReason = firstCandidate?.finishReason;
|
|
26
|
+
const finishReason = (data?.candidates?.[0])?.finishReason;
|
|
30
27
|
if (this.errorFinish.includes(finishReason)) throw new require_safety.GoogleAISafetyError(response, `Finish reason: ${finishReason}`);
|
|
31
28
|
return data;
|
|
32
29
|
}
|
|
@@ -112,29 +109,25 @@ function inferMimeTypeFromUrl(url) {
|
|
|
112
109
|
tif: "image/tiff"
|
|
113
110
|
};
|
|
114
111
|
try {
|
|
115
|
-
const
|
|
116
|
-
const extension = pathname.split(".").pop()?.toLowerCase().split(/[?#]/)[0];
|
|
112
|
+
const extension = new URL(url).pathname.split(".").pop()?.toLowerCase().split(/[?#]/)[0];
|
|
117
113
|
return extension ? mimeTypeMap[extension] : void 0;
|
|
118
114
|
} catch {
|
|
119
115
|
const match = url.match(/\.([a-zA-Z0-9]+)(?:[?#]|$)/);
|
|
120
|
-
if (match)
|
|
121
|
-
|
|
122
|
-
return mimeTypeMap[extension];
|
|
123
|
-
}
|
|
124
|
-
return void 0;
|
|
116
|
+
if (match) return mimeTypeMap[match[1].toLowerCase()];
|
|
117
|
+
return;
|
|
125
118
|
}
|
|
126
119
|
}
|
|
127
120
|
function normalizeSpeechConfig(config) {
|
|
128
|
-
function isSpeechConfig(config
|
|
129
|
-
return typeof config
|
|
121
|
+
function isSpeechConfig(config) {
|
|
122
|
+
return typeof config === "object" && (Object.hasOwn(config, "voiceConfig") || Object.hasOwn(config, "multiSpeakerVoiceConfig"));
|
|
130
123
|
}
|
|
131
|
-
function hasLanguage(config
|
|
132
|
-
return typeof config
|
|
124
|
+
function hasLanguage(config) {
|
|
125
|
+
return typeof config === "object" && Object.hasOwn(config, "languageCode");
|
|
133
126
|
}
|
|
134
|
-
function hasVoice(config
|
|
135
|
-
return Object.hasOwn(config
|
|
127
|
+
function hasVoice(config) {
|
|
128
|
+
return Object.hasOwn(config, "voice");
|
|
136
129
|
}
|
|
137
|
-
if (typeof config === "undefined") return
|
|
130
|
+
if (typeof config === "undefined") return;
|
|
138
131
|
if (isSpeechConfig(config)) return config;
|
|
139
132
|
let languageCode;
|
|
140
133
|
let voice;
|
|
@@ -147,14 +140,10 @@ function normalizeSpeechConfig(config) {
|
|
|
147
140
|
}
|
|
148
141
|
let ret;
|
|
149
142
|
if (typeof voice === "string") ret = { voiceConfig: { prebuiltVoiceConfig: { voiceName: voice } } };
|
|
150
|
-
else {
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
voiceConfig: { prebuiltVoiceConfig: { voiceName: v.name } }
|
|
155
|
-
}));
|
|
156
|
-
ret = { multiSpeakerVoiceConfig: { speakerVoiceConfigs } };
|
|
157
|
-
}
|
|
143
|
+
else ret = { multiSpeakerVoiceConfig: { speakerVoiceConfigs: (Array.isArray(voice) ? voice : [voice]).map((v) => ({
|
|
144
|
+
speaker: v.speaker,
|
|
145
|
+
voiceConfig: { prebuiltVoiceConfig: { voiceName: v.name } }
|
|
146
|
+
})) } };
|
|
158
147
|
if (languageCode) ret.languageCode = languageCode;
|
|
159
148
|
return ret;
|
|
160
149
|
}
|
|
@@ -168,13 +157,10 @@ function getGeminiAPI(config) {
|
|
|
168
157
|
if (!url) throw new Error("Missing Image URL");
|
|
169
158
|
const mimeTypeAndData = extractMimeType(url);
|
|
170
159
|
if (mimeTypeAndData) return { inlineData: mimeTypeAndData };
|
|
171
|
-
else {
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
fileUri: url
|
|
176
|
-
} };
|
|
177
|
-
}
|
|
160
|
+
else return { fileData: {
|
|
161
|
+
mimeType: inferMimeTypeFromUrl(url) || "image/png",
|
|
162
|
+
fileUri: url
|
|
163
|
+
} };
|
|
178
164
|
}
|
|
179
165
|
function messageContentImageUrl(content) {
|
|
180
166
|
const ret = messageContentImageUrlData(content);
|
|
@@ -229,7 +215,7 @@ function getGeminiAPI(config) {
|
|
|
229
215
|
},
|
|
230
216
|
fromStandardImageBlock(block) {
|
|
231
217
|
if (block.source_type === "url") {
|
|
232
|
-
const data = (0,
|
|
218
|
+
const data = (0, _langchain_core_messages.parseBase64DataUrl)({ dataUrl: block.url });
|
|
233
219
|
if (data) return { inlineData: {
|
|
234
220
|
mimeType: data.mime_type,
|
|
235
221
|
data: data.data
|
|
@@ -251,7 +237,7 @@ function getGeminiAPI(config) {
|
|
|
251
237
|
},
|
|
252
238
|
fromStandardAudioBlock(block) {
|
|
253
239
|
if (block.source_type === "url") {
|
|
254
|
-
const data = (0,
|
|
240
|
+
const data = (0, _langchain_core_messages.parseBase64DataUrl)({ dataUrl: block.url });
|
|
255
241
|
if (data) return { inlineData: {
|
|
256
242
|
mimeType: data.mime_type,
|
|
257
243
|
data: data.data
|
|
@@ -270,7 +256,7 @@ function getGeminiAPI(config) {
|
|
|
270
256
|
fromStandardFileBlock(block) {
|
|
271
257
|
if (block.source_type === "text") return { text: block.text };
|
|
272
258
|
if (block.source_type === "url") {
|
|
273
|
-
const data = (0,
|
|
259
|
+
const data = (0, _langchain_core_messages.parseBase64DataUrl)({ dataUrl: block.url });
|
|
274
260
|
if (data) return { inlineData: {
|
|
275
261
|
mimeType: data.mime_type,
|
|
276
262
|
data: data.data
|
|
@@ -297,25 +283,28 @@ function getGeminiAPI(config) {
|
|
|
297
283
|
break;
|
|
298
284
|
case "media": return await messageContentMedia(content);
|
|
299
285
|
case "reasoning": return messageContentReasoning(content);
|
|
286
|
+
case "input_audio":
|
|
287
|
+
if ("input_audio" in content) return { inlineData: {
|
|
288
|
+
mimeType: `audio/${content.input_audio.format}`,
|
|
289
|
+
data: content.input_audio.data
|
|
290
|
+
} };
|
|
291
|
+
break;
|
|
300
292
|
default: throw new Error(`Unsupported type "${content.type}" received while converting message to message parts: ${JSON.stringify(content)}`);
|
|
301
293
|
}
|
|
302
294
|
throw new Error(`Cannot coerce "${content.type}" message part into a string.`);
|
|
303
295
|
}
|
|
304
296
|
async function messageContentComplexToParts(content) {
|
|
305
|
-
const contents = content.map((m) => (0,
|
|
297
|
+
const contents = content.map((m) => (0, _langchain_core_messages.isDataContentBlock)(m) ? (0, _langchain_core_messages.convertToProviderContentBlock)(m, standardContentBlockConverter) : messageContentComplexToPart(m));
|
|
306
298
|
return Promise.all(contents);
|
|
307
299
|
}
|
|
308
300
|
async function messageContentToParts(content) {
|
|
309
|
-
|
|
301
|
+
return (await messageContentComplexToParts(typeof content === "string" ? [{
|
|
310
302
|
type: "text",
|
|
311
303
|
text: content
|
|
312
|
-
}] : content
|
|
313
|
-
const allParts = await messageContentComplexToParts(messageContent);
|
|
314
|
-
const parts = allParts.reduce((acc, val) => {
|
|
304
|
+
}] : content)).reduce((acc, val) => {
|
|
315
305
|
if (val) return [...acc, val];
|
|
316
306
|
else return acc;
|
|
317
307
|
}, []);
|
|
318
|
-
return parts;
|
|
319
308
|
}
|
|
320
309
|
function messageToolCallsToParts(toolCalls) {
|
|
321
310
|
if (!toolCalls || toolCalls.length === 0) return [];
|
|
@@ -339,7 +328,7 @@ function getGeminiAPI(config) {
|
|
|
339
328
|
async function roleMessageToContent(role, message) {
|
|
340
329
|
const contentParts = await messageContentToParts(message.content);
|
|
341
330
|
let toolParts;
|
|
342
|
-
if ((0,
|
|
331
|
+
if ((0, _langchain_core_messages.isAIMessage)(message) && !!message.tool_calls?.length) toolParts = message.tool_calls.map((toolCall) => ({ functionCall: {
|
|
343
332
|
name: toolCall.name,
|
|
344
333
|
args: toolCall.args
|
|
345
334
|
} }));
|
|
@@ -356,21 +345,20 @@ function getGeminiAPI(config) {
|
|
|
356
345
|
}];
|
|
357
346
|
}
|
|
358
347
|
async function systemMessageToContent(message) {
|
|
359
|
-
return config?.useSystemInstruction ? roleMessageToContent("system", message) : [...await roleMessageToContent("user", message), ...await roleMessageToContent("model", new
|
|
348
|
+
return config?.useSystemInstruction ? roleMessageToContent("system", message) : [...await roleMessageToContent("user", message), ...await roleMessageToContent("model", new _langchain_core_messages.AIMessage("Ok"))];
|
|
360
349
|
}
|
|
361
350
|
function toolMessageToContent(message, prevMessage) {
|
|
362
351
|
const contentStr = typeof message.content === "string" ? message.content : message.content.reduce((acc, content) => {
|
|
363
352
|
if (content.type === "text") return acc + content.text;
|
|
364
353
|
else return acc;
|
|
365
354
|
}, "");
|
|
366
|
-
const responseName = ((0,
|
|
355
|
+
const responseName = ((0, _langchain_core_messages.isAIMessage)(prevMessage) && !!prevMessage.tool_calls?.length ? prevMessage.tool_calls[0].name : prevMessage.name) ?? message.tool_call_id;
|
|
367
356
|
try {
|
|
368
|
-
const content = JSON.parse(contentStr);
|
|
369
357
|
return [{
|
|
370
358
|
role: "function",
|
|
371
359
|
parts: [{ functionResponse: {
|
|
372
360
|
name: responseName,
|
|
373
|
-
response: { content }
|
|
361
|
+
response: { content: JSON.parse(contentStr) }
|
|
374
362
|
} }]
|
|
375
363
|
}];
|
|
376
364
|
} catch (_) {
|
|
@@ -384,10 +372,10 @@ function getGeminiAPI(config) {
|
|
|
384
372
|
}
|
|
385
373
|
}
|
|
386
374
|
async function baseMessageToContent(message, prevMessage) {
|
|
387
|
-
if (
|
|
388
|
-
else if (
|
|
389
|
-
else if (
|
|
390
|
-
else if (
|
|
375
|
+
if (_langchain_core_messages.SystemMessage.isInstance(message)) return systemMessageToContent(message);
|
|
376
|
+
else if (_langchain_core_messages.HumanMessage.isInstance(message)) return roleMessageToContent("user", message);
|
|
377
|
+
else if (_langchain_core_messages.AIMessage.isInstance(message)) return roleMessageToContent("model", message);
|
|
378
|
+
else if (_langchain_core_messages.ToolMessage.isInstance(message)) {
|
|
391
379
|
if (!prevMessage) throw new Error("Tool messages cannot be the first message passed to the model.");
|
|
392
380
|
return toolMessageToContent(message, prevMessage);
|
|
393
381
|
} else {
|
|
@@ -421,8 +409,7 @@ function getGeminiAPI(config) {
|
|
|
421
409
|
};
|
|
422
410
|
}
|
|
423
411
|
function inlineDataPartToMessageContent(part) {
|
|
424
|
-
|
|
425
|
-
if (mimeType.startsWith("image")) return inlineDataPartToMessageContentImage(part);
|
|
412
|
+
if ((part?.inlineData?.mimeType ?? "").startsWith("image")) return inlineDataPartToMessageContentImage(part);
|
|
426
413
|
else return inlineDataPartToMessageContentMedia(part);
|
|
427
414
|
}
|
|
428
415
|
function fileDataPartToMessageContent(part) {
|
|
@@ -488,31 +475,22 @@ function getGeminiAPI(config) {
|
|
|
488
475
|
else return response.data;
|
|
489
476
|
}
|
|
490
477
|
function responseToParts(response) {
|
|
491
|
-
|
|
492
|
-
const parts = responseData?.candidates?.[0]?.content?.parts ?? [];
|
|
493
|
-
return parts;
|
|
478
|
+
return responseToGenerateContentResponseData(response)?.candidates?.[0]?.content?.parts ?? [];
|
|
494
479
|
}
|
|
495
480
|
function partToText(part) {
|
|
496
481
|
return "text" in part ? part.text : "";
|
|
497
482
|
}
|
|
498
483
|
function responseToString(response) {
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
const val = partToText(part);
|
|
502
|
-
return acc + val;
|
|
484
|
+
return responseToParts(response).reduce((acc, part) => {
|
|
485
|
+
return acc + partToText(part);
|
|
503
486
|
}, "");
|
|
504
|
-
return ret;
|
|
505
487
|
}
|
|
506
488
|
function safeResponseTo(response, responseTo) {
|
|
507
489
|
const safetyHandler = config?.safetyHandler ?? new DefaultGeminiSafetyHandler();
|
|
508
490
|
try {
|
|
509
|
-
|
|
510
|
-
return responseTo(safeResponse);
|
|
491
|
+
return responseTo(safetyHandler.handle(response));
|
|
511
492
|
} catch (xx) {
|
|
512
|
-
if (xx instanceof require_safety.GoogleAISafetyError)
|
|
513
|
-
const ret = responseTo(xx.response);
|
|
514
|
-
xx.reply = ret;
|
|
515
|
-
}
|
|
493
|
+
if (xx instanceof require_safety.GoogleAISafetyError) xx.reply = responseTo(xx.response);
|
|
516
494
|
throw xx;
|
|
517
495
|
}
|
|
518
496
|
}
|
|
@@ -523,11 +501,10 @@ function getGeminiAPI(config) {
|
|
|
523
501
|
const token = result?.token;
|
|
524
502
|
const logprob = result?.logProbability;
|
|
525
503
|
const encoder = new TextEncoder();
|
|
526
|
-
const bytes = Array.from(encoder.encode(token));
|
|
527
504
|
return {
|
|
528
505
|
token,
|
|
529
506
|
logprob,
|
|
530
|
-
bytes
|
|
507
|
+
bytes: Array.from(encoder.encode(token))
|
|
531
508
|
};
|
|
532
509
|
}
|
|
533
510
|
function candidateToLogprobs(candidate) {
|
|
@@ -548,25 +525,21 @@ function getGeminiAPI(config) {
|
|
|
548
525
|
const retrieval = candidate?.urlRetrievalMetadata?.urlRetrievalContexts ?? [];
|
|
549
526
|
const context = candidate?.urlContextMetadata?.urlMetadata ?? [];
|
|
550
527
|
const all = [...retrieval, ...context];
|
|
551
|
-
if (all.length === 0) return
|
|
528
|
+
if (all.length === 0) return;
|
|
552
529
|
else return { urlMetadata: all };
|
|
553
530
|
}
|
|
554
531
|
function addModalityCounts(modalityTokenCounts, details) {
|
|
555
532
|
modalityTokenCounts?.forEach((modalityTokenCount) => {
|
|
556
533
|
const { modality, tokenCount } = modalityTokenCount;
|
|
557
534
|
const modalityLc = modality.toLowerCase();
|
|
558
|
-
|
|
559
|
-
details[modalityLc] = currentCount + tokenCount;
|
|
535
|
+
details[modalityLc] = (details[modalityLc] ?? 0) + tokenCount;
|
|
560
536
|
});
|
|
561
537
|
}
|
|
562
538
|
function responseToUsageMetadata(response) {
|
|
563
539
|
if ("usageMetadata" in response.data) {
|
|
564
|
-
const
|
|
565
|
-
const usageMetadata = data?.usageMetadata;
|
|
540
|
+
const usageMetadata = (response?.data)?.usageMetadata;
|
|
566
541
|
const input_tokens = usageMetadata.promptTokenCount ?? 0;
|
|
567
|
-
const
|
|
568
|
-
const thoughtsTokenCount = usageMetadata.thoughtsTokenCount ?? 0;
|
|
569
|
-
const output_tokens = candidatesTokenCount + thoughtsTokenCount;
|
|
542
|
+
const output_tokens = (usageMetadata.candidatesTokenCount ?? 0) + (usageMetadata.thoughtsTokenCount ?? 0);
|
|
570
543
|
const total_tokens = usageMetadata.totalTokenCount ?? input_tokens + output_tokens;
|
|
571
544
|
const input_token_details = {};
|
|
572
545
|
addModalityCounts(usageMetadata.promptTokensDetails, input_token_details);
|
|
@@ -574,16 +547,14 @@ function getGeminiAPI(config) {
|
|
|
574
547
|
const output_token_details = {};
|
|
575
548
|
addModalityCounts(usageMetadata?.candidatesTokensDetails, output_token_details);
|
|
576
549
|
if (typeof usageMetadata?.thoughtsTokenCount === "number") output_token_details.reasoning = usageMetadata.thoughtsTokenCount;
|
|
577
|
-
|
|
550
|
+
return {
|
|
578
551
|
input_tokens,
|
|
579
552
|
output_tokens,
|
|
580
553
|
total_tokens,
|
|
581
554
|
input_token_details,
|
|
582
555
|
output_token_details
|
|
583
556
|
};
|
|
584
|
-
return ret;
|
|
585
557
|
}
|
|
586
|
-
return void 0;
|
|
587
558
|
}
|
|
588
559
|
function responseToGenerationInfo(response) {
|
|
589
560
|
const data = Array.isArray(response.data) && response.data[0] ? response.data[0] : response.data && response.data.candidates ? response.data : void 0;
|
|
@@ -609,7 +580,7 @@ function getGeminiAPI(config) {
|
|
|
609
580
|
return ret;
|
|
610
581
|
}
|
|
611
582
|
function responseToChatGeneration(response) {
|
|
612
|
-
return new
|
|
583
|
+
return new _langchain_core_outputs.ChatGenerationChunk({
|
|
613
584
|
text: responseToString(response),
|
|
614
585
|
message: partToMessageChunk(responseToParts(response)[0]),
|
|
615
586
|
generationInfo: responseToGenerationInfo(response)
|
|
@@ -627,10 +598,10 @@ function getGeminiAPI(config) {
|
|
|
627
598
|
}
|
|
628
599
|
function partToMessageChunk(part) {
|
|
629
600
|
const fields = partsToBaseMessageChunkFields([part]);
|
|
630
|
-
if (typeof fields.content === "string") return new
|
|
601
|
+
if (typeof fields.content === "string") return new _langchain_core_messages.AIMessageChunk(fields);
|
|
631
602
|
else if (fields.content?.every((item) => item.type === "text")) {
|
|
632
603
|
const newContent = fields.content.map((item) => "text" in item ? item.text : "").join("");
|
|
633
|
-
return new
|
|
604
|
+
return new _langchain_core_messages.AIMessageChunk({
|
|
634
605
|
...fields,
|
|
635
606
|
content: newContent,
|
|
636
607
|
response_metadata: {
|
|
@@ -639,24 +610,21 @@ function getGeminiAPI(config) {
|
|
|
639
610
|
}
|
|
640
611
|
});
|
|
641
612
|
}
|
|
642
|
-
return new
|
|
613
|
+
return new _langchain_core_messages.AIMessageChunk(fields);
|
|
643
614
|
}
|
|
644
615
|
function partToChatGeneration(part) {
|
|
645
616
|
const message = partToMessageChunk(part);
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
return new __langchain_core_outputs.ChatGenerationChunk({
|
|
649
|
-
text,
|
|
617
|
+
return new _langchain_core_outputs.ChatGenerationChunk({
|
|
618
|
+
text: partToText(part),
|
|
650
619
|
message,
|
|
651
|
-
generationInfo
|
|
620
|
+
generationInfo: {}
|
|
652
621
|
});
|
|
653
622
|
}
|
|
654
623
|
function groundingSupportByPart(groundingSupports) {
|
|
655
624
|
const ret = [];
|
|
656
625
|
if (!groundingSupports || groundingSupports.length === 0) return [];
|
|
657
626
|
groundingSupports?.forEach((groundingSupport) => {
|
|
658
|
-
const
|
|
659
|
-
const partIndex = segment?.partIndex ?? 0;
|
|
627
|
+
const partIndex = (groundingSupport?.segment)?.partIndex ?? 0;
|
|
660
628
|
if (ret[partIndex]) ret[partIndex].push(groundingSupport);
|
|
661
629
|
else ret[partIndex] = [groundingSupport];
|
|
662
630
|
});
|
|
@@ -669,7 +637,7 @@ function getGeminiAPI(config) {
|
|
|
669
637
|
const groundingMetadata = candidate?.groundingMetadata;
|
|
670
638
|
const citationMetadata = candidate?.citationMetadata;
|
|
671
639
|
const groundingParts = groundingSupportByPart(groundingMetadata?.groundingSupports);
|
|
672
|
-
|
|
640
|
+
return parts.map((part, index) => {
|
|
673
641
|
const gen = partToChatGeneration(part);
|
|
674
642
|
if (!gen.generationInfo) gen.generationInfo = {};
|
|
675
643
|
if (groundingMetadata) {
|
|
@@ -680,11 +648,9 @@ function getGeminiAPI(config) {
|
|
|
680
648
|
if (citationMetadata) gen.generationInfo.citationMetadata = citationMetadata;
|
|
681
649
|
return gen;
|
|
682
650
|
});
|
|
683
|
-
return ret;
|
|
684
651
|
}
|
|
685
652
|
function combineContent(gen, forceComplex = false) {
|
|
686
|
-
|
|
687
|
-
if (allString && !forceComplex) return gen.map((item) => item.message.content).join("");
|
|
653
|
+
if (gen.every((item) => typeof item.message.content === "string") && !forceComplex) return gen.map((item) => item.message.content).join("");
|
|
688
654
|
else {
|
|
689
655
|
const ret = [];
|
|
690
656
|
gen.forEach((item) => {
|
|
@@ -703,20 +669,18 @@ function getGeminiAPI(config) {
|
|
|
703
669
|
return gen.map((item) => item.text ?? "").join("");
|
|
704
670
|
}
|
|
705
671
|
function combineToolCalls(gen) {
|
|
706
|
-
let ret = new
|
|
672
|
+
let ret = new _langchain_core_messages.AIMessageChunk("");
|
|
707
673
|
gen.forEach((item) => {
|
|
708
674
|
const message = item?.message;
|
|
709
|
-
ret = (0,
|
|
675
|
+
ret = (0, _langchain_core_utils_stream.concat)(ret, message);
|
|
710
676
|
});
|
|
711
677
|
return ret;
|
|
712
678
|
}
|
|
713
679
|
function combineAdditionalKwargs(gen) {
|
|
714
680
|
const ret = {};
|
|
715
681
|
gen.forEach((item) => {
|
|
716
|
-
const
|
|
717
|
-
|
|
718
|
-
const keys = Object.keys(kwargs);
|
|
719
|
-
keys.forEach((key) => {
|
|
682
|
+
const kwargs = (item?.message)?.additional_kwargs ?? {};
|
|
683
|
+
Object.keys(kwargs).forEach((key) => {
|
|
720
684
|
const value = kwargs[key];
|
|
721
685
|
if (Object.hasOwn(ret, key) && Array.isArray(ret[key]) && Array.isArray(value)) ret[key].push(...value);
|
|
722
686
|
else ret[key] = value;
|
|
@@ -731,17 +695,15 @@ function getGeminiAPI(config) {
|
|
|
731
695
|
const combinedToolCalls = combineToolCalls(gen.content);
|
|
732
696
|
const kwargs = combineAdditionalKwargs(gen.content);
|
|
733
697
|
const lastContent = gen.content[gen.content.length - 1];
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
return [new __langchain_core_outputs.ChatGenerationChunk({
|
|
744
|
-
message,
|
|
698
|
+
return [new _langchain_core_outputs.ChatGenerationChunk({
|
|
699
|
+
message: new _langchain_core_messages.AIMessageChunk({
|
|
700
|
+
content: combinedContent,
|
|
701
|
+
additional_kwargs: kwargs,
|
|
702
|
+
response_metadata: { model_provider: "google-vertexai" },
|
|
703
|
+
usage_metadata: responseToUsageMetadata(response),
|
|
704
|
+
tool_calls: combinedToolCalls.tool_calls,
|
|
705
|
+
invalid_tool_calls: combinedToolCalls.invalid_tool_calls
|
|
706
|
+
}),
|
|
745
707
|
text: combinedText,
|
|
746
708
|
generationInfo: lastContent.generationInfo
|
|
747
709
|
})];
|
|
@@ -780,8 +742,7 @@ function getGeminiAPI(config) {
|
|
|
780
742
|
return ret;
|
|
781
743
|
}
|
|
782
744
|
function responseToBaseMessageFields(response) {
|
|
783
|
-
|
|
784
|
-
return partsToBaseMessageChunkFields(parts);
|
|
745
|
+
return partsToBaseMessageChunkFields(responseToParts(response));
|
|
785
746
|
}
|
|
786
747
|
function partsToSignatures(parts) {
|
|
787
748
|
return parts.map((part) => part?.thoughtSignature ?? "");
|
|
@@ -827,16 +788,14 @@ function getGeminiAPI(config) {
|
|
|
827
788
|
return fields;
|
|
828
789
|
}
|
|
829
790
|
function responseToBaseMessage(response) {
|
|
830
|
-
|
|
831
|
-
return new __langchain_core_messages.AIMessage(fields);
|
|
791
|
+
return new _langchain_core_messages.AIMessage(responseToBaseMessageFields(response));
|
|
832
792
|
}
|
|
833
793
|
function safeResponseToBaseMessage(response) {
|
|
834
794
|
return safeResponseTo(response, responseToBaseMessage);
|
|
835
795
|
}
|
|
836
796
|
function responseToChatResult(response) {
|
|
837
|
-
const generations = responseToChatGenerations(response);
|
|
838
797
|
return {
|
|
839
|
-
generations,
|
|
798
|
+
generations: responseToChatGenerations(response),
|
|
840
799
|
llmOutput: responseToGenerationInfo(response)
|
|
841
800
|
};
|
|
842
801
|
}
|
|
@@ -852,17 +811,14 @@ function getGeminiAPI(config) {
|
|
|
852
811
|
}
|
|
853
812
|
}
|
|
854
813
|
async function formatMessageContents(input, _parameters) {
|
|
855
|
-
|
|
856
|
-
const contents = [{
|
|
814
|
+
return [{
|
|
857
815
|
role: "user",
|
|
858
|
-
parts
|
|
816
|
+
parts: await messageContentToParts(input)
|
|
859
817
|
}];
|
|
860
|
-
return contents;
|
|
861
818
|
}
|
|
862
819
|
async function formatBaseMessageContents(input, _parameters) {
|
|
863
820
|
const inputPromises = input.map((msg, i) => baseMessageToContent(msg, input[i - 1]));
|
|
864
|
-
|
|
865
|
-
return inputs.reduce((acc, cur) => {
|
|
821
|
+
return (await Promise.all(inputPromises)).reduce((acc, cur) => {
|
|
866
822
|
if (cur.every((content) => content.role === "system")) return acc;
|
|
867
823
|
if (cur[0]?.role === "function" && acc.length > 0 && acc[acc.length - 1].role === "function") acc[acc.length - 1].parts = [...acc[acc.length - 1].parts, ...cur[0].parts];
|
|
868
824
|
else acc.push(...cur);
|
|
@@ -931,8 +887,7 @@ function getGeminiAPI(config) {
|
|
|
931
887
|
}
|
|
932
888
|
async function formatSystemInstruction(input) {
|
|
933
889
|
if (!config?.useSystemInstruction) return {};
|
|
934
|
-
|
|
935
|
-
switch (it) {
|
|
890
|
+
switch (inputType(input)) {
|
|
936
891
|
case "BaseMessageArray": return formatBaseMessageSystemInstruction(input);
|
|
937
892
|
default: return {};
|
|
938
893
|
}
|
|
@@ -947,7 +902,6 @@ function getGeminiAPI(config) {
|
|
|
947
902
|
}
|
|
948
903
|
function searchToolName(tool) {
|
|
949
904
|
for (const name of require_types.GeminiSearchToolAttributes) if (name in tool) return name;
|
|
950
|
-
return void 0;
|
|
951
905
|
}
|
|
952
906
|
function cleanGeminiTool(tool) {
|
|
953
907
|
const orig = searchToolName(tool);
|
|
@@ -961,7 +915,7 @@ function getGeminiAPI(config) {
|
|
|
961
915
|
const langChainTools = [];
|
|
962
916
|
const otherTools = [];
|
|
963
917
|
tools.forEach((tool) => {
|
|
964
|
-
if ((0,
|
|
918
|
+
if ((0, _langchain_core_utils_function_calling.isLangChainTool)(tool)) langChainTools.push(tool);
|
|
965
919
|
else otherTools.push(cleanGeminiTool(tool));
|
|
966
920
|
});
|
|
967
921
|
const result = [...otherTools];
|
|
@@ -969,7 +923,7 @@ function getGeminiAPI(config) {
|
|
|
969
923
|
return result;
|
|
970
924
|
}
|
|
971
925
|
function formatToolConfig(parameters) {
|
|
972
|
-
if (!parameters.tool_choice || typeof parameters.tool_choice !== "string") return
|
|
926
|
+
if (!parameters.tool_choice || typeof parameters.tool_choice !== "string") return;
|
|
973
927
|
if ([
|
|
974
928
|
"auto",
|
|
975
929
|
"any",
|