@langchain/google-common 0.0.1 → 0.0.2

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.
@@ -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
- return {
6
- text: content.text,
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.map((content) => {
46
- // eslint-disable-next-line default-case
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: messageContentToParts(message.content),
103
+ parts,
61
104
  },
62
105
  ];
63
106
  }
@@ -67,6 +110,32 @@ 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
+ const content = JSON.parse(contentStr);
125
+ return [
126
+ {
127
+ role: "function",
128
+ parts: [
129
+ {
130
+ functionResponse: {
131
+ name: message.tool_call_id,
132
+ response: content,
133
+ },
134
+ },
135
+ ],
136
+ },
137
+ ];
138
+ }
70
139
  export function baseMessageToContent(message) {
71
140
  const type = message._getType();
72
141
  switch (type) {
@@ -76,6 +145,8 @@ export function baseMessageToContent(message) {
76
145
  return roleMessageToContent("user", message);
77
146
  case "ai":
78
147
  return roleMessageToContent("model", message);
148
+ case "tool":
149
+ return toolMessageToContent(message);
79
150
  default:
80
151
  console.log(`Unsupported message type: ${type}`);
81
152
  return [];
@@ -125,6 +196,49 @@ export function partsToMessageContent(parts) {
125
196
  return acc;
126
197
  }, []);
127
198
  }
199
+ function toolRawToTool(raw) {
200
+ return {
201
+ id: raw.id,
202
+ type: raw.type,
203
+ function: {
204
+ name: raw.function.name,
205
+ arguments: JSON.stringify(raw.function.arguments),
206
+ },
207
+ };
208
+ }
209
+ function functionCallPartToToolRaw(part) {
210
+ return {
211
+ id: part?.functionCall?.name ?? "",
212
+ type: "function",
213
+ function: {
214
+ name: part.functionCall.name,
215
+ arguments: part.functionCall.args ?? {},
216
+ },
217
+ };
218
+ }
219
+ export function partsToToolsRaw(parts) {
220
+ return parts
221
+ .map((part) => {
222
+ if (part === undefined || part === null) {
223
+ return null;
224
+ }
225
+ else if ("functionCall" in part) {
226
+ return functionCallPartToToolRaw(part);
227
+ }
228
+ else {
229
+ return null;
230
+ }
231
+ })
232
+ .reduce((acc, content) => {
233
+ if (content) {
234
+ acc.push(content);
235
+ }
236
+ return acc;
237
+ }, []);
238
+ }
239
+ export function toolsRawToTools(raws) {
240
+ return raws.map((raw) => toolRawToTool(raw));
241
+ }
128
242
  export function responseToGenerateContentResponseData(response) {
129
243
  if ("nextChunk" in response.data) {
130
244
  throw new Error("Cannot convert Stream to GenerateContentResponseData");
@@ -215,8 +329,8 @@ export function chunkToString(chunk) {
215
329
  }
216
330
  }
217
331
  export function partToMessage(part) {
218
- const content = partsToMessageContent([part]);
219
- return new AIMessageChunk({ content });
332
+ const fields = partsToBaseMessageFields([part]);
333
+ return new AIMessageChunk(fields);
220
334
  }
221
335
  export function partToChatGeneration(part) {
222
336
  const message = partToMessage(part);
@@ -231,14 +345,26 @@ export function responseToChatGenerations(response) {
231
345
  const ret = parts.map((part) => partToChatGeneration(part));
232
346
  return ret;
233
347
  }
234
- export function responseToMessageContent(response) {
348
+ export function responseToBaseMessageFields(response) {
235
349
  const parts = responseToParts(response);
236
- return partsToMessageContent(parts);
350
+ return partsToBaseMessageFields(parts);
351
+ }
352
+ export function partsToBaseMessageFields(parts) {
353
+ const fields = {
354
+ content: partsToMessageContent(parts),
355
+ };
356
+ const rawTools = partsToToolsRaw(parts);
357
+ if (rawTools.length > 0) {
358
+ const tools = toolsRawToTools(rawTools);
359
+ fields.additional_kwargs = {
360
+ tool_calls: tools,
361
+ };
362
+ }
363
+ return fields;
237
364
  }
238
365
  export function responseToBaseMessage(response) {
239
- return new AIMessage({
240
- content: responseToMessageContent(response),
241
- });
366
+ const fields = responseToBaseMessageFields(response);
367
+ return new AIMessage(fields);
242
368
  }
243
369
  export function safeResponseToBaseMessage(response, safetyHandler) {
244
370
  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,7 @@
1
+ export * from "./common.js";
2
+ export * from "./failed_handler.js";
3
+ export * from "./gemini.js";
4
+ export * from "./zod_to_gemini_parameters.js";
5
+ export * from "./palm.js";
6
+ export * from "./safety.js";
7
+ export * from "./stream.js";
@@ -0,0 +1,7 @@
1
+ export * from "./common.js";
2
+ export * from "./failed_handler.js";
3
+ export * from "./gemini.js";
4
+ export * from "./zod_to_gemini_parameters.js";
5
+ export * from "./palm.js";
6
+ export * from "./safety.js";
7
+ export * from "./stream.js";
@@ -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,3 @@
1
+ import type { z } from "zod";
2
+ import { GeminiFunctionSchema } from "../types.js";
3
+ export declare function zodToGeminiParameters(zodObj: z.ZodType<any>): GeminiFunctionSchema;
@@ -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.1",
3
+ "version": "0.0.2",
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'