@langchain/google-genai 0.0.19 → 0.0.21

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.
@@ -3,7 +3,7 @@ import { CallbackManagerForLLMRun } from "@langchain/core/callbacks/manager";
3
3
  import { AIMessageChunk, BaseMessage } from "@langchain/core/messages";
4
4
  import { ChatGenerationChunk, ChatResult } from "@langchain/core/outputs";
5
5
  import { BaseChatModel, LangSmithParams, type BaseChatModelParams } from "@langchain/core/language_models/chat_models";
6
- import { BaseLanguageModelCallOptions, BaseLanguageModelInput, StructuredOutputMethodOptions } from "@langchain/core/language_models/base";
6
+ import { BaseLanguageModelCallOptions, BaseLanguageModelInput, StructuredOutputMethodOptions, ToolDefinition } from "@langchain/core/language_models/base";
7
7
  import { StructuredToolInterface } from "@langchain/core/tools";
8
8
  import { Runnable } from "@langchain/core/runnables";
9
9
  import type { z } from "zod";
@@ -160,7 +160,7 @@ export declare class ChatGoogleGenerativeAI extends BaseChatModel<GoogleGenerati
160
160
  getLsParams(options: this["ParsedCallOptions"]): LangSmithParams;
161
161
  _combineLLMOutput(): never[];
162
162
  _llmType(): string;
163
- bindTools(tools: (StructuredToolInterface | Record<string, unknown>)[], kwargs?: Partial<GoogleGenerativeAIChatCallOptions>): Runnable<BaseLanguageModelInput, AIMessageChunk, GoogleGenerativeAIChatCallOptions>;
163
+ bindTools(tools: (StructuredToolInterface | Record<string, unknown> | ToolDefinition)[], kwargs?: Partial<GoogleGenerativeAIChatCallOptions>): Runnable<BaseLanguageModelInput, AIMessageChunk, GoogleGenerativeAIChatCallOptions>;
164
164
  invocationParams(options?: this["ParsedCallOptions"]): Omit<GenerateContentRequest, "contents">;
165
165
  _generate(messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): Promise<ChatResult>;
166
166
  _streamResponseChunks(messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): AsyncGenerator<ChatGenerationChunk>;
@@ -4,6 +4,7 @@ exports.convertToGenerativeAITools = exports.convertResponseContentToChatGenerat
4
4
  const messages_1 = require("@langchain/core/messages");
5
5
  const outputs_1 = require("@langchain/core/outputs");
6
6
  const function_calling_1 = require("@langchain/core/utils/function_calling");
7
+ const base_1 = require("@langchain/core/language_models/base");
7
8
  const zod_to_genai_parameters_js_1 = require("./zod_to_genai_parameters.cjs");
8
9
  function getMessageAuthor(message) {
9
10
  const type = message._getType();
@@ -53,84 +54,84 @@ function messageContentMedia(content) {
53
54
  }
54
55
  throw new Error("Invalid media content");
55
56
  }
56
- function convertMessageContentToParts(message, isMultimodalModel, role) {
57
- if (typeof message.content === "string") {
57
+ function convertMessageContentToParts(message, isMultimodalModel) {
58
+ if (typeof message.content === "string" && message.content !== "") {
58
59
  return [{ text: message.content }];
59
60
  }
60
- let functionCallParts = [];
61
- if (role === "function") {
62
- if (message.name && typeof message.content === "string") {
63
- functionCallParts.push({
61
+ let functionCalls = [];
62
+ let functionResponses = [];
63
+ let messageParts = [];
64
+ if ("tool_calls" in message &&
65
+ Array.isArray(message.tool_calls) &&
66
+ message.tool_calls.length > 0) {
67
+ functionCalls = message.tool_calls.map((tc) => ({
68
+ functionCall: {
69
+ name: tc.name,
70
+ args: tc.args,
71
+ },
72
+ }));
73
+ }
74
+ else if (message._getType() === "tool" && message.name && message.content) {
75
+ functionResponses = [
76
+ {
64
77
  functionResponse: {
65
78
  name: message.name,
66
79
  response: message.content,
67
80
  },
68
- });
69
- }
70
- else {
71
- throw new Error("ChatGoogleGenerativeAI requires tool messages to contain the tool name, and a string content.");
72
- }
73
- }
74
- if ("tool_calls" in message) {
75
- const castMessage = message;
76
- if (castMessage.tool_calls && castMessage.tool_calls.length > 0) {
77
- functionCallParts = castMessage.tool_calls.map((tc) => ({
78
- functionCall: {
79
- name: tc.name,
80
- args: tc.args,
81
- },
82
- }));
83
- }
81
+ },
82
+ ];
84
83
  }
85
- const messageContentParts = message.content.map((c) => {
86
- if (c.type === "text") {
87
- return {
88
- text: c.text,
89
- };
90
- }
91
- if (c.type === "image_url") {
92
- if (!isMultimodalModel) {
93
- throw new Error(`This model does not support images`);
94
- }
95
- let source;
96
- if (typeof c.image_url === "string") {
97
- source = c.image_url;
98
- }
99
- else if (typeof c.image_url === "object" && "url" in c.image_url) {
100
- source = c.image_url.url;
84
+ else if (Array.isArray(message.content)) {
85
+ messageParts = message.content.map((c) => {
86
+ if (c.type === "text") {
87
+ return {
88
+ text: c.text,
89
+ };
101
90
  }
102
- else {
103
- throw new Error("Please provide image as base64 encoded data URL");
91
+ if (c.type === "image_url") {
92
+ if (!isMultimodalModel) {
93
+ throw new Error(`This model does not support images`);
94
+ }
95
+ let source;
96
+ if (typeof c.image_url === "string") {
97
+ source = c.image_url;
98
+ }
99
+ else if (typeof c.image_url === "object" && "url" in c.image_url) {
100
+ source = c.image_url.url;
101
+ }
102
+ else {
103
+ throw new Error("Please provide image as base64 encoded data URL");
104
+ }
105
+ const [dm, data] = source.split(",");
106
+ if (!dm.startsWith("data:")) {
107
+ throw new Error("Please provide image as base64 encoded data URL");
108
+ }
109
+ const [mimeType, encoding] = dm.replace(/^data:/, "").split(";");
110
+ if (encoding !== "base64") {
111
+ throw new Error("Please provide image as base64 encoded data URL");
112
+ }
113
+ return {
114
+ inlineData: {
115
+ data,
116
+ mimeType,
117
+ },
118
+ };
104
119
  }
105
- const [dm, data] = source.split(",");
106
- if (!dm.startsWith("data:")) {
107
- throw new Error("Please provide image as base64 encoded data URL");
120
+ else if (c.type === "media") {
121
+ return messageContentMedia(c);
108
122
  }
109
- const [mimeType, encoding] = dm.replace(/^data:/, "").split(";");
110
- if (encoding !== "base64") {
111
- throw new Error("Please provide image as base64 encoded data URL");
123
+ else if (c.type === "tool_use") {
124
+ return {
125
+ functionCall: {
126
+ name: c.name,
127
+ args: c.input,
128
+ },
129
+ };
112
130
  }
113
- return {
114
- inlineData: {
115
- data,
116
- mimeType,
117
- },
118
- };
119
- }
120
- else if (c.type === "media") {
121
- return messageContentMedia(c);
122
- }
123
- else if (c.type === "tool_use") {
124
- return {
125
- functionCall: {
126
- name: c.name,
127
- args: c.input,
128
- },
129
- };
130
- }
131
- throw new Error(`Unknown content type ${c.type}`);
132
- });
133
- return [...messageContentParts, ...functionCallParts];
131
+ throw new Error(`Unknown content type ${c.type}`);
132
+ });
133
+ }
134
+ return [...messageParts, ...functionCalls, ...functionResponses];
134
135
  }
135
136
  exports.convertMessageContentToParts = convertMessageContentToParts;
136
137
  function convertBaseMessagesToContent(messages, isMultimodalModel) {
@@ -149,7 +150,7 @@ function convertBaseMessagesToContent(messages, isMultimodalModel) {
149
150
  prevContent.role === role) {
150
151
  throw new Error("Google Generative AI requires alternate messages between authors");
151
152
  }
152
- const parts = convertMessageContentToParts(message, isMultimodalModel, role);
153
+ const parts = convertMessageContentToParts(message, isMultimodalModel);
153
154
  if (acc.mergeWithPreviousContent) {
154
155
  const prevContent = acc.content[acc.content.length - 1];
155
156
  if (!prevContent) {
@@ -257,6 +258,14 @@ function convertToGenerativeAITools(structuredTools) {
257
258
  parameters: jsonSchema,
258
259
  };
259
260
  }
261
+ if ((0, base_1.isOpenAITool)(structuredTool)) {
262
+ return {
263
+ name: structuredTool.function.name,
264
+ description: structuredTool.function.description ??
265
+ `A function available to call.`,
266
+ parameters: (0, zod_to_genai_parameters_js_1.jsonSchemaToGeminiParameters)(structuredTool.function.parameters),
267
+ };
268
+ }
260
269
  return structuredTool;
261
270
  }),
262
271
  },
@@ -2,6 +2,7 @@ import { EnhancedGenerateContentResponse, Content, Part, type FunctionDeclaratio
2
2
  import { BaseMessage, UsageMetadata } from "@langchain/core/messages";
3
3
  import { ChatGenerationChunk, ChatResult } from "@langchain/core/outputs";
4
4
  import { StructuredToolInterface } from "@langchain/core/tools";
5
+ import { ToolDefinition } from "@langchain/core/language_models/base";
5
6
  export declare function getMessageAuthor(message: BaseMessage): string;
6
7
  /**
7
8
  * Maps a message type to a Google Generative AI chat author.
@@ -10,7 +11,7 @@ export declare function getMessageAuthor(message: BaseMessage): string;
10
11
  * @returns The message type mapped to a Google Generative AI chat author.
11
12
  */
12
13
  export declare function convertAuthorToRole(author: string): (typeof POSSIBLE_ROLES)[number];
13
- export declare function convertMessageContentToParts(message: BaseMessage, isMultimodalModel: boolean, role: (typeof POSSIBLE_ROLES)[number]): Part[];
14
+ export declare function convertMessageContentToParts(message: BaseMessage, isMultimodalModel: boolean): Part[];
14
15
  export declare function convertBaseMessagesToContent(messages: BaseMessage[], isMultimodalModel: boolean): Content[];
15
16
  export declare function mapGenerateContentResultToChatResult(response: EnhancedGenerateContentResponse, extra?: {
16
17
  usageMetadata: UsageMetadata | undefined;
@@ -19,4 +20,4 @@ export declare function convertResponseContentToChatGenerationChunk(response: En
19
20
  usageMetadata?: UsageMetadata | undefined;
20
21
  index: number;
21
22
  }): ChatGenerationChunk | null;
22
- export declare function convertToGenerativeAITools(structuredTools: (StructuredToolInterface | Record<string, unknown>)[]): GoogleGenerativeAIFunctionDeclarationsTool[];
23
+ export declare function convertToGenerativeAITools(structuredTools: (StructuredToolInterface | Record<string, unknown> | ToolDefinition)[]): GoogleGenerativeAIFunctionDeclarationsTool[];
@@ -1,7 +1,8 @@
1
1
  import { AIMessage, AIMessageChunk, ChatMessage, isBaseMessage, } from "@langchain/core/messages";
2
2
  import { ChatGenerationChunk, } from "@langchain/core/outputs";
3
3
  import { isStructuredTool } from "@langchain/core/utils/function_calling";
4
- import { zodToGenerativeAIParameters } from "./zod_to_genai_parameters.js";
4
+ import { isOpenAITool, } from "@langchain/core/language_models/base";
5
+ import { jsonSchemaToGeminiParameters, zodToGenerativeAIParameters, } from "./zod_to_genai_parameters.js";
5
6
  export function getMessageAuthor(message) {
6
7
  const type = message._getType();
7
8
  if (ChatMessage.isInstance(message)) {
@@ -48,84 +49,84 @@ function messageContentMedia(content) {
48
49
  }
49
50
  throw new Error("Invalid media content");
50
51
  }
51
- export function convertMessageContentToParts(message, isMultimodalModel, role) {
52
- if (typeof message.content === "string") {
52
+ export function convertMessageContentToParts(message, isMultimodalModel) {
53
+ if (typeof message.content === "string" && message.content !== "") {
53
54
  return [{ text: message.content }];
54
55
  }
55
- let functionCallParts = [];
56
- if (role === "function") {
57
- if (message.name && typeof message.content === "string") {
58
- functionCallParts.push({
56
+ let functionCalls = [];
57
+ let functionResponses = [];
58
+ let messageParts = [];
59
+ if ("tool_calls" in message &&
60
+ Array.isArray(message.tool_calls) &&
61
+ message.tool_calls.length > 0) {
62
+ functionCalls = message.tool_calls.map((tc) => ({
63
+ functionCall: {
64
+ name: tc.name,
65
+ args: tc.args,
66
+ },
67
+ }));
68
+ }
69
+ else if (message._getType() === "tool" && message.name && message.content) {
70
+ functionResponses = [
71
+ {
59
72
  functionResponse: {
60
73
  name: message.name,
61
74
  response: message.content,
62
75
  },
63
- });
64
- }
65
- else {
66
- throw new Error("ChatGoogleGenerativeAI requires tool messages to contain the tool name, and a string content.");
67
- }
68
- }
69
- if ("tool_calls" in message) {
70
- const castMessage = message;
71
- if (castMessage.tool_calls && castMessage.tool_calls.length > 0) {
72
- functionCallParts = castMessage.tool_calls.map((tc) => ({
73
- functionCall: {
74
- name: tc.name,
75
- args: tc.args,
76
- },
77
- }));
78
- }
76
+ },
77
+ ];
79
78
  }
80
- const messageContentParts = message.content.map((c) => {
81
- if (c.type === "text") {
82
- return {
83
- text: c.text,
84
- };
85
- }
86
- if (c.type === "image_url") {
87
- if (!isMultimodalModel) {
88
- throw new Error(`This model does not support images`);
89
- }
90
- let source;
91
- if (typeof c.image_url === "string") {
92
- source = c.image_url;
93
- }
94
- else if (typeof c.image_url === "object" && "url" in c.image_url) {
95
- source = c.image_url.url;
79
+ else if (Array.isArray(message.content)) {
80
+ messageParts = message.content.map((c) => {
81
+ if (c.type === "text") {
82
+ return {
83
+ text: c.text,
84
+ };
96
85
  }
97
- else {
98
- throw new Error("Please provide image as base64 encoded data URL");
86
+ if (c.type === "image_url") {
87
+ if (!isMultimodalModel) {
88
+ throw new Error(`This model does not support images`);
89
+ }
90
+ let source;
91
+ if (typeof c.image_url === "string") {
92
+ source = c.image_url;
93
+ }
94
+ else if (typeof c.image_url === "object" && "url" in c.image_url) {
95
+ source = c.image_url.url;
96
+ }
97
+ else {
98
+ throw new Error("Please provide image as base64 encoded data URL");
99
+ }
100
+ const [dm, data] = source.split(",");
101
+ if (!dm.startsWith("data:")) {
102
+ throw new Error("Please provide image as base64 encoded data URL");
103
+ }
104
+ const [mimeType, encoding] = dm.replace(/^data:/, "").split(";");
105
+ if (encoding !== "base64") {
106
+ throw new Error("Please provide image as base64 encoded data URL");
107
+ }
108
+ return {
109
+ inlineData: {
110
+ data,
111
+ mimeType,
112
+ },
113
+ };
99
114
  }
100
- const [dm, data] = source.split(",");
101
- if (!dm.startsWith("data:")) {
102
- throw new Error("Please provide image as base64 encoded data URL");
115
+ else if (c.type === "media") {
116
+ return messageContentMedia(c);
103
117
  }
104
- const [mimeType, encoding] = dm.replace(/^data:/, "").split(";");
105
- if (encoding !== "base64") {
106
- throw new Error("Please provide image as base64 encoded data URL");
118
+ else if (c.type === "tool_use") {
119
+ return {
120
+ functionCall: {
121
+ name: c.name,
122
+ args: c.input,
123
+ },
124
+ };
107
125
  }
108
- return {
109
- inlineData: {
110
- data,
111
- mimeType,
112
- },
113
- };
114
- }
115
- else if (c.type === "media") {
116
- return messageContentMedia(c);
117
- }
118
- else if (c.type === "tool_use") {
119
- return {
120
- functionCall: {
121
- name: c.name,
122
- args: c.input,
123
- },
124
- };
125
- }
126
- throw new Error(`Unknown content type ${c.type}`);
127
- });
128
- return [...messageContentParts, ...functionCallParts];
126
+ throw new Error(`Unknown content type ${c.type}`);
127
+ });
128
+ }
129
+ return [...messageParts, ...functionCalls, ...functionResponses];
129
130
  }
130
131
  export function convertBaseMessagesToContent(messages, isMultimodalModel) {
131
132
  return messages.reduce((acc, message, index) => {
@@ -143,7 +144,7 @@ export function convertBaseMessagesToContent(messages, isMultimodalModel) {
143
144
  prevContent.role === role) {
144
145
  throw new Error("Google Generative AI requires alternate messages between authors");
145
146
  }
146
- const parts = convertMessageContentToParts(message, isMultimodalModel, role);
147
+ const parts = convertMessageContentToParts(message, isMultimodalModel);
147
148
  if (acc.mergeWithPreviousContent) {
148
149
  const prevContent = acc.content[acc.content.length - 1];
149
150
  if (!prevContent) {
@@ -248,6 +249,14 @@ export function convertToGenerativeAITools(structuredTools) {
248
249
  parameters: jsonSchema,
249
250
  };
250
251
  }
252
+ if (isOpenAITool(structuredTool)) {
253
+ return {
254
+ name: structuredTool.function.name,
255
+ description: structuredTool.function.description ??
256
+ `A function available to call.`,
257
+ parameters: jsonSchemaToGeminiParameters(structuredTool.function.parameters),
258
+ };
259
+ }
251
260
  return structuredTool;
252
261
  }),
253
262
  },
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  /* eslint-disable @typescript-eslint/no-unused-vars */
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.zodToGenerativeAIParameters = exports.removeAdditionalProperties = void 0;
4
+ exports.jsonSchemaToGeminiParameters = exports.zodToGenerativeAIParameters = exports.removeAdditionalProperties = void 0;
5
5
  const zod_to_json_schema_1 = require("zod-to-json-schema");
6
6
  function removeAdditionalProperties(
7
7
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -37,3 +37,14 @@ zodObj) {
37
37
  return rest;
38
38
  }
39
39
  exports.zodToGenerativeAIParameters = zodToGenerativeAIParameters;
40
+ function jsonSchemaToGeminiParameters(
41
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
42
+ schema) {
43
+ // Gemini doesn't accept either the $schema or additionalProperties
44
+ // attributes, so we need to explicitly remove them.
45
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
46
+ const jsonSchema = removeAdditionalProperties(schema);
47
+ const { $schema, ...rest } = jsonSchema;
48
+ return rest;
49
+ }
50
+ exports.jsonSchemaToGeminiParameters = jsonSchemaToGeminiParameters;
@@ -10,3 +10,4 @@ export interface GenerativeAIJsonSchemaDirty extends GenerativeAIJsonSchema {
10
10
  }
11
11
  export declare function removeAdditionalProperties(obj: Record<string, any>): GenerativeAIJsonSchema;
12
12
  export declare function zodToGenerativeAIParameters(zodObj: z.ZodType<any>): GenerativeAIFunctionDeclarationSchema;
13
+ export declare function jsonSchemaToGeminiParameters(schema: Record<string, any>): GenerativeAIFunctionDeclarationSchema;
@@ -32,3 +32,13 @@ zodObj) {
32
32
  const { $schema, ...rest } = jsonSchema;
33
33
  return rest;
34
34
  }
35
+ export function jsonSchemaToGeminiParameters(
36
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
37
+ schema) {
38
+ // Gemini doesn't accept either the $schema or additionalProperties
39
+ // attributes, so we need to explicitly remove them.
40
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
41
+ const jsonSchema = removeAdditionalProperties(schema);
42
+ const { $schema, ...rest } = jsonSchema;
43
+ return rest;
44
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/google-genai",
3
- "version": "0.0.19",
3
+ "version": "0.0.21",
4
4
  "description": "Sample integration for LangChain.js",
5
5
  "type": "module",
6
6
  "engines": {
@@ -36,7 +36,7 @@
36
36
  "license": "MIT",
37
37
  "dependencies": {
38
38
  "@google/generative-ai": "^0.7.0",
39
- "@langchain/core": ">=0.2.5 <0.3.0",
39
+ "@langchain/core": ">=0.2.9 <0.3.0",
40
40
  "zod-to-json-schema": "^3.22.4"
41
41
  },
42
42
  "devDependencies": {