@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/utils/gemini.js
CHANGED
|
@@ -2,9 +2,14 @@ import { AIMessage, AIMessageChunk, } from "@langchain/core/messages";
|
|
|
2
2
|
import { ChatGenerationChunk, } from "@langchain/core/outputs";
|
|
3
3
|
import { GoogleAISafetyError } from "./safety.js";
|
|
4
4
|
function messageContentText(content) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
if (content?.text && content?.text.length > 0) {
|
|
6
|
+
return {
|
|
7
|
+
text: content.text,
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
else {
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
8
13
|
}
|
|
9
14
|
function messageContentImageUrl(content) {
|
|
10
15
|
const url = typeof content.image_url === "string"
|
|
@@ -42,22 +47,60 @@ export function messageContentToParts(content) {
|
|
|
42
47
|
]
|
|
43
48
|
: content;
|
|
44
49
|
// eslint-disable-next-line array-callback-return
|
|
45
|
-
const parts = messageContent
|
|
46
|
-
|
|
50
|
+
const parts = messageContent
|
|
51
|
+
.map((content) => {
|
|
47
52
|
switch (content.type) {
|
|
48
53
|
case "text":
|
|
49
54
|
return messageContentText(content);
|
|
50
55
|
case "image_url":
|
|
51
56
|
return messageContentImageUrl(content);
|
|
57
|
+
default:
|
|
58
|
+
throw new Error(`Unsupported type received while converting message to message parts`);
|
|
52
59
|
}
|
|
53
|
-
})
|
|
60
|
+
})
|
|
61
|
+
.reduce((acc, val) => {
|
|
62
|
+
if (val) {
|
|
63
|
+
return [...acc, val];
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
return acc;
|
|
67
|
+
}
|
|
68
|
+
}, []);
|
|
54
69
|
return parts;
|
|
55
70
|
}
|
|
71
|
+
function messageToolCallsToParts(toolCalls) {
|
|
72
|
+
if (!toolCalls || toolCalls.length === 0) {
|
|
73
|
+
return [];
|
|
74
|
+
}
|
|
75
|
+
return toolCalls.map((tool) => {
|
|
76
|
+
let args = {};
|
|
77
|
+
if (tool?.function?.arguments) {
|
|
78
|
+
const argStr = tool.function.arguments;
|
|
79
|
+
args = JSON.parse(argStr);
|
|
80
|
+
}
|
|
81
|
+
return {
|
|
82
|
+
functionCall: {
|
|
83
|
+
name: tool.function.name,
|
|
84
|
+
args,
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
function messageKwargsToParts(kwargs) {
|
|
90
|
+
const ret = [];
|
|
91
|
+
if (kwargs?.tool_calls) {
|
|
92
|
+
ret.push(...messageToolCallsToParts(kwargs.tool_calls));
|
|
93
|
+
}
|
|
94
|
+
return ret;
|
|
95
|
+
}
|
|
56
96
|
function roleMessageToContent(role, message) {
|
|
97
|
+
const contentParts = messageContentToParts(message.content);
|
|
98
|
+
const toolParts = messageKwargsToParts(message.additional_kwargs);
|
|
99
|
+
const parts = [...contentParts, ...toolParts];
|
|
57
100
|
return [
|
|
58
101
|
{
|
|
59
102
|
role,
|
|
60
|
-
parts
|
|
103
|
+
parts,
|
|
61
104
|
},
|
|
62
105
|
];
|
|
63
106
|
}
|
|
@@ -67,6 +110,51 @@ function systemMessageToContent(message) {
|
|
|
67
110
|
...roleMessageToContent("model", new AIMessage("Ok")),
|
|
68
111
|
];
|
|
69
112
|
}
|
|
113
|
+
function toolMessageToContent(message) {
|
|
114
|
+
const contentStr = typeof message.content === "string"
|
|
115
|
+
? message.content
|
|
116
|
+
: message.content.reduce((acc, content) => {
|
|
117
|
+
if (content.type === "text") {
|
|
118
|
+
return acc + content.text;
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
return acc;
|
|
122
|
+
}
|
|
123
|
+
}, "");
|
|
124
|
+
try {
|
|
125
|
+
const content = JSON.parse(contentStr);
|
|
126
|
+
return [
|
|
127
|
+
{
|
|
128
|
+
role: "function",
|
|
129
|
+
parts: [
|
|
130
|
+
{
|
|
131
|
+
functionResponse: {
|
|
132
|
+
name: message.tool_call_id,
|
|
133
|
+
response: content,
|
|
134
|
+
},
|
|
135
|
+
},
|
|
136
|
+
],
|
|
137
|
+
},
|
|
138
|
+
];
|
|
139
|
+
}
|
|
140
|
+
catch (_) {
|
|
141
|
+
return [
|
|
142
|
+
{
|
|
143
|
+
role: "function",
|
|
144
|
+
parts: [
|
|
145
|
+
{
|
|
146
|
+
functionResponse: {
|
|
147
|
+
name: message.tool_call_id,
|
|
148
|
+
response: {
|
|
149
|
+
response: contentStr,
|
|
150
|
+
},
|
|
151
|
+
},
|
|
152
|
+
},
|
|
153
|
+
],
|
|
154
|
+
},
|
|
155
|
+
];
|
|
156
|
+
}
|
|
157
|
+
}
|
|
70
158
|
export function baseMessageToContent(message) {
|
|
71
159
|
const type = message._getType();
|
|
72
160
|
switch (type) {
|
|
@@ -76,6 +164,8 @@ export function baseMessageToContent(message) {
|
|
|
76
164
|
return roleMessageToContent("user", message);
|
|
77
165
|
case "ai":
|
|
78
166
|
return roleMessageToContent("model", message);
|
|
167
|
+
case "tool":
|
|
168
|
+
return toolMessageToContent(message);
|
|
79
169
|
default:
|
|
80
170
|
console.log(`Unsupported message type: ${type}`);
|
|
81
171
|
return [];
|
|
@@ -125,6 +215,49 @@ export function partsToMessageContent(parts) {
|
|
|
125
215
|
return acc;
|
|
126
216
|
}, []);
|
|
127
217
|
}
|
|
218
|
+
function toolRawToTool(raw) {
|
|
219
|
+
return {
|
|
220
|
+
id: raw.id,
|
|
221
|
+
type: raw.type,
|
|
222
|
+
function: {
|
|
223
|
+
name: raw.function.name,
|
|
224
|
+
arguments: JSON.stringify(raw.function.arguments),
|
|
225
|
+
},
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
function functionCallPartToToolRaw(part) {
|
|
229
|
+
return {
|
|
230
|
+
id: part?.functionCall?.name ?? "",
|
|
231
|
+
type: "function",
|
|
232
|
+
function: {
|
|
233
|
+
name: part.functionCall.name,
|
|
234
|
+
arguments: part.functionCall.args ?? {},
|
|
235
|
+
},
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
export function partsToToolsRaw(parts) {
|
|
239
|
+
return parts
|
|
240
|
+
.map((part) => {
|
|
241
|
+
if (part === undefined || part === null) {
|
|
242
|
+
return null;
|
|
243
|
+
}
|
|
244
|
+
else if ("functionCall" in part) {
|
|
245
|
+
return functionCallPartToToolRaw(part);
|
|
246
|
+
}
|
|
247
|
+
else {
|
|
248
|
+
return null;
|
|
249
|
+
}
|
|
250
|
+
})
|
|
251
|
+
.reduce((acc, content) => {
|
|
252
|
+
if (content) {
|
|
253
|
+
acc.push(content);
|
|
254
|
+
}
|
|
255
|
+
return acc;
|
|
256
|
+
}, []);
|
|
257
|
+
}
|
|
258
|
+
export function toolsRawToTools(raws) {
|
|
259
|
+
return raws.map((raw) => toolRawToTool(raw));
|
|
260
|
+
}
|
|
128
261
|
export function responseToGenerateContentResponseData(response) {
|
|
129
262
|
if ("nextChunk" in response.data) {
|
|
130
263
|
throw new Error("Cannot convert Stream to GenerateContentResponseData");
|
|
@@ -215,8 +348,20 @@ export function chunkToString(chunk) {
|
|
|
215
348
|
}
|
|
216
349
|
}
|
|
217
350
|
export function partToMessage(part) {
|
|
218
|
-
const
|
|
219
|
-
|
|
351
|
+
const fields = partsToBaseMessageFields([part]);
|
|
352
|
+
if (typeof fields.content === "string") {
|
|
353
|
+
return new AIMessageChunk(fields);
|
|
354
|
+
}
|
|
355
|
+
else if (fields.content.every((item) => item.type === "text")) {
|
|
356
|
+
const newContent = fields.content
|
|
357
|
+
.map((item) => ("text" in item ? item.text : ""))
|
|
358
|
+
.join("");
|
|
359
|
+
return new AIMessageChunk({
|
|
360
|
+
...fields,
|
|
361
|
+
content: newContent,
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
return new AIMessageChunk(fields);
|
|
220
365
|
}
|
|
221
366
|
export function partToChatGeneration(part) {
|
|
222
367
|
const message = partToMessage(part);
|
|
@@ -231,14 +376,26 @@ export function responseToChatGenerations(response) {
|
|
|
231
376
|
const ret = parts.map((part) => partToChatGeneration(part));
|
|
232
377
|
return ret;
|
|
233
378
|
}
|
|
234
|
-
export function
|
|
379
|
+
export function responseToBaseMessageFields(response) {
|
|
235
380
|
const parts = responseToParts(response);
|
|
236
|
-
return
|
|
381
|
+
return partsToBaseMessageFields(parts);
|
|
382
|
+
}
|
|
383
|
+
export function partsToBaseMessageFields(parts) {
|
|
384
|
+
const fields = {
|
|
385
|
+
content: partsToMessageContent(parts),
|
|
386
|
+
};
|
|
387
|
+
const rawTools = partsToToolsRaw(parts);
|
|
388
|
+
if (rawTools.length > 0) {
|
|
389
|
+
const tools = toolsRawToTools(rawTools);
|
|
390
|
+
fields.additional_kwargs = {
|
|
391
|
+
tool_calls: tools,
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
return fields;
|
|
237
395
|
}
|
|
238
396
|
export function responseToBaseMessage(response) {
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
});
|
|
397
|
+
const fields = responseToBaseMessageFields(response);
|
|
398
|
+
return new AIMessage(fields);
|
|
242
399
|
}
|
|
243
400
|
export function safeResponseToBaseMessage(response, safetyHandler) {
|
|
244
401
|
return safeResponseTo(response, safetyHandler, responseToBaseMessage);
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./common.cjs"), exports);
|
|
18
|
+
__exportStar(require("./failed_handler.cjs"), exports);
|
|
19
|
+
__exportStar(require("./gemini.cjs"), exports);
|
|
20
|
+
__exportStar(require("./zod_to_gemini_parameters.cjs"), exports);
|
|
21
|
+
__exportStar(require("./palm.cjs"), exports);
|
|
22
|
+
__exportStar(require("./safety.cjs"), exports);
|
|
23
|
+
__exportStar(require("./stream.cjs"), exports);
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.zodToGeminiParameters = void 0;
|
|
4
|
+
const zod_to_json_schema_1 = require("zod-to-json-schema");
|
|
5
|
+
function zodToGeminiParameters(
|
|
6
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
7
|
+
zodObj) {
|
|
8
|
+
// Gemini doesn't accept either the $schema or additionalProperties
|
|
9
|
+
// attributes, so we need to explicitly remove them.
|
|
10
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
11
|
+
const jsonSchema = (0, zod_to_json_schema_1.zodToJsonSchema)(zodObj);
|
|
12
|
+
const { $schema, additionalProperties, ...rest } = jsonSchema;
|
|
13
|
+
return rest;
|
|
14
|
+
}
|
|
15
|
+
exports.zodToGeminiParameters = zodToGeminiParameters;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { zodToJsonSchema } from "zod-to-json-schema";
|
|
2
|
+
export function zodToGeminiParameters(
|
|
3
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
4
|
+
zodObj) {
|
|
5
|
+
// Gemini doesn't accept either the $schema or additionalProperties
|
|
6
|
+
// attributes, so we need to explicitly remove them.
|
|
7
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
8
|
+
const jsonSchema = zodToJsonSchema(zodObj);
|
|
9
|
+
const { $schema, additionalProperties, ...rest } = jsonSchema;
|
|
10
|
+
return rest;
|
|
11
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@langchain/google-common",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"description": "Core types and classes for Google services.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -39,7 +39,8 @@
|
|
|
39
39
|
"author": "LangChain",
|
|
40
40
|
"license": "MIT",
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@langchain/core": "~0.1.1"
|
|
42
|
+
"@langchain/core": "~0.1.1",
|
|
43
|
+
"zod-to-json-schema": "^3.22.4"
|
|
43
44
|
},
|
|
44
45
|
"devDependencies": {
|
|
45
46
|
"@jest/globals": "^29.5.0",
|
|
@@ -63,7 +64,8 @@
|
|
|
63
64
|
"release-it": "^15.10.1",
|
|
64
65
|
"rollup": "^4.5.2",
|
|
65
66
|
"ts-jest": "^29.1.0",
|
|
66
|
-
"typescript": "<5.2.0"
|
|
67
|
+
"typescript": "<5.2.0",
|
|
68
|
+
"zod": "^3.22.4"
|
|
67
69
|
},
|
|
68
70
|
"publishConfig": {
|
|
69
71
|
"access": "public"
|
|
@@ -78,6 +80,24 @@
|
|
|
78
80
|
"import": "./index.js",
|
|
79
81
|
"require": "./index.cjs"
|
|
80
82
|
},
|
|
83
|
+
"./utils": {
|
|
84
|
+
"types": {
|
|
85
|
+
"import": "./utils.d.ts",
|
|
86
|
+
"require": "./utils.d.cts",
|
|
87
|
+
"default": "./utils.d.ts"
|
|
88
|
+
},
|
|
89
|
+
"import": "./utils.js",
|
|
90
|
+
"require": "./utils.cjs"
|
|
91
|
+
},
|
|
92
|
+
"./types": {
|
|
93
|
+
"types": {
|
|
94
|
+
"import": "./types.d.ts",
|
|
95
|
+
"require": "./types.d.cts",
|
|
96
|
+
"default": "./types.d.ts"
|
|
97
|
+
},
|
|
98
|
+
"import": "./types.js",
|
|
99
|
+
"require": "./types.cjs"
|
|
100
|
+
},
|
|
81
101
|
"./package.json": "./package.json"
|
|
82
102
|
},
|
|
83
103
|
"files": [
|
|
@@ -85,6 +105,14 @@
|
|
|
85
105
|
"index.cjs",
|
|
86
106
|
"index.js",
|
|
87
107
|
"index.d.ts",
|
|
88
|
-
"index.d.cts"
|
|
108
|
+
"index.d.cts",
|
|
109
|
+
"utils.cjs",
|
|
110
|
+
"utils.js",
|
|
111
|
+
"utils.d.ts",
|
|
112
|
+
"utils.d.cts",
|
|
113
|
+
"types.cjs",
|
|
114
|
+
"types.js",
|
|
115
|
+
"types.d.ts",
|
|
116
|
+
"types.d.cts"
|
|
89
117
|
]
|
|
90
118
|
}
|
package/types.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('./dist/types.cjs');
|
package/types.d.cts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './dist/types.js'
|
package/types.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './dist/types.js'
|
package/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './dist/types.js'
|
package/utils.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('./dist/utils/index.cjs');
|
package/utils.d.cts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './dist/utils/index.js'
|
package/utils.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './dist/utils/index.js'
|
package/utils.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './dist/utils/index.js'
|