@langchain/google-common 2.1.18 → 2.1.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 +16 -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 +83 -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 +58 -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 +3 -3
- 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
|
|
@@ -308,20 +294,17 @@ function getGeminiAPI(config) {
|
|
|
308
294
|
throw new Error(`Cannot coerce "${content.type}" message part into a string.`);
|
|
309
295
|
}
|
|
310
296
|
async function messageContentComplexToParts(content) {
|
|
311
|
-
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));
|
|
312
298
|
return Promise.all(contents);
|
|
313
299
|
}
|
|
314
300
|
async function messageContentToParts(content) {
|
|
315
|
-
|
|
301
|
+
return (await messageContentComplexToParts(typeof content === "string" ? [{
|
|
316
302
|
type: "text",
|
|
317
303
|
text: content
|
|
318
|
-
}] : content
|
|
319
|
-
const allParts = await messageContentComplexToParts(messageContent);
|
|
320
|
-
const parts = allParts.reduce((acc, val) => {
|
|
304
|
+
}] : content)).reduce((acc, val) => {
|
|
321
305
|
if (val) return [...acc, val];
|
|
322
306
|
else return acc;
|
|
323
307
|
}, []);
|
|
324
|
-
return parts;
|
|
325
308
|
}
|
|
326
309
|
function messageToolCallsToParts(toolCalls) {
|
|
327
310
|
if (!toolCalls || toolCalls.length === 0) return [];
|
|
@@ -345,7 +328,7 @@ function getGeminiAPI(config) {
|
|
|
345
328
|
async function roleMessageToContent(role, message) {
|
|
346
329
|
const contentParts = await messageContentToParts(message.content);
|
|
347
330
|
let toolParts;
|
|
348
|
-
if ((0,
|
|
331
|
+
if ((0, _langchain_core_messages.isAIMessage)(message) && !!message.tool_calls?.length) toolParts = message.tool_calls.map((toolCall) => ({ functionCall: {
|
|
349
332
|
name: toolCall.name,
|
|
350
333
|
args: toolCall.args
|
|
351
334
|
} }));
|
|
@@ -362,21 +345,20 @@ function getGeminiAPI(config) {
|
|
|
362
345
|
}];
|
|
363
346
|
}
|
|
364
347
|
async function systemMessageToContent(message) {
|
|
365
|
-
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"))];
|
|
366
349
|
}
|
|
367
350
|
function toolMessageToContent(message, prevMessage) {
|
|
368
351
|
const contentStr = typeof message.content === "string" ? message.content : message.content.reduce((acc, content) => {
|
|
369
352
|
if (content.type === "text") return acc + content.text;
|
|
370
353
|
else return acc;
|
|
371
354
|
}, "");
|
|
372
|
-
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;
|
|
373
356
|
try {
|
|
374
|
-
const content = JSON.parse(contentStr);
|
|
375
357
|
return [{
|
|
376
358
|
role: "function",
|
|
377
359
|
parts: [{ functionResponse: {
|
|
378
360
|
name: responseName,
|
|
379
|
-
response: { content }
|
|
361
|
+
response: { content: JSON.parse(contentStr) }
|
|
380
362
|
} }]
|
|
381
363
|
}];
|
|
382
364
|
} catch (_) {
|
|
@@ -390,10 +372,10 @@ function getGeminiAPI(config) {
|
|
|
390
372
|
}
|
|
391
373
|
}
|
|
392
374
|
async function baseMessageToContent(message, prevMessage) {
|
|
393
|
-
if (
|
|
394
|
-
else if (
|
|
395
|
-
else if (
|
|
396
|
-
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)) {
|
|
397
379
|
if (!prevMessage) throw new Error("Tool messages cannot be the first message passed to the model.");
|
|
398
380
|
return toolMessageToContent(message, prevMessage);
|
|
399
381
|
} else {
|
|
@@ -427,8 +409,7 @@ function getGeminiAPI(config) {
|
|
|
427
409
|
};
|
|
428
410
|
}
|
|
429
411
|
function inlineDataPartToMessageContent(part) {
|
|
430
|
-
|
|
431
|
-
if (mimeType.startsWith("image")) return inlineDataPartToMessageContentImage(part);
|
|
412
|
+
if ((part?.inlineData?.mimeType ?? "").startsWith("image")) return inlineDataPartToMessageContentImage(part);
|
|
432
413
|
else return inlineDataPartToMessageContentMedia(part);
|
|
433
414
|
}
|
|
434
415
|
function fileDataPartToMessageContent(part) {
|
|
@@ -494,31 +475,22 @@ function getGeminiAPI(config) {
|
|
|
494
475
|
else return response.data;
|
|
495
476
|
}
|
|
496
477
|
function responseToParts(response) {
|
|
497
|
-
|
|
498
|
-
const parts = responseData?.candidates?.[0]?.content?.parts ?? [];
|
|
499
|
-
return parts;
|
|
478
|
+
return responseToGenerateContentResponseData(response)?.candidates?.[0]?.content?.parts ?? [];
|
|
500
479
|
}
|
|
501
480
|
function partToText(part) {
|
|
502
481
|
return "text" in part ? part.text : "";
|
|
503
482
|
}
|
|
504
483
|
function responseToString(response) {
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
const val = partToText(part);
|
|
508
|
-
return acc + val;
|
|
484
|
+
return responseToParts(response).reduce((acc, part) => {
|
|
485
|
+
return acc + partToText(part);
|
|
509
486
|
}, "");
|
|
510
|
-
return ret;
|
|
511
487
|
}
|
|
512
488
|
function safeResponseTo(response, responseTo) {
|
|
513
489
|
const safetyHandler = config?.safetyHandler ?? new DefaultGeminiSafetyHandler();
|
|
514
490
|
try {
|
|
515
|
-
|
|
516
|
-
return responseTo(safeResponse);
|
|
491
|
+
return responseTo(safetyHandler.handle(response));
|
|
517
492
|
} catch (xx) {
|
|
518
|
-
if (xx instanceof require_safety.GoogleAISafetyError)
|
|
519
|
-
const ret = responseTo(xx.response);
|
|
520
|
-
xx.reply = ret;
|
|
521
|
-
}
|
|
493
|
+
if (xx instanceof require_safety.GoogleAISafetyError) xx.reply = responseTo(xx.response);
|
|
522
494
|
throw xx;
|
|
523
495
|
}
|
|
524
496
|
}
|
|
@@ -529,11 +501,10 @@ function getGeminiAPI(config) {
|
|
|
529
501
|
const token = result?.token;
|
|
530
502
|
const logprob = result?.logProbability;
|
|
531
503
|
const encoder = new TextEncoder();
|
|
532
|
-
const bytes = Array.from(encoder.encode(token));
|
|
533
504
|
return {
|
|
534
505
|
token,
|
|
535
506
|
logprob,
|
|
536
|
-
bytes
|
|
507
|
+
bytes: Array.from(encoder.encode(token))
|
|
537
508
|
};
|
|
538
509
|
}
|
|
539
510
|
function candidateToLogprobs(candidate) {
|
|
@@ -554,25 +525,21 @@ function getGeminiAPI(config) {
|
|
|
554
525
|
const retrieval = candidate?.urlRetrievalMetadata?.urlRetrievalContexts ?? [];
|
|
555
526
|
const context = candidate?.urlContextMetadata?.urlMetadata ?? [];
|
|
556
527
|
const all = [...retrieval, ...context];
|
|
557
|
-
if (all.length === 0) return
|
|
528
|
+
if (all.length === 0) return;
|
|
558
529
|
else return { urlMetadata: all };
|
|
559
530
|
}
|
|
560
531
|
function addModalityCounts(modalityTokenCounts, details) {
|
|
561
532
|
modalityTokenCounts?.forEach((modalityTokenCount) => {
|
|
562
533
|
const { modality, tokenCount } = modalityTokenCount;
|
|
563
534
|
const modalityLc = modality.toLowerCase();
|
|
564
|
-
|
|
565
|
-
details[modalityLc] = currentCount + tokenCount;
|
|
535
|
+
details[modalityLc] = (details[modalityLc] ?? 0) + tokenCount;
|
|
566
536
|
});
|
|
567
537
|
}
|
|
568
538
|
function responseToUsageMetadata(response) {
|
|
569
539
|
if ("usageMetadata" in response.data) {
|
|
570
|
-
const
|
|
571
|
-
const usageMetadata = data?.usageMetadata;
|
|
540
|
+
const usageMetadata = (response?.data)?.usageMetadata;
|
|
572
541
|
const input_tokens = usageMetadata.promptTokenCount ?? 0;
|
|
573
|
-
const
|
|
574
|
-
const thoughtsTokenCount = usageMetadata.thoughtsTokenCount ?? 0;
|
|
575
|
-
const output_tokens = candidatesTokenCount + thoughtsTokenCount;
|
|
542
|
+
const output_tokens = (usageMetadata.candidatesTokenCount ?? 0) + (usageMetadata.thoughtsTokenCount ?? 0);
|
|
576
543
|
const total_tokens = usageMetadata.totalTokenCount ?? input_tokens + output_tokens;
|
|
577
544
|
const input_token_details = {};
|
|
578
545
|
addModalityCounts(usageMetadata.promptTokensDetails, input_token_details);
|
|
@@ -580,16 +547,14 @@ function getGeminiAPI(config) {
|
|
|
580
547
|
const output_token_details = {};
|
|
581
548
|
addModalityCounts(usageMetadata?.candidatesTokensDetails, output_token_details);
|
|
582
549
|
if (typeof usageMetadata?.thoughtsTokenCount === "number") output_token_details.reasoning = usageMetadata.thoughtsTokenCount;
|
|
583
|
-
|
|
550
|
+
return {
|
|
584
551
|
input_tokens,
|
|
585
552
|
output_tokens,
|
|
586
553
|
total_tokens,
|
|
587
554
|
input_token_details,
|
|
588
555
|
output_token_details
|
|
589
556
|
};
|
|
590
|
-
return ret;
|
|
591
557
|
}
|
|
592
|
-
return void 0;
|
|
593
558
|
}
|
|
594
559
|
function responseToGenerationInfo(response) {
|
|
595
560
|
const data = Array.isArray(response.data) && response.data[0] ? response.data[0] : response.data && response.data.candidates ? response.data : void 0;
|
|
@@ -615,7 +580,7 @@ function getGeminiAPI(config) {
|
|
|
615
580
|
return ret;
|
|
616
581
|
}
|
|
617
582
|
function responseToChatGeneration(response) {
|
|
618
|
-
return new
|
|
583
|
+
return new _langchain_core_outputs.ChatGenerationChunk({
|
|
619
584
|
text: responseToString(response),
|
|
620
585
|
message: partToMessageChunk(responseToParts(response)[0]),
|
|
621
586
|
generationInfo: responseToGenerationInfo(response)
|
|
@@ -633,10 +598,10 @@ function getGeminiAPI(config) {
|
|
|
633
598
|
}
|
|
634
599
|
function partToMessageChunk(part) {
|
|
635
600
|
const fields = partsToBaseMessageChunkFields([part]);
|
|
636
|
-
if (typeof fields.content === "string") return new
|
|
601
|
+
if (typeof fields.content === "string") return new _langchain_core_messages.AIMessageChunk(fields);
|
|
637
602
|
else if (fields.content?.every((item) => item.type === "text")) {
|
|
638
603
|
const newContent = fields.content.map((item) => "text" in item ? item.text : "").join("");
|
|
639
|
-
return new
|
|
604
|
+
return new _langchain_core_messages.AIMessageChunk({
|
|
640
605
|
...fields,
|
|
641
606
|
content: newContent,
|
|
642
607
|
response_metadata: {
|
|
@@ -645,24 +610,21 @@ function getGeminiAPI(config) {
|
|
|
645
610
|
}
|
|
646
611
|
});
|
|
647
612
|
}
|
|
648
|
-
return new
|
|
613
|
+
return new _langchain_core_messages.AIMessageChunk(fields);
|
|
649
614
|
}
|
|
650
615
|
function partToChatGeneration(part) {
|
|
651
616
|
const message = partToMessageChunk(part);
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
return new __langchain_core_outputs.ChatGenerationChunk({
|
|
655
|
-
text,
|
|
617
|
+
return new _langchain_core_outputs.ChatGenerationChunk({
|
|
618
|
+
text: partToText(part),
|
|
656
619
|
message,
|
|
657
|
-
generationInfo
|
|
620
|
+
generationInfo: {}
|
|
658
621
|
});
|
|
659
622
|
}
|
|
660
623
|
function groundingSupportByPart(groundingSupports) {
|
|
661
624
|
const ret = [];
|
|
662
625
|
if (!groundingSupports || groundingSupports.length === 0) return [];
|
|
663
626
|
groundingSupports?.forEach((groundingSupport) => {
|
|
664
|
-
const
|
|
665
|
-
const partIndex = segment?.partIndex ?? 0;
|
|
627
|
+
const partIndex = (groundingSupport?.segment)?.partIndex ?? 0;
|
|
666
628
|
if (ret[partIndex]) ret[partIndex].push(groundingSupport);
|
|
667
629
|
else ret[partIndex] = [groundingSupport];
|
|
668
630
|
});
|
|
@@ -675,7 +637,7 @@ function getGeminiAPI(config) {
|
|
|
675
637
|
const groundingMetadata = candidate?.groundingMetadata;
|
|
676
638
|
const citationMetadata = candidate?.citationMetadata;
|
|
677
639
|
const groundingParts = groundingSupportByPart(groundingMetadata?.groundingSupports);
|
|
678
|
-
|
|
640
|
+
return parts.map((part, index) => {
|
|
679
641
|
const gen = partToChatGeneration(part);
|
|
680
642
|
if (!gen.generationInfo) gen.generationInfo = {};
|
|
681
643
|
if (groundingMetadata) {
|
|
@@ -686,11 +648,9 @@ function getGeminiAPI(config) {
|
|
|
686
648
|
if (citationMetadata) gen.generationInfo.citationMetadata = citationMetadata;
|
|
687
649
|
return gen;
|
|
688
650
|
});
|
|
689
|
-
return ret;
|
|
690
651
|
}
|
|
691
652
|
function combineContent(gen, forceComplex = false) {
|
|
692
|
-
|
|
693
|
-
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("");
|
|
694
654
|
else {
|
|
695
655
|
const ret = [];
|
|
696
656
|
gen.forEach((item) => {
|
|
@@ -709,20 +669,18 @@ function getGeminiAPI(config) {
|
|
|
709
669
|
return gen.map((item) => item.text ?? "").join("");
|
|
710
670
|
}
|
|
711
671
|
function combineToolCalls(gen) {
|
|
712
|
-
let ret = new
|
|
672
|
+
let ret = new _langchain_core_messages.AIMessageChunk("");
|
|
713
673
|
gen.forEach((item) => {
|
|
714
674
|
const message = item?.message;
|
|
715
|
-
ret = (0,
|
|
675
|
+
ret = (0, _langchain_core_utils_stream.concat)(ret, message);
|
|
716
676
|
});
|
|
717
677
|
return ret;
|
|
718
678
|
}
|
|
719
679
|
function combineAdditionalKwargs(gen) {
|
|
720
680
|
const ret = {};
|
|
721
681
|
gen.forEach((item) => {
|
|
722
|
-
const
|
|
723
|
-
|
|
724
|
-
const keys = Object.keys(kwargs);
|
|
725
|
-
keys.forEach((key) => {
|
|
682
|
+
const kwargs = (item?.message)?.additional_kwargs ?? {};
|
|
683
|
+
Object.keys(kwargs).forEach((key) => {
|
|
726
684
|
const value = kwargs[key];
|
|
727
685
|
if (Object.hasOwn(ret, key) && Array.isArray(ret[key]) && Array.isArray(value)) ret[key].push(...value);
|
|
728
686
|
else ret[key] = value;
|
|
@@ -737,17 +695,15 @@ function getGeminiAPI(config) {
|
|
|
737
695
|
const combinedToolCalls = combineToolCalls(gen.content);
|
|
738
696
|
const kwargs = combineAdditionalKwargs(gen.content);
|
|
739
697
|
const lastContent = gen.content[gen.content.length - 1];
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
return [new __langchain_core_outputs.ChatGenerationChunk({
|
|
750
|
-
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
|
+
}),
|
|
751
707
|
text: combinedText,
|
|
752
708
|
generationInfo: lastContent.generationInfo
|
|
753
709
|
})];
|
|
@@ -786,8 +742,7 @@ function getGeminiAPI(config) {
|
|
|
786
742
|
return ret;
|
|
787
743
|
}
|
|
788
744
|
function responseToBaseMessageFields(response) {
|
|
789
|
-
|
|
790
|
-
return partsToBaseMessageChunkFields(parts);
|
|
745
|
+
return partsToBaseMessageChunkFields(responseToParts(response));
|
|
791
746
|
}
|
|
792
747
|
function partsToSignatures(parts) {
|
|
793
748
|
return parts.map((part) => part?.thoughtSignature ?? "");
|
|
@@ -833,16 +788,14 @@ function getGeminiAPI(config) {
|
|
|
833
788
|
return fields;
|
|
834
789
|
}
|
|
835
790
|
function responseToBaseMessage(response) {
|
|
836
|
-
|
|
837
|
-
return new __langchain_core_messages.AIMessage(fields);
|
|
791
|
+
return new _langchain_core_messages.AIMessage(responseToBaseMessageFields(response));
|
|
838
792
|
}
|
|
839
793
|
function safeResponseToBaseMessage(response) {
|
|
840
794
|
return safeResponseTo(response, responseToBaseMessage);
|
|
841
795
|
}
|
|
842
796
|
function responseToChatResult(response) {
|
|
843
|
-
const generations = responseToChatGenerations(response);
|
|
844
797
|
return {
|
|
845
|
-
generations,
|
|
798
|
+
generations: responseToChatGenerations(response),
|
|
846
799
|
llmOutput: responseToGenerationInfo(response)
|
|
847
800
|
};
|
|
848
801
|
}
|
|
@@ -858,17 +811,14 @@ function getGeminiAPI(config) {
|
|
|
858
811
|
}
|
|
859
812
|
}
|
|
860
813
|
async function formatMessageContents(input, _parameters) {
|
|
861
|
-
|
|
862
|
-
const contents = [{
|
|
814
|
+
return [{
|
|
863
815
|
role: "user",
|
|
864
|
-
parts
|
|
816
|
+
parts: await messageContentToParts(input)
|
|
865
817
|
}];
|
|
866
|
-
return contents;
|
|
867
818
|
}
|
|
868
819
|
async function formatBaseMessageContents(input, _parameters) {
|
|
869
820
|
const inputPromises = input.map((msg, i) => baseMessageToContent(msg, input[i - 1]));
|
|
870
|
-
|
|
871
|
-
return inputs.reduce((acc, cur) => {
|
|
821
|
+
return (await Promise.all(inputPromises)).reduce((acc, cur) => {
|
|
872
822
|
if (cur.every((content) => content.role === "system")) return acc;
|
|
873
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];
|
|
874
824
|
else acc.push(...cur);
|
|
@@ -937,8 +887,7 @@ function getGeminiAPI(config) {
|
|
|
937
887
|
}
|
|
938
888
|
async function formatSystemInstruction(input) {
|
|
939
889
|
if (!config?.useSystemInstruction) return {};
|
|
940
|
-
|
|
941
|
-
switch (it) {
|
|
890
|
+
switch (inputType(input)) {
|
|
942
891
|
case "BaseMessageArray": return formatBaseMessageSystemInstruction(input);
|
|
943
892
|
default: return {};
|
|
944
893
|
}
|
|
@@ -953,7 +902,6 @@ function getGeminiAPI(config) {
|
|
|
953
902
|
}
|
|
954
903
|
function searchToolName(tool) {
|
|
955
904
|
for (const name of require_types.GeminiSearchToolAttributes) if (name in tool) return name;
|
|
956
|
-
return void 0;
|
|
957
905
|
}
|
|
958
906
|
function cleanGeminiTool(tool) {
|
|
959
907
|
const orig = searchToolName(tool);
|
|
@@ -967,7 +915,7 @@ function getGeminiAPI(config) {
|
|
|
967
915
|
const langChainTools = [];
|
|
968
916
|
const otherTools = [];
|
|
969
917
|
tools.forEach((tool) => {
|
|
970
|
-
if ((0,
|
|
918
|
+
if ((0, _langchain_core_utils_function_calling.isLangChainTool)(tool)) langChainTools.push(tool);
|
|
971
919
|
else otherTools.push(cleanGeminiTool(tool));
|
|
972
920
|
});
|
|
973
921
|
const result = [...otherTools];
|
|
@@ -975,7 +923,7 @@ function getGeminiAPI(config) {
|
|
|
975
923
|
return result;
|
|
976
924
|
}
|
|
977
925
|
function formatToolConfig(parameters) {
|
|
978
|
-
if (!parameters.tool_choice || typeof parameters.tool_choice !== "string") return
|
|
926
|
+
if (!parameters.tool_choice || typeof parameters.tool_choice !== "string") return;
|
|
979
927
|
if ([
|
|
980
928
|
"auto",
|
|
981
929
|
"any",
|