@langchain/google-common 0.0.1 → 0.0.3
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/README.md +7 -2
- package/dist/chat_models.cjs +103 -4
- package/dist/chat_models.d.ts +13 -4
- package/dist/chat_models.js +103 -4
- package/dist/connection.cjs +48 -4
- package/dist/connection.d.ts +14 -7
- package/dist/connection.js +48 -4
- package/dist/index.cjs +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/llms.cjs +2 -2
- package/dist/llms.d.ts +1 -1
- package/dist/llms.js +2 -2
- package/dist/types.d.ts +32 -3
- package/dist/utils/common.cjs +71 -12
- package/dist/utils/common.d.ts +3 -3
- package/dist/utils/common.js +71 -12
- package/dist/utils/gemini.cjs +176 -16
- package/dist/utils/gemini.d.ts +24 -2
- package/dist/utils/gemini.js +171 -14
- package/dist/utils/index.cjs +23 -0
- package/dist/utils/index.d.ts +7 -0
- package/dist/utils/index.js +7 -0
- package/dist/utils/zod_to_gemini_parameters.cjs +15 -0
- package/dist/utils/zod_to_gemini_parameters.d.ts +3 -0
- package/dist/utils/zod_to_gemini_parameters.js +11 -0
- package/package.json +32 -4
- package/types.cjs +1 -0
- package/types.d.cts +1 -0
- package/types.d.ts +1 -0
- package/types.js +1 -0
- package/utils.cjs +1 -0
- package/utils.d.cts +1 -0
- package/utils.d.ts +1 -0
- package/utils.js +1 -0
package/dist/llms.d.ts
CHANGED
|
@@ -18,7 +18,7 @@ export declare abstract class GoogleBaseLLM<AuthOptions> extends LLM<BaseLanguag
|
|
|
18
18
|
static lc_name(): string;
|
|
19
19
|
originalFields?: GoogleBaseLLMInput<AuthOptions>;
|
|
20
20
|
lc_serializable: boolean;
|
|
21
|
-
|
|
21
|
+
modelName: string;
|
|
22
22
|
temperature: number;
|
|
23
23
|
maxOutputTokens: number;
|
|
24
24
|
topP: number;
|
package/dist/llms.js
CHANGED
|
@@ -50,7 +50,7 @@ export class GoogleBaseLLM extends LLM {
|
|
|
50
50
|
writable: true,
|
|
51
51
|
value: true
|
|
52
52
|
});
|
|
53
|
-
Object.defineProperty(this, "
|
|
53
|
+
Object.defineProperty(this, "modelName", {
|
|
54
54
|
enumerable: true,
|
|
55
55
|
configurable: true,
|
|
56
56
|
writable: true,
|
|
@@ -153,7 +153,7 @@ export class GoogleBaseLLM extends LLM {
|
|
|
153
153
|
* in order to handle public APi calls to `generate()`.
|
|
154
154
|
*/
|
|
155
155
|
async _call(prompt, options) {
|
|
156
|
-
const parameters = copyAIModelParams(this);
|
|
156
|
+
const parameters = copyAIModelParams(this, options);
|
|
157
157
|
const result = await this.connection.request(prompt, parameters, options);
|
|
158
158
|
const ret = safeResponseToString(result, this.safetyHandler);
|
|
159
159
|
return ret;
|
package/dist/types.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import type { BaseLLMParams } from "@langchain/core/language_models/llms";
|
|
2
|
+
import { BaseLanguageModelCallOptions } from "@langchain/core/language_models/base";
|
|
3
|
+
import { StructuredToolInterface } from "@langchain/core/tools";
|
|
2
4
|
import type { JsonStream } from "./utils/stream.js";
|
|
3
5
|
/**
|
|
4
6
|
* Parameters needed to setup the client connection.
|
|
@@ -36,8 +38,10 @@ export interface GoogleAISafetySetting {
|
|
|
36
38
|
threshold: string;
|
|
37
39
|
}
|
|
38
40
|
export interface GoogleAIModelParams {
|
|
39
|
-
/**
|
|
41
|
+
/** @deprecated Prefer `modelName` */
|
|
40
42
|
model?: string;
|
|
43
|
+
/** Model to use */
|
|
44
|
+
modelName?: string;
|
|
41
45
|
/** Sampling temperature to use */
|
|
42
46
|
temperature?: number;
|
|
43
47
|
/**
|
|
@@ -67,8 +71,16 @@ export interface GoogleAIModelParams {
|
|
|
67
71
|
stopSequences?: string[];
|
|
68
72
|
safetySettings?: GoogleAISafetySetting[];
|
|
69
73
|
}
|
|
74
|
+
/**
|
|
75
|
+
* The params which can be passed to the API at request time.
|
|
76
|
+
*/
|
|
77
|
+
export interface GoogleAIModelRequestParams extends GoogleAIModelParams {
|
|
78
|
+
tools?: StructuredToolInterface[] | GeminiTool[];
|
|
79
|
+
}
|
|
70
80
|
export interface GoogleAIBaseLLMInput<AuthOptions> extends BaseLLMParams, GoogleConnectionParams<AuthOptions>, GoogleAIModelParams, GoogleAISafetyParams {
|
|
71
81
|
}
|
|
82
|
+
export interface GoogleAIBaseLanguageModelCallOptions extends BaseLanguageModelCallOptions, GoogleAIModelRequestParams, GoogleAISafetyParams {
|
|
83
|
+
}
|
|
72
84
|
/**
|
|
73
85
|
* Input to LLM class.
|
|
74
86
|
*/
|
|
@@ -113,13 +125,30 @@ export interface GeminiSafetyRating {
|
|
|
113
125
|
category: string;
|
|
114
126
|
probability: string;
|
|
115
127
|
}
|
|
116
|
-
export type GeminiRole = "user" | "model";
|
|
128
|
+
export type GeminiRole = "user" | "model" | "function";
|
|
117
129
|
export interface GeminiContent {
|
|
118
130
|
parts: GeminiPart[];
|
|
119
131
|
role: GeminiRole;
|
|
120
132
|
}
|
|
121
133
|
export interface GeminiTool {
|
|
122
|
-
|
|
134
|
+
functionDeclarations?: GeminiFunctionDeclaration[];
|
|
135
|
+
}
|
|
136
|
+
export interface GeminiFunctionDeclaration {
|
|
137
|
+
name: string;
|
|
138
|
+
description: string;
|
|
139
|
+
parameters?: GeminiFunctionSchema;
|
|
140
|
+
}
|
|
141
|
+
export interface GeminiFunctionSchema {
|
|
142
|
+
type: GeminiFunctionSchemaType;
|
|
143
|
+
format?: string;
|
|
144
|
+
description?: string;
|
|
145
|
+
nullable?: boolean;
|
|
146
|
+
enum?: string[];
|
|
147
|
+
properties?: Record<string, GeminiFunctionSchema>;
|
|
148
|
+
required?: string[];
|
|
149
|
+
items?: GeminiFunctionSchema;
|
|
150
|
+
}
|
|
151
|
+
export type GeminiFunctionSchemaType = "string" | "number" | "integer" | "boolean" | "array" | "object";
|
|
123
152
|
export interface GeminiGenerationConfig {
|
|
124
153
|
stopSequences?: string[];
|
|
125
154
|
candidateCount?: number;
|
package/dist/utils/common.cjs
CHANGED
|
@@ -2,19 +2,78 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.copyAndValidateModelParamsInto = exports.validateModelParams = exports.modelToFamily = exports.copyAIModelParamsInto = exports.copyAIModelParams = void 0;
|
|
4
4
|
const gemini_js_1 = require("./gemini.cjs");
|
|
5
|
-
function copyAIModelParams(params) {
|
|
6
|
-
return copyAIModelParamsInto(params, {});
|
|
5
|
+
function copyAIModelParams(params, options) {
|
|
6
|
+
return copyAIModelParamsInto(params, options, {});
|
|
7
7
|
}
|
|
8
8
|
exports.copyAIModelParams = copyAIModelParams;
|
|
9
|
-
function copyAIModelParamsInto(params, target) {
|
|
9
|
+
function copyAIModelParamsInto(params, options, target) {
|
|
10
10
|
const ret = target || {};
|
|
11
|
-
ret.
|
|
12
|
-
ret.temperature =
|
|
13
|
-
|
|
14
|
-
ret.
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
11
|
+
ret.modelName = options?.modelName ?? params?.modelName ?? target.modelName;
|
|
12
|
+
ret.temperature =
|
|
13
|
+
options?.temperature ?? params?.temperature ?? target.temperature;
|
|
14
|
+
ret.maxOutputTokens =
|
|
15
|
+
options?.maxOutputTokens ??
|
|
16
|
+
params?.maxOutputTokens ??
|
|
17
|
+
target.maxOutputTokens;
|
|
18
|
+
ret.topP = options?.topP ?? params?.topP ?? target.topP;
|
|
19
|
+
ret.topK = options?.topK ?? params?.topK ?? target.topK;
|
|
20
|
+
ret.stopSequences =
|
|
21
|
+
options?.stopSequences ?? params?.stopSequences ?? target.stopSequences;
|
|
22
|
+
ret.safetySettings =
|
|
23
|
+
options?.safetySettings ?? params?.safetySettings ?? target.safetySettings;
|
|
24
|
+
ret.tools = options?.tools;
|
|
25
|
+
// Ensure tools are formatted properly for Gemini
|
|
26
|
+
const geminiTools = options?.tools
|
|
27
|
+
?.map((tool) => {
|
|
28
|
+
if ("function" in tool &&
|
|
29
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
30
|
+
"parameters" in tool.function) {
|
|
31
|
+
// Tool is in OpenAI format. Convert to Gemini then return.
|
|
32
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
33
|
+
const castTool = tool.function;
|
|
34
|
+
const cleanedParameters = castTool.parameters;
|
|
35
|
+
if ("$schema" in cleanedParameters) {
|
|
36
|
+
delete cleanedParameters.$schema;
|
|
37
|
+
}
|
|
38
|
+
if ("additionalProperties" in cleanedParameters) {
|
|
39
|
+
delete cleanedParameters.additionalProperties;
|
|
40
|
+
}
|
|
41
|
+
const toolInGeminiFormat = {
|
|
42
|
+
functionDeclarations: [
|
|
43
|
+
{
|
|
44
|
+
name: castTool.name,
|
|
45
|
+
description: castTool.description,
|
|
46
|
+
parameters: cleanedParameters,
|
|
47
|
+
},
|
|
48
|
+
],
|
|
49
|
+
};
|
|
50
|
+
return toolInGeminiFormat;
|
|
51
|
+
}
|
|
52
|
+
else if ("functionDeclarations" in tool) {
|
|
53
|
+
return tool;
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
})
|
|
59
|
+
.filter((tool) => tool !== null);
|
|
60
|
+
const structuredOutputTools = options?.tools
|
|
61
|
+
?.map((tool) => {
|
|
62
|
+
if ("lc_namespace" in tool) {
|
|
63
|
+
return tool;
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
})
|
|
69
|
+
.filter((tool) => tool !== null);
|
|
70
|
+
if (structuredOutputTools &&
|
|
71
|
+
structuredOutputTools.length > 0 &&
|
|
72
|
+
geminiTools &&
|
|
73
|
+
geminiTools.length > 0) {
|
|
74
|
+
throw new Error(`Cannot mix structured tools with Gemini tools.\nReceived ${structuredOutputTools.length} structured tools and ${geminiTools.length} Gemini tools.`);
|
|
75
|
+
}
|
|
76
|
+
ret.tools = geminiTools ?? structuredOutputTools;
|
|
18
77
|
return ret;
|
|
19
78
|
}
|
|
20
79
|
exports.copyAIModelParamsInto = copyAIModelParamsInto;
|
|
@@ -32,7 +91,7 @@ function modelToFamily(modelName) {
|
|
|
32
91
|
exports.modelToFamily = modelToFamily;
|
|
33
92
|
function validateModelParams(params) {
|
|
34
93
|
const testParams = params ?? {};
|
|
35
|
-
switch (modelToFamily(testParams.
|
|
94
|
+
switch (modelToFamily(testParams.modelName)) {
|
|
36
95
|
case "gemini":
|
|
37
96
|
return (0, gemini_js_1.validateGeminiParams)(testParams);
|
|
38
97
|
default:
|
|
@@ -41,7 +100,7 @@ function validateModelParams(params) {
|
|
|
41
100
|
}
|
|
42
101
|
exports.validateModelParams = validateModelParams;
|
|
43
102
|
function copyAndValidateModelParamsInto(params, target) {
|
|
44
|
-
copyAIModelParamsInto(params, target);
|
|
103
|
+
copyAIModelParamsInto(params, undefined, target);
|
|
45
104
|
validateModelParams(target);
|
|
46
105
|
return target;
|
|
47
106
|
}
|
package/dist/utils/common.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type { GoogleAIModelParams, GoogleLLMModelFamily } from "../types.js";
|
|
2
|
-
export declare function copyAIModelParams(params: GoogleAIModelParams | undefined):
|
|
3
|
-
export declare function copyAIModelParamsInto(params: GoogleAIModelParams | undefined, target: GoogleAIModelParams):
|
|
1
|
+
import type { GoogleAIBaseLanguageModelCallOptions, GoogleAIModelParams, GoogleAIModelRequestParams, GoogleLLMModelFamily } from "../types.js";
|
|
2
|
+
export declare function copyAIModelParams(params: GoogleAIModelParams | undefined, options: GoogleAIBaseLanguageModelCallOptions | undefined): GoogleAIModelRequestParams;
|
|
3
|
+
export declare function copyAIModelParamsInto(params: GoogleAIModelParams | undefined, options: GoogleAIBaseLanguageModelCallOptions | undefined, target: GoogleAIModelParams): GoogleAIModelRequestParams;
|
|
4
4
|
export declare function modelToFamily(modelName: string | undefined): GoogleLLMModelFamily;
|
|
5
5
|
export declare function validateModelParams(params: GoogleAIModelParams | undefined): void;
|
|
6
6
|
export declare function copyAndValidateModelParamsInto(params: GoogleAIModelParams | undefined, target: GoogleAIModelParams): GoogleAIModelParams;
|
package/dist/utils/common.js
CHANGED
|
@@ -1,16 +1,75 @@
|
|
|
1
1
|
import { isModelGemini, validateGeminiParams } from "./gemini.js";
|
|
2
|
-
export function copyAIModelParams(params) {
|
|
3
|
-
return copyAIModelParamsInto(params, {});
|
|
2
|
+
export function copyAIModelParams(params, options) {
|
|
3
|
+
return copyAIModelParamsInto(params, options, {});
|
|
4
4
|
}
|
|
5
|
-
export function copyAIModelParamsInto(params, target) {
|
|
5
|
+
export function copyAIModelParamsInto(params, options, target) {
|
|
6
6
|
const ret = target || {};
|
|
7
|
-
ret.
|
|
8
|
-
ret.temperature =
|
|
9
|
-
|
|
10
|
-
ret.
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
7
|
+
ret.modelName = options?.modelName ?? params?.modelName ?? target.modelName;
|
|
8
|
+
ret.temperature =
|
|
9
|
+
options?.temperature ?? params?.temperature ?? target.temperature;
|
|
10
|
+
ret.maxOutputTokens =
|
|
11
|
+
options?.maxOutputTokens ??
|
|
12
|
+
params?.maxOutputTokens ??
|
|
13
|
+
target.maxOutputTokens;
|
|
14
|
+
ret.topP = options?.topP ?? params?.topP ?? target.topP;
|
|
15
|
+
ret.topK = options?.topK ?? params?.topK ?? target.topK;
|
|
16
|
+
ret.stopSequences =
|
|
17
|
+
options?.stopSequences ?? params?.stopSequences ?? target.stopSequences;
|
|
18
|
+
ret.safetySettings =
|
|
19
|
+
options?.safetySettings ?? params?.safetySettings ?? target.safetySettings;
|
|
20
|
+
ret.tools = options?.tools;
|
|
21
|
+
// Ensure tools are formatted properly for Gemini
|
|
22
|
+
const geminiTools = options?.tools
|
|
23
|
+
?.map((tool) => {
|
|
24
|
+
if ("function" in tool &&
|
|
25
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
26
|
+
"parameters" in tool.function) {
|
|
27
|
+
// Tool is in OpenAI format. Convert to Gemini then return.
|
|
28
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
29
|
+
const castTool = tool.function;
|
|
30
|
+
const cleanedParameters = castTool.parameters;
|
|
31
|
+
if ("$schema" in cleanedParameters) {
|
|
32
|
+
delete cleanedParameters.$schema;
|
|
33
|
+
}
|
|
34
|
+
if ("additionalProperties" in cleanedParameters) {
|
|
35
|
+
delete cleanedParameters.additionalProperties;
|
|
36
|
+
}
|
|
37
|
+
const toolInGeminiFormat = {
|
|
38
|
+
functionDeclarations: [
|
|
39
|
+
{
|
|
40
|
+
name: castTool.name,
|
|
41
|
+
description: castTool.description,
|
|
42
|
+
parameters: cleanedParameters,
|
|
43
|
+
},
|
|
44
|
+
],
|
|
45
|
+
};
|
|
46
|
+
return toolInGeminiFormat;
|
|
47
|
+
}
|
|
48
|
+
else if ("functionDeclarations" in tool) {
|
|
49
|
+
return tool;
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
})
|
|
55
|
+
.filter((tool) => tool !== null);
|
|
56
|
+
const structuredOutputTools = options?.tools
|
|
57
|
+
?.map((tool) => {
|
|
58
|
+
if ("lc_namespace" in tool) {
|
|
59
|
+
return tool;
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
})
|
|
65
|
+
.filter((tool) => tool !== null);
|
|
66
|
+
if (structuredOutputTools &&
|
|
67
|
+
structuredOutputTools.length > 0 &&
|
|
68
|
+
geminiTools &&
|
|
69
|
+
geminiTools.length > 0) {
|
|
70
|
+
throw new Error(`Cannot mix structured tools with Gemini tools.\nReceived ${structuredOutputTools.length} structured tools and ${geminiTools.length} Gemini tools.`);
|
|
71
|
+
}
|
|
72
|
+
ret.tools = geminiTools ?? structuredOutputTools;
|
|
14
73
|
return ret;
|
|
15
74
|
}
|
|
16
75
|
export function modelToFamily(modelName) {
|
|
@@ -26,7 +85,7 @@ export function modelToFamily(modelName) {
|
|
|
26
85
|
}
|
|
27
86
|
export function validateModelParams(params) {
|
|
28
87
|
const testParams = params ?? {};
|
|
29
|
-
switch (modelToFamily(testParams.
|
|
88
|
+
switch (modelToFamily(testParams.modelName)) {
|
|
30
89
|
case "gemini":
|
|
31
90
|
return validateGeminiParams(testParams);
|
|
32
91
|
default:
|
|
@@ -34,7 +93,7 @@ export function validateModelParams(params) {
|
|
|
34
93
|
}
|
|
35
94
|
}
|
|
36
95
|
export function copyAndValidateModelParamsInto(params, target) {
|
|
37
|
-
copyAIModelParamsInto(params, target);
|
|
96
|
+
copyAIModelParamsInto(params, undefined, target);
|
|
38
97
|
validateModelParams(target);
|
|
39
98
|
return target;
|
|
40
99
|
}
|
package/dist/utils/gemini.cjs
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
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.
|
|
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.partToMessage = 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;
|
|
4
4
|
const messages_1 = require("@langchain/core/messages");
|
|
5
5
|
const outputs_1 = require("@langchain/core/outputs");
|
|
6
6
|
const safety_js_1 = require("./safety.cjs");
|
|
7
7
|
function messageContentText(content) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
if (content?.text && content?.text.length > 0) {
|
|
9
|
+
return {
|
|
10
|
+
text: content.text,
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
11
16
|
}
|
|
12
17
|
function messageContentImageUrl(content) {
|
|
13
18
|
const url = typeof content.image_url === "string"
|
|
@@ -45,23 +50,61 @@ function messageContentToParts(content) {
|
|
|
45
50
|
]
|
|
46
51
|
: content;
|
|
47
52
|
// eslint-disable-next-line array-callback-return
|
|
48
|
-
const parts = messageContent
|
|
49
|
-
|
|
53
|
+
const parts = messageContent
|
|
54
|
+
.map((content) => {
|
|
50
55
|
switch (content.type) {
|
|
51
56
|
case "text":
|
|
52
57
|
return messageContentText(content);
|
|
53
58
|
case "image_url":
|
|
54
59
|
return messageContentImageUrl(content);
|
|
60
|
+
default:
|
|
61
|
+
throw new Error(`Unsupported type received while converting message to message parts`);
|
|
55
62
|
}
|
|
56
|
-
})
|
|
63
|
+
})
|
|
64
|
+
.reduce((acc, val) => {
|
|
65
|
+
if (val) {
|
|
66
|
+
return [...acc, val];
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
return acc;
|
|
70
|
+
}
|
|
71
|
+
}, []);
|
|
57
72
|
return parts;
|
|
58
73
|
}
|
|
59
74
|
exports.messageContentToParts = messageContentToParts;
|
|
75
|
+
function messageToolCallsToParts(toolCalls) {
|
|
76
|
+
if (!toolCalls || toolCalls.length === 0) {
|
|
77
|
+
return [];
|
|
78
|
+
}
|
|
79
|
+
return toolCalls.map((tool) => {
|
|
80
|
+
let args = {};
|
|
81
|
+
if (tool?.function?.arguments) {
|
|
82
|
+
const argStr = tool.function.arguments;
|
|
83
|
+
args = JSON.parse(argStr);
|
|
84
|
+
}
|
|
85
|
+
return {
|
|
86
|
+
functionCall: {
|
|
87
|
+
name: tool.function.name,
|
|
88
|
+
args,
|
|
89
|
+
},
|
|
90
|
+
};
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
function messageKwargsToParts(kwargs) {
|
|
94
|
+
const ret = [];
|
|
95
|
+
if (kwargs?.tool_calls) {
|
|
96
|
+
ret.push(...messageToolCallsToParts(kwargs.tool_calls));
|
|
97
|
+
}
|
|
98
|
+
return ret;
|
|
99
|
+
}
|
|
60
100
|
function roleMessageToContent(role, message) {
|
|
101
|
+
const contentParts = messageContentToParts(message.content);
|
|
102
|
+
const toolParts = messageKwargsToParts(message.additional_kwargs);
|
|
103
|
+
const parts = [...contentParts, ...toolParts];
|
|
61
104
|
return [
|
|
62
105
|
{
|
|
63
106
|
role,
|
|
64
|
-
parts
|
|
107
|
+
parts,
|
|
65
108
|
},
|
|
66
109
|
];
|
|
67
110
|
}
|
|
@@ -71,6 +114,51 @@ function systemMessageToContent(message) {
|
|
|
71
114
|
...roleMessageToContent("model", new messages_1.AIMessage("Ok")),
|
|
72
115
|
];
|
|
73
116
|
}
|
|
117
|
+
function toolMessageToContent(message) {
|
|
118
|
+
const contentStr = typeof message.content === "string"
|
|
119
|
+
? message.content
|
|
120
|
+
: message.content.reduce((acc, content) => {
|
|
121
|
+
if (content.type === "text") {
|
|
122
|
+
return acc + content.text;
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
return acc;
|
|
126
|
+
}
|
|
127
|
+
}, "");
|
|
128
|
+
try {
|
|
129
|
+
const content = JSON.parse(contentStr);
|
|
130
|
+
return [
|
|
131
|
+
{
|
|
132
|
+
role: "function",
|
|
133
|
+
parts: [
|
|
134
|
+
{
|
|
135
|
+
functionResponse: {
|
|
136
|
+
name: message.tool_call_id,
|
|
137
|
+
response: content,
|
|
138
|
+
},
|
|
139
|
+
},
|
|
140
|
+
],
|
|
141
|
+
},
|
|
142
|
+
];
|
|
143
|
+
}
|
|
144
|
+
catch (_) {
|
|
145
|
+
return [
|
|
146
|
+
{
|
|
147
|
+
role: "function",
|
|
148
|
+
parts: [
|
|
149
|
+
{
|
|
150
|
+
functionResponse: {
|
|
151
|
+
name: message.tool_call_id,
|
|
152
|
+
response: {
|
|
153
|
+
response: contentStr,
|
|
154
|
+
},
|
|
155
|
+
},
|
|
156
|
+
},
|
|
157
|
+
],
|
|
158
|
+
},
|
|
159
|
+
];
|
|
160
|
+
}
|
|
161
|
+
}
|
|
74
162
|
function baseMessageToContent(message) {
|
|
75
163
|
const type = message._getType();
|
|
76
164
|
switch (type) {
|
|
@@ -80,6 +168,8 @@ function baseMessageToContent(message) {
|
|
|
80
168
|
return roleMessageToContent("user", message);
|
|
81
169
|
case "ai":
|
|
82
170
|
return roleMessageToContent("model", message);
|
|
171
|
+
case "tool":
|
|
172
|
+
return toolMessageToContent(message);
|
|
83
173
|
default:
|
|
84
174
|
console.log(`Unsupported message type: ${type}`);
|
|
85
175
|
return [];
|
|
@@ -131,6 +221,51 @@ function partsToMessageContent(parts) {
|
|
|
131
221
|
}, []);
|
|
132
222
|
}
|
|
133
223
|
exports.partsToMessageContent = partsToMessageContent;
|
|
224
|
+
function toolRawToTool(raw) {
|
|
225
|
+
return {
|
|
226
|
+
id: raw.id,
|
|
227
|
+
type: raw.type,
|
|
228
|
+
function: {
|
|
229
|
+
name: raw.function.name,
|
|
230
|
+
arguments: JSON.stringify(raw.function.arguments),
|
|
231
|
+
},
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
function functionCallPartToToolRaw(part) {
|
|
235
|
+
return {
|
|
236
|
+
id: part?.functionCall?.name ?? "",
|
|
237
|
+
type: "function",
|
|
238
|
+
function: {
|
|
239
|
+
name: part.functionCall.name,
|
|
240
|
+
arguments: part.functionCall.args ?? {},
|
|
241
|
+
},
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
function partsToToolsRaw(parts) {
|
|
245
|
+
return parts
|
|
246
|
+
.map((part) => {
|
|
247
|
+
if (part === undefined || part === null) {
|
|
248
|
+
return null;
|
|
249
|
+
}
|
|
250
|
+
else if ("functionCall" in part) {
|
|
251
|
+
return functionCallPartToToolRaw(part);
|
|
252
|
+
}
|
|
253
|
+
else {
|
|
254
|
+
return null;
|
|
255
|
+
}
|
|
256
|
+
})
|
|
257
|
+
.reduce((acc, content) => {
|
|
258
|
+
if (content) {
|
|
259
|
+
acc.push(content);
|
|
260
|
+
}
|
|
261
|
+
return acc;
|
|
262
|
+
}, []);
|
|
263
|
+
}
|
|
264
|
+
exports.partsToToolsRaw = partsToToolsRaw;
|
|
265
|
+
function toolsRawToTools(raws) {
|
|
266
|
+
return raws.map((raw) => toolRawToTool(raw));
|
|
267
|
+
}
|
|
268
|
+
exports.toolsRawToTools = toolsRawToTools;
|
|
134
269
|
function responseToGenerateContentResponseData(response) {
|
|
135
270
|
if ("nextChunk" in response.data) {
|
|
136
271
|
throw new Error("Cannot convert Stream to GenerateContentResponseData");
|
|
@@ -231,8 +366,20 @@ function chunkToString(chunk) {
|
|
|
231
366
|
}
|
|
232
367
|
exports.chunkToString = chunkToString;
|
|
233
368
|
function partToMessage(part) {
|
|
234
|
-
const
|
|
235
|
-
|
|
369
|
+
const fields = partsToBaseMessageFields([part]);
|
|
370
|
+
if (typeof fields.content === "string") {
|
|
371
|
+
return new messages_1.AIMessageChunk(fields);
|
|
372
|
+
}
|
|
373
|
+
else if (fields.content.every((item) => item.type === "text")) {
|
|
374
|
+
const newContent = fields.content
|
|
375
|
+
.map((item) => ("text" in item ? item.text : ""))
|
|
376
|
+
.join("");
|
|
377
|
+
return new messages_1.AIMessageChunk({
|
|
378
|
+
...fields,
|
|
379
|
+
content: newContent,
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
return new messages_1.AIMessageChunk(fields);
|
|
236
383
|
}
|
|
237
384
|
exports.partToMessage = partToMessage;
|
|
238
385
|
function partToChatGeneration(part) {
|
|
@@ -250,15 +397,28 @@ function responseToChatGenerations(response) {
|
|
|
250
397
|
return ret;
|
|
251
398
|
}
|
|
252
399
|
exports.responseToChatGenerations = responseToChatGenerations;
|
|
253
|
-
function
|
|
400
|
+
function responseToBaseMessageFields(response) {
|
|
254
401
|
const parts = responseToParts(response);
|
|
255
|
-
return
|
|
402
|
+
return partsToBaseMessageFields(parts);
|
|
403
|
+
}
|
|
404
|
+
exports.responseToBaseMessageFields = responseToBaseMessageFields;
|
|
405
|
+
function partsToBaseMessageFields(parts) {
|
|
406
|
+
const fields = {
|
|
407
|
+
content: partsToMessageContent(parts),
|
|
408
|
+
};
|
|
409
|
+
const rawTools = partsToToolsRaw(parts);
|
|
410
|
+
if (rawTools.length > 0) {
|
|
411
|
+
const tools = toolsRawToTools(rawTools);
|
|
412
|
+
fields.additional_kwargs = {
|
|
413
|
+
tool_calls: tools,
|
|
414
|
+
};
|
|
415
|
+
}
|
|
416
|
+
return fields;
|
|
256
417
|
}
|
|
257
|
-
exports.
|
|
418
|
+
exports.partsToBaseMessageFields = partsToBaseMessageFields;
|
|
258
419
|
function responseToBaseMessage(response) {
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
});
|
|
420
|
+
const fields = responseToBaseMessageFields(response);
|
|
421
|
+
return new messages_1.AIMessage(fields);
|
|
262
422
|
}
|
|
263
423
|
exports.responseToBaseMessage = responseToBaseMessage;
|
|
264
424
|
function safeResponseToBaseMessage(response, safetyHandler) {
|
package/dist/utils/gemini.d.ts
CHANGED
|
@@ -1,9 +1,29 @@
|
|
|
1
|
-
import { BaseMessage, BaseMessageChunk, MessageContent } from "@langchain/core/messages";
|
|
1
|
+
import { BaseMessage, BaseMessageChunk, BaseMessageFields, MessageContent } from "@langchain/core/messages";
|
|
2
2
|
import { ChatGeneration, ChatGenerationChunk, ChatResult, Generation } from "@langchain/core/outputs";
|
|
3
3
|
import type { GoogleLLMResponse, GoogleAIModelParams, GeminiPart, GeminiContent, GenerateContentResponseData, GoogleAISafetyHandler } from "../types.js";
|
|
4
4
|
export declare function messageContentToParts(content: MessageContent): GeminiPart[];
|
|
5
5
|
export declare function baseMessageToContent(message: BaseMessage): GeminiContent[];
|
|
6
6
|
export declare function partsToMessageContent(parts: GeminiPart[]): MessageContent;
|
|
7
|
+
interface FunctionCall {
|
|
8
|
+
name: string;
|
|
9
|
+
arguments: string;
|
|
10
|
+
}
|
|
11
|
+
interface ToolCall {
|
|
12
|
+
id: string;
|
|
13
|
+
type: "function";
|
|
14
|
+
function: FunctionCall;
|
|
15
|
+
}
|
|
16
|
+
interface FunctionCallRaw {
|
|
17
|
+
name: string;
|
|
18
|
+
arguments: object;
|
|
19
|
+
}
|
|
20
|
+
interface ToolCallRaw {
|
|
21
|
+
id: string;
|
|
22
|
+
type: "function";
|
|
23
|
+
function: FunctionCallRaw;
|
|
24
|
+
}
|
|
25
|
+
export declare function partsToToolsRaw(parts: GeminiPart[]): ToolCallRaw[];
|
|
26
|
+
export declare function toolsRawToTools(raws: ToolCallRaw[]): ToolCall[];
|
|
7
27
|
export declare function responseToGenerateContentResponseData(response: GoogleLLMResponse): GenerateContentResponseData;
|
|
8
28
|
export declare function responseToParts(response: GoogleLLMResponse): GeminiPart[];
|
|
9
29
|
export declare function partToText(part: GeminiPart): string;
|
|
@@ -17,7 +37,8 @@ export declare function chunkToString(chunk: BaseMessageChunk): string;
|
|
|
17
37
|
export declare function partToMessage(part: GeminiPart): BaseMessageChunk;
|
|
18
38
|
export declare function partToChatGeneration(part: GeminiPart): ChatGeneration;
|
|
19
39
|
export declare function responseToChatGenerations(response: GoogleLLMResponse): ChatGeneration[];
|
|
20
|
-
export declare function
|
|
40
|
+
export declare function responseToBaseMessageFields(response: GoogleLLMResponse): BaseMessageFields;
|
|
41
|
+
export declare function partsToBaseMessageFields(parts: GeminiPart[]): BaseMessageFields;
|
|
21
42
|
export declare function responseToBaseMessage(response: GoogleLLMResponse): BaseMessage;
|
|
22
43
|
export declare function safeResponseToBaseMessage(response: GoogleLLMResponse, safetyHandler: GoogleAISafetyHandler): BaseMessage;
|
|
23
44
|
export declare function responseToChatResult(response: GoogleLLMResponse): ChatResult;
|
|
@@ -46,3 +67,4 @@ export declare class MessageGeminiSafetyHandler extends DefaultGeminiSafetyHandl
|
|
|
46
67
|
setMessage(data: GenerateContentResponseData): GenerateContentResponseData;
|
|
47
68
|
handleData(response: GoogleLLMResponse, data: GenerateContentResponseData): GenerateContentResponseData;
|
|
48
69
|
}
|
|
70
|
+
export {};
|