@langchain/google-common 0.0.9 → 0.0.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/dist/connection.cjs +3 -9
- package/dist/connection.js +3 -9
- package/dist/types.d.ts +3 -2
- package/dist/utils/gemini.cjs +28 -6
- package/dist/utils/gemini.d.ts +19 -0
- package/dist/utils/gemini.js +26 -5
- package/package.json +1 -1
package/dist/connection.cjs
CHANGED
|
@@ -242,19 +242,13 @@ class AbstractGoogleLLMConnection extends GoogleAIConnection {
|
|
|
242
242
|
return this.structuredToolsToGeminiTools(tools);
|
|
243
243
|
}
|
|
244
244
|
else {
|
|
245
|
+
if (tools.length === 1 && !tools[0].functionDeclarations?.length) {
|
|
246
|
+
return [];
|
|
247
|
+
}
|
|
245
248
|
return tools;
|
|
246
249
|
}
|
|
247
250
|
}
|
|
248
251
|
formatData(input, parameters) {
|
|
249
|
-
/*
|
|
250
|
-
const parts = messageContentToParts(input);
|
|
251
|
-
const contents: GeminiContent[] = [
|
|
252
|
-
{
|
|
253
|
-
role: "user", // Required by Vertex AI
|
|
254
|
-
parts,
|
|
255
|
-
}
|
|
256
|
-
]
|
|
257
|
-
*/
|
|
258
252
|
const contents = this.formatContents(input, parameters);
|
|
259
253
|
const generationConfig = this.formatGenerationConfig(input, parameters);
|
|
260
254
|
const tools = this.formatTools(input, parameters);
|
package/dist/connection.js
CHANGED
|
@@ -236,19 +236,13 @@ export class AbstractGoogleLLMConnection extends GoogleAIConnection {
|
|
|
236
236
|
return this.structuredToolsToGeminiTools(tools);
|
|
237
237
|
}
|
|
238
238
|
else {
|
|
239
|
+
if (tools.length === 1 && !tools[0].functionDeclarations?.length) {
|
|
240
|
+
return [];
|
|
241
|
+
}
|
|
239
242
|
return tools;
|
|
240
243
|
}
|
|
241
244
|
}
|
|
242
245
|
formatData(input, parameters) {
|
|
243
|
-
/*
|
|
244
|
-
const parts = messageContentToParts(input);
|
|
245
|
-
const contents: GeminiContent[] = [
|
|
246
|
-
{
|
|
247
|
-
role: "user", // Required by Vertex AI
|
|
248
|
-
parts,
|
|
249
|
-
}
|
|
250
|
-
]
|
|
251
|
-
*/
|
|
252
246
|
const contents = this.formatContents(input, parameters);
|
|
253
247
|
const generationConfig = this.formatGenerationConfig(input, parameters);
|
|
254
248
|
const tools = this.formatTools(input, parameters);
|
package/dist/types.d.ts
CHANGED
|
@@ -124,10 +124,10 @@ export interface GeminiSafetySetting {
|
|
|
124
124
|
category: string;
|
|
125
125
|
threshold: string;
|
|
126
126
|
}
|
|
127
|
-
export
|
|
127
|
+
export type GeminiSafetyRating = {
|
|
128
128
|
category: string;
|
|
129
129
|
probability: string;
|
|
130
|
-
}
|
|
130
|
+
} & Record<string, unknown>;
|
|
131
131
|
export type GeminiRole = "user" | "model" | "function";
|
|
132
132
|
export interface GeminiContent {
|
|
133
133
|
parts: GeminiPart[];
|
|
@@ -183,6 +183,7 @@ interface GeminiResponsePromptFeedback {
|
|
|
183
183
|
export interface GenerateContentResponseData {
|
|
184
184
|
candidates: GeminiResponseCandidate[];
|
|
185
185
|
promptFeedback: GeminiResponsePromptFeedback;
|
|
186
|
+
usageMetadata: Record<string, unknown>;
|
|
186
187
|
}
|
|
187
188
|
export type GoogleLLMModelFamily = null | "palm" | "gemini";
|
|
188
189
|
export type GoogleLLMResponseData = JsonStream | GenerateContentResponseData | GenerateContentResponseData[];
|
package/dist/utils/gemini.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MessageGeminiSafetyHandler = exports.DefaultGeminiSafetyHandler = exports.isModelGemini = exports.validateGeminiParams = exports.safeResponseToChatResult = exports.responseToChatResult = exports.safeResponseToBaseMessage = exports.responseToBaseMessage = exports.partsToBaseMessageFields = exports.responseToBaseMessageFields = exports.responseToChatGenerations = exports.partToChatGeneration = exports.partToMessageChunk = exports.chunkToString = exports.safeResponseToChatGeneration = exports.responseToChatGeneration = exports.safeResponseToGeneration = exports.responseToGeneration = exports.safeResponseToString = exports.responseToString = exports.partToText = exports.responseToParts = exports.responseToGenerateContentResponseData = exports.toolsRawToTools = exports.partsToToolsRaw = exports.partsToMessageContent = exports.baseMessageToContent = exports.messageContentToParts = void 0;
|
|
3
|
+
exports.MessageGeminiSafetyHandler = exports.DefaultGeminiSafetyHandler = exports.isModelGemini = exports.validateGeminiParams = exports.safeResponseToChatResult = exports.responseToChatResult = exports.safeResponseToBaseMessage = exports.responseToBaseMessage = exports.partsToBaseMessageFields = exports.responseToBaseMessageFields = exports.responseToChatGenerations = exports.partToChatGeneration = exports.partToMessageChunk = exports.chunkToString = exports.safeResponseToChatGeneration = exports.responseToChatGeneration = exports.safeResponseToGeneration = exports.responseToGeneration = exports.responseToGenerationInfo = exports.safeResponseToString = exports.responseToString = exports.partToText = exports.responseToParts = exports.responseToGenerateContentResponseData = exports.toolsRawToTools = exports.partsToToolsRaw = exports.partsToMessageContent = exports.baseMessageToContent = exports.messageContentToParts = void 0;
|
|
4
4
|
const uuid_1 = require("uuid");
|
|
5
5
|
const messages_1 = require("@langchain/core/messages");
|
|
6
6
|
const outputs_1 = require("@langchain/core/outputs");
|
|
@@ -379,10 +379,32 @@ function safeResponseToString(response, safetyHandler) {
|
|
|
379
379
|
return safeResponseTo(response, safetyHandler, responseToString);
|
|
380
380
|
}
|
|
381
381
|
exports.safeResponseToString = safeResponseToString;
|
|
382
|
+
function responseToGenerationInfo(response) {
|
|
383
|
+
if (!Array.isArray(response.data)) {
|
|
384
|
+
return {};
|
|
385
|
+
}
|
|
386
|
+
const data = response.data[0];
|
|
387
|
+
return {
|
|
388
|
+
usage_metadata: {
|
|
389
|
+
prompt_token_count: data.usageMetadata?.promptTokenCount,
|
|
390
|
+
candidates_token_count: data.usageMetadata?.candidatesTokenCount,
|
|
391
|
+
total_token_count: data.usageMetadata?.totalTokenCount,
|
|
392
|
+
},
|
|
393
|
+
safety_ratings: data.candidates[0]?.safetyRatings?.map((rating) => ({
|
|
394
|
+
category: rating.category,
|
|
395
|
+
probability: rating.probability,
|
|
396
|
+
probability_score: rating.probabilityScore,
|
|
397
|
+
severity: rating.severity,
|
|
398
|
+
severity_score: rating.severityScore,
|
|
399
|
+
})),
|
|
400
|
+
finish_reason: data.candidates[0]?.finishReason,
|
|
401
|
+
};
|
|
402
|
+
}
|
|
403
|
+
exports.responseToGenerationInfo = responseToGenerationInfo;
|
|
382
404
|
function responseToGeneration(response) {
|
|
383
405
|
return {
|
|
384
406
|
text: responseToString(response),
|
|
385
|
-
generationInfo: response,
|
|
407
|
+
generationInfo: responseToGenerationInfo(response),
|
|
386
408
|
};
|
|
387
409
|
}
|
|
388
410
|
exports.responseToGeneration = responseToGeneration;
|
|
@@ -394,7 +416,7 @@ function responseToChatGeneration(response) {
|
|
|
394
416
|
return new outputs_1.ChatGenerationChunk({
|
|
395
417
|
text: responseToString(response),
|
|
396
418
|
message: partToMessageChunk(responseToParts(response)[0]),
|
|
397
|
-
generationInfo: response,
|
|
419
|
+
generationInfo: responseToGenerationInfo(response),
|
|
398
420
|
});
|
|
399
421
|
}
|
|
400
422
|
exports.responseToChatGeneration = responseToChatGeneration;
|
|
@@ -452,7 +474,7 @@ function responseToChatGenerations(response) {
|
|
|
452
474
|
if (ret.every((item) => typeof item.message.content === "string")) {
|
|
453
475
|
const combinedContent = ret.map((item) => item.message.content).join("");
|
|
454
476
|
const combinedText = ret.map((item) => item.text).join("");
|
|
455
|
-
const toolCallChunks = ret[ret.length - 1]
|
|
477
|
+
const toolCallChunks = ret[ret.length - 1]?.message.additional_kwargs?.tool_calls?.map((toolCall, i) => ({
|
|
456
478
|
name: toolCall.function.name,
|
|
457
479
|
args: toolCall.function.arguments,
|
|
458
480
|
id: toolCall.id,
|
|
@@ -462,7 +484,7 @@ function responseToChatGenerations(response) {
|
|
|
462
484
|
new outputs_1.ChatGenerationChunk({
|
|
463
485
|
message: new messages_1.AIMessageChunk({
|
|
464
486
|
content: combinedContent,
|
|
465
|
-
additional_kwargs: ret[ret.length - 1]
|
|
487
|
+
additional_kwargs: ret[ret.length - 1]?.message.additional_kwargs,
|
|
466
488
|
tool_call_chunks: toolCallChunks,
|
|
467
489
|
}),
|
|
468
490
|
text: combinedText,
|
|
@@ -525,7 +547,7 @@ function responseToChatResult(response) {
|
|
|
525
547
|
const generations = responseToChatGenerations(response);
|
|
526
548
|
return {
|
|
527
549
|
generations,
|
|
528
|
-
llmOutput: response,
|
|
550
|
+
llmOutput: responseToGenerationInfo(response),
|
|
529
551
|
};
|
|
530
552
|
}
|
|
531
553
|
exports.responseToChatResult = responseToChatResult;
|
package/dist/utils/gemini.d.ts
CHANGED
|
@@ -29,6 +29,25 @@ export declare function responseToParts(response: GoogleLLMResponse): GeminiPart
|
|
|
29
29
|
export declare function partToText(part: GeminiPart): string;
|
|
30
30
|
export declare function responseToString(response: GoogleLLMResponse): string;
|
|
31
31
|
export declare function safeResponseToString(response: GoogleLLMResponse, safetyHandler: GoogleAISafetyHandler): string;
|
|
32
|
+
export declare function responseToGenerationInfo(response: GoogleLLMResponse): {
|
|
33
|
+
usage_metadata?: undefined;
|
|
34
|
+
safety_ratings?: undefined;
|
|
35
|
+
finish_reason?: undefined;
|
|
36
|
+
} | {
|
|
37
|
+
usage_metadata: {
|
|
38
|
+
prompt_token_count: unknown;
|
|
39
|
+
candidates_token_count: unknown;
|
|
40
|
+
total_token_count: unknown;
|
|
41
|
+
};
|
|
42
|
+
safety_ratings: {
|
|
43
|
+
category: string;
|
|
44
|
+
probability: string;
|
|
45
|
+
probability_score: unknown;
|
|
46
|
+
severity: unknown;
|
|
47
|
+
severity_score: unknown;
|
|
48
|
+
}[];
|
|
49
|
+
finish_reason: string;
|
|
50
|
+
};
|
|
32
51
|
export declare function responseToGeneration(response: GoogleLLMResponse): Generation;
|
|
33
52
|
export declare function safeResponseToGeneration(response: GoogleLLMResponse, safetyHandler: GoogleAISafetyHandler): Generation;
|
|
34
53
|
export declare function responseToChatGeneration(response: GoogleLLMResponse): ChatGenerationChunk;
|
package/dist/utils/gemini.js
CHANGED
|
@@ -366,10 +366,31 @@ function safeResponseTo(response, safetyHandler, responseTo) {
|
|
|
366
366
|
export function safeResponseToString(response, safetyHandler) {
|
|
367
367
|
return safeResponseTo(response, safetyHandler, responseToString);
|
|
368
368
|
}
|
|
369
|
+
export function responseToGenerationInfo(response) {
|
|
370
|
+
if (!Array.isArray(response.data)) {
|
|
371
|
+
return {};
|
|
372
|
+
}
|
|
373
|
+
const data = response.data[0];
|
|
374
|
+
return {
|
|
375
|
+
usage_metadata: {
|
|
376
|
+
prompt_token_count: data.usageMetadata?.promptTokenCount,
|
|
377
|
+
candidates_token_count: data.usageMetadata?.candidatesTokenCount,
|
|
378
|
+
total_token_count: data.usageMetadata?.totalTokenCount,
|
|
379
|
+
},
|
|
380
|
+
safety_ratings: data.candidates[0]?.safetyRatings?.map((rating) => ({
|
|
381
|
+
category: rating.category,
|
|
382
|
+
probability: rating.probability,
|
|
383
|
+
probability_score: rating.probabilityScore,
|
|
384
|
+
severity: rating.severity,
|
|
385
|
+
severity_score: rating.severityScore,
|
|
386
|
+
})),
|
|
387
|
+
finish_reason: data.candidates[0]?.finishReason,
|
|
388
|
+
};
|
|
389
|
+
}
|
|
369
390
|
export function responseToGeneration(response) {
|
|
370
391
|
return {
|
|
371
392
|
text: responseToString(response),
|
|
372
|
-
generationInfo: response,
|
|
393
|
+
generationInfo: responseToGenerationInfo(response),
|
|
373
394
|
};
|
|
374
395
|
}
|
|
375
396
|
export function safeResponseToGeneration(response, safetyHandler) {
|
|
@@ -379,7 +400,7 @@ export function responseToChatGeneration(response) {
|
|
|
379
400
|
return new ChatGenerationChunk({
|
|
380
401
|
text: responseToString(response),
|
|
381
402
|
message: partToMessageChunk(responseToParts(response)[0]),
|
|
382
|
-
generationInfo: response,
|
|
403
|
+
generationInfo: responseToGenerationInfo(response),
|
|
383
404
|
});
|
|
384
405
|
}
|
|
385
406
|
export function safeResponseToChatGeneration(response, safetyHandler) {
|
|
@@ -432,7 +453,7 @@ export function responseToChatGenerations(response) {
|
|
|
432
453
|
if (ret.every((item) => typeof item.message.content === "string")) {
|
|
433
454
|
const combinedContent = ret.map((item) => item.message.content).join("");
|
|
434
455
|
const combinedText = ret.map((item) => item.text).join("");
|
|
435
|
-
const toolCallChunks = ret[ret.length - 1]
|
|
456
|
+
const toolCallChunks = ret[ret.length - 1]?.message.additional_kwargs?.tool_calls?.map((toolCall, i) => ({
|
|
436
457
|
name: toolCall.function.name,
|
|
437
458
|
args: toolCall.function.arguments,
|
|
438
459
|
id: toolCall.id,
|
|
@@ -442,7 +463,7 @@ export function responseToChatGenerations(response) {
|
|
|
442
463
|
new ChatGenerationChunk({
|
|
443
464
|
message: new AIMessageChunk({
|
|
444
465
|
content: combinedContent,
|
|
445
|
-
additional_kwargs: ret[ret.length - 1]
|
|
466
|
+
additional_kwargs: ret[ret.length - 1]?.message.additional_kwargs,
|
|
446
467
|
tool_call_chunks: toolCallChunks,
|
|
447
468
|
}),
|
|
448
469
|
text: combinedText,
|
|
@@ -500,7 +521,7 @@ export function responseToChatResult(response) {
|
|
|
500
521
|
const generations = responseToChatGenerations(response);
|
|
501
522
|
return {
|
|
502
523
|
generations,
|
|
503
|
-
llmOutput: response,
|
|
524
|
+
llmOutput: responseToGenerationInfo(response),
|
|
504
525
|
};
|
|
505
526
|
}
|
|
506
527
|
export function safeResponseToChatResult(response, safetyHandler) {
|