@langchain/google-genai
Advanced tools
+9
-0
| # @langchain/google-genai | ||
| ## 2.1.21 | ||
| ### Patch Changes | ||
| - [#10106](https://github.com/langchain-ai/langchainjs/pull/10106) [`9f30267`](https://github.com/langchain-ai/langchainjs/commit/9f30267e95a2a42fac71f1d3674b84c5a190dbbc) Thanks [@hntrl](https://github.com/hntrl)! - Add package version metadata to runnable traces. Each package now stamps its version in `this.metadata.versions` at construction time, making version info available in LangSmith trace metadata. | ||
| - Updated dependencies [[`9f30267`](https://github.com/langchain-ai/langchainjs/commit/9f30267e95a2a42fac71f1d3674b84c5a190dbbc), [`403a99f`](https://github.com/langchain-ai/langchainjs/commit/403a99fd826383f30300809ae077e1c967023520), [`3b1fd54`](https://github.com/langchain-ai/langchainjs/commit/3b1fd5458a4aa29c398122829f383f21b5ac39da), [`77bd982`](https://github.com/langchain-ai/langchainjs/commit/77bd98274a885e947d76f7a9c6dd0b3763453218)]: | ||
| - @langchain/core@1.1.29 | ||
| ## 2.1.20 | ||
@@ -4,0 +13,0 @@ |
@@ -428,2 +428,3 @@ const require_zod_to_genai_parameters = require('./utils/zod_to_genai_parameters.cjs'); | ||
| super(fields); | ||
| this._addVersion("@langchain/google-genai", "2.1.21"); | ||
| this.model = fields.model.replace(/^models\//, ""); | ||
@@ -430,0 +431,0 @@ this.maxOutputTokens = fields.maxOutputTokens ?? this.maxOutputTokens; |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"chat_models.cjs","names":["BaseChatModel","GenerativeAI","convertToolsToGenAI","convertBaseMessagesToContent","convertUsageMetadata","mapGenerateContentResultToChatResult","convertResponseContentToChatGenerationChunk","PROFILES","schemaToGenerativeAIParameters","GoogleGenerativeAIToolsOutputParser","removeAdditionalProperties","JsonOutputParser","RunnablePassthrough","RunnableSequence"],"sources":["../src/chat_models.ts"],"sourcesContent":["import {\n GenerativeModel,\n GoogleGenerativeAI as GenerativeAI,\n FunctionDeclarationsTool as GoogleGenerativeAIFunctionDeclarationsTool,\n FunctionDeclaration as GenerativeAIFunctionDeclaration,\n type FunctionDeclarationSchema as GenerativeAIFunctionDeclarationSchema,\n GenerateContentRequest,\n SafetySetting,\n Part as GenerativeAIPart,\n ModelParams,\n RequestOptions,\n type CachedContent,\n Schema,\n} from \"@google/generative-ai\";\nimport { CallbackManagerForLLMRun } from \"@langchain/core/callbacks/manager\";\nimport {\n AIMessageChunk,\n BaseMessage,\n UsageMetadata,\n} from \"@langchain/core/messages\";\nimport { ChatGenerationChunk, ChatResult } from \"@langchain/core/outputs\";\nimport { getEnvironmentVariable } from \"@langchain/core/utils/env\";\nimport {\n BaseChatModel,\n type BaseChatModelCallOptions,\n type LangSmithParams,\n type BaseChatModelParams,\n} from \"@langchain/core/language_models/chat_models\";\nimport { ModelProfile } from \"@langchain/core/language_models/profile\";\nimport { NewTokenIndices } from \"@langchain/core/callbacks/base\";\nimport {\n BaseLanguageModelInput,\n StructuredOutputMethodOptions,\n} from \"@langchain/core/language_models/base\";\nimport {\n Runnable,\n RunnablePassthrough,\n RunnableSequence,\n} from \"@langchain/core/runnables\";\nimport {\n InferInteropZodOutput,\n InteropZodType,\n isInteropZodSchema,\n} from \"@langchain/core/utils/types\";\nimport {\n BaseLLMOutputParser,\n JsonOutputParser,\n} from \"@langchain/core/output_parsers\";\nimport {\n schemaToGenerativeAIParameters,\n removeAdditionalProperties,\n} from \"./utils/zod_to_genai_parameters.js\";\nimport {\n convertBaseMessagesToContent,\n convertResponseContentToChatGenerationChunk,\n convertUsageMetadata,\n mapGenerateContentResultToChatResult,\n} from \"./utils/common.js\";\nimport { GoogleGenerativeAIToolsOutputParser } from \"./output_parsers.js\";\nimport {\n GoogleGenerativeAIThinkingConfig,\n GoogleGenerativeAIToolType,\n} from \"./types.js\";\nimport { convertToolsToGenAI } from \"./utils/tools.js\";\nimport PROFILES from \"./profiles.js\";\n\ninterface TokenUsage {\n completionTokens?: number;\n promptTokens?: number;\n totalTokens?: number;\n}\n\nexport type BaseMessageExamplePair = {\n input: BaseMessage;\n output: BaseMessage;\n};\n\nexport interface GoogleGenerativeAIChatCallOptions extends BaseChatModelCallOptions {\n tools?: GoogleGenerativeAIToolType[];\n /**\n * Allowed functions to call when the mode is \"any\".\n * If empty, any one of the provided functions are called.\n */\n allowedFunctionNames?: string[];\n /**\n * Whether or not to include usage data, like token counts\n * in the streamed response chunks.\n * @default true\n */\n streamUsage?: boolean;\n\n /**\n * JSON schema to be returned by the model.\n */\n responseSchema?: Schema;\n}\n\n/**\n * An interface defining the input to the ChatGoogleGenerativeAI class.\n */\nexport interface GoogleGenerativeAIChatInput\n extends\n BaseChatModelParams,\n Pick<GoogleGenerativeAIChatCallOptions, \"streamUsage\"> {\n /**\n * Model Name to use\n *\n * Note: The format must follow the pattern - `{model}`\n */\n model: ModelParams[\"model\"];\n\n /**\n * Controls the randomness of the output.\n *\n * Values can range from [0.0,2.0], inclusive. A value closer to 2.0\n * will produce responses that are more varied and creative, while\n * a value closer to 0.0 will typically result in less surprising\n * responses from the model.\n *\n * Note: The default value varies by model\n */\n temperature?: number;\n\n /**\n * Maximum number of tokens to generate in the completion.\n */\n maxOutputTokens?: number;\n\n /**\n * Top-p changes how the model selects tokens for output.\n *\n * Tokens are selected from most probable to least until the sum\n * of their probabilities equals the top-p value.\n *\n * For example, if tokens A, B, and C have a probability of\n * .3, .2, and .1 and the top-p value is .5, then the model will\n * select either A or B as the next token (using temperature).\n *\n * Note: The default value varies by model\n */\n topP?: number;\n\n /**\n * Top-k changes how the model selects tokens for output.\n *\n * A top-k of 1 means the selected token is the most probable among\n * all tokens in the model's vocabulary (also called greedy decoding),\n * while a top-k of 3 means that the next token is selected from\n * among the 3 most probable tokens (using temperature).\n *\n * Note: The default value varies by model\n */\n topK?: number;\n\n /**\n * The set of character sequences (up to 5) that will stop output generation.\n * If specified, the API will stop at the first appearance of a stop\n * sequence.\n *\n * Note: The stop sequence will not be included as part of the response.\n * Note: stopSequences is only supported for Gemini models\n */\n stopSequences?: string[];\n\n /**\n * A list of unique `SafetySetting` instances for blocking unsafe content. The API will block\n * any prompts and responses that fail to meet the thresholds set by these settings. If there\n * is no `SafetySetting` for a given `SafetyCategory` provided in the list, the API will use\n * the default safety setting for that category.\n */\n safetySettings?: SafetySetting[];\n\n /**\n * Google API key to use\n */\n apiKey?: string;\n\n /**\n * Google API version to use\n */\n apiVersion?: string;\n\n /**\n * Google API base URL to use\n */\n baseUrl?: string;\n\n /**\n * Google API custom headers to use\n */\n customHeaders?: Record<string, string>;\n\n /** Whether to stream the results or not */\n streaming?: boolean;\n\n /**\n * Whether or not to force the model to respond with JSON.\n * Available for `gemini-1.5` models and later.\n * @default false\n */\n json?: boolean;\n\n /**\n * Whether or not model supports system instructions.\n * The following models support system instructions:\n * - All Gemini 1.5 Pro model versions\n * - All Gemini 1.5 Flash model versions\n * - Gemini 1.0 Pro version gemini-1.0-pro-002\n */\n convertSystemMessageToHumanContent?: boolean | undefined;\n\n /**\n * Optional. Config for thinking features. An error will be returned if this\n * field is set for models that don't support thinking.\n */\n thinkingConfig?: GoogleGenerativeAIThinkingConfig;\n}\n\n/**\n * Google Generative AI chat model integration.\n *\n * Setup:\n * Install `@langchain/google-genai` and set an environment variable named `GOOGLE_API_KEY`.\n *\n * ```bash\n * npm install @langchain/google-genai\n * export GOOGLE_API_KEY=\"your-api-key\"\n * ```\n *\n * ## [Constructor args](https://api.js.langchain.com/classes/langchain_google_genai.ChatGoogleGenerativeAI.html#constructor)\n *\n * ## [Runtime args](https://api.js.langchain.com/interfaces/langchain_google_genai.GoogleGenerativeAIChatCallOptions.html)\n *\n * Runtime args can be passed as the second argument to any of the base runnable methods `.invoke`. `.stream`, `.batch`, etc.\n * They can also be passed via `.withConfig`, or the second arg in `.bindTools`, like shown in the examples below:\n *\n * ```typescript\n * // When calling `.withConfig`, call options should be passed via the first argument\n * const llmWithArgsBound = llm.withConfig({\n * stop: [\"\\n\"],\n * });\n *\n * // When calling `.bindTools`, call options should be passed via the second argument\n * const llmWithTools = llm.bindTools(\n * [...],\n * {\n * stop: [\"\\n\"],\n * }\n * );\n * ```\n *\n * ## Examples\n *\n * <details open>\n * <summary><strong>Instantiate</strong></summary>\n *\n * ```typescript\n * import { ChatGoogleGenerativeAI } from '@langchain/google-genai';\n *\n * const llm = new ChatGoogleGenerativeAI({\n * model: \"gemini-1.5-flash\",\n * temperature: 0,\n * maxRetries: 2,\n * // apiKey: \"...\",\n * // other params...\n * });\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Invoking</strong></summary>\n *\n * ```typescript\n * const input = `Translate \"I love programming\" into French.`;\n *\n * // Models also accept a list of chat messages or a formatted prompt\n * const result = await llm.invoke(input);\n * console.log(result);\n * ```\n *\n * ```txt\n * AIMessage {\n * \"content\": \"There are a few ways to translate \\\"I love programming\\\" into French, depending on the level of formality and nuance you want to convey:\\n\\n**Formal:**\\n\\n* **J'aime la programmation.** (This is the most literal and formal translation.)\\n\\n**Informal:**\\n\\n* **J'adore programmer.** (This is a more enthusiastic and informal translation.)\\n* **J'aime beaucoup programmer.** (This is a slightly less enthusiastic but still informal translation.)\\n\\n**More specific:**\\n\\n* **J'aime beaucoup coder.** (This specifically refers to writing code.)\\n* **J'aime beaucoup développer des logiciels.** (This specifically refers to developing software.)\\n\\nThe best translation will depend on the context and your intended audience. \\n\",\n * \"response_metadata\": {\n * \"finishReason\": \"STOP\",\n * \"index\": 0,\n * \"safetyRatings\": [\n * {\n * \"category\": \"HARM_CATEGORY_SEXUALLY_EXPLICIT\",\n * \"probability\": \"NEGLIGIBLE\"\n * },\n * {\n * \"category\": \"HARM_CATEGORY_HATE_SPEECH\",\n * \"probability\": \"NEGLIGIBLE\"\n * },\n * {\n * \"category\": \"HARM_CATEGORY_HARASSMENT\",\n * \"probability\": \"NEGLIGIBLE\"\n * },\n * {\n * \"category\": \"HARM_CATEGORY_DANGEROUS_CONTENT\",\n * \"probability\": \"NEGLIGIBLE\"\n * }\n * ]\n * },\n * \"usage_metadata\": {\n * \"input_tokens\": 10,\n * \"output_tokens\": 149,\n * \"total_tokens\": 159\n * }\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Streaming Chunks</strong></summary>\n *\n * ```typescript\n * for await (const chunk of await llm.stream(input)) {\n * console.log(chunk);\n * }\n * ```\n *\n * ```txt\n * AIMessageChunk {\n * \"content\": \"There\",\n * \"response_metadata\": {\n * \"index\": 0\n * }\n * \"usage_metadata\": {\n * \"input_tokens\": 10,\n * \"output_tokens\": 1,\n * \"total_tokens\": 11\n * }\n * }\n * AIMessageChunk {\n * \"content\": \" are a few ways to translate \\\"I love programming\\\" into French, depending on\",\n * }\n * AIMessageChunk {\n * \"content\": \" the level of formality and nuance you want to convey:\\n\\n**Formal:**\\n\\n\",\n * }\n * AIMessageChunk {\n * \"content\": \"* **J'aime la programmation.** (This is the most literal and formal translation.)\\n\\n**Informal:**\\n\\n* **J'adore programmer.** (This\",\n * }\n * AIMessageChunk {\n * \"content\": \" is a more enthusiastic and informal translation.)\\n* **J'aime beaucoup programmer.** (This is a slightly less enthusiastic but still informal translation.)\\n\\n**More\",\n * }\n * AIMessageChunk {\n * \"content\": \" specific:**\\n\\n* **J'aime beaucoup coder.** (This specifically refers to writing code.)\\n* **J'aime beaucoup développer des logiciels.** (This specifically refers to developing software.)\\n\\nThe best translation will depend on the context and\",\n * }\n * AIMessageChunk {\n * \"content\": \" your intended audience. \\n\",\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Aggregate Streamed Chunks</strong></summary>\n *\n * ```typescript\n * import { AIMessageChunk } from '@langchain/core/messages';\n * import { concat } from '@langchain/core/utils/stream';\n *\n * const stream = await llm.stream(input);\n * let full: AIMessageChunk | undefined;\n * for await (const chunk of stream) {\n * full = !full ? chunk : concat(full, chunk);\n * }\n * console.log(full);\n * ```\n *\n * ```txt\n * AIMessageChunk {\n * \"content\": \"There are a few ways to translate \\\"I love programming\\\" into French, depending on the level of formality and nuance you want to convey:\\n\\n**Formal:**\\n\\n* **J'aime la programmation.** (This is the most literal and formal translation.)\\n\\n**Informal:**\\n\\n* **J'adore programmer.** (This is a more enthusiastic and informal translation.)\\n* **J'aime beaucoup programmer.** (This is a slightly less enthusiastic but still informal translation.)\\n\\n**More specific:**\\n\\n* **J'aime beaucoup coder.** (This specifically refers to writing code.)\\n* **J'aime beaucoup développer des logiciels.** (This specifically refers to developing software.)\\n\\nThe best translation will depend on the context and your intended audience. \\n\",\n * \"usage_metadata\": {\n * \"input_tokens\": 10,\n * \"output_tokens\": 277,\n * \"total_tokens\": 287\n * }\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Bind tools</strong></summary>\n *\n * ```typescript\n * import { z } from 'zod';\n *\n * const GetWeather = {\n * name: \"GetWeather\",\n * description: \"Get the current weather in a given location\",\n * schema: z.object({\n * location: z.string().describe(\"The city and state, e.g. San Francisco, CA\")\n * }),\n * }\n *\n * const GetPopulation = {\n * name: \"GetPopulation\",\n * description: \"Get the current population in a given location\",\n * schema: z.object({\n * location: z.string().describe(\"The city and state, e.g. San Francisco, CA\")\n * }),\n * }\n *\n * const llmWithTools = llm.bindTools([GetWeather, GetPopulation]);\n * const aiMsg = await llmWithTools.invoke(\n * \"Which city is hotter today and which is bigger: LA or NY?\"\n * );\n * console.log(aiMsg.tool_calls);\n * ```\n *\n * ```txt\n * [\n * {\n * name: 'GetWeather',\n * args: { location: 'Los Angeles, CA' },\n * type: 'tool_call'\n * },\n * {\n * name: 'GetWeather',\n * args: { location: 'New York, NY' },\n * type: 'tool_call'\n * },\n * {\n * name: 'GetPopulation',\n * args: { location: 'Los Angeles, CA' },\n * type: 'tool_call'\n * },\n * {\n * name: 'GetPopulation',\n * args: { location: 'New York, NY' },\n * type: 'tool_call'\n * }\n * ]\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Structured Output</strong></summary>\n *\n * ```typescript\n * const Joke = z.object({\n * setup: z.string().describe(\"The setup of the joke\"),\n * punchline: z.string().describe(\"The punchline to the joke\"),\n * rating: z.number().optional().describe(\"How funny the joke is, from 1 to 10\")\n * }).describe('Joke to tell user.');\n *\n * const structuredLlm = llm.withStructuredOutput(Joke, { name: \"Joke\" });\n * const jokeResult = await structuredLlm.invoke(\"Tell me a joke about cats\");\n * console.log(jokeResult);\n * ```\n *\n * ```txt\n * {\n * setup: \"Why don\\\\'t cats play poker?\",\n * punchline: \"Why don\\\\'t cats play poker? Because they always have an ace up their sleeve!\"\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Multimodal</strong></summary>\n *\n * ```typescript\n * import { HumanMessage } from '@langchain/core/messages';\n *\n * const imageUrl = \"https://example.com/image.jpg\";\n * const imageData = await fetch(imageUrl).then(res => res.arrayBuffer());\n * const base64Image = Buffer.from(imageData).toString('base64');\n *\n * const message = new HumanMessage({\n * content: [\n * { type: \"text\", text: \"describe the weather in this image\" },\n * {\n * type: \"image_url\",\n * image_url: { url: `data:image/jpeg;base64,${base64Image}` },\n * },\n * ]\n * });\n *\n * const imageDescriptionAiMsg = await llm.invoke([message]);\n * console.log(imageDescriptionAiMsg.content);\n * ```\n *\n * ```txt\n * The weather in the image appears to be clear and sunny. The sky is mostly blue with a few scattered white clouds, indicating fair weather. The bright sunlight is casting shadows on the green, grassy hill, suggesting it is a pleasant day with good visibility. There are no signs of rain or stormy conditions.\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Usage Metadata</strong></summary>\n *\n * ```typescript\n * const aiMsgForMetadata = await llm.invoke(input);\n * console.log(aiMsgForMetadata.usage_metadata);\n * ```\n *\n * ```txt\n * { input_tokens: 10, output_tokens: 149, total_tokens: 159 }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Response Metadata</strong></summary>\n *\n * ```typescript\n * const aiMsgForResponseMetadata = await llm.invoke(input);\n * console.log(aiMsgForResponseMetadata.response_metadata);\n * ```\n *\n * ```txt\n * {\n * finishReason: 'STOP',\n * index: 0,\n * safetyRatings: [\n * {\n * category: 'HARM_CATEGORY_SEXUALLY_EXPLICIT',\n * probability: 'NEGLIGIBLE'\n * },\n * {\n * category: 'HARM_CATEGORY_HATE_SPEECH',\n * probability: 'NEGLIGIBLE'\n * },\n * { category: 'HARM_CATEGORY_HARASSMENT', probability: 'NEGLIGIBLE' },\n * {\n * category: 'HARM_CATEGORY_DANGEROUS_CONTENT',\n * probability: 'NEGLIGIBLE'\n * }\n * ]\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Document Messages</strong></summary>\n *\n * This example will show you how to pass documents such as PDFs to Google\n * Generative AI through messages.\n *\n * ```typescript\n * const pdfPath = \"/Users/my_user/Downloads/invoice.pdf\";\n * const pdfBase64 = await fs.readFile(pdfPath, \"base64\");\n *\n * const response = await llm.invoke([\n * [\"system\", \"Use the provided documents to answer the question\"],\n * [\n * \"user\",\n * [\n * {\n * type: \"application/pdf\", // If the `type` field includes a single slash (`/`), it will be treated as inline data.\n * data: pdfBase64,\n * },\n * {\n * type: \"text\",\n * text: \"Summarize the contents of this PDF\",\n * },\n * ],\n * ],\n * ]);\n *\n * console.log(response.content);\n * ```\n *\n * ```txt\n * This is a billing invoice from Twitter Developers for X API Basic Access. The transaction date is January 7, 2025,\n * and the amount is $194.34, which has been paid. The subscription period is from January 7, 2025 21:02 to February 7, 2025 00:00 (UTC).\n * The tax is $0.00, with a tax rate of 0%. The total amount is $194.34. The payment was made using a Visa card ending in 7022,\n * expiring in 12/2026. The billing address is Brace Sproul, 1234 Main Street, San Francisco, CA, US 94103. The company being billed is\n * X Corp, located at 865 FM 1209 Building 2, Bastrop, TX, US 78602. Terms and conditions apply.\n * ```\n * </details>\n *\n * <br />\n */\nexport class ChatGoogleGenerativeAI\n extends BaseChatModel<GoogleGenerativeAIChatCallOptions, AIMessageChunk>\n implements GoogleGenerativeAIChatInput\n{\n static lc_name() {\n return \"ChatGoogleGenerativeAI\";\n }\n\n lc_serializable = true;\n\n get lc_secrets(): { [key: string]: string } | undefined {\n return {\n apiKey: \"GOOGLE_API_KEY\",\n };\n }\n\n lc_namespace = [\"langchain\", \"chat_models\", \"google_genai\"];\n\n get lc_aliases() {\n return {\n apiKey: \"google_api_key\",\n };\n }\n\n model: string;\n\n temperature?: number; // default value chosen based on model\n\n maxOutputTokens?: number;\n\n topP?: number; // default value chosen based on model\n\n topK?: number; // default value chosen based on model\n\n stopSequences: string[] = [];\n\n safetySettings?: SafetySetting[];\n\n apiKey?: string;\n\n streaming = false;\n\n json?: boolean;\n\n streamUsage = true;\n\n convertSystemMessageToHumanContent: boolean | undefined;\n\n thinkingConfig?: GoogleGenerativeAIThinkingConfig;\n\n private client: GenerativeModel;\n\n get _isMultimodalModel() {\n return (\n this.model.includes(\"vision\") ||\n this.model.startsWith(\"gemini-1.5\") ||\n this.model.startsWith(\"gemini-2\") ||\n (this.model.startsWith(\"gemma-3-\") &&\n !this.model.startsWith(\"gemma-3-1b\")) || // gemma-3 models are multimodal(but gemma-3n-* and gemma-3-1b are not)\n this.model.startsWith(\"gemini-3\")\n );\n }\n\n constructor(\n model: ModelParams[\"model\"],\n fields?: Omit<GoogleGenerativeAIChatInput, \"model\">\n );\n constructor(fields: GoogleGenerativeAIChatInput);\n constructor(\n modelOrFields: ModelParams[\"model\"] | GoogleGenerativeAIChatInput,\n fieldsArg?: Omit<GoogleGenerativeAIChatInput, \"model\">\n ) {\n const fields =\n typeof modelOrFields === \"string\"\n ? { ...(fieldsArg ?? {}), model: modelOrFields }\n : modelOrFields;\n super(fields);\n\n this.model = fields.model.replace(/^models\\//, \"\");\n\n this.maxOutputTokens = fields.maxOutputTokens ?? this.maxOutputTokens;\n\n if (this.maxOutputTokens && this.maxOutputTokens < 0) {\n throw new Error(\"`maxOutputTokens` must be a positive integer\");\n }\n\n this.temperature = fields.temperature ?? this.temperature;\n if (this.temperature && (this.temperature < 0 || this.temperature > 2)) {\n throw new Error(\"`temperature` must be in the range of [0.0,2.0]\");\n }\n\n this.topP = fields.topP ?? this.topP;\n if (this.topP && this.topP < 0) {\n throw new Error(\"`topP` must be a positive integer\");\n }\n\n if (this.topP && this.topP > 1) {\n throw new Error(\"`topP` must be below 1.\");\n }\n\n this.topK = fields.topK ?? this.topK;\n if (this.topK && this.topK < 0) {\n throw new Error(\"`topK` must be a positive integer\");\n }\n\n this.stopSequences = fields.stopSequences ?? this.stopSequences;\n\n this.apiKey = fields.apiKey ?? getEnvironmentVariable(\"GOOGLE_API_KEY\");\n if (!this.apiKey) {\n throw new Error(\n \"Please set an API key for Google GenerativeAI \" +\n \"in the environment variable GOOGLE_API_KEY \" +\n \"or in the `apiKey` field of the \" +\n \"ChatGoogleGenerativeAI constructor\"\n );\n }\n\n this.safetySettings = fields.safetySettings ?? this.safetySettings;\n if (this.safetySettings && this.safetySettings.length > 0) {\n const safetySettingsSet = new Set(\n this.safetySettings.map((s) => s.category)\n );\n if (safetySettingsSet.size !== this.safetySettings.length) {\n throw new Error(\n \"The categories in `safetySettings` array must be unique\"\n );\n }\n }\n\n this.streaming = fields.streaming ?? this.streaming;\n this.json = fields.json;\n\n this.thinkingConfig = fields.thinkingConfig ?? this.thinkingConfig;\n\n this.client = new GenerativeAI(this.apiKey).getGenerativeModel(\n {\n model: this.model,\n safetySettings: this.safetySettings as SafetySetting[],\n generationConfig: {\n stopSequences: this.stopSequences,\n maxOutputTokens: this.maxOutputTokens,\n temperature: this.temperature,\n topP: this.topP,\n topK: this.topK,\n ...(this.json ? { responseMimeType: \"application/json\" } : {}),\n ...(this.thinkingConfig\n ? { thinkingConfig: this.thinkingConfig }\n : {}),\n },\n },\n {\n apiVersion: fields.apiVersion,\n baseUrl: fields.baseUrl,\n customHeaders: fields.customHeaders,\n }\n );\n this.streamUsage = fields.streamUsage ?? this.streamUsage;\n }\n\n useCachedContent(\n cachedContent: CachedContent,\n modelParams?: ModelParams,\n requestOptions?: RequestOptions\n ): void {\n if (!this.apiKey) return;\n this.client = new GenerativeAI(\n this.apiKey\n ).getGenerativeModelFromCachedContent(\n cachedContent,\n modelParams,\n requestOptions\n );\n }\n\n get useSystemInstruction(): boolean {\n return typeof this.convertSystemMessageToHumanContent === \"boolean\"\n ? !this.convertSystemMessageToHumanContent\n : this.computeUseSystemInstruction;\n }\n\n get computeUseSystemInstruction(): boolean {\n // This works on models from April 2024 and later\n // Vertex AI: gemini-1.5-pro and gemini-1.0-002 and later\n // AI Studio: gemini-1.5-pro-latest\n if (this.model === \"gemini-1.0-pro-001\") {\n return false;\n } else if (this.model.startsWith(\"gemini-pro-vision\")) {\n return false;\n } else if (this.model.startsWith(\"gemini-1.0-pro-vision\")) {\n return false;\n } else if (this.model === \"gemini-pro\") {\n // on AI Studio gemini-pro is still pointing at gemini-1.0-pro-001\n return false;\n }\n return true;\n }\n\n getLsParams(options: this[\"ParsedCallOptions\"]): LangSmithParams {\n return {\n ls_provider: \"google_genai\",\n ls_model_name: this.model,\n ls_model_type: \"chat\",\n ls_temperature: this.client.generationConfig.temperature,\n ls_max_tokens: this.client.generationConfig.maxOutputTokens,\n ls_stop: options.stop,\n };\n }\n\n _combineLLMOutput() {\n return [];\n }\n\n _llmType() {\n return \"googlegenerativeai\";\n }\n\n override bindTools(\n tools: GoogleGenerativeAIToolType[],\n kwargs?: Partial<GoogleGenerativeAIChatCallOptions>\n ): Runnable<\n BaseLanguageModelInput,\n AIMessageChunk,\n GoogleGenerativeAIChatCallOptions\n > {\n return this.withConfig({\n tools: convertToolsToGenAI(tools)?.tools,\n ...kwargs,\n });\n }\n\n invocationParams(\n options?: this[\"ParsedCallOptions\"]\n ): Omit<GenerateContentRequest, \"contents\"> {\n const toolsAndConfig = options?.tools?.length\n ? convertToolsToGenAI(options.tools, {\n toolChoice: options.tool_choice,\n allowedFunctionNames: options.allowedFunctionNames,\n })\n : undefined;\n\n if (options?.responseSchema) {\n this.client.generationConfig.responseSchema = options.responseSchema;\n this.client.generationConfig.responseMimeType = \"application/json\";\n } else {\n this.client.generationConfig.responseSchema = undefined;\n this.client.generationConfig.responseMimeType = this.json\n ? \"application/json\"\n : undefined;\n }\n\n return {\n ...(toolsAndConfig?.tools ? { tools: toolsAndConfig.tools } : {}),\n ...(toolsAndConfig?.toolConfig\n ? { toolConfig: toolsAndConfig.toolConfig }\n : {}),\n };\n }\n\n async _generate(\n messages: BaseMessage[],\n options: this[\"ParsedCallOptions\"],\n runManager?: CallbackManagerForLLMRun\n ): Promise<ChatResult> {\n options.signal?.throwIfAborted();\n const prompt = convertBaseMessagesToContent(\n messages,\n this._isMultimodalModel,\n this.useSystemInstruction,\n this.model\n );\n let actualPrompt = prompt;\n if (prompt[0].role === \"system\") {\n const [systemInstruction] = prompt;\n this.client.systemInstruction = systemInstruction;\n actualPrompt = prompt.slice(1);\n }\n const parameters = this.invocationParams(options);\n\n // Handle streaming\n if (this.streaming) {\n const tokenUsage: TokenUsage = {};\n const stream = this._streamResponseChunks(messages, options, runManager);\n const finalChunks: ChatGenerationChunk[] = [];\n\n for await (const chunk of stream) {\n const index =\n (chunk.generationInfo as NewTokenIndices)?.completion ?? 0;\n if (finalChunks[index] === undefined) {\n finalChunks[index] = chunk;\n } else {\n finalChunks[index] = finalChunks[index].concat(chunk);\n }\n }\n const generations = finalChunks.filter(\n (c): c is ChatGenerationChunk => c !== undefined\n );\n\n return { generations, llmOutput: { estimatedTokenUsage: tokenUsage } };\n }\n\n const res = await this.completionWithRetry({\n ...parameters,\n contents: actualPrompt,\n });\n\n let usageMetadata: UsageMetadata | undefined;\n if (\"usageMetadata\" in res.response) {\n usageMetadata = convertUsageMetadata(\n res.response.usageMetadata,\n this.model\n );\n }\n\n const generationResult = mapGenerateContentResultToChatResult(\n res.response,\n {\n usageMetadata,\n }\n );\n // may not have generations in output if there was a refusal for safety reasons, malformed function call, etc.\n if (generationResult.generations?.length > 0) {\n await runManager?.handleLLMNewToken(\n generationResult.generations[0]?.text ?? \"\"\n );\n }\n return generationResult;\n }\n\n async *_streamResponseChunks(\n messages: BaseMessage[],\n options: this[\"ParsedCallOptions\"],\n runManager?: CallbackManagerForLLMRun\n ): AsyncGenerator<ChatGenerationChunk> {\n const prompt = convertBaseMessagesToContent(\n messages,\n this._isMultimodalModel,\n this.useSystemInstruction,\n this.model\n );\n let actualPrompt = prompt;\n if (prompt[0].role === \"system\") {\n const [systemInstruction] = prompt;\n this.client.systemInstruction = systemInstruction;\n actualPrompt = prompt.slice(1);\n }\n const parameters = this.invocationParams(options);\n const request = {\n ...parameters,\n contents: actualPrompt,\n };\n const stream = await this.caller.callWithOptions(\n { signal: options?.signal },\n async () => {\n const { stream } = await this.client.generateContentStream(request, {\n signal: options?.signal,\n });\n return stream;\n }\n );\n\n let usageMetadata: UsageMetadata | undefined;\n // Keep prior cumulative counts for calculating token deltas while streaming\n let prevPromptTokenCount = 0;\n let prevCandidatesTokenCount = 0;\n let prevTotalTokenCount = 0;\n let index = 0;\n for await (const response of stream) {\n if (options.signal?.aborted) {\n return;\n }\n if (\n \"usageMetadata\" in response &&\n response.usageMetadata !== undefined &&\n this.streamUsage !== false &&\n options.streamUsage !== false\n ) {\n usageMetadata = convertUsageMetadata(\n response.usageMetadata,\n this.model\n );\n\n // Under the hood, LangChain combines the prompt tokens. Google returns the updated\n // total each time, so we need to find the difference between the tokens.\n const newPromptTokenCount =\n response.usageMetadata.promptTokenCount ?? 0;\n usageMetadata.input_tokens = Math.max(\n 0,\n newPromptTokenCount - prevPromptTokenCount\n );\n prevPromptTokenCount = newPromptTokenCount;\n\n const newCandidatesTokenCount =\n response.usageMetadata.candidatesTokenCount ?? 0;\n usageMetadata.output_tokens = Math.max(\n 0,\n newCandidatesTokenCount - prevCandidatesTokenCount\n );\n prevCandidatesTokenCount = newCandidatesTokenCount;\n\n const newTotalTokenCount = response.usageMetadata.totalTokenCount ?? 0;\n usageMetadata.total_tokens = Math.max(\n 0,\n newTotalTokenCount - prevTotalTokenCount\n );\n prevTotalTokenCount = newTotalTokenCount;\n }\n\n const chunk = convertResponseContentToChatGenerationChunk(response, {\n usageMetadata,\n index,\n });\n index += 1;\n if (!chunk) {\n continue;\n }\n\n yield chunk;\n await runManager?.handleLLMNewToken(chunk.text ?? \"\");\n }\n }\n\n async completionWithRetry(\n request: string | GenerateContentRequest | (string | GenerativeAIPart)[],\n options?: this[\"ParsedCallOptions\"]\n ) {\n return this.caller.callWithOptions(\n { signal: options?.signal },\n async () => {\n try {\n return await this.client.generateContent(request, {\n signal: options?.signal,\n });\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } catch (e: any) {\n // TODO: Improve error handling\n if (e.message?.includes(\"400 Bad Request\")) {\n e.status = 400;\n }\n throw e;\n }\n }\n );\n }\n\n /**\n * Return profiling information for the model.\n *\n * Provides information about the model's capabilities and constraints,\n * including token limits, multimodal support, and advanced features like\n * tool calling and structured output.\n *\n * @returns {ModelProfile} An object describing the model's capabilities and constraints\n *\n * @example\n * ```typescript\n * const model = new ChatGoogleGenerativeAI({ model: \"gemini-1.5-flash\" });\n * const profile = model.profile;\n * console.log(profile.maxInputTokens); // 2000000\n * console.log(profile.imageInputs); // true\n * ```\n */\n get profile(): ModelProfile {\n return PROFILES[this.model] ?? {};\n }\n\n withStructuredOutput<\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n outputSchema:\n | InteropZodType<RunOutput>\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<false>\n ): Runnable<BaseLanguageModelInput, RunOutput>;\n\n withStructuredOutput<\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n outputSchema:\n | InteropZodType<RunOutput>\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<true>\n ): Runnable<BaseLanguageModelInput, { raw: BaseMessage; parsed: RunOutput }>;\n\n withStructuredOutput<\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n outputSchema:\n | InteropZodType<RunOutput>\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<boolean>\n ):\n | Runnable<BaseLanguageModelInput, RunOutput>\n | Runnable<\n BaseLanguageModelInput,\n { raw: BaseMessage; parsed: RunOutput }\n > {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const schema: InteropZodType<RunOutput> | Record<string, any> =\n outputSchema;\n const name = config?.name;\n const method = config?.method;\n const includeRaw = config?.includeRaw;\n if (method === \"jsonMode\") {\n throw new Error(\n `ChatGoogleGenerativeAI only supports \"jsonSchema\" or \"functionCalling\" as a method.`\n );\n }\n\n let llm;\n let outputParser: BaseLLMOutputParser<RunOutput>;\n if (method === \"functionCalling\") {\n let functionName = name ?? \"extract\";\n let tools: GoogleGenerativeAIFunctionDeclarationsTool[];\n if (isInteropZodSchema(schema)) {\n const jsonSchema = schemaToGenerativeAIParameters(schema);\n tools = [\n {\n functionDeclarations: [\n {\n name: functionName,\n description:\n jsonSchema.description ?? \"A function available to call.\",\n parameters: jsonSchema as GenerativeAIFunctionDeclarationSchema,\n },\n ],\n },\n ];\n outputParser = new GoogleGenerativeAIToolsOutputParser<\n InferInteropZodOutput<typeof schema>\n >({\n returnSingle: true,\n keyName: functionName,\n zodSchema: schema,\n });\n } else {\n let geminiFunctionDefinition: GenerativeAIFunctionDeclaration;\n if (\n typeof schema.name === \"string\" &&\n typeof schema.parameters === \"object\" &&\n schema.parameters != null\n ) {\n geminiFunctionDefinition = schema as GenerativeAIFunctionDeclaration;\n geminiFunctionDefinition.parameters = removeAdditionalProperties(\n schema.parameters\n ) as GenerativeAIFunctionDeclarationSchema;\n functionName = schema.name;\n } else {\n geminiFunctionDefinition = {\n name: functionName,\n description: schema.description ?? \"\",\n parameters: removeAdditionalProperties(\n schema\n ) as GenerativeAIFunctionDeclarationSchema,\n };\n }\n tools = [\n {\n functionDeclarations: [geminiFunctionDefinition],\n },\n ];\n outputParser = new GoogleGenerativeAIToolsOutputParser<RunOutput>({\n returnSingle: true,\n keyName: functionName,\n });\n }\n llm = this.bindTools(tools).withConfig({\n allowedFunctionNames: [functionName],\n });\n } else {\n const jsonSchema = schemaToGenerativeAIParameters(schema);\n llm = this.withConfig({\n responseSchema: jsonSchema as Schema,\n });\n outputParser = new JsonOutputParser();\n }\n\n if (!includeRaw) {\n return llm.pipe(outputParser).withConfig({\n runName: \"ChatGoogleGenerativeAIStructuredOutput\",\n }) as Runnable<BaseLanguageModelInput, RunOutput>;\n }\n\n const parserAssign = RunnablePassthrough.assign({\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n parsed: (input: any, config) => outputParser.invoke(input.raw, config),\n });\n const parserNone = RunnablePassthrough.assign({\n parsed: () => null,\n });\n const parsedWithFallback = parserAssign.withFallbacks({\n fallbacks: [parserNone],\n });\n return RunnableSequence.from<\n BaseLanguageModelInput,\n { raw: BaseMessage; parsed: RunOutput }\n >([\n {\n raw: llm,\n },\n parsedWithFallback,\n ]).withConfig({\n runName: \"StructuredOutputRunnable\",\n });\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAilBA,IAAa,yBAAb,cACUA,0DAEV;CACE,OAAO,UAAU;AACf,SAAO;;CAGT,kBAAkB;CAElB,IAAI,aAAoD;AACtD,SAAO,EACL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erativeAI","convertToolsToGenAI","convertBaseMessagesToContent","convertUsageMetadata","mapGenerateContentResultToChatResult","convertResponseContentToChatGenerationChunk","PROFILES","schemaToGenerativeAIParameters","GoogleGenerativeAIToolsOutputParser","removeAdditionalProperties","JsonOutputParser","RunnablePassthrough","RunnableSequence"],"sources":["../src/chat_models.ts"],"sourcesContent":["import {\n GenerativeModel,\n GoogleGenerativeAI as GenerativeAI,\n FunctionDeclarationsTool as GoogleGenerativeAIFunctionDeclarationsTool,\n FunctionDeclaration as GenerativeAIFunctionDeclaration,\n type FunctionDeclarationSchema as GenerativeAIFunctionDeclarationSchema,\n GenerateContentRequest,\n SafetySetting,\n Part as GenerativeAIPart,\n ModelParams,\n RequestOptions,\n type CachedContent,\n Schema,\n} from \"@google/generative-ai\";\nimport { CallbackManagerForLLMRun } from \"@langchain/core/callbacks/manager\";\nimport {\n AIMessageChunk,\n BaseMessage,\n UsageMetadata,\n} from \"@langchain/core/messages\";\nimport { ChatGenerationChunk, ChatResult } from \"@langchain/core/outputs\";\nimport { getEnvironmentVariable } from \"@langchain/core/utils/env\";\nimport {\n BaseChatModel,\n type BaseChatModelCallOptions,\n type LangSmithParams,\n type BaseChatModelParams,\n} from \"@langchain/core/language_models/chat_models\";\nimport { ModelProfile } from \"@langchain/core/language_models/profile\";\nimport { NewTokenIndices } from \"@langchain/core/callbacks/base\";\nimport {\n BaseLanguageModelInput,\n StructuredOutputMethodOptions,\n} from \"@langchain/core/language_models/base\";\nimport {\n Runnable,\n RunnablePassthrough,\n RunnableSequence,\n} from \"@langchain/core/runnables\";\nimport {\n InferInteropZodOutput,\n InteropZodType,\n isInteropZodSchema,\n} from \"@langchain/core/utils/types\";\nimport {\n BaseLLMOutputParser,\n JsonOutputParser,\n} from \"@langchain/core/output_parsers\";\nimport {\n schemaToGenerativeAIParameters,\n removeAdditionalProperties,\n} from \"./utils/zod_to_genai_parameters.js\";\nimport {\n convertBaseMessagesToContent,\n convertResponseContentToChatGenerationChunk,\n convertUsageMetadata,\n mapGenerateContentResultToChatResult,\n} from \"./utils/common.js\";\nimport { GoogleGenerativeAIToolsOutputParser } from \"./output_parsers.js\";\nimport {\n GoogleGenerativeAIThinkingConfig,\n GoogleGenerativeAIToolType,\n} from \"./types.js\";\nimport { convertToolsToGenAI } from \"./utils/tools.js\";\nimport PROFILES from \"./profiles.js\";\n\ninterface TokenUsage {\n completionTokens?: number;\n promptTokens?: number;\n totalTokens?: number;\n}\n\nexport type BaseMessageExamplePair = {\n input: BaseMessage;\n output: BaseMessage;\n};\n\nexport interface GoogleGenerativeAIChatCallOptions extends BaseChatModelCallOptions {\n tools?: GoogleGenerativeAIToolType[];\n /**\n * Allowed functions to call when the mode is \"any\".\n * If empty, any one of the provided functions are called.\n */\n allowedFunctionNames?: string[];\n /**\n * Whether or not to include usage data, like token counts\n * in the streamed response chunks.\n * @default true\n */\n streamUsage?: boolean;\n\n /**\n * JSON schema to be returned by the model.\n */\n responseSchema?: Schema;\n}\n\n/**\n * An interface defining the input to the ChatGoogleGenerativeAI class.\n */\nexport interface GoogleGenerativeAIChatInput\n extends\n BaseChatModelParams,\n Pick<GoogleGenerativeAIChatCallOptions, \"streamUsage\"> {\n /**\n * Model Name to use\n *\n * Note: The format must follow the pattern - `{model}`\n */\n model: ModelParams[\"model\"];\n\n /**\n * Controls the randomness of the output.\n *\n * Values can range from [0.0,2.0], inclusive. A value closer to 2.0\n * will produce responses that are more varied and creative, while\n * a value closer to 0.0 will typically result in less surprising\n * responses from the model.\n *\n * Note: The default value varies by model\n */\n temperature?: number;\n\n /**\n * Maximum number of tokens to generate in the completion.\n */\n maxOutputTokens?: number;\n\n /**\n * Top-p changes how the model selects tokens for output.\n *\n * Tokens are selected from most probable to least until the sum\n * of their probabilities equals the top-p value.\n *\n * For example, if tokens A, B, and C have a probability of\n * .3, .2, and .1 and the top-p value is .5, then the model will\n * select either A or B as the next token (using temperature).\n *\n * Note: The default value varies by model\n */\n topP?: number;\n\n /**\n * Top-k changes how the model selects tokens for output.\n *\n * A top-k of 1 means the selected token is the most probable among\n * all tokens in the model's vocabulary (also called greedy decoding),\n * while a top-k of 3 means that the next token is selected from\n * among the 3 most probable tokens (using temperature).\n *\n * Note: The default value varies by model\n */\n topK?: number;\n\n /**\n * The set of character sequences (up to 5) that will stop output generation.\n * If specified, the API will stop at the first appearance of a stop\n * sequence.\n *\n * Note: The stop sequence will not be included as part of the response.\n * Note: stopSequences is only supported for Gemini models\n */\n stopSequences?: string[];\n\n /**\n * A list of unique `SafetySetting` instances for blocking unsafe content. The API will block\n * any prompts and responses that fail to meet the thresholds set by these settings. If there\n * is no `SafetySetting` for a given `SafetyCategory` provided in the list, the API will use\n * the default safety setting for that category.\n */\n safetySettings?: SafetySetting[];\n\n /**\n * Google API key to use\n */\n apiKey?: string;\n\n /**\n * Google API version to use\n */\n apiVersion?: string;\n\n /**\n * Google API base URL to use\n */\n baseUrl?: string;\n\n /**\n * Google API custom headers to use\n */\n customHeaders?: Record<string, string>;\n\n /** Whether to stream the results or not */\n streaming?: boolean;\n\n /**\n * Whether or not to force the model to respond with JSON.\n * Available for `gemini-1.5` models and later.\n * @default false\n */\n json?: boolean;\n\n /**\n * Whether or not model supports system instructions.\n * The following models support system instructions:\n * - All Gemini 1.5 Pro model versions\n * - All Gemini 1.5 Flash model versions\n * - Gemini 1.0 Pro version gemini-1.0-pro-002\n */\n convertSystemMessageToHumanContent?: boolean | undefined;\n\n /**\n * Optional. Config for thinking features. An error will be returned if this\n * field is set for models that don't support thinking.\n */\n thinkingConfig?: GoogleGenerativeAIThinkingConfig;\n}\n\n/**\n * Google Generative AI chat model integration.\n *\n * Setup:\n * Install `@langchain/google-genai` and set an environment variable named `GOOGLE_API_KEY`.\n *\n * ```bash\n * npm install @langchain/google-genai\n * export GOOGLE_API_KEY=\"your-api-key\"\n * ```\n *\n * ## [Constructor args](https://api.js.langchain.com/classes/langchain_google_genai.ChatGoogleGenerativeAI.html#constructor)\n *\n * ## [Runtime args](https://api.js.langchain.com/interfaces/langchain_google_genai.GoogleGenerativeAIChatCallOptions.html)\n *\n * Runtime args can be passed as the second argument to any of the base runnable methods `.invoke`. `.stream`, `.batch`, etc.\n * They can also be passed via `.withConfig`, or the second arg in `.bindTools`, like shown in the examples below:\n *\n * ```typescript\n * // When calling `.withConfig`, call options should be passed via the first argument\n * const llmWithArgsBound = llm.withConfig({\n * stop: [\"\\n\"],\n * });\n *\n * // When calling `.bindTools`, call options should be passed via the second argument\n * const llmWithTools = llm.bindTools(\n * [...],\n * {\n * stop: [\"\\n\"],\n * }\n * );\n * ```\n *\n * ## Examples\n *\n * <details open>\n * <summary><strong>Instantiate</strong></summary>\n *\n * ```typescript\n * import { ChatGoogleGenerativeAI } from '@langchain/google-genai';\n *\n * const llm = new ChatGoogleGenerativeAI({\n * model: \"gemini-1.5-flash\",\n * temperature: 0,\n * maxRetries: 2,\n * // apiKey: \"...\",\n * // other params...\n * });\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Invoking</strong></summary>\n *\n * ```typescript\n * const input = `Translate \"I love programming\" into French.`;\n *\n * // Models also accept a list of chat messages or a formatted prompt\n * const result = await llm.invoke(input);\n * console.log(result);\n * ```\n *\n * ```txt\n * AIMessage {\n * \"content\": \"There are a few ways to translate \\\"I love programming\\\" into French, depending on the level of formality and nuance you want to convey:\\n\\n**Formal:**\\n\\n* **J'aime la programmation.** (This is the most literal and formal translation.)\\n\\n**Informal:**\\n\\n* **J'adore programmer.** (This is a more enthusiastic and informal translation.)\\n* **J'aime beaucoup programmer.** (This is a slightly less enthusiastic but still informal translation.)\\n\\n**More specific:**\\n\\n* **J'aime beaucoup coder.** (This specifically refers to writing code.)\\n* **J'aime beaucoup développer des logiciels.** (This specifically refers to developing software.)\\n\\nThe best translation will depend on the context and your intended audience. \\n\",\n * \"response_metadata\": {\n * \"finishReason\": \"STOP\",\n * \"index\": 0,\n * \"safetyRatings\": [\n * {\n * \"category\": \"HARM_CATEGORY_SEXUALLY_EXPLICIT\",\n * \"probability\": \"NEGLIGIBLE\"\n * },\n * {\n * \"category\": \"HARM_CATEGORY_HATE_SPEECH\",\n * \"probability\": \"NEGLIGIBLE\"\n * },\n * {\n * \"category\": \"HARM_CATEGORY_HARASSMENT\",\n * \"probability\": \"NEGLIGIBLE\"\n * },\n * {\n * \"category\": \"HARM_CATEGORY_DANGEROUS_CONTENT\",\n * \"probability\": \"NEGLIGIBLE\"\n * }\n * ]\n * },\n * \"usage_metadata\": {\n * \"input_tokens\": 10,\n * \"output_tokens\": 149,\n * \"total_tokens\": 159\n * }\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Streaming Chunks</strong></summary>\n *\n * ```typescript\n * for await (const chunk of await llm.stream(input)) {\n * console.log(chunk);\n * }\n * ```\n *\n * ```txt\n * AIMessageChunk {\n * \"content\": \"There\",\n * \"response_metadata\": {\n * \"index\": 0\n * }\n * \"usage_metadata\": {\n * \"input_tokens\": 10,\n * \"output_tokens\": 1,\n * \"total_tokens\": 11\n * }\n * }\n * AIMessageChunk {\n * \"content\": \" are a few ways to translate \\\"I love programming\\\" into French, depending on\",\n * }\n * AIMessageChunk {\n * \"content\": \" the level of formality and nuance you want to convey:\\n\\n**Formal:**\\n\\n\",\n * }\n * AIMessageChunk {\n * \"content\": \"* **J'aime la programmation.** (This is the most literal and formal translation.)\\n\\n**Informal:**\\n\\n* **J'adore programmer.** (This\",\n * }\n * AIMessageChunk {\n * \"content\": \" is a more enthusiastic and informal translation.)\\n* **J'aime beaucoup programmer.** (This is a slightly less enthusiastic but still informal translation.)\\n\\n**More\",\n * }\n * AIMessageChunk {\n * \"content\": \" specific:**\\n\\n* **J'aime beaucoup coder.** (This specifically refers to writing code.)\\n* **J'aime beaucoup développer des logiciels.** (This specifically refers to developing software.)\\n\\nThe best translation will depend on the context and\",\n * }\n * AIMessageChunk {\n * \"content\": \" your intended audience. \\n\",\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Aggregate Streamed Chunks</strong></summary>\n *\n * ```typescript\n * import { AIMessageChunk } from '@langchain/core/messages';\n * import { concat } from '@langchain/core/utils/stream';\n *\n * const stream = await llm.stream(input);\n * let full: AIMessageChunk | undefined;\n * for await (const chunk of stream) {\n * full = !full ? chunk : concat(full, chunk);\n * }\n * console.log(full);\n * ```\n *\n * ```txt\n * AIMessageChunk {\n * \"content\": \"There are a few ways to translate \\\"I love programming\\\" into French, depending on the level of formality and nuance you want to convey:\\n\\n**Formal:**\\n\\n* **J'aime la programmation.** (This is the most literal and formal translation.)\\n\\n**Informal:**\\n\\n* **J'adore programmer.** (This is a more enthusiastic and informal translation.)\\n* **J'aime beaucoup programmer.** (This is a slightly less enthusiastic but still informal translation.)\\n\\n**More specific:**\\n\\n* **J'aime beaucoup coder.** (This specifically refers to writing code.)\\n* **J'aime beaucoup développer des logiciels.** (This specifically refers to developing software.)\\n\\nThe best translation will depend on the context and your intended audience. \\n\",\n * \"usage_metadata\": {\n * \"input_tokens\": 10,\n * \"output_tokens\": 277,\n * \"total_tokens\": 287\n * }\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Bind tools</strong></summary>\n *\n * ```typescript\n * import { z } from 'zod';\n *\n * const GetWeather = {\n * name: \"GetWeather\",\n * description: \"Get the current weather in a given location\",\n * schema: z.object({\n * location: z.string().describe(\"The city and state, e.g. San Francisco, CA\")\n * }),\n * }\n *\n * const GetPopulation = {\n * name: \"GetPopulation\",\n * description: \"Get the current population in a given location\",\n * schema: z.object({\n * location: z.string().describe(\"The city and state, e.g. San Francisco, CA\")\n * }),\n * }\n *\n * const llmWithTools = llm.bindTools([GetWeather, GetPopulation]);\n * const aiMsg = await llmWithTools.invoke(\n * \"Which city is hotter today and which is bigger: LA or NY?\"\n * );\n * console.log(aiMsg.tool_calls);\n * ```\n *\n * ```txt\n * [\n * {\n * name: 'GetWeather',\n * args: { location: 'Los Angeles, CA' },\n * type: 'tool_call'\n * },\n * {\n * name: 'GetWeather',\n * args: { location: 'New York, NY' },\n * type: 'tool_call'\n * },\n * {\n * name: 'GetPopulation',\n * args: { location: 'Los Angeles, CA' },\n * type: 'tool_call'\n * },\n * {\n * name: 'GetPopulation',\n * args: { location: 'New York, NY' },\n * type: 'tool_call'\n * }\n * ]\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Structured Output</strong></summary>\n *\n * ```typescript\n * const Joke = z.object({\n * setup: z.string().describe(\"The setup of the joke\"),\n * punchline: z.string().describe(\"The punchline to the joke\"),\n * rating: z.number().optional().describe(\"How funny the joke is, from 1 to 10\")\n * }).describe('Joke to tell user.');\n *\n * const structuredLlm = llm.withStructuredOutput(Joke, { name: \"Joke\" });\n * const jokeResult = await structuredLlm.invoke(\"Tell me a joke about cats\");\n * console.log(jokeResult);\n * ```\n *\n * ```txt\n * {\n * setup: \"Why don\\\\'t cats play poker?\",\n * punchline: \"Why don\\\\'t cats play poker? Because they always have an ace up their sleeve!\"\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Multimodal</strong></summary>\n *\n * ```typescript\n * import { HumanMessage } from '@langchain/core/messages';\n *\n * const imageUrl = \"https://example.com/image.jpg\";\n * const imageData = await fetch(imageUrl).then(res => res.arrayBuffer());\n * const base64Image = Buffer.from(imageData).toString('base64');\n *\n * const message = new HumanMessage({\n * content: [\n * { type: \"text\", text: \"describe the weather in this image\" },\n * {\n * type: \"image_url\",\n * image_url: { url: `data:image/jpeg;base64,${base64Image}` },\n * },\n * ]\n * });\n *\n * const imageDescriptionAiMsg = await llm.invoke([message]);\n * console.log(imageDescriptionAiMsg.content);\n * ```\n *\n * ```txt\n * The weather in the image appears to be clear and sunny. The sky is mostly blue with a few scattered white clouds, indicating fair weather. The bright sunlight is casting shadows on the green, grassy hill, suggesting it is a pleasant day with good visibility. There are no signs of rain or stormy conditions.\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Usage Metadata</strong></summary>\n *\n * ```typescript\n * const aiMsgForMetadata = await llm.invoke(input);\n * console.log(aiMsgForMetadata.usage_metadata);\n * ```\n *\n * ```txt\n * { input_tokens: 10, output_tokens: 149, total_tokens: 159 }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Response Metadata</strong></summary>\n *\n * ```typescript\n * const aiMsgForResponseMetadata = await llm.invoke(input);\n * console.log(aiMsgForResponseMetadata.response_metadata);\n * ```\n *\n * ```txt\n * {\n * finishReason: 'STOP',\n * index: 0,\n * safetyRatings: [\n * {\n * category: 'HARM_CATEGORY_SEXUALLY_EXPLICIT',\n * probability: 'NEGLIGIBLE'\n * },\n * {\n * category: 'HARM_CATEGORY_HATE_SPEECH',\n * probability: 'NEGLIGIBLE'\n * },\n * { category: 'HARM_CATEGORY_HARASSMENT', probability: 'NEGLIGIBLE' },\n * {\n * category: 'HARM_CATEGORY_DANGEROUS_CONTENT',\n * probability: 'NEGLIGIBLE'\n * }\n * ]\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Document Messages</strong></summary>\n *\n * This example will show you how to pass documents such as PDFs to Google\n * Generative AI through messages.\n *\n * ```typescript\n * const pdfPath = \"/Users/my_user/Downloads/invoice.pdf\";\n * const pdfBase64 = await fs.readFile(pdfPath, \"base64\");\n *\n * const response = await llm.invoke([\n * [\"system\", \"Use the provided documents to answer the question\"],\n * [\n * \"user\",\n * [\n * {\n * type: \"application/pdf\", // If the `type` field includes a single slash (`/`), it will be treated as inline data.\n * data: pdfBase64,\n * },\n * {\n * type: \"text\",\n * text: \"Summarize the contents of this PDF\",\n * },\n * ],\n * ],\n * ]);\n *\n * console.log(response.content);\n * ```\n *\n * ```txt\n * This is a billing invoice from Twitter Developers for X API Basic Access. The transaction date is January 7, 2025,\n * and the amount is $194.34, which has been paid. The subscription period is from January 7, 2025 21:02 to February 7, 2025 00:00 (UTC).\n * The tax is $0.00, with a tax rate of 0%. The total amount is $194.34. The payment was made using a Visa card ending in 7022,\n * expiring in 12/2026. The billing address is Brace Sproul, 1234 Main Street, San Francisco, CA, US 94103. The company being billed is\n * X Corp, located at 865 FM 1209 Building 2, Bastrop, TX, US 78602. Terms and conditions apply.\n * ```\n * </details>\n *\n * <br />\n */\nexport class ChatGoogleGenerativeAI\n extends BaseChatModel<GoogleGenerativeAIChatCallOptions, AIMessageChunk>\n implements GoogleGenerativeAIChatInput\n{\n static lc_name() {\n return \"ChatGoogleGenerativeAI\";\n }\n\n lc_serializable = true;\n\n get lc_secrets(): { [key: string]: string } | undefined {\n return {\n apiKey: \"GOOGLE_API_KEY\",\n };\n }\n\n lc_namespace = [\"langchain\", \"chat_models\", \"google_genai\"];\n\n get lc_aliases() {\n return {\n apiKey: \"google_api_key\",\n };\n }\n\n model: string;\n\n temperature?: number; // default value chosen based on model\n\n maxOutputTokens?: number;\n\n topP?: number; // default value chosen based on model\n\n topK?: number; // default value chosen based on model\n\n stopSequences: string[] = [];\n\n safetySettings?: SafetySetting[];\n\n apiKey?: string;\n\n streaming = false;\n\n json?: boolean;\n\n streamUsage = true;\n\n convertSystemMessageToHumanContent: boolean | undefined;\n\n thinkingConfig?: GoogleGenerativeAIThinkingConfig;\n\n private client: GenerativeModel;\n\n get _isMultimodalModel() {\n return (\n this.model.includes(\"vision\") ||\n this.model.startsWith(\"gemini-1.5\") ||\n this.model.startsWith(\"gemini-2\") ||\n (this.model.startsWith(\"gemma-3-\") &&\n !this.model.startsWith(\"gemma-3-1b\")) || // gemma-3 models are multimodal(but gemma-3n-* and gemma-3-1b are not)\n this.model.startsWith(\"gemini-3\")\n );\n }\n\n constructor(\n model: ModelParams[\"model\"],\n fields?: Omit<GoogleGenerativeAIChatInput, \"model\">\n );\n constructor(fields: GoogleGenerativeAIChatInput);\n constructor(\n modelOrFields: ModelParams[\"model\"] | GoogleGenerativeAIChatInput,\n fieldsArg?: Omit<GoogleGenerativeAIChatInput, \"model\">\n ) {\n const fields =\n typeof modelOrFields === \"string\"\n ? { ...(fieldsArg ?? {}), model: modelOrFields }\n : modelOrFields;\n super(fields);\n this._addVersion(\"@langchain/google-genai\", __PKG_VERSION__);\n\n this.model = fields.model.replace(/^models\\//, \"\");\n\n this.maxOutputTokens = fields.maxOutputTokens ?? this.maxOutputTokens;\n\n if (this.maxOutputTokens && this.maxOutputTokens < 0) {\n throw new Error(\"`maxOutputTokens` must be a positive integer\");\n }\n\n this.temperature = fields.temperature ?? this.temperature;\n if (this.temperature && (this.temperature < 0 || this.temperature > 2)) {\n throw new Error(\"`temperature` must be in the range of [0.0,2.0]\");\n }\n\n this.topP = fields.topP ?? this.topP;\n if (this.topP && this.topP < 0) {\n throw new Error(\"`topP` must be a positive integer\");\n }\n\n if (this.topP && this.topP > 1) {\n throw new Error(\"`topP` must be below 1.\");\n }\n\n this.topK = fields.topK ?? this.topK;\n if (this.topK && this.topK < 0) {\n throw new Error(\"`topK` must be a positive integer\");\n }\n\n this.stopSequences = fields.stopSequences ?? this.stopSequences;\n\n this.apiKey = fields.apiKey ?? getEnvironmentVariable(\"GOOGLE_API_KEY\");\n if (!this.apiKey) {\n throw new Error(\n \"Please set an API key for Google GenerativeAI \" +\n \"in the environment variable GOOGLE_API_KEY \" +\n \"or in the `apiKey` field of the \" +\n \"ChatGoogleGenerativeAI constructor\"\n );\n }\n\n this.safetySettings = fields.safetySettings ?? this.safetySettings;\n if (this.safetySettings && this.safetySettings.length > 0) {\n const safetySettingsSet = new Set(\n this.safetySettings.map((s) => s.category)\n );\n if (safetySettingsSet.size !== this.safetySettings.length) {\n throw new Error(\n \"The categories in `safetySettings` array must be unique\"\n );\n }\n }\n\n this.streaming = fields.streaming ?? this.streaming;\n this.json = fields.json;\n\n this.thinkingConfig = fields.thinkingConfig ?? this.thinkingConfig;\n\n this.client = new GenerativeAI(this.apiKey).getGenerativeModel(\n {\n model: this.model,\n safetySettings: this.safetySettings as SafetySetting[],\n generationConfig: {\n stopSequences: this.stopSequences,\n maxOutputTokens: this.maxOutputTokens,\n temperature: this.temperature,\n topP: this.topP,\n topK: this.topK,\n ...(this.json ? { responseMimeType: \"application/json\" } : {}),\n ...(this.thinkingConfig\n ? { thinkingConfig: this.thinkingConfig }\n : {}),\n },\n },\n {\n apiVersion: fields.apiVersion,\n baseUrl: fields.baseUrl,\n customHeaders: fields.customHeaders,\n }\n );\n this.streamUsage = fields.streamUsage ?? this.streamUsage;\n }\n\n useCachedContent(\n cachedContent: CachedContent,\n modelParams?: ModelParams,\n requestOptions?: RequestOptions\n ): void {\n if (!this.apiKey) return;\n this.client = new GenerativeAI(\n this.apiKey\n ).getGenerativeModelFromCachedContent(\n cachedContent,\n modelParams,\n requestOptions\n );\n }\n\n get useSystemInstruction(): boolean {\n return typeof this.convertSystemMessageToHumanContent === \"boolean\"\n ? !this.convertSystemMessageToHumanContent\n : this.computeUseSystemInstruction;\n }\n\n get computeUseSystemInstruction(): boolean {\n // This works on models from April 2024 and later\n // Vertex AI: gemini-1.5-pro and gemini-1.0-002 and later\n // AI Studio: gemini-1.5-pro-latest\n if (this.model === \"gemini-1.0-pro-001\") {\n return false;\n } else if (this.model.startsWith(\"gemini-pro-vision\")) {\n return false;\n } else if (this.model.startsWith(\"gemini-1.0-pro-vision\")) {\n return false;\n } else if (this.model === \"gemini-pro\") {\n // on AI Studio gemini-pro is still pointing at gemini-1.0-pro-001\n return false;\n }\n return true;\n }\n\n getLsParams(options: this[\"ParsedCallOptions\"]): LangSmithParams {\n return {\n ls_provider: \"google_genai\",\n ls_model_name: this.model,\n ls_model_type: \"chat\",\n ls_temperature: this.client.generationConfig.temperature,\n ls_max_tokens: this.client.generationConfig.maxOutputTokens,\n ls_stop: options.stop,\n };\n }\n\n _combineLLMOutput() {\n return [];\n }\n\n _llmType() {\n return \"googlegenerativeai\";\n }\n\n override bindTools(\n tools: GoogleGenerativeAIToolType[],\n kwargs?: Partial<GoogleGenerativeAIChatCallOptions>\n ): Runnable<\n BaseLanguageModelInput,\n AIMessageChunk,\n GoogleGenerativeAIChatCallOptions\n > {\n return this.withConfig({\n tools: convertToolsToGenAI(tools)?.tools,\n ...kwargs,\n });\n }\n\n invocationParams(\n options?: this[\"ParsedCallOptions\"]\n ): Omit<GenerateContentRequest, \"contents\"> {\n const toolsAndConfig = options?.tools?.length\n ? convertToolsToGenAI(options.tools, {\n toolChoice: options.tool_choice,\n allowedFunctionNames: options.allowedFunctionNames,\n })\n : undefined;\n\n if (options?.responseSchema) {\n this.client.generationConfig.responseSchema = options.responseSchema;\n this.client.generationConfig.responseMimeType = \"application/json\";\n } else {\n this.client.generationConfig.responseSchema = undefined;\n this.client.generationConfig.responseMimeType = this.json\n ? \"application/json\"\n : undefined;\n }\n\n return {\n ...(toolsAndConfig?.tools ? { tools: toolsAndConfig.tools } : {}),\n ...(toolsAndConfig?.toolConfig\n ? { toolConfig: toolsAndConfig.toolConfig }\n : {}),\n };\n }\n\n async _generate(\n messages: BaseMessage[],\n options: this[\"ParsedCallOptions\"],\n runManager?: CallbackManagerForLLMRun\n ): Promise<ChatResult> {\n options.signal?.throwIfAborted();\n const prompt = convertBaseMessagesToContent(\n messages,\n this._isMultimodalModel,\n this.useSystemInstruction,\n this.model\n );\n let actualPrompt = prompt;\n if (prompt[0].role === \"system\") {\n const [systemInstruction] = prompt;\n this.client.systemInstruction = systemInstruction;\n actualPrompt = prompt.slice(1);\n }\n const parameters = this.invocationParams(options);\n\n // Handle streaming\n if (this.streaming) {\n const tokenUsage: TokenUsage = {};\n const stream = this._streamResponseChunks(messages, options, runManager);\n const finalChunks: ChatGenerationChunk[] = [];\n\n for await (const chunk of stream) {\n const index =\n (chunk.generationInfo as NewTokenIndices)?.completion ?? 0;\n if (finalChunks[index] === undefined) {\n finalChunks[index] = chunk;\n } else {\n finalChunks[index] = finalChunks[index].concat(chunk);\n }\n }\n const generations = finalChunks.filter(\n (c): c is ChatGenerationChunk => c !== undefined\n );\n\n return { generations, llmOutput: { estimatedTokenUsage: tokenUsage } };\n }\n\n const res = await this.completionWithRetry({\n ...parameters,\n contents: actualPrompt,\n });\n\n let usageMetadata: UsageMetadata | undefined;\n if (\"usageMetadata\" in res.response) {\n usageMetadata = convertUsageMetadata(\n res.response.usageMetadata,\n this.model\n );\n }\n\n const generationResult = mapGenerateContentResultToChatResult(\n res.response,\n {\n usageMetadata,\n }\n );\n // may not have generations in output if there was a refusal for safety reasons, malformed function call, etc.\n if (generationResult.generations?.length > 0) {\n await runManager?.handleLLMNewToken(\n generationResult.generations[0]?.text ?? \"\"\n );\n }\n return generationResult;\n }\n\n async *_streamResponseChunks(\n messages: BaseMessage[],\n options: this[\"ParsedCallOptions\"],\n runManager?: CallbackManagerForLLMRun\n ): AsyncGenerator<ChatGenerationChunk> {\n const prompt = convertBaseMessagesToContent(\n messages,\n this._isMultimodalModel,\n this.useSystemInstruction,\n this.model\n );\n let actualPrompt = prompt;\n if (prompt[0].role === \"system\") {\n const [systemInstruction] = prompt;\n this.client.systemInstruction = systemInstruction;\n actualPrompt = prompt.slice(1);\n }\n const parameters = this.invocationParams(options);\n const request = {\n ...parameters,\n contents: actualPrompt,\n };\n const stream = await this.caller.callWithOptions(\n { signal: options?.signal },\n async () => {\n const { stream } = await this.client.generateContentStream(request, {\n signal: options?.signal,\n });\n return stream;\n }\n );\n\n let usageMetadata: UsageMetadata | undefined;\n // Keep prior cumulative counts for calculating token deltas while streaming\n let prevPromptTokenCount = 0;\n let prevCandidatesTokenCount = 0;\n let prevTotalTokenCount = 0;\n let index = 0;\n for await (const response of stream) {\n if (options.signal?.aborted) {\n return;\n }\n if (\n \"usageMetadata\" in response &&\n response.usageMetadata !== undefined &&\n this.streamUsage !== false &&\n options.streamUsage !== false\n ) {\n usageMetadata = convertUsageMetadata(\n response.usageMetadata,\n this.model\n );\n\n // Under the hood, LangChain combines the prompt tokens. Google returns the updated\n // total each time, so we need to find the difference between the tokens.\n const newPromptTokenCount =\n response.usageMetadata.promptTokenCount ?? 0;\n usageMetadata.input_tokens = Math.max(\n 0,\n newPromptTokenCount - prevPromptTokenCount\n );\n prevPromptTokenCount = newPromptTokenCount;\n\n const newCandidatesTokenCount =\n response.usageMetadata.candidatesTokenCount ?? 0;\n usageMetadata.output_tokens = Math.max(\n 0,\n newCandidatesTokenCount - prevCandidatesTokenCount\n );\n prevCandidatesTokenCount = newCandidatesTokenCount;\n\n const newTotalTokenCount = response.usageMetadata.totalTokenCount ?? 0;\n usageMetadata.total_tokens = Math.max(\n 0,\n newTotalTokenCount - prevTotalTokenCount\n );\n prevTotalTokenCount = newTotalTokenCount;\n }\n\n const chunk = convertResponseContentToChatGenerationChunk(response, {\n usageMetadata,\n index,\n });\n index += 1;\n if (!chunk) {\n continue;\n }\n\n yield chunk;\n await runManager?.handleLLMNewToken(chunk.text ?? \"\");\n }\n }\n\n async completionWithRetry(\n request: string | GenerateContentRequest | (string | GenerativeAIPart)[],\n options?: this[\"ParsedCallOptions\"]\n ) {\n return this.caller.callWithOptions(\n { signal: options?.signal },\n async () => {\n try {\n return await this.client.generateContent(request, {\n signal: options?.signal,\n });\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } catch (e: any) {\n // TODO: Improve error handling\n if (e.message?.includes(\"400 Bad Request\")) {\n e.status = 400;\n }\n throw e;\n }\n }\n );\n }\n\n /**\n * Return profiling information for the model.\n *\n * Provides information about the model's capabilities and constraints,\n * including token limits, multimodal support, and advanced features like\n * tool calling and structured output.\n *\n * @returns {ModelProfile} An object describing the model's capabilities and constraints\n *\n * @example\n * ```typescript\n * const model = new ChatGoogleGenerativeAI({ model: \"gemini-1.5-flash\" });\n * const profile = model.profile;\n * console.log(profile.maxInputTokens); // 2000000\n * console.log(profile.imageInputs); // true\n * ```\n */\n get profile(): ModelProfile {\n return PROFILES[this.model] ?? {};\n }\n\n withStructuredOutput<\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n outputSchema:\n | InteropZodType<RunOutput>\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<false>\n ): Runnable<BaseLanguageModelInput, RunOutput>;\n\n withStructuredOutput<\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n outputSchema:\n | InteropZodType<RunOutput>\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<true>\n ): Runnable<BaseLanguageModelInput, { raw: BaseMessage; parsed: RunOutput }>;\n\n withStructuredOutput<\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n outputSchema:\n | InteropZodType<RunOutput>\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<boolean>\n ):\n | Runnable<BaseLanguageModelInput, RunOutput>\n | Runnable<\n BaseLanguageModelInput,\n { raw: BaseMessage; parsed: RunOutput }\n > {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const schema: InteropZodType<RunOutput> | Record<string, any> =\n outputSchema;\n const name = config?.name;\n const method = config?.method;\n const includeRaw = config?.includeRaw;\n if (method === \"jsonMode\") {\n throw new Error(\n `ChatGoogleGenerativeAI only supports \"jsonSchema\" or \"functionCalling\" as a method.`\n );\n }\n\n let llm;\n let outputParser: BaseLLMOutputParser<RunOutput>;\n if (method === \"functionCalling\") {\n let functionName = name ?? \"extract\";\n let tools: GoogleGenerativeAIFunctionDeclarationsTool[];\n if (isInteropZodSchema(schema)) {\n const jsonSchema = schemaToGenerativeAIParameters(schema);\n tools = [\n {\n functionDeclarations: [\n {\n name: functionName,\n description:\n jsonSchema.description ?? \"A function available to call.\",\n parameters: jsonSchema as GenerativeAIFunctionDeclarationSchema,\n },\n ],\n },\n ];\n outputParser = new GoogleGenerativeAIToolsOutputParser<\n InferInteropZodOutput<typeof schema>\n >({\n returnSingle: true,\n keyName: functionName,\n zodSchema: schema,\n });\n } else {\n let geminiFunctionDefinition: GenerativeAIFunctionDeclaration;\n if (\n typeof schema.name === \"string\" &&\n typeof schema.parameters === \"object\" &&\n schema.parameters != null\n ) {\n geminiFunctionDefinition = schema as GenerativeAIFunctionDeclaration;\n geminiFunctionDefinition.parameters = removeAdditionalProperties(\n schema.parameters\n ) as GenerativeAIFunctionDeclarationSchema;\n functionName = schema.name;\n } else {\n geminiFunctionDefinition = {\n name: functionName,\n description: schema.description ?? \"\",\n parameters: removeAdditionalProperties(\n schema\n ) as GenerativeAIFunctionDeclarationSchema,\n };\n }\n tools = [\n {\n functionDeclarations: [geminiFunctionDefinition],\n },\n ];\n outputParser = new GoogleGenerativeAIToolsOutputParser<RunOutput>({\n returnSingle: true,\n keyName: functionName,\n });\n }\n llm = this.bindTools(tools).withConfig({\n allowedFunctionNames: [functionName],\n });\n } else {\n const jsonSchema = schemaToGenerativeAIParameters(schema);\n llm = this.withConfig({\n responseSchema: jsonSchema as Schema,\n });\n outputParser = new JsonOutputParser();\n }\n\n if (!includeRaw) {\n return llm.pipe(outputParser).withConfig({\n runName: \"ChatGoogleGenerativeAIStructuredOutput\",\n }) as Runnable<BaseLanguageModelInput, RunOutput>;\n }\n\n const parserAssign = RunnablePassthrough.assign({\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n parsed: (input: any, config) => outputParser.invoke(input.raw, config),\n });\n const parserNone = RunnablePassthrough.assign({\n parsed: () => null,\n });\n const parsedWithFallback = parserAssign.withFallbacks({\n fallbacks: [parserNone],\n });\n return RunnableSequence.from<\n BaseLanguageModelInput,\n { raw: BaseMessage; parsed: RunOutput }\n >([\n {\n raw: llm,\n },\n parsedWithFallback,\n ]).withConfig({\n runName: \"StructuredOutputRunnable\",\n });\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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,IAMA,2BAAA,CAAA;EAiBJ,WAAA,CAAY,OAAA,8BAAqC,eAAA;EAWjD,iBAAA,CAAA;EAIA,QAAA,CAAA;EAIS,SAAA,CACP,KAAA,EAAO,0BAAA,IACP,MAAA,GAAS,OAAA,CAAQ,iCAAA,IAChB,QAAA,CACD,sBAAA,EACA,cAAA,EACA,iCAAA;EAQF,gBAAA,CACE,OAAA,+BACC,IAAA,CAAK,sBAAA;EA0BF,SAAA,CACJ,QAAA,EAAU,WAAA,IACV,OAAA,6BACA,UAAA,GAAa,wBAAA,GACZ,OAAA,CAAQ,UAAA;EAkEJ,qBAAA,CACL,QAAA,EAAU,WAAA,IACV,OAAA,6BACA,UAAA,GAAa,wBAAA,GACZ,cAAA,CAAe,mBAAA;EAyFZ,mBAAA,CACJ,OAAA,WAAkB,sBAAA,aAAmC,IAAA,KACrD,OAAA,+BAAmC,OAAA,CADkC,sBAAA,CAClC,qBAAA;;;;;;;;;;;;;;;;;;MAsCjC,OAAA,CAAA,GAAW,YAAA;EAIf,oBAAA,mBAEoB,MAAA,gBAAsB,MAAA,cAAA,CAExC,YAAA,EACI,cAAA,CAAe,SAAA,IAEf,MAAA,eACJ,MAAA,GAAS,6BAAA,UACR,QAAA,CAAS,sBAAA,EAAwB,SAAA;EAEpC,oBAAA,mBAEoB,MAAA,gBAAsB,MAAA,cAAA,CAExC,YAAA,EACI,cAAA,CAAe,SAAA,IAEf,MAAA,eACJ,MAAA,GAAS,6BAAA,SACR,QAAA,CAAS,sBAAA;IAA0B,GAAA,EAAK,WAAA;IAAa,MAAA,EAAQ,SAAA;EAAA;AAAA"} |
@@ -428,2 +428,3 @@ import { removeAdditionalProperties, schemaToGenerativeAIParameters } from "./utils/zod_to_genai_parameters.js"; | ||
| super(fields); | ||
| this._addVersion("@langchain/google-genai", "2.1.21"); | ||
| this.model = fields.model.replace(/^models\//, ""); | ||
@@ -430,0 +431,0 @@ this.maxOutputTokens = fields.maxOutputTokens ?? this.maxOutputTokens; |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"file":"chat_models.js","names":["GenerativeAI"],"sources":["../src/chat_models.ts"],"sourcesContent":["import {\n GenerativeModel,\n GoogleGenerativeAI as GenerativeAI,\n FunctionDeclarationsTool as GoogleGenerativeAIFunctionDeclarationsTool,\n FunctionDeclaration as GenerativeAIFunctionDeclaration,\n type FunctionDeclarationSchema as GenerativeAIFunctionDeclarationSchema,\n GenerateContentRequest,\n SafetySetting,\n Part as GenerativeAIPart,\n ModelParams,\n RequestOptions,\n type CachedContent,\n Schema,\n} from \"@google/generative-ai\";\nimport { CallbackManagerForLLMRun } from \"@langchain/core/callbacks/manager\";\nimport {\n AIMessageChunk,\n BaseMessage,\n UsageMetadata,\n} from \"@langchain/core/messages\";\nimport { ChatGenerationChunk, ChatResult } from \"@langchain/core/outputs\";\nimport { getEnvironmentVariable } from \"@langchain/core/utils/env\";\nimport {\n BaseChatModel,\n type BaseChatModelCallOptions,\n type LangSmithParams,\n type BaseChatModelParams,\n} from \"@langchain/core/language_models/chat_models\";\nimport { ModelProfile } from \"@langchain/core/language_models/profile\";\nimport { NewTokenIndices } from \"@langchain/core/callbacks/base\";\nimport {\n BaseLanguageModelInput,\n StructuredOutputMethodOptions,\n} from \"@langchain/core/language_models/base\";\nimport {\n Runnable,\n RunnablePassthrough,\n RunnableSequence,\n} from \"@langchain/core/runnables\";\nimport {\n InferInteropZodOutput,\n InteropZodType,\n isInteropZodSchema,\n} from \"@langchain/core/utils/types\";\nimport {\n BaseLLMOutputParser,\n JsonOutputParser,\n} from \"@langchain/core/output_parsers\";\nimport {\n schemaToGenerativeAIParameters,\n removeAdditionalProperties,\n} from \"./utils/zod_to_genai_parameters.js\";\nimport {\n convertBaseMessagesToContent,\n convertResponseContentToChatGenerationChunk,\n convertUsageMetadata,\n mapGenerateContentResultToChatResult,\n} from \"./utils/common.js\";\nimport { GoogleGenerativeAIToolsOutputParser } from \"./output_parsers.js\";\nimport {\n GoogleGenerativeAIThinkingConfig,\n GoogleGenerativeAIToolType,\n} from \"./types.js\";\nimport { convertToolsToGenAI } from \"./utils/tools.js\";\nimport PROFILES from \"./profiles.js\";\n\ninterface TokenUsage {\n completionTokens?: number;\n promptTokens?: number;\n totalTokens?: number;\n}\n\nexport type BaseMessageExamplePair = {\n input: BaseMessage;\n output: BaseMessage;\n};\n\nexport interface GoogleGenerativeAIChatCallOptions extends BaseChatModelCallOptions {\n tools?: GoogleGenerativeAIToolType[];\n /**\n * Allowed functions to call when the mode is \"any\".\n * If empty, any one of the provided functions are called.\n */\n allowedFunctionNames?: string[];\n /**\n * Whether or not to include usage data, like token counts\n * in the streamed response chunks.\n * @default true\n */\n streamUsage?: boolean;\n\n /**\n * JSON schema to be returned by the model.\n */\n responseSchema?: Schema;\n}\n\n/**\n * An interface defining the input to the ChatGoogleGenerativeAI class.\n */\nexport interface GoogleGenerativeAIChatInput\n extends\n BaseChatModelParams,\n Pick<GoogleGenerativeAIChatCallOptions, \"streamUsage\"> {\n /**\n * Model Name to use\n *\n * Note: The format must follow the pattern - `{model}`\n */\n model: ModelParams[\"model\"];\n\n /**\n * Controls the randomness of the output.\n *\n * Values can range from [0.0,2.0], inclusive. A value closer to 2.0\n * will produce responses that are more varied and creative, while\n * a value closer to 0.0 will typically result in less surprising\n * responses from the model.\n *\n * Note: The default value varies by model\n */\n temperature?: number;\n\n /**\n * Maximum number of tokens to generate in the completion.\n */\n maxOutputTokens?: number;\n\n /**\n * Top-p changes how the model selects tokens for output.\n *\n * Tokens are selected from most probable to least until the sum\n * of their probabilities equals the top-p value.\n *\n * For example, if tokens A, B, and C have a probability of\n * .3, .2, and .1 and the top-p value is .5, then the model will\n * select either A or B as the next token (using temperature).\n *\n * Note: The default value varies by model\n */\n topP?: number;\n\n /**\n * Top-k changes how the model selects tokens for output.\n *\n * A top-k of 1 means the selected token is the most probable among\n * all tokens in the model's vocabulary (also called greedy decoding),\n * while a top-k of 3 means that the next token is selected from\n * among the 3 most probable tokens (using temperature).\n *\n * Note: The default value varies by model\n */\n topK?: number;\n\n /**\n * The set of character sequences (up to 5) that will stop output generation.\n * If specified, the API will stop at the first appearance of a stop\n * sequence.\n *\n * Note: The stop sequence will not be included as part of the response.\n * Note: stopSequences is only supported for Gemini models\n */\n stopSequences?: string[];\n\n /**\n * A list of unique `SafetySetting` instances for blocking unsafe content. The API will block\n * any prompts and responses that fail to meet the thresholds set by these settings. If there\n * is no `SafetySetting` for a given `SafetyCategory` provided in the list, the API will use\n * the default safety setting for that category.\n */\n safetySettings?: SafetySetting[];\n\n /**\n * Google API key to use\n */\n apiKey?: string;\n\n /**\n * Google API version to use\n */\n apiVersion?: string;\n\n /**\n * Google API base URL to use\n */\n baseUrl?: string;\n\n /**\n * Google API custom headers to use\n */\n customHeaders?: Record<string, string>;\n\n /** Whether to stream the results or not */\n streaming?: boolean;\n\n /**\n * Whether or not to force the model to respond with JSON.\n * Available for `gemini-1.5` models and later.\n * @default false\n */\n json?: boolean;\n\n /**\n * Whether or not model supports system instructions.\n * The following models support system instructions:\n * - All Gemini 1.5 Pro model versions\n * - All Gemini 1.5 Flash model versions\n * - Gemini 1.0 Pro version gemini-1.0-pro-002\n */\n convertSystemMessageToHumanContent?: boolean | undefined;\n\n /**\n * Optional. Config for thinking features. An error will be returned if this\n * field is set for models that don't support thinking.\n */\n thinkingConfig?: GoogleGenerativeAIThinkingConfig;\n}\n\n/**\n * Google Generative AI chat model integration.\n *\n * Setup:\n * Install `@langchain/google-genai` and set an environment variable named `GOOGLE_API_KEY`.\n *\n * ```bash\n * npm install @langchain/google-genai\n * export GOOGLE_API_KEY=\"your-api-key\"\n * ```\n *\n * ## [Constructor args](https://api.js.langchain.com/classes/langchain_google_genai.ChatGoogleGenerativeAI.html#constructor)\n *\n * ## [Runtime args](https://api.js.langchain.com/interfaces/langchain_google_genai.GoogleGenerativeAIChatCallOptions.html)\n *\n * Runtime args can be passed as the second argument to any of the base runnable methods `.invoke`. `.stream`, `.batch`, etc.\n * They can also be passed via `.withConfig`, or the second arg in `.bindTools`, like shown in the examples below:\n *\n * ```typescript\n * // When calling `.withConfig`, call options should be passed via the first argument\n * const llmWithArgsBound = llm.withConfig({\n * stop: [\"\\n\"],\n * });\n *\n * // When calling `.bindTools`, call options should be passed via the second argument\n * const llmWithTools = llm.bindTools(\n * [...],\n * {\n * stop: [\"\\n\"],\n * }\n * );\n * ```\n *\n * ## Examples\n *\n * <details open>\n * <summary><strong>Instantiate</strong></summary>\n *\n * ```typescript\n * import { ChatGoogleGenerativeAI } from '@langchain/google-genai';\n *\n * const llm = new ChatGoogleGenerativeAI({\n * model: \"gemini-1.5-flash\",\n * temperature: 0,\n * maxRetries: 2,\n * // apiKey: \"...\",\n * // other params...\n * });\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Invoking</strong></summary>\n *\n * ```typescript\n * const input = `Translate \"I love programming\" into French.`;\n *\n * // Models also accept a list of chat messages or a formatted prompt\n * const result = await llm.invoke(input);\n * console.log(result);\n * ```\n *\n * ```txt\n * AIMessage {\n * \"content\": \"There are a few ways to translate \\\"I love programming\\\" into French, depending on the level of formality and nuance you want to convey:\\n\\n**Formal:**\\n\\n* **J'aime la programmation.** (This is the most literal and formal translation.)\\n\\n**Informal:**\\n\\n* **J'adore programmer.** (This is a more enthusiastic and informal translation.)\\n* **J'aime beaucoup programmer.** (This is a slightly less enthusiastic but still informal translation.)\\n\\n**More specific:**\\n\\n* **J'aime beaucoup coder.** (This specifically refers to writing code.)\\n* **J'aime beaucoup développer des logiciels.** (This specifically refers to developing software.)\\n\\nThe best translation will depend on the context and your intended audience. \\n\",\n * \"response_metadata\": {\n * \"finishReason\": \"STOP\",\n * \"index\": 0,\n * \"safetyRatings\": [\n * {\n * \"category\": \"HARM_CATEGORY_SEXUALLY_EXPLICIT\",\n * \"probability\": \"NEGLIGIBLE\"\n * },\n * {\n * \"category\": \"HARM_CATEGORY_HATE_SPEECH\",\n * \"probability\": \"NEGLIGIBLE\"\n * },\n * {\n * \"category\": \"HARM_CATEGORY_HARASSMENT\",\n * \"probability\": \"NEGLIGIBLE\"\n * },\n * {\n * \"category\": \"HARM_CATEGORY_DANGEROUS_CONTENT\",\n * \"probability\": \"NEGLIGIBLE\"\n * }\n * ]\n * },\n * \"usage_metadata\": {\n * \"input_tokens\": 10,\n * \"output_tokens\": 149,\n * \"total_tokens\": 159\n * }\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Streaming Chunks</strong></summary>\n *\n * ```typescript\n * for await (const chunk of await llm.stream(input)) {\n * console.log(chunk);\n * }\n * ```\n *\n * ```txt\n * AIMessageChunk {\n * \"content\": \"There\",\n * \"response_metadata\": {\n * \"index\": 0\n * }\n * \"usage_metadata\": {\n * \"input_tokens\": 10,\n * \"output_tokens\": 1,\n * \"total_tokens\": 11\n * }\n * }\n * AIMessageChunk {\n * \"content\": \" are a few ways to translate \\\"I love programming\\\" into French, depending on\",\n * }\n * AIMessageChunk {\n * \"content\": \" the level of formality and nuance you want to convey:\\n\\n**Formal:**\\n\\n\",\n * }\n * AIMessageChunk {\n * \"content\": \"* **J'aime la programmation.** (This is the most literal and formal translation.)\\n\\n**Informal:**\\n\\n* **J'adore programmer.** (This\",\n * }\n * AIMessageChunk {\n * \"content\": \" is a more enthusiastic and informal translation.)\\n* **J'aime beaucoup programmer.** (This is a slightly less enthusiastic but still informal translation.)\\n\\n**More\",\n * }\n * AIMessageChunk {\n * \"content\": \" specific:**\\n\\n* **J'aime beaucoup coder.** (This specifically refers to writing code.)\\n* **J'aime beaucoup développer des logiciels.** (This specifically refers to developing software.)\\n\\nThe best translation will depend on the context and\",\n * }\n * AIMessageChunk {\n * \"content\": \" your intended audience. \\n\",\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Aggregate Streamed Chunks</strong></summary>\n *\n * ```typescript\n * import { AIMessageChunk } from '@langchain/core/messages';\n * import { concat } from '@langchain/core/utils/stream';\n *\n * const stream = await llm.stream(input);\n * let full: AIMessageChunk | undefined;\n * for await (const chunk of stream) {\n * full = !full ? chunk : concat(full, chunk);\n * }\n * console.log(full);\n * ```\n *\n * ```txt\n * AIMessageChunk {\n * \"content\": \"There are a few ways to translate \\\"I love programming\\\" into French, depending on the level of formality and nuance you want to convey:\\n\\n**Formal:**\\n\\n* **J'aime la programmation.** (This is the most literal and formal translation.)\\n\\n**Informal:**\\n\\n* **J'adore programmer.** (This is a more enthusiastic and informal translation.)\\n* **J'aime beaucoup programmer.** (This is a slightly less enthusiastic but still informal translation.)\\n\\n**More specific:**\\n\\n* **J'aime beaucoup coder.** (This specifically refers to writing code.)\\n* **J'aime beaucoup développer des logiciels.** (This specifically refers to developing software.)\\n\\nThe best translation will depend on the context and your intended audience. \\n\",\n * \"usage_metadata\": {\n * \"input_tokens\": 10,\n * \"output_tokens\": 277,\n * \"total_tokens\": 287\n * }\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Bind tools</strong></summary>\n *\n * ```typescript\n * import { z } from 'zod';\n *\n * const GetWeather = {\n * name: \"GetWeather\",\n * description: \"Get the current weather in a given location\",\n * schema: z.object({\n * location: z.string().describe(\"The city and state, e.g. San Francisco, CA\")\n * }),\n * }\n *\n * const GetPopulation = {\n * name: \"GetPopulation\",\n * description: \"Get the current population in a given location\",\n * schema: z.object({\n * location: z.string().describe(\"The city and state, e.g. San Francisco, CA\")\n * }),\n * }\n *\n * const llmWithTools = llm.bindTools([GetWeather, GetPopulation]);\n * const aiMsg = await llmWithTools.invoke(\n * \"Which city is hotter today and which is bigger: LA or NY?\"\n * );\n * console.log(aiMsg.tool_calls);\n * ```\n *\n * ```txt\n * [\n * {\n * name: 'GetWeather',\n * args: { location: 'Los Angeles, CA' },\n * type: 'tool_call'\n * },\n * {\n * name: 'GetWeather',\n * args: { location: 'New York, NY' },\n * type: 'tool_call'\n * },\n * {\n * name: 'GetPopulation',\n * args: { location: 'Los Angeles, CA' },\n * type: 'tool_call'\n * },\n * {\n * name: 'GetPopulation',\n * args: { location: 'New York, NY' },\n * type: 'tool_call'\n * }\n * ]\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Structured Output</strong></summary>\n *\n * ```typescript\n * const Joke = z.object({\n * setup: z.string().describe(\"The setup of the joke\"),\n * punchline: z.string().describe(\"The punchline to the joke\"),\n * rating: z.number().optional().describe(\"How funny the joke is, from 1 to 10\")\n * }).describe('Joke to tell user.');\n *\n * const structuredLlm = llm.withStructuredOutput(Joke, { name: \"Joke\" });\n * const jokeResult = await structuredLlm.invoke(\"Tell me a joke about cats\");\n * console.log(jokeResult);\n * ```\n *\n * ```txt\n * {\n * setup: \"Why don\\\\'t cats play poker?\",\n * punchline: \"Why don\\\\'t cats play poker? Because they always have an ace up their sleeve!\"\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Multimodal</strong></summary>\n *\n * ```typescript\n * import { HumanMessage } from '@langchain/core/messages';\n *\n * const imageUrl = \"https://example.com/image.jpg\";\n * const imageData = await fetch(imageUrl).then(res => res.arrayBuffer());\n * const base64Image = Buffer.from(imageData).toString('base64');\n *\n * const message = new HumanMessage({\n * content: [\n * { type: \"text\", text: \"describe the weather in this image\" },\n * {\n * type: \"image_url\",\n * image_url: { url: `data:image/jpeg;base64,${base64Image}` },\n * },\n * ]\n * });\n *\n * const imageDescriptionAiMsg = await llm.invoke([message]);\n * console.log(imageDescriptionAiMsg.content);\n * ```\n *\n * ```txt\n * The weather in the image appears to be clear and sunny. The sky is mostly blue with a few scattered white clouds, indicating fair weather. The bright sunlight is casting shadows on the green, grassy hill, suggesting it is a pleasant day with good visibility. There are no signs of rain or stormy conditions.\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Usage Metadata</strong></summary>\n *\n * ```typescript\n * const aiMsgForMetadata = await llm.invoke(input);\n * console.log(aiMsgForMetadata.usage_metadata);\n * ```\n *\n * ```txt\n * { input_tokens: 10, output_tokens: 149, total_tokens: 159 }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Response Metadata</strong></summary>\n *\n * ```typescript\n * const aiMsgForResponseMetadata = await llm.invoke(input);\n * console.log(aiMsgForResponseMetadata.response_metadata);\n * ```\n *\n * ```txt\n * {\n * finishReason: 'STOP',\n * index: 0,\n * safetyRatings: [\n * {\n * category: 'HARM_CATEGORY_SEXUALLY_EXPLICIT',\n * probability: 'NEGLIGIBLE'\n * },\n * {\n * category: 'HARM_CATEGORY_HATE_SPEECH',\n * probability: 'NEGLIGIBLE'\n * },\n * { category: 'HARM_CATEGORY_HARASSMENT', probability: 'NEGLIGIBLE' },\n * {\n * category: 'HARM_CATEGORY_DANGEROUS_CONTENT',\n * probability: 'NEGLIGIBLE'\n * }\n * ]\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Document Messages</strong></summary>\n *\n * This example will show you how to pass documents such as PDFs to Google\n * Generative AI through messages.\n *\n * ```typescript\n * const pdfPath = \"/Users/my_user/Downloads/invoice.pdf\";\n * const pdfBase64 = await fs.readFile(pdfPath, \"base64\");\n *\n * const response = await llm.invoke([\n * [\"system\", \"Use the provided documents to answer the question\"],\n * [\n * \"user\",\n * [\n * {\n * type: \"application/pdf\", // If the `type` field includes a single slash (`/`), it will be treated as inline data.\n * data: pdfBase64,\n * },\n * {\n * type: \"text\",\n * text: \"Summarize the contents of this PDF\",\n * },\n * ],\n * ],\n * ]);\n *\n * console.log(response.content);\n * ```\n *\n * ```txt\n * This is a billing invoice from Twitter Developers for X API Basic Access. The transaction date is January 7, 2025,\n * and the amount is $194.34, which has been paid. The subscription period is from January 7, 2025 21:02 to February 7, 2025 00:00 (UTC).\n * The tax is $0.00, with a tax rate of 0%. The total amount is $194.34. The payment was made using a Visa card ending in 7022,\n * expiring in 12/2026. The billing address is Brace Sproul, 1234 Main Street, San Francisco, CA, US 94103. The company being billed is\n * X Corp, located at 865 FM 1209 Building 2, Bastrop, TX, US 78602. Terms and conditions apply.\n * ```\n * </details>\n *\n * <br />\n */\nexport class ChatGoogleGenerativeAI\n extends BaseChatModel<GoogleGenerativeAIChatCallOptions, AIMessageChunk>\n implements GoogleGenerativeAIChatInput\n{\n static lc_name() {\n return \"ChatGoogleGenerativeAI\";\n }\n\n lc_serializable = true;\n\n get lc_secrets(): { [key: string]: string } | undefined {\n return {\n apiKey: \"GOOGLE_API_KEY\",\n };\n }\n\n lc_namespace = [\"langchain\", \"chat_models\", \"google_genai\"];\n\n get lc_aliases() {\n return {\n apiKey: \"google_api_key\",\n };\n }\n\n model: string;\n\n temperature?: number; // default value chosen based on model\n\n maxOutputTokens?: number;\n\n topP?: number; // default value chosen based on model\n\n topK?: number; // default value chosen based on model\n\n stopSequences: string[] = [];\n\n safetySettings?: SafetySetting[];\n\n apiKey?: string;\n\n streaming = false;\n\n json?: boolean;\n\n streamUsage = true;\n\n convertSystemMessageToHumanContent: boolean | undefined;\n\n thinkingConfig?: GoogleGenerativeAIThinkingConfig;\n\n private client: GenerativeModel;\n\n get _isMultimodalModel() {\n return (\n this.model.includes(\"vision\") ||\n this.model.startsWith(\"gemini-1.5\") ||\n this.model.startsWith(\"gemini-2\") ||\n (this.model.startsWith(\"gemma-3-\") &&\n !this.model.startsWith(\"gemma-3-1b\")) || // gemma-3 models are multimodal(but gemma-3n-* and gemma-3-1b are not)\n this.model.startsWith(\"gemini-3\")\n );\n }\n\n constructor(\n model: ModelParams[\"model\"],\n fields?: Omit<GoogleGenerativeAIChatInput, \"model\">\n );\n constructor(fields: GoogleGenerativeAIChatInput);\n constructor(\n modelOrFields: ModelParams[\"model\"] | GoogleGenerativeAIChatInput,\n fieldsArg?: Omit<GoogleGenerativeAIChatInput, \"model\">\n ) {\n const fields =\n typeof modelOrFields === \"string\"\n ? { ...(fieldsArg ?? {}), model: modelOrFields }\n : modelOrFields;\n super(fields);\n\n this.model = fields.model.replace(/^models\\//, \"\");\n\n this.maxOutputTokens = fields.maxOutputTokens ?? this.maxOutputTokens;\n\n if (this.maxOutputTokens && this.maxOutputTokens < 0) {\n throw new Error(\"`maxOutputTokens` must be a positive integer\");\n }\n\n this.temperature = fields.temperature ?? this.temperature;\n if (this.temperature && (this.temperature < 0 || this.temperature > 2)) {\n throw new Error(\"`temperature` must be in the range of [0.0,2.0]\");\n }\n\n this.topP = fields.topP ?? this.topP;\n if (this.topP && this.topP < 0) {\n throw new Error(\"`topP` must be a positive integer\");\n }\n\n if (this.topP && this.topP > 1) {\n throw new Error(\"`topP` must be below 1.\");\n }\n\n this.topK = fields.topK ?? this.topK;\n if (this.topK && this.topK < 0) {\n throw new Error(\"`topK` must be a positive integer\");\n }\n\n this.stopSequences = fields.stopSequences ?? this.stopSequences;\n\n this.apiKey = fields.apiKey ?? getEnvironmentVariable(\"GOOGLE_API_KEY\");\n if (!this.apiKey) {\n throw new Error(\n \"Please set an API key for Google GenerativeAI \" +\n \"in the environment variable GOOGLE_API_KEY \" +\n \"or in the `apiKey` field of the \" +\n \"ChatGoogleGenerativeAI constructor\"\n );\n }\n\n this.safetySettings = fields.safetySettings ?? this.safetySettings;\n if (this.safetySettings && this.safetySettings.length > 0) {\n const safetySettingsSet = new Set(\n this.safetySettings.map((s) => s.category)\n );\n if (safetySettingsSet.size !== this.safetySettings.length) {\n throw new Error(\n \"The categories in `safetySettings` array must be unique\"\n );\n }\n }\n\n this.streaming = fields.streaming ?? this.streaming;\n this.json = fields.json;\n\n this.thinkingConfig = fields.thinkingConfig ?? this.thinkingConfig;\n\n this.client = new GenerativeAI(this.apiKey).getGenerativeModel(\n {\n model: this.model,\n safetySettings: this.safetySettings as SafetySetting[],\n generationConfig: {\n stopSequences: this.stopSequences,\n maxOutputTokens: this.maxOutputTokens,\n temperature: this.temperature,\n topP: this.topP,\n topK: this.topK,\n ...(this.json ? { responseMimeType: \"application/json\" } : {}),\n ...(this.thinkingConfig\n ? { thinkingConfig: this.thinkingConfig }\n : {}),\n },\n },\n {\n apiVersion: fields.apiVersion,\n baseUrl: fields.baseUrl,\n customHeaders: fields.customHeaders,\n }\n );\n this.streamUsage = fields.streamUsage ?? this.streamUsage;\n }\n\n useCachedContent(\n cachedContent: CachedContent,\n modelParams?: ModelParams,\n requestOptions?: RequestOptions\n ): void {\n if (!this.apiKey) return;\n this.client = new GenerativeAI(\n this.apiKey\n ).getGenerativeModelFromCachedContent(\n cachedContent,\n modelParams,\n requestOptions\n );\n }\n\n get useSystemInstruction(): boolean {\n return typeof this.convertSystemMessageToHumanContent === \"boolean\"\n ? !this.convertSystemMessageToHumanContent\n : this.computeUseSystemInstruction;\n }\n\n get computeUseSystemInstruction(): boolean {\n // This works on models from April 2024 and later\n // Vertex AI: gemini-1.5-pro and gemini-1.0-002 and later\n // AI Studio: gemini-1.5-pro-latest\n if (this.model === \"gemini-1.0-pro-001\") {\n return false;\n } else if (this.model.startsWith(\"gemini-pro-vision\")) {\n return false;\n } else if (this.model.startsWith(\"gemini-1.0-pro-vision\")) {\n return false;\n } else if (this.model === \"gemini-pro\") {\n // on AI Studio gemini-pro is still pointing at gemini-1.0-pro-001\n return false;\n }\n return true;\n }\n\n getLsParams(options: this[\"ParsedCallOptions\"]): LangSmithParams {\n return {\n ls_provider: \"google_genai\",\n ls_model_name: this.model,\n ls_model_type: \"chat\",\n ls_temperature: this.client.generationConfig.temperature,\n ls_max_tokens: this.client.generationConfig.maxOutputTokens,\n ls_stop: options.stop,\n };\n }\n\n _combineLLMOutput() {\n return [];\n }\n\n _llmType() {\n return \"googlegenerativeai\";\n }\n\n override bindTools(\n tools: GoogleGenerativeAIToolType[],\n kwargs?: Partial<GoogleGenerativeAIChatCallOptions>\n ): Runnable<\n BaseLanguageModelInput,\n AIMessageChunk,\n GoogleGenerativeAIChatCallOptions\n > {\n return this.withConfig({\n tools: convertToolsToGenAI(tools)?.tools,\n ...kwargs,\n });\n }\n\n invocationParams(\n options?: this[\"ParsedCallOptions\"]\n ): Omit<GenerateContentRequest, \"contents\"> {\n const toolsAndConfig = options?.tools?.length\n ? convertToolsToGenAI(options.tools, {\n toolChoice: options.tool_choice,\n allowedFunctionNames: options.allowedFunctionNames,\n })\n : undefined;\n\n if (options?.responseSchema) {\n this.client.generationConfig.responseSchema = options.responseSchema;\n this.client.generationConfig.responseMimeType = \"application/json\";\n } else {\n this.client.generationConfig.responseSchema = undefined;\n this.client.generationConfig.responseMimeType = this.json\n ? \"application/json\"\n : undefined;\n }\n\n return {\n ...(toolsAndConfig?.tools ? { tools: toolsAndConfig.tools } : {}),\n ...(toolsAndConfig?.toolConfig\n ? { toolConfig: toolsAndConfig.toolConfig }\n : {}),\n };\n }\n\n async _generate(\n messages: BaseMessage[],\n options: this[\"ParsedCallOptions\"],\n runManager?: CallbackManagerForLLMRun\n ): Promise<ChatResult> {\n options.signal?.throwIfAborted();\n const prompt = convertBaseMessagesToContent(\n messages,\n this._isMultimodalModel,\n this.useSystemInstruction,\n this.model\n );\n let actualPrompt = prompt;\n if (prompt[0].role === \"system\") {\n const [systemInstruction] = prompt;\n this.client.systemInstruction = systemInstruction;\n actualPrompt = prompt.slice(1);\n }\n const parameters = this.invocationParams(options);\n\n // Handle streaming\n if (this.streaming) {\n const tokenUsage: TokenUsage = {};\n const stream = this._streamResponseChunks(messages, options, runManager);\n const finalChunks: ChatGenerationChunk[] = [];\n\n for await (const chunk of stream) {\n const index =\n (chunk.generationInfo as NewTokenIndices)?.completion ?? 0;\n if (finalChunks[index] === undefined) {\n finalChunks[index] = chunk;\n } else {\n finalChunks[index] = finalChunks[index].concat(chunk);\n }\n }\n const generations = finalChunks.filter(\n (c): c is ChatGenerationChunk => c !== undefined\n );\n\n return { generations, llmOutput: { estimatedTokenUsage: tokenUsage } };\n }\n\n const res = await this.completionWithRetry({\n ...parameters,\n contents: actualPrompt,\n });\n\n let usageMetadata: UsageMetadata | undefined;\n if (\"usageMetadata\" in res.response) {\n usageMetadata = convertUsageMetadata(\n res.response.usageMetadata,\n this.model\n );\n }\n\n const generationResult = mapGenerateContentResultToChatResult(\n res.response,\n {\n usageMetadata,\n }\n );\n // may not have generations in output if there was a refusal for safety reasons, malformed function call, etc.\n if (generationResult.generations?.length > 0) {\n await runManager?.handleLLMNewToken(\n generationResult.generations[0]?.text ?? \"\"\n );\n }\n return generationResult;\n }\n\n async *_streamResponseChunks(\n messages: BaseMessage[],\n options: this[\"ParsedCallOptions\"],\n runManager?: CallbackManagerForLLMRun\n ): AsyncGenerator<ChatGenerationChunk> {\n const prompt = convertBaseMessagesToContent(\n messages,\n this._isMultimodalModel,\n this.useSystemInstruction,\n this.model\n );\n let actualPrompt = prompt;\n if (prompt[0].role === \"system\") {\n const [systemInstruction] = prompt;\n this.client.systemInstruction = systemInstruction;\n actualPrompt = prompt.slice(1);\n }\n const parameters = this.invocationParams(options);\n const request = {\n ...parameters,\n contents: actualPrompt,\n };\n const stream = await this.caller.callWithOptions(\n { signal: options?.signal },\n async () => {\n const { stream } = await this.client.generateContentStream(request, {\n signal: options?.signal,\n });\n return stream;\n }\n );\n\n let usageMetadata: UsageMetadata | undefined;\n // Keep prior cumulative counts for calculating token deltas while streaming\n let prevPromptTokenCount = 0;\n let prevCandidatesTokenCount = 0;\n let prevTotalTokenCount = 0;\n let index = 0;\n for await (const response of stream) {\n if (options.signal?.aborted) {\n return;\n }\n if (\n \"usageMetadata\" in response &&\n response.usageMetadata !== undefined &&\n this.streamUsage !== false &&\n options.streamUsage !== false\n ) {\n usageMetadata = convertUsageMetadata(\n response.usageMetadata,\n this.model\n );\n\n // Under the hood, LangChain combines the prompt tokens. Google returns the updated\n // total each time, so we need to find the difference between the tokens.\n const newPromptTokenCount =\n response.usageMetadata.promptTokenCount ?? 0;\n usageMetadata.input_tokens = Math.max(\n 0,\n newPromptTokenCount - prevPromptTokenCount\n );\n prevPromptTokenCount = newPromptTokenCount;\n\n const newCandidatesTokenCount =\n response.usageMetadata.candidatesTokenCount ?? 0;\n usageMetadata.output_tokens = Math.max(\n 0,\n newCandidatesTokenCount - prevCandidatesTokenCount\n );\n prevCandidatesTokenCount = newCandidatesTokenCount;\n\n const newTotalTokenCount = response.usageMetadata.totalTokenCount ?? 0;\n usageMetadata.total_tokens = Math.max(\n 0,\n newTotalTokenCount - prevTotalTokenCount\n );\n prevTotalTokenCount = newTotalTokenCount;\n }\n\n const chunk = convertResponseContentToChatGenerationChunk(response, {\n usageMetadata,\n index,\n });\n index += 1;\n if (!chunk) {\n continue;\n }\n\n yield chunk;\n await runManager?.handleLLMNewToken(chunk.text ?? \"\");\n }\n }\n\n async completionWithRetry(\n request: string | GenerateContentRequest | (string | GenerativeAIPart)[],\n options?: this[\"ParsedCallOptions\"]\n ) {\n return this.caller.callWithOptions(\n { signal: options?.signal },\n async () => {\n try {\n return await this.client.generateContent(request, {\n signal: options?.signal,\n });\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } catch (e: any) {\n // TODO: Improve error handling\n if (e.message?.includes(\"400 Bad Request\")) {\n e.status = 400;\n }\n throw e;\n }\n }\n );\n }\n\n /**\n * Return profiling information for the model.\n *\n * Provides information about the model's capabilities and constraints,\n * including token limits, multimodal support, and advanced features like\n * tool calling and structured output.\n *\n * @returns {ModelProfile} An object describing the model's capabilities and constraints\n *\n * @example\n * ```typescript\n * const model = new ChatGoogleGenerativeAI({ model: \"gemini-1.5-flash\" });\n * const profile = model.profile;\n * console.log(profile.maxInputTokens); // 2000000\n * console.log(profile.imageInputs); // true\n * ```\n */\n get profile(): ModelProfile {\n return PROFILES[this.model] ?? {};\n }\n\n withStructuredOutput<\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n outputSchema:\n | InteropZodType<RunOutput>\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<false>\n ): Runnable<BaseLanguageModelInput, RunOutput>;\n\n withStructuredOutput<\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n outputSchema:\n | InteropZodType<RunOutput>\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<true>\n ): Runnable<BaseLanguageModelInput, { raw: BaseMessage; parsed: RunOutput }>;\n\n withStructuredOutput<\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n outputSchema:\n | InteropZodType<RunOutput>\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<boolean>\n ):\n | Runnable<BaseLanguageModelInput, RunOutput>\n | Runnable<\n BaseLanguageModelInput,\n { raw: BaseMessage; parsed: RunOutput }\n > {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const schema: InteropZodType<RunOutput> | Record<string, any> =\n outputSchema;\n const name = config?.name;\n const method = config?.method;\n const includeRaw = config?.includeRaw;\n if (method === \"jsonMode\") {\n throw new Error(\n `ChatGoogleGenerativeAI only supports \"jsonSchema\" or \"functionCalling\" as a method.`\n );\n }\n\n let llm;\n let outputParser: BaseLLMOutputParser<RunOutput>;\n if (method === \"functionCalling\") {\n let functionName = name ?? \"extract\";\n let tools: GoogleGenerativeAIFunctionDeclarationsTool[];\n if (isInteropZodSchema(schema)) {\n const jsonSchema = schemaToGenerativeAIParameters(schema);\n tools = [\n {\n functionDeclarations: [\n {\n name: functionName,\n description:\n jsonSchema.description ?? \"A function available to call.\",\n parameters: jsonSchema as GenerativeAIFunctionDeclarationSchema,\n },\n ],\n },\n ];\n outputParser = new GoogleGenerativeAIToolsOutputParser<\n InferInteropZodOutput<typeof schema>\n >({\n returnSingle: true,\n keyName: functionName,\n zodSchema: schema,\n });\n } else {\n let geminiFunctionDefinition: GenerativeAIFunctionDeclaration;\n if (\n typeof schema.name === \"string\" &&\n typeof schema.parameters === \"object\" &&\n schema.parameters != null\n ) {\n geminiFunctionDefinition = schema as GenerativeAIFunctionDeclaration;\n geminiFunctionDefinition.parameters = removeAdditionalProperties(\n schema.parameters\n ) as GenerativeAIFunctionDeclarationSchema;\n functionName = schema.name;\n } else {\n geminiFunctionDefinition = {\n name: functionName,\n description: schema.description ?? \"\",\n parameters: removeAdditionalProperties(\n schema\n ) as GenerativeAIFunctionDeclarationSchema,\n };\n }\n tools = [\n {\n functionDeclarations: [geminiFunctionDefinition],\n },\n ];\n outputParser = new GoogleGenerativeAIToolsOutputParser<RunOutput>({\n returnSingle: true,\n keyName: functionName,\n });\n }\n llm = this.bindTools(tools).withConfig({\n allowedFunctionNames: [functionName],\n });\n } else {\n const jsonSchema = schemaToGenerativeAIParameters(schema);\n llm = this.withConfig({\n responseSchema: jsonSchema as Schema,\n });\n outputParser = new JsonOutputParser();\n }\n\n if (!includeRaw) {\n return llm.pipe(outputParser).withConfig({\n runName: \"ChatGoogleGenerativeAIStructuredOutput\",\n }) as Runnable<BaseLanguageModelInput, RunOutput>;\n }\n\n const parserAssign = RunnablePassthrough.assign({\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n parsed: (input: any, config) => outputParser.invoke(input.raw, config),\n });\n const parserNone = RunnablePassthrough.assign({\n parsed: () => null,\n });\n const parsedWithFallback = parserAssign.withFallbacks({\n fallbacks: [parserNone],\n });\n return RunnableSequence.from<\n BaseLanguageModelInput,\n { raw: BaseMessage; parsed: RunOutput }\n >([\n {\n raw: llm,\n },\n parsedWithFallback,\n ]).withConfig({\n runName: \"StructuredOutputRunnable\",\n });\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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AAW,CAAC,WAAW,EACxB,CAAC;AACF,SAAO,iBAAiB,KAGtB,CACA,EACE,KAAK,KACN,EACD,mBACD,CAAC,CAAC,WAAW,EACZ,SAAS,4BACV,CAAC"} | ||
| {"version":3,"file":"chat_models.js","names":["GenerativeAI"],"sources":["../src/chat_models.ts"],"sourcesContent":["import {\n GenerativeModel,\n GoogleGenerativeAI as GenerativeAI,\n FunctionDeclarationsTool as GoogleGenerativeAIFunctionDeclarationsTool,\n FunctionDeclaration as GenerativeAIFunctionDeclaration,\n type FunctionDeclarationSchema as GenerativeAIFunctionDeclarationSchema,\n GenerateContentRequest,\n SafetySetting,\n Part as GenerativeAIPart,\n ModelParams,\n RequestOptions,\n type CachedContent,\n Schema,\n} from \"@google/generative-ai\";\nimport { CallbackManagerForLLMRun } from \"@langchain/core/callbacks/manager\";\nimport {\n AIMessageChunk,\n BaseMessage,\n UsageMetadata,\n} from \"@langchain/core/messages\";\nimport { ChatGenerationChunk, ChatResult } from \"@langchain/core/outputs\";\nimport { getEnvironmentVariable } from \"@langchain/core/utils/env\";\nimport {\n BaseChatModel,\n type BaseChatModelCallOptions,\n type LangSmithParams,\n type BaseChatModelParams,\n} from \"@langchain/core/language_models/chat_models\";\nimport { ModelProfile } from \"@langchain/core/language_models/profile\";\nimport { NewTokenIndices } from \"@langchain/core/callbacks/base\";\nimport {\n BaseLanguageModelInput,\n StructuredOutputMethodOptions,\n} from \"@langchain/core/language_models/base\";\nimport {\n Runnable,\n RunnablePassthrough,\n RunnableSequence,\n} from \"@langchain/core/runnables\";\nimport {\n InferInteropZodOutput,\n InteropZodType,\n isInteropZodSchema,\n} from \"@langchain/core/utils/types\";\nimport {\n BaseLLMOutputParser,\n JsonOutputParser,\n} from \"@langchain/core/output_parsers\";\nimport {\n schemaToGenerativeAIParameters,\n removeAdditionalProperties,\n} from \"./utils/zod_to_genai_parameters.js\";\nimport {\n convertBaseMessagesToContent,\n convertResponseContentToChatGenerationChunk,\n convertUsageMetadata,\n mapGenerateContentResultToChatResult,\n} from \"./utils/common.js\";\nimport { GoogleGenerativeAIToolsOutputParser } from \"./output_parsers.js\";\nimport {\n GoogleGenerativeAIThinkingConfig,\n GoogleGenerativeAIToolType,\n} from \"./types.js\";\nimport { convertToolsToGenAI } from \"./utils/tools.js\";\nimport PROFILES from \"./profiles.js\";\n\ninterface TokenUsage {\n completionTokens?: number;\n promptTokens?: number;\n totalTokens?: number;\n}\n\nexport type BaseMessageExamplePair = {\n input: BaseMessage;\n output: BaseMessage;\n};\n\nexport interface GoogleGenerativeAIChatCallOptions extends BaseChatModelCallOptions {\n tools?: GoogleGenerativeAIToolType[];\n /**\n * Allowed functions to call when the mode is \"any\".\n * If empty, any one of the provided functions are called.\n */\n allowedFunctionNames?: string[];\n /**\n * Whether or not to include usage data, like token counts\n * in the streamed response chunks.\n * @default true\n */\n streamUsage?: boolean;\n\n /**\n * JSON schema to be returned by the model.\n */\n responseSchema?: Schema;\n}\n\n/**\n * An interface defining the input to the ChatGoogleGenerativeAI class.\n */\nexport interface GoogleGenerativeAIChatInput\n extends\n BaseChatModelParams,\n Pick<GoogleGenerativeAIChatCallOptions, \"streamUsage\"> {\n /**\n * Model Name to use\n *\n * Note: The format must follow the pattern - `{model}`\n */\n model: ModelParams[\"model\"];\n\n /**\n * Controls the randomness of the output.\n *\n * Values can range from [0.0,2.0], inclusive. A value closer to 2.0\n * will produce responses that are more varied and creative, while\n * a value closer to 0.0 will typically result in less surprising\n * responses from the model.\n *\n * Note: The default value varies by model\n */\n temperature?: number;\n\n /**\n * Maximum number of tokens to generate in the completion.\n */\n maxOutputTokens?: number;\n\n /**\n * Top-p changes how the model selects tokens for output.\n *\n * Tokens are selected from most probable to least until the sum\n * of their probabilities equals the top-p value.\n *\n * For example, if tokens A, B, and C have a probability of\n * .3, .2, and .1 and the top-p value is .5, then the model will\n * select either A or B as the next token (using temperature).\n *\n * Note: The default value varies by model\n */\n topP?: number;\n\n /**\n * Top-k changes how the model selects tokens for output.\n *\n * A top-k of 1 means the selected token is the most probable among\n * all tokens in the model's vocabulary (also called greedy decoding),\n * while a top-k of 3 means that the next token is selected from\n * among the 3 most probable tokens (using temperature).\n *\n * Note: The default value varies by model\n */\n topK?: number;\n\n /**\n * The set of character sequences (up to 5) that will stop output generation.\n * If specified, the API will stop at the first appearance of a stop\n * sequence.\n *\n * Note: The stop sequence will not be included as part of the response.\n * Note: stopSequences is only supported for Gemini models\n */\n stopSequences?: string[];\n\n /**\n * A list of unique `SafetySetting` instances for blocking unsafe content. The API will block\n * any prompts and responses that fail to meet the thresholds set by these settings. If there\n * is no `SafetySetting` for a given `SafetyCategory` provided in the list, the API will use\n * the default safety setting for that category.\n */\n safetySettings?: SafetySetting[];\n\n /**\n * Google API key to use\n */\n apiKey?: string;\n\n /**\n * Google API version to use\n */\n apiVersion?: string;\n\n /**\n * Google API base URL to use\n */\n baseUrl?: string;\n\n /**\n * Google API custom headers to use\n */\n customHeaders?: Record<string, string>;\n\n /** Whether to stream the results or not */\n streaming?: boolean;\n\n /**\n * Whether or not to force the model to respond with JSON.\n * Available for `gemini-1.5` models and later.\n * @default false\n */\n json?: boolean;\n\n /**\n * Whether or not model supports system instructions.\n * The following models support system instructions:\n * - All Gemini 1.5 Pro model versions\n * - All Gemini 1.5 Flash model versions\n * - Gemini 1.0 Pro version gemini-1.0-pro-002\n */\n convertSystemMessageToHumanContent?: boolean | undefined;\n\n /**\n * Optional. Config for thinking features. An error will be returned if this\n * field is set for models that don't support thinking.\n */\n thinkingConfig?: GoogleGenerativeAIThinkingConfig;\n}\n\n/**\n * Google Generative AI chat model integration.\n *\n * Setup:\n * Install `@langchain/google-genai` and set an environment variable named `GOOGLE_API_KEY`.\n *\n * ```bash\n * npm install @langchain/google-genai\n * export GOOGLE_API_KEY=\"your-api-key\"\n * ```\n *\n * ## [Constructor args](https://api.js.langchain.com/classes/langchain_google_genai.ChatGoogleGenerativeAI.html#constructor)\n *\n * ## [Runtime args](https://api.js.langchain.com/interfaces/langchain_google_genai.GoogleGenerativeAIChatCallOptions.html)\n *\n * Runtime args can be passed as the second argument to any of the base runnable methods `.invoke`. `.stream`, `.batch`, etc.\n * They can also be passed via `.withConfig`, or the second arg in `.bindTools`, like shown in the examples below:\n *\n * ```typescript\n * // When calling `.withConfig`, call options should be passed via the first argument\n * const llmWithArgsBound = llm.withConfig({\n * stop: [\"\\n\"],\n * });\n *\n * // When calling `.bindTools`, call options should be passed via the second argument\n * const llmWithTools = llm.bindTools(\n * [...],\n * {\n * stop: [\"\\n\"],\n * }\n * );\n * ```\n *\n * ## Examples\n *\n * <details open>\n * <summary><strong>Instantiate</strong></summary>\n *\n * ```typescript\n * import { ChatGoogleGenerativeAI } from '@langchain/google-genai';\n *\n * const llm = new ChatGoogleGenerativeAI({\n * model: \"gemini-1.5-flash\",\n * temperature: 0,\n * maxRetries: 2,\n * // apiKey: \"...\",\n * // other params...\n * });\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Invoking</strong></summary>\n *\n * ```typescript\n * const input = `Translate \"I love programming\" into French.`;\n *\n * // Models also accept a list of chat messages or a formatted prompt\n * const result = await llm.invoke(input);\n * console.log(result);\n * ```\n *\n * ```txt\n * AIMessage {\n * \"content\": \"There are a few ways to translate \\\"I love programming\\\" into French, depending on the level of formality and nuance you want to convey:\\n\\n**Formal:**\\n\\n* **J'aime la programmation.** (This is the most literal and formal translation.)\\n\\n**Informal:**\\n\\n* **J'adore programmer.** (This is a more enthusiastic and informal translation.)\\n* **J'aime beaucoup programmer.** (This is a slightly less enthusiastic but still informal translation.)\\n\\n**More specific:**\\n\\n* **J'aime beaucoup coder.** (This specifically refers to writing code.)\\n* **J'aime beaucoup développer des logiciels.** (This specifically refers to developing software.)\\n\\nThe best translation will depend on the context and your intended audience. \\n\",\n * \"response_metadata\": {\n * \"finishReason\": \"STOP\",\n * \"index\": 0,\n * \"safetyRatings\": [\n * {\n * \"category\": \"HARM_CATEGORY_SEXUALLY_EXPLICIT\",\n * \"probability\": \"NEGLIGIBLE\"\n * },\n * {\n * \"category\": \"HARM_CATEGORY_HATE_SPEECH\",\n * \"probability\": \"NEGLIGIBLE\"\n * },\n * {\n * \"category\": \"HARM_CATEGORY_HARASSMENT\",\n * \"probability\": \"NEGLIGIBLE\"\n * },\n * {\n * \"category\": \"HARM_CATEGORY_DANGEROUS_CONTENT\",\n * \"probability\": \"NEGLIGIBLE\"\n * }\n * ]\n * },\n * \"usage_metadata\": {\n * \"input_tokens\": 10,\n * \"output_tokens\": 149,\n * \"total_tokens\": 159\n * }\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Streaming Chunks</strong></summary>\n *\n * ```typescript\n * for await (const chunk of await llm.stream(input)) {\n * console.log(chunk);\n * }\n * ```\n *\n * ```txt\n * AIMessageChunk {\n * \"content\": \"There\",\n * \"response_metadata\": {\n * \"index\": 0\n * }\n * \"usage_metadata\": {\n * \"input_tokens\": 10,\n * \"output_tokens\": 1,\n * \"total_tokens\": 11\n * }\n * }\n * AIMessageChunk {\n * \"content\": \" are a few ways to translate \\\"I love programming\\\" into French, depending on\",\n * }\n * AIMessageChunk {\n * \"content\": \" the level of formality and nuance you want to convey:\\n\\n**Formal:**\\n\\n\",\n * }\n * AIMessageChunk {\n * \"content\": \"* **J'aime la programmation.** (This is the most literal and formal translation.)\\n\\n**Informal:**\\n\\n* **J'adore programmer.** (This\",\n * }\n * AIMessageChunk {\n * \"content\": \" is a more enthusiastic and informal translation.)\\n* **J'aime beaucoup programmer.** (This is a slightly less enthusiastic but still informal translation.)\\n\\n**More\",\n * }\n * AIMessageChunk {\n * \"content\": \" specific:**\\n\\n* **J'aime beaucoup coder.** (This specifically refers to writing code.)\\n* **J'aime beaucoup développer des logiciels.** (This specifically refers to developing software.)\\n\\nThe best translation will depend on the context and\",\n * }\n * AIMessageChunk {\n * \"content\": \" your intended audience. \\n\",\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Aggregate Streamed Chunks</strong></summary>\n *\n * ```typescript\n * import { AIMessageChunk } from '@langchain/core/messages';\n * import { concat } from '@langchain/core/utils/stream';\n *\n * const stream = await llm.stream(input);\n * let full: AIMessageChunk | undefined;\n * for await (const chunk of stream) {\n * full = !full ? chunk : concat(full, chunk);\n * }\n * console.log(full);\n * ```\n *\n * ```txt\n * AIMessageChunk {\n * \"content\": \"There are a few ways to translate \\\"I love programming\\\" into French, depending on the level of formality and nuance you want to convey:\\n\\n**Formal:**\\n\\n* **J'aime la programmation.** (This is the most literal and formal translation.)\\n\\n**Informal:**\\n\\n* **J'adore programmer.** (This is a more enthusiastic and informal translation.)\\n* **J'aime beaucoup programmer.** (This is a slightly less enthusiastic but still informal translation.)\\n\\n**More specific:**\\n\\n* **J'aime beaucoup coder.** (This specifically refers to writing code.)\\n* **J'aime beaucoup développer des logiciels.** (This specifically refers to developing software.)\\n\\nThe best translation will depend on the context and your intended audience. \\n\",\n * \"usage_metadata\": {\n * \"input_tokens\": 10,\n * \"output_tokens\": 277,\n * \"total_tokens\": 287\n * }\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Bind tools</strong></summary>\n *\n * ```typescript\n * import { z } from 'zod';\n *\n * const GetWeather = {\n * name: \"GetWeather\",\n * description: \"Get the current weather in a given location\",\n * schema: z.object({\n * location: z.string().describe(\"The city and state, e.g. San Francisco, CA\")\n * }),\n * }\n *\n * const GetPopulation = {\n * name: \"GetPopulation\",\n * description: \"Get the current population in a given location\",\n * schema: z.object({\n * location: z.string().describe(\"The city and state, e.g. San Francisco, CA\")\n * }),\n * }\n *\n * const llmWithTools = llm.bindTools([GetWeather, GetPopulation]);\n * const aiMsg = await llmWithTools.invoke(\n * \"Which city is hotter today and which is bigger: LA or NY?\"\n * );\n * console.log(aiMsg.tool_calls);\n * ```\n *\n * ```txt\n * [\n * {\n * name: 'GetWeather',\n * args: { location: 'Los Angeles, CA' },\n * type: 'tool_call'\n * },\n * {\n * name: 'GetWeather',\n * args: { location: 'New York, NY' },\n * type: 'tool_call'\n * },\n * {\n * name: 'GetPopulation',\n * args: { location: 'Los Angeles, CA' },\n * type: 'tool_call'\n * },\n * {\n * name: 'GetPopulation',\n * args: { location: 'New York, NY' },\n * type: 'tool_call'\n * }\n * ]\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Structured Output</strong></summary>\n *\n * ```typescript\n * const Joke = z.object({\n * setup: z.string().describe(\"The setup of the joke\"),\n * punchline: z.string().describe(\"The punchline to the joke\"),\n * rating: z.number().optional().describe(\"How funny the joke is, from 1 to 10\")\n * }).describe('Joke to tell user.');\n *\n * const structuredLlm = llm.withStructuredOutput(Joke, { name: \"Joke\" });\n * const jokeResult = await structuredLlm.invoke(\"Tell me a joke about cats\");\n * console.log(jokeResult);\n * ```\n *\n * ```txt\n * {\n * setup: \"Why don\\\\'t cats play poker?\",\n * punchline: \"Why don\\\\'t cats play poker? Because they always have an ace up their sleeve!\"\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Multimodal</strong></summary>\n *\n * ```typescript\n * import { HumanMessage } from '@langchain/core/messages';\n *\n * const imageUrl = \"https://example.com/image.jpg\";\n * const imageData = await fetch(imageUrl).then(res => res.arrayBuffer());\n * const base64Image = Buffer.from(imageData).toString('base64');\n *\n * const message = new HumanMessage({\n * content: [\n * { type: \"text\", text: \"describe the weather in this image\" },\n * {\n * type: \"image_url\",\n * image_url: { url: `data:image/jpeg;base64,${base64Image}` },\n * },\n * ]\n * });\n *\n * const imageDescriptionAiMsg = await llm.invoke([message]);\n * console.log(imageDescriptionAiMsg.content);\n * ```\n *\n * ```txt\n * The weather in the image appears to be clear and sunny. The sky is mostly blue with a few scattered white clouds, indicating fair weather. The bright sunlight is casting shadows on the green, grassy hill, suggesting it is a pleasant day with good visibility. There are no signs of rain or stormy conditions.\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Usage Metadata</strong></summary>\n *\n * ```typescript\n * const aiMsgForMetadata = await llm.invoke(input);\n * console.log(aiMsgForMetadata.usage_metadata);\n * ```\n *\n * ```txt\n * { input_tokens: 10, output_tokens: 149, total_tokens: 159 }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Response Metadata</strong></summary>\n *\n * ```typescript\n * const aiMsgForResponseMetadata = await llm.invoke(input);\n * console.log(aiMsgForResponseMetadata.response_metadata);\n * ```\n *\n * ```txt\n * {\n * finishReason: 'STOP',\n * index: 0,\n * safetyRatings: [\n * {\n * category: 'HARM_CATEGORY_SEXUALLY_EXPLICIT',\n * probability: 'NEGLIGIBLE'\n * },\n * {\n * category: 'HARM_CATEGORY_HATE_SPEECH',\n * probability: 'NEGLIGIBLE'\n * },\n * { category: 'HARM_CATEGORY_HARASSMENT', probability: 'NEGLIGIBLE' },\n * {\n * category: 'HARM_CATEGORY_DANGEROUS_CONTENT',\n * probability: 'NEGLIGIBLE'\n * }\n * ]\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Document Messages</strong></summary>\n *\n * This example will show you how to pass documents such as PDFs to Google\n * Generative AI through messages.\n *\n * ```typescript\n * const pdfPath = \"/Users/my_user/Downloads/invoice.pdf\";\n * const pdfBase64 = await fs.readFile(pdfPath, \"base64\");\n *\n * const response = await llm.invoke([\n * [\"system\", \"Use the provided documents to answer the question\"],\n * [\n * \"user\",\n * [\n * {\n * type: \"application/pdf\", // If the `type` field includes a single slash (`/`), it will be treated as inline data.\n * data: pdfBase64,\n * },\n * {\n * type: \"text\",\n * text: \"Summarize the contents of this PDF\",\n * },\n * ],\n * ],\n * ]);\n *\n * console.log(response.content);\n * ```\n *\n * ```txt\n * This is a billing invoice from Twitter Developers for X API Basic Access. The transaction date is January 7, 2025,\n * and the amount is $194.34, which has been paid. The subscription period is from January 7, 2025 21:02 to February 7, 2025 00:00 (UTC).\n * The tax is $0.00, with a tax rate of 0%. The total amount is $194.34. The payment was made using a Visa card ending in 7022,\n * expiring in 12/2026. The billing address is Brace Sproul, 1234 Main Street, San Francisco, CA, US 94103. The company being billed is\n * X Corp, located at 865 FM 1209 Building 2, Bastrop, TX, US 78602. Terms and conditions apply.\n * ```\n * </details>\n *\n * <br />\n */\nexport class ChatGoogleGenerativeAI\n extends BaseChatModel<GoogleGenerativeAIChatCallOptions, AIMessageChunk>\n implements GoogleGenerativeAIChatInput\n{\n static lc_name() {\n return \"ChatGoogleGenerativeAI\";\n }\n\n lc_serializable = true;\n\n get lc_secrets(): { [key: string]: string } | undefined {\n return {\n apiKey: \"GOOGLE_API_KEY\",\n };\n }\n\n lc_namespace = [\"langchain\", \"chat_models\", \"google_genai\"];\n\n get lc_aliases() {\n return {\n apiKey: \"google_api_key\",\n };\n }\n\n model: string;\n\n temperature?: number; // default value chosen based on model\n\n maxOutputTokens?: number;\n\n topP?: number; // default value chosen based on model\n\n topK?: number; // default value chosen based on model\n\n stopSequences: string[] = [];\n\n safetySettings?: SafetySetting[];\n\n apiKey?: string;\n\n streaming = false;\n\n json?: boolean;\n\n streamUsage = true;\n\n convertSystemMessageToHumanContent: boolean | undefined;\n\n thinkingConfig?: GoogleGenerativeAIThinkingConfig;\n\n private client: GenerativeModel;\n\n get _isMultimodalModel() {\n return (\n this.model.includes(\"vision\") ||\n this.model.startsWith(\"gemini-1.5\") ||\n this.model.startsWith(\"gemini-2\") ||\n (this.model.startsWith(\"gemma-3-\") &&\n !this.model.startsWith(\"gemma-3-1b\")) || // gemma-3 models are multimodal(but gemma-3n-* and gemma-3-1b are not)\n this.model.startsWith(\"gemini-3\")\n );\n }\n\n constructor(\n model: ModelParams[\"model\"],\n fields?: Omit<GoogleGenerativeAIChatInput, \"model\">\n );\n constructor(fields: GoogleGenerativeAIChatInput);\n constructor(\n modelOrFields: ModelParams[\"model\"] | GoogleGenerativeAIChatInput,\n fieldsArg?: Omit<GoogleGenerativeAIChatInput, \"model\">\n ) {\n const fields =\n typeof modelOrFields === \"string\"\n ? { ...(fieldsArg ?? {}), model: modelOrFields }\n : modelOrFields;\n super(fields);\n this._addVersion(\"@langchain/google-genai\", __PKG_VERSION__);\n\n this.model = fields.model.replace(/^models\\//, \"\");\n\n this.maxOutputTokens = fields.maxOutputTokens ?? this.maxOutputTokens;\n\n if (this.maxOutputTokens && this.maxOutputTokens < 0) {\n throw new Error(\"`maxOutputTokens` must be a positive integer\");\n }\n\n this.temperature = fields.temperature ?? this.temperature;\n if (this.temperature && (this.temperature < 0 || this.temperature > 2)) {\n throw new Error(\"`temperature` must be in the range of [0.0,2.0]\");\n }\n\n this.topP = fields.topP ?? this.topP;\n if (this.topP && this.topP < 0) {\n throw new Error(\"`topP` must be a positive integer\");\n }\n\n if (this.topP && this.topP > 1) {\n throw new Error(\"`topP` must be below 1.\");\n }\n\n this.topK = fields.topK ?? this.topK;\n if (this.topK && this.topK < 0) {\n throw new Error(\"`topK` must be a positive integer\");\n }\n\n this.stopSequences = fields.stopSequences ?? this.stopSequences;\n\n this.apiKey = fields.apiKey ?? getEnvironmentVariable(\"GOOGLE_API_KEY\");\n if (!this.apiKey) {\n throw new Error(\n \"Please set an API key for Google GenerativeAI \" +\n \"in the environment variable GOOGLE_API_KEY \" +\n \"or in the `apiKey` field of the \" +\n \"ChatGoogleGenerativeAI constructor\"\n );\n }\n\n this.safetySettings = fields.safetySettings ?? this.safetySettings;\n if (this.safetySettings && this.safetySettings.length > 0) {\n const safetySettingsSet = new Set(\n this.safetySettings.map((s) => s.category)\n );\n if (safetySettingsSet.size !== this.safetySettings.length) {\n throw new Error(\n \"The categories in `safetySettings` array must be unique\"\n );\n }\n }\n\n this.streaming = fields.streaming ?? this.streaming;\n this.json = fields.json;\n\n this.thinkingConfig = fields.thinkingConfig ?? this.thinkingConfig;\n\n this.client = new GenerativeAI(this.apiKey).getGenerativeModel(\n {\n model: this.model,\n safetySettings: this.safetySettings as SafetySetting[],\n generationConfig: {\n stopSequences: this.stopSequences,\n maxOutputTokens: this.maxOutputTokens,\n temperature: this.temperature,\n topP: this.topP,\n topK: this.topK,\n ...(this.json ? { responseMimeType: \"application/json\" } : {}),\n ...(this.thinkingConfig\n ? { thinkingConfig: this.thinkingConfig }\n : {}),\n },\n },\n {\n apiVersion: fields.apiVersion,\n baseUrl: fields.baseUrl,\n customHeaders: fields.customHeaders,\n }\n );\n this.streamUsage = fields.streamUsage ?? this.streamUsage;\n }\n\n useCachedContent(\n cachedContent: CachedContent,\n modelParams?: ModelParams,\n requestOptions?: RequestOptions\n ): void {\n if (!this.apiKey) return;\n this.client = new GenerativeAI(\n this.apiKey\n ).getGenerativeModelFromCachedContent(\n cachedContent,\n modelParams,\n requestOptions\n );\n }\n\n get useSystemInstruction(): boolean {\n return typeof this.convertSystemMessageToHumanContent === \"boolean\"\n ? !this.convertSystemMessageToHumanContent\n : this.computeUseSystemInstruction;\n }\n\n get computeUseSystemInstruction(): boolean {\n // This works on models from April 2024 and later\n // Vertex AI: gemini-1.5-pro and gemini-1.0-002 and later\n // AI Studio: gemini-1.5-pro-latest\n if (this.model === \"gemini-1.0-pro-001\") {\n return false;\n } else if (this.model.startsWith(\"gemini-pro-vision\")) {\n return false;\n } else if (this.model.startsWith(\"gemini-1.0-pro-vision\")) {\n return false;\n } else if (this.model === \"gemini-pro\") {\n // on AI Studio gemini-pro is still pointing at gemini-1.0-pro-001\n return false;\n }\n return true;\n }\n\n getLsParams(options: this[\"ParsedCallOptions\"]): LangSmithParams {\n return {\n ls_provider: \"google_genai\",\n ls_model_name: this.model,\n ls_model_type: \"chat\",\n ls_temperature: this.client.generationConfig.temperature,\n ls_max_tokens: this.client.generationConfig.maxOutputTokens,\n ls_stop: options.stop,\n };\n }\n\n _combineLLMOutput() {\n return [];\n }\n\n _llmType() {\n return \"googlegenerativeai\";\n }\n\n override bindTools(\n tools: GoogleGenerativeAIToolType[],\n kwargs?: Partial<GoogleGenerativeAIChatCallOptions>\n ): Runnable<\n BaseLanguageModelInput,\n AIMessageChunk,\n GoogleGenerativeAIChatCallOptions\n > {\n return this.withConfig({\n tools: convertToolsToGenAI(tools)?.tools,\n ...kwargs,\n });\n }\n\n invocationParams(\n options?: this[\"ParsedCallOptions\"]\n ): Omit<GenerateContentRequest, \"contents\"> {\n const toolsAndConfig = options?.tools?.length\n ? convertToolsToGenAI(options.tools, {\n toolChoice: options.tool_choice,\n allowedFunctionNames: options.allowedFunctionNames,\n })\n : undefined;\n\n if (options?.responseSchema) {\n this.client.generationConfig.responseSchema = options.responseSchema;\n this.client.generationConfig.responseMimeType = \"application/json\";\n } else {\n this.client.generationConfig.responseSchema = undefined;\n this.client.generationConfig.responseMimeType = this.json\n ? \"application/json\"\n : undefined;\n }\n\n return {\n ...(toolsAndConfig?.tools ? { tools: toolsAndConfig.tools } : {}),\n ...(toolsAndConfig?.toolConfig\n ? { toolConfig: toolsAndConfig.toolConfig }\n : {}),\n };\n }\n\n async _generate(\n messages: BaseMessage[],\n options: this[\"ParsedCallOptions\"],\n runManager?: CallbackManagerForLLMRun\n ): Promise<ChatResult> {\n options.signal?.throwIfAborted();\n const prompt = convertBaseMessagesToContent(\n messages,\n this._isMultimodalModel,\n this.useSystemInstruction,\n this.model\n );\n let actualPrompt = prompt;\n if (prompt[0].role === \"system\") {\n const [systemInstruction] = prompt;\n this.client.systemInstruction = systemInstruction;\n actualPrompt = prompt.slice(1);\n }\n const parameters = this.invocationParams(options);\n\n // Handle streaming\n if (this.streaming) {\n const tokenUsage: TokenUsage = {};\n const stream = this._streamResponseChunks(messages, options, runManager);\n const finalChunks: ChatGenerationChunk[] = [];\n\n for await (const chunk of stream) {\n const index =\n (chunk.generationInfo as NewTokenIndices)?.completion ?? 0;\n if (finalChunks[index] === undefined) {\n finalChunks[index] = chunk;\n } else {\n finalChunks[index] = finalChunks[index].concat(chunk);\n }\n }\n const generations = finalChunks.filter(\n (c): c is ChatGenerationChunk => c !== undefined\n );\n\n return { generations, llmOutput: { estimatedTokenUsage: tokenUsage } };\n }\n\n const res = await this.completionWithRetry({\n ...parameters,\n contents: actualPrompt,\n });\n\n let usageMetadata: UsageMetadata | undefined;\n if (\"usageMetadata\" in res.response) {\n usageMetadata = convertUsageMetadata(\n res.response.usageMetadata,\n this.model\n );\n }\n\n const generationResult = mapGenerateContentResultToChatResult(\n res.response,\n {\n usageMetadata,\n }\n );\n // may not have generations in output if there was a refusal for safety reasons, malformed function call, etc.\n if (generationResult.generations?.length > 0) {\n await runManager?.handleLLMNewToken(\n generationResult.generations[0]?.text ?? \"\"\n );\n }\n return generationResult;\n }\n\n async *_streamResponseChunks(\n messages: BaseMessage[],\n options: this[\"ParsedCallOptions\"],\n runManager?: CallbackManagerForLLMRun\n ): AsyncGenerator<ChatGenerationChunk> {\n const prompt = convertBaseMessagesToContent(\n messages,\n this._isMultimodalModel,\n this.useSystemInstruction,\n this.model\n );\n let actualPrompt = prompt;\n if (prompt[0].role === \"system\") {\n const [systemInstruction] = prompt;\n this.client.systemInstruction = systemInstruction;\n actualPrompt = prompt.slice(1);\n }\n const parameters = this.invocationParams(options);\n const request = {\n ...parameters,\n contents: actualPrompt,\n };\n const stream = await this.caller.callWithOptions(\n { signal: options?.signal },\n async () => {\n const { stream } = await this.client.generateContentStream(request, {\n signal: options?.signal,\n });\n return stream;\n }\n );\n\n let usageMetadata: UsageMetadata | undefined;\n // Keep prior cumulative counts for calculating token deltas while streaming\n let prevPromptTokenCount = 0;\n let prevCandidatesTokenCount = 0;\n let prevTotalTokenCount = 0;\n let index = 0;\n for await (const response of stream) {\n if (options.signal?.aborted) {\n return;\n }\n if (\n \"usageMetadata\" in response &&\n response.usageMetadata !== undefined &&\n this.streamUsage !== false &&\n options.streamUsage !== false\n ) {\n usageMetadata = convertUsageMetadata(\n response.usageMetadata,\n this.model\n );\n\n // Under the hood, LangChain combines the prompt tokens. Google returns the updated\n // total each time, so we need to find the difference between the tokens.\n const newPromptTokenCount =\n response.usageMetadata.promptTokenCount ?? 0;\n usageMetadata.input_tokens = Math.max(\n 0,\n newPromptTokenCount - prevPromptTokenCount\n );\n prevPromptTokenCount = newPromptTokenCount;\n\n const newCandidatesTokenCount =\n response.usageMetadata.candidatesTokenCount ?? 0;\n usageMetadata.output_tokens = Math.max(\n 0,\n newCandidatesTokenCount - prevCandidatesTokenCount\n );\n prevCandidatesTokenCount = newCandidatesTokenCount;\n\n const newTotalTokenCount = response.usageMetadata.totalTokenCount ?? 0;\n usageMetadata.total_tokens = Math.max(\n 0,\n newTotalTokenCount - prevTotalTokenCount\n );\n prevTotalTokenCount = newTotalTokenCount;\n }\n\n const chunk = convertResponseContentToChatGenerationChunk(response, {\n usageMetadata,\n index,\n });\n index += 1;\n if (!chunk) {\n continue;\n }\n\n yield chunk;\n await runManager?.handleLLMNewToken(chunk.text ?? \"\");\n }\n }\n\n async completionWithRetry(\n request: string | GenerateContentRequest | (string | GenerativeAIPart)[],\n options?: this[\"ParsedCallOptions\"]\n ) {\n return this.caller.callWithOptions(\n { signal: options?.signal },\n async () => {\n try {\n return await this.client.generateContent(request, {\n signal: options?.signal,\n });\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } catch (e: any) {\n // TODO: Improve error handling\n if (e.message?.includes(\"400 Bad Request\")) {\n e.status = 400;\n }\n throw e;\n }\n }\n );\n }\n\n /**\n * Return profiling information for the model.\n *\n * Provides information about the model's capabilities and constraints,\n * including token limits, multimodal support, and advanced features like\n * tool calling and structured output.\n *\n * @returns {ModelProfile} An object describing the model's capabilities and constraints\n *\n * @example\n * ```typescript\n * const model = new ChatGoogleGenerativeAI({ model: \"gemini-1.5-flash\" });\n * const profile = model.profile;\n * console.log(profile.maxInputTokens); // 2000000\n * console.log(profile.imageInputs); // true\n * ```\n */\n get profile(): ModelProfile {\n return PROFILES[this.model] ?? {};\n }\n\n withStructuredOutput<\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n outputSchema:\n | InteropZodType<RunOutput>\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<false>\n ): Runnable<BaseLanguageModelInput, RunOutput>;\n\n withStructuredOutput<\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n outputSchema:\n | InteropZodType<RunOutput>\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<true>\n ): Runnable<BaseLanguageModelInput, { raw: BaseMessage; parsed: RunOutput }>;\n\n withStructuredOutput<\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n RunOutput extends Record<string, any> = Record<string, any>,\n >(\n outputSchema:\n | InteropZodType<RunOutput>\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n | Record<string, any>,\n config?: StructuredOutputMethodOptions<boolean>\n ):\n | Runnable<BaseLanguageModelInput, RunOutput>\n | Runnable<\n BaseLanguageModelInput,\n { raw: BaseMessage; parsed: RunOutput }\n > {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const schema: InteropZodType<RunOutput> | Record<string, any> =\n outputSchema;\n const name = config?.name;\n const method = config?.method;\n const includeRaw = config?.includeRaw;\n if (method === \"jsonMode\") {\n throw new Error(\n `ChatGoogleGenerativeAI only supports \"jsonSchema\" or \"functionCalling\" as a method.`\n );\n }\n\n let llm;\n let outputParser: BaseLLMOutputParser<RunOutput>;\n if (method === \"functionCalling\") {\n let functionName = name ?? \"extract\";\n let tools: GoogleGenerativeAIFunctionDeclarationsTool[];\n if (isInteropZodSchema(schema)) {\n const jsonSchema = schemaToGenerativeAIParameters(schema);\n tools = [\n {\n functionDeclarations: [\n {\n name: functionName,\n description:\n jsonSchema.description ?? \"A function available to call.\",\n parameters: jsonSchema as GenerativeAIFunctionDeclarationSchema,\n },\n ],\n },\n ];\n outputParser = new GoogleGenerativeAIToolsOutputParser<\n InferInteropZodOutput<typeof schema>\n >({\n returnSingle: true,\n keyName: functionName,\n zodSchema: schema,\n });\n } else {\n let geminiFunctionDefinition: GenerativeAIFunctionDeclaration;\n if (\n typeof schema.name === \"string\" &&\n typeof schema.parameters === \"object\" &&\n schema.parameters != null\n ) {\n geminiFunctionDefinition = schema as GenerativeAIFunctionDeclaration;\n geminiFunctionDefinition.parameters = removeAdditionalProperties(\n schema.parameters\n ) as GenerativeAIFunctionDeclarationSchema;\n functionName = schema.name;\n } else {\n geminiFunctionDefinition = {\n name: functionName,\n description: schema.description ?? \"\",\n parameters: removeAdditionalProperties(\n schema\n ) as GenerativeAIFunctionDeclarationSchema,\n };\n }\n tools = [\n {\n functionDeclarations: [geminiFunctionDefinition],\n },\n ];\n outputParser = new GoogleGenerativeAIToolsOutputParser<RunOutput>({\n returnSingle: true,\n keyName: functionName,\n });\n }\n llm = this.bindTools(tools).withConfig({\n allowedFunctionNames: [functionName],\n });\n } else {\n const jsonSchema = schemaToGenerativeAIParameters(schema);\n llm = this.withConfig({\n responseSchema: jsonSchema as Schema,\n });\n outputParser = new JsonOutputParser();\n }\n\n if (!includeRaw) {\n return llm.pipe(outputParser).withConfig({\n runName: \"ChatGoogleGenerativeAIStructuredOutput\",\n }) as Runnable<BaseLanguageModelInput, RunOutput>;\n }\n\n const parserAssign = RunnablePassthrough.assign({\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n parsed: (input: any, config) => outputParser.invoke(input.raw, config),\n });\n const parserNone = RunnablePassthrough.assign({\n parsed: () => null,\n });\n const parsedWithFallback = parserAssign.withFallbacks({\n fallbacks: [parserNone],\n });\n return RunnableSequence.from<\n BaseLanguageModelInput,\n { raw: BaseMessage; parsed: RunOutput }\n >([\n {\n raw: llm,\n },\n parsedWithFallback,\n ]).withConfig({\n runName: \"StructuredOutputRunnable\",\n });\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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,eAAe,oBAAoB,OAAO,EAE9C,SAAS,OAAY,WAAW,aAAa,OAAO,MAAM,KAAK,OAAO,EACvE,CAAC;EACF,MAAM,aAAa,oBAAoB,OAAO,EAC5C,cAAc,MACf,CAAC;EACF,MAAM,qBAAqB,aAAa,cAAc,EACpD,WAAW,CAAC,WAAW,EACxB,CAAC;AACF,SAAO,iBAAiB,KAGtB,CACA,EACE,KAAK,KACN,EACD,mBACD,CAAC,CAAC,WAAW,EACZ,SAAS,4BACV,CAAC"} |
+4
-4
| { | ||
| "name": "@langchain/google-genai", | ||
| "version": "2.1.20", | ||
| "version": "2.1.21", | ||
| "description": "Google Generative AI integration for LangChain.js", | ||
@@ -21,7 +21,7 @@ "author": "LangChain", | ||
| "peerDependencies": { | ||
| "@langchain/core": "^1.1.27" | ||
| "@langchain/core": "^1.1.29" | ||
| }, | ||
| "devDependencies": { | ||
| "@jest/globals": "^30.2.0", | ||
| "@swc/core": "^1.15.11", | ||
| "@swc/core": "^1.15.13", | ||
| "@swc/jest": "^0.2.29", | ||
@@ -39,3 +39,3 @@ "@tsconfig/recommended": "^1.0.3", | ||
| "zod": "^3.25.76", | ||
| "@langchain/core": "^1.1.27", | ||
| "@langchain/core": "^1.1.29", | ||
| "@langchain/eslint": "0.1.1", | ||
@@ -42,0 +42,0 @@ "@langchain/standard-tests": "0.0.23", |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
444051
0.27%4213
0.05%