@langchain/google-genai 0.2.17 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/LICENSE +6 -6
  3. package/README.md +8 -8
  4. package/dist/_virtual/rolldown_runtime.cjs +25 -0
  5. package/dist/chat_models.cjs +667 -845
  6. package/dist/chat_models.cjs.map +1 -0
  7. package/dist/chat_models.d.cts +556 -0
  8. package/dist/chat_models.d.cts.map +1 -0
  9. package/dist/chat_models.d.ts +171 -157
  10. package/dist/chat_models.d.ts.map +1 -0
  11. package/dist/chat_models.js +665 -840
  12. package/dist/chat_models.js.map +1 -0
  13. package/dist/embeddings.cjs +97 -151
  14. package/dist/embeddings.cjs.map +1 -0
  15. package/dist/embeddings.d.cts +104 -0
  16. package/dist/embeddings.d.cts.map +1 -0
  17. package/dist/embeddings.d.ts +76 -70
  18. package/dist/embeddings.d.ts.map +1 -0
  19. package/dist/embeddings.js +93 -144
  20. package/dist/embeddings.js.map +1 -0
  21. package/dist/index.cjs +5 -18
  22. package/dist/index.d.cts +3 -0
  23. package/dist/index.d.ts +3 -2
  24. package/dist/index.js +4 -2
  25. package/dist/output_parsers.cjs +47 -75
  26. package/dist/output_parsers.cjs.map +1 -0
  27. package/dist/output_parsers.js +47 -72
  28. package/dist/output_parsers.js.map +1 -0
  29. package/dist/types.d.cts +8 -0
  30. package/dist/types.d.cts.map +1 -0
  31. package/dist/types.d.ts +7 -2
  32. package/dist/types.d.ts.map +1 -0
  33. package/dist/utils/common.cjs +356 -549
  34. package/dist/utils/common.cjs.map +1 -0
  35. package/dist/utils/common.js +357 -545
  36. package/dist/utils/common.js.map +1 -0
  37. package/dist/utils/tools.cjs +65 -102
  38. package/dist/utils/tools.cjs.map +1 -0
  39. package/dist/utils/tools.js +64 -99
  40. package/dist/utils/tools.js.map +1 -0
  41. package/dist/utils/zod_to_genai_parameters.cjs +31 -49
  42. package/dist/utils/zod_to_genai_parameters.cjs.map +1 -0
  43. package/dist/utils/zod_to_genai_parameters.js +29 -45
  44. package/dist/utils/zod_to_genai_parameters.js.map +1 -0
  45. package/package.json +42 -51
  46. package/dist/output_parsers.d.ts +0 -20
  47. package/dist/types.cjs +0 -2
  48. package/dist/types.js +0 -1
  49. package/dist/utils/common.d.ts +0 -22
  50. package/dist/utils/tools.d.ts +0 -10
  51. package/dist/utils/zod_to_genai_parameters.d.ts +0 -14
  52. package/index.cjs +0 -1
  53. package/index.d.cts +0 -1
  54. package/index.d.ts +0 -1
  55. package/index.js +0 -1
@@ -1,843 +1,668 @@
1
- import { GoogleGenerativeAI as GenerativeAI, } from "@google/generative-ai";
2
- import { getEnvironmentVariable } from "@langchain/core/utils/env";
3
- import { BaseChatModel, } from "@langchain/core/language_models/chat_models";
4
- import { RunnablePassthrough, RunnableSequence, } from "@langchain/core/runnables";
5
- import { isInteropZodSchema, } from "@langchain/core/utils/types";
6
- import { JsonOutputParser, } from "@langchain/core/output_parsers";
7
- import { schemaToGenerativeAIParameters, removeAdditionalProperties, } from "./utils/zod_to_genai_parameters.js";
8
- import { convertBaseMessagesToContent, convertResponseContentToChatGenerationChunk, mapGenerateContentResultToChatResult, } from "./utils/common.js";
1
+ import { removeAdditionalProperties, schemaToGenerativeAIParameters } from "./utils/zod_to_genai_parameters.js";
2
+ import { convertBaseMessagesToContent, convertResponseContentToChatGenerationChunk, mapGenerateContentResultToChatResult } from "./utils/common.js";
9
3
  import { GoogleGenerativeAIToolsOutputParser } from "./output_parsers.js";
10
4
  import { convertToolsToGenAI } from "./utils/tools.js";
5
+ import { GoogleGenerativeAI } from "@google/generative-ai";
6
+ import { getEnvironmentVariable } from "@langchain/core/utils/env";
7
+ import { BaseChatModel } from "@langchain/core/language_models/chat_models";
8
+ import { RunnablePassthrough, RunnableSequence } from "@langchain/core/runnables";
9
+ import { isInteropZodSchema } from "@langchain/core/utils/types";
10
+ import { JsonOutputParser } from "@langchain/core/output_parsers";
11
+
12
+ //#region src/chat_models.ts
11
13
  /**
12
- * Google Generative AI chat model integration.
13
- *
14
- * Setup:
15
- * Install `@langchain/google-genai` and set an environment variable named `GOOGLE_API_KEY`.
16
- *
17
- * ```bash
18
- * npm install @langchain/google-genai
19
- * export GOOGLE_API_KEY="your-api-key"
20
- * ```
21
- *
22
- * ## [Constructor args](https://api.js.langchain.com/classes/langchain_google_genai.ChatGoogleGenerativeAI.html#constructor)
23
- *
24
- * ## [Runtime args](https://api.js.langchain.com/interfaces/langchain_google_genai.GoogleGenerativeAIChatCallOptions.html)
25
- *
26
- * Runtime args can be passed as the second argument to any of the base runnable methods `.invoke`. `.stream`, `.batch`, etc.
27
- * They can also be passed via `.withConfig`, or the second arg in `.bindTools`, like shown in the examples below:
28
- *
29
- * ```typescript
30
- * // When calling `.withConfig`, call options should be passed via the first argument
31
- * const llmWithArgsBound = llm.withConfig({
32
- * stop: ["\n"],
33
- * });
34
- *
35
- * // When calling `.bindTools`, call options should be passed via the second argument
36
- * const llmWithTools = llm.bindTools(
37
- * [...],
38
- * {
39
- * stop: ["\n"],
40
- * }
41
- * );
42
- * ```
43
- *
44
- * ## Examples
45
- *
46
- * <details open>
47
- * <summary><strong>Instantiate</strong></summary>
48
- *
49
- * ```typescript
50
- * import { ChatGoogleGenerativeAI } from '@langchain/google-genai';
51
- *
52
- * const llm = new ChatGoogleGenerativeAI({
53
- * model: "gemini-1.5-flash",
54
- * temperature: 0,
55
- * maxRetries: 2,
56
- * // apiKey: "...",
57
- * // other params...
58
- * });
59
- * ```
60
- * </details>
61
- *
62
- * <br />
63
- *
64
- * <details>
65
- * <summary><strong>Invoking</strong></summary>
66
- *
67
- * ```typescript
68
- * const input = `Translate "I love programming" into French.`;
69
- *
70
- * // Models also accept a list of chat messages or a formatted prompt
71
- * const result = await llm.invoke(input);
72
- * console.log(result);
73
- * ```
74
- *
75
- * ```txt
76
- * AIMessage {
77
- * "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",
78
- * "response_metadata": {
79
- * "finishReason": "STOP",
80
- * "index": 0,
81
- * "safetyRatings": [
82
- * {
83
- * "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
84
- * "probability": "NEGLIGIBLE"
85
- * },
86
- * {
87
- * "category": "HARM_CATEGORY_HATE_SPEECH",
88
- * "probability": "NEGLIGIBLE"
89
- * },
90
- * {
91
- * "category": "HARM_CATEGORY_HARASSMENT",
92
- * "probability": "NEGLIGIBLE"
93
- * },
94
- * {
95
- * "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
96
- * "probability": "NEGLIGIBLE"
97
- * }
98
- * ]
99
- * },
100
- * "usage_metadata": {
101
- * "input_tokens": 10,
102
- * "output_tokens": 149,
103
- * "total_tokens": 159
104
- * }
105
- * }
106
- * ```
107
- * </details>
108
- *
109
- * <br />
110
- *
111
- * <details>
112
- * <summary><strong>Streaming Chunks</strong></summary>
113
- *
114
- * ```typescript
115
- * for await (const chunk of await llm.stream(input)) {
116
- * console.log(chunk);
117
- * }
118
- * ```
119
- *
120
- * ```txt
121
- * AIMessageChunk {
122
- * "content": "There",
123
- * "response_metadata": {
124
- * "index": 0
125
- * }
126
- * "usage_metadata": {
127
- * "input_tokens": 10,
128
- * "output_tokens": 1,
129
- * "total_tokens": 11
130
- * }
131
- * }
132
- * AIMessageChunk {
133
- * "content": " are a few ways to translate \"I love programming\" into French, depending on",
134
- * }
135
- * AIMessageChunk {
136
- * "content": " the level of formality and nuance you want to convey:\n\n**Formal:**\n\n",
137
- * }
138
- * AIMessageChunk {
139
- * "content": "* **J'aime la programmation.** (This is the most literal and formal translation.)\n\n**Informal:**\n\n* **J'adore programmer.** (This",
140
- * }
141
- * AIMessageChunk {
142
- * "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",
143
- * }
144
- * AIMessageChunk {
145
- * "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",
146
- * }
147
- * AIMessageChunk {
148
- * "content": " your intended audience. \n",
149
- * }
150
- * ```
151
- * </details>
152
- *
153
- * <br />
154
- *
155
- * <details>
156
- * <summary><strong>Aggregate Streamed Chunks</strong></summary>
157
- *
158
- * ```typescript
159
- * import { AIMessageChunk } from '@langchain/core/messages';
160
- * import { concat } from '@langchain/core/utils/stream';
161
- *
162
- * const stream = await llm.stream(input);
163
- * let full: AIMessageChunk | undefined;
164
- * for await (const chunk of stream) {
165
- * full = !full ? chunk : concat(full, chunk);
166
- * }
167
- * console.log(full);
168
- * ```
169
- *
170
- * ```txt
171
- * AIMessageChunk {
172
- * "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",
173
- * "usage_metadata": {
174
- * "input_tokens": 10,
175
- * "output_tokens": 277,
176
- * "total_tokens": 287
177
- * }
178
- * }
179
- * ```
180
- * </details>
181
- *
182
- * <br />
183
- *
184
- * <details>
185
- * <summary><strong>Bind tools</strong></summary>
186
- *
187
- * ```typescript
188
- * import { z } from 'zod';
189
- *
190
- * const GetWeather = {
191
- * name: "GetWeather",
192
- * description: "Get the current weather in a given location",
193
- * schema: z.object({
194
- * location: z.string().describe("The city and state, e.g. San Francisco, CA")
195
- * }),
196
- * }
197
- *
198
- * const GetPopulation = {
199
- * name: "GetPopulation",
200
- * description: "Get the current population in a given location",
201
- * schema: z.object({
202
- * location: z.string().describe("The city and state, e.g. San Francisco, CA")
203
- * }),
204
- * }
205
- *
206
- * const llmWithTools = llm.bindTools([GetWeather, GetPopulation]);
207
- * const aiMsg = await llmWithTools.invoke(
208
- * "Which city is hotter today and which is bigger: LA or NY?"
209
- * );
210
- * console.log(aiMsg.tool_calls);
211
- * ```
212
- *
213
- * ```txt
214
- * [
215
- * {
216
- * name: 'GetWeather',
217
- * args: { location: 'Los Angeles, CA' },
218
- * type: 'tool_call'
219
- * },
220
- * {
221
- * name: 'GetWeather',
222
- * args: { location: 'New York, NY' },
223
- * type: 'tool_call'
224
- * },
225
- * {
226
- * name: 'GetPopulation',
227
- * args: { location: 'Los Angeles, CA' },
228
- * type: 'tool_call'
229
- * },
230
- * {
231
- * name: 'GetPopulation',
232
- * args: { location: 'New York, NY' },
233
- * type: 'tool_call'
234
- * }
235
- * ]
236
- * ```
237
- * </details>
238
- *
239
- * <br />
240
- *
241
- * <details>
242
- * <summary><strong>Structured Output</strong></summary>
243
- *
244
- * ```typescript
245
- * const Joke = z.object({
246
- * setup: z.string().describe("The setup of the joke"),
247
- * punchline: z.string().describe("The punchline to the joke"),
248
- * rating: z.number().optional().describe("How funny the joke is, from 1 to 10")
249
- * }).describe('Joke to tell user.');
250
- *
251
- * const structuredLlm = llm.withStructuredOutput(Joke, { name: "Joke" });
252
- * const jokeResult = await structuredLlm.invoke("Tell me a joke about cats");
253
- * console.log(jokeResult);
254
- * ```
255
- *
256
- * ```txt
257
- * {
258
- * setup: "Why don\\'t cats play poker?",
259
- * punchline: "Why don\\'t cats play poker? Because they always have an ace up their sleeve!"
260
- * }
261
- * ```
262
- * </details>
263
- *
264
- * <br />
265
- *
266
- * <details>
267
- * <summary><strong>Multimodal</strong></summary>
268
- *
269
- * ```typescript
270
- * import { HumanMessage } from '@langchain/core/messages';
271
- *
272
- * const imageUrl = "https://example.com/image.jpg";
273
- * const imageData = await fetch(imageUrl).then(res => res.arrayBuffer());
274
- * const base64Image = Buffer.from(imageData).toString('base64');
275
- *
276
- * const message = new HumanMessage({
277
- * content: [
278
- * { type: "text", text: "describe the weather in this image" },
279
- * {
280
- * type: "image_url",
281
- * image_url: { url: `data:image/jpeg;base64,${base64Image}` },
282
- * },
283
- * ]
284
- * });
285
- *
286
- * const imageDescriptionAiMsg = await llm.invoke([message]);
287
- * console.log(imageDescriptionAiMsg.content);
288
- * ```
289
- *
290
- * ```txt
291
- * 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.
292
- * ```
293
- * </details>
294
- *
295
- * <br />
296
- *
297
- * <details>
298
- * <summary><strong>Usage Metadata</strong></summary>
299
- *
300
- * ```typescript
301
- * const aiMsgForMetadata = await llm.invoke(input);
302
- * console.log(aiMsgForMetadata.usage_metadata);
303
- * ```
304
- *
305
- * ```txt
306
- * { input_tokens: 10, output_tokens: 149, total_tokens: 159 }
307
- * ```
308
- * </details>
309
- *
310
- * <br />
311
- *
312
- * <details>
313
- * <summary><strong>Response Metadata</strong></summary>
314
- *
315
- * ```typescript
316
- * const aiMsgForResponseMetadata = await llm.invoke(input);
317
- * console.log(aiMsgForResponseMetadata.response_metadata);
318
- * ```
319
- *
320
- * ```txt
321
- * {
322
- * finishReason: 'STOP',
323
- * index: 0,
324
- * safetyRatings: [
325
- * {
326
- * category: 'HARM_CATEGORY_SEXUALLY_EXPLICIT',
327
- * probability: 'NEGLIGIBLE'
328
- * },
329
- * {
330
- * category: 'HARM_CATEGORY_HATE_SPEECH',
331
- * probability: 'NEGLIGIBLE'
332
- * },
333
- * { category: 'HARM_CATEGORY_HARASSMENT', probability: 'NEGLIGIBLE' },
334
- * {
335
- * category: 'HARM_CATEGORY_DANGEROUS_CONTENT',
336
- * probability: 'NEGLIGIBLE'
337
- * }
338
- * ]
339
- * }
340
- * ```
341
- * </details>
342
- *
343
- * <br />
344
- *
345
- * <details>
346
- * <summary><strong>Document Messages</strong></summary>
347
- *
348
- * This example will show you how to pass documents such as PDFs to Google
349
- * Generative AI through messages.
350
- *
351
- * ```typescript
352
- * const pdfPath = "/Users/my_user/Downloads/invoice.pdf";
353
- * const pdfBase64 = await fs.readFile(pdfPath, "base64");
354
- *
355
- * const response = await llm.invoke([
356
- * ["system", "Use the provided documents to answer the question"],
357
- * [
358
- * "user",
359
- * [
360
- * {
361
- * type: "application/pdf", // If the `type` field includes a single slash (`/`), it will be treated as inline data.
362
- * data: pdfBase64,
363
- * },
364
- * {
365
- * type: "text",
366
- * text: "Summarize the contents of this PDF",
367
- * },
368
- * ],
369
- * ],
370
- * ]);
371
- *
372
- * console.log(response.content);
373
- * ```
374
- *
375
- * ```txt
376
- * This is a billing invoice from Twitter Developers for X API Basic Access. The transaction date is January 7, 2025,
377
- * 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).
378
- * 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,
379
- * expiring in 12/2026. The billing address is Brace Sproul, 1234 Main Street, San Francisco, CA, US 94103. The company being billed is
380
- * X Corp, located at 865 FM 1209 Building 2, Bastrop, TX, US 78602. Terms and conditions apply.
381
- * ```
382
- * </details>
383
- *
384
- * <br />
385
- */
386
- export class ChatGoogleGenerativeAI extends BaseChatModel {
387
- static lc_name() {
388
- return "ChatGoogleGenerativeAI";
389
- }
390
- get lc_secrets() {
391
- return {
392
- apiKey: "GOOGLE_API_KEY",
393
- };
394
- }
395
- get lc_aliases() {
396
- return {
397
- apiKey: "google_api_key",
398
- };
399
- }
400
- get _isMultimodalModel() {
401
- return (this.model.includes("vision") ||
402
- this.model.startsWith("gemini-1.5") ||
403
- this.model.startsWith("gemini-2") ||
404
- (this.model.startsWith("gemma-3-") &&
405
- !this.model.startsWith("gemma-3-1b")) // gemma-3 models are multimodal(but gemma-3n-* and gemma-3-1b are not)
406
- );
407
- }
408
- constructor(fields) {
409
- super(fields);
410
- Object.defineProperty(this, "lc_serializable", {
411
- enumerable: true,
412
- configurable: true,
413
- writable: true,
414
- value: true
415
- });
416
- Object.defineProperty(this, "lc_namespace", {
417
- enumerable: true,
418
- configurable: true,
419
- writable: true,
420
- value: ["langchain", "chat_models", "google_genai"]
421
- });
422
- Object.defineProperty(this, "model", {
423
- enumerable: true,
424
- configurable: true,
425
- writable: true,
426
- value: void 0
427
- });
428
- Object.defineProperty(this, "temperature", {
429
- enumerable: true,
430
- configurable: true,
431
- writable: true,
432
- value: void 0
433
- }); // default value chosen based on model
434
- Object.defineProperty(this, "maxOutputTokens", {
435
- enumerable: true,
436
- configurable: true,
437
- writable: true,
438
- value: void 0
439
- });
440
- Object.defineProperty(this, "topP", {
441
- enumerable: true,
442
- configurable: true,
443
- writable: true,
444
- value: void 0
445
- }); // default value chosen based on model
446
- Object.defineProperty(this, "topK", {
447
- enumerable: true,
448
- configurable: true,
449
- writable: true,
450
- value: void 0
451
- }); // default value chosen based on model
452
- Object.defineProperty(this, "stopSequences", {
453
- enumerable: true,
454
- configurable: true,
455
- writable: true,
456
- value: []
457
- });
458
- Object.defineProperty(this, "safetySettings", {
459
- enumerable: true,
460
- configurable: true,
461
- writable: true,
462
- value: void 0
463
- });
464
- Object.defineProperty(this, "apiKey", {
465
- enumerable: true,
466
- configurable: true,
467
- writable: true,
468
- value: void 0
469
- });
470
- Object.defineProperty(this, "streaming", {
471
- enumerable: true,
472
- configurable: true,
473
- writable: true,
474
- value: false
475
- });
476
- Object.defineProperty(this, "json", {
477
- enumerable: true,
478
- configurable: true,
479
- writable: true,
480
- value: void 0
481
- });
482
- Object.defineProperty(this, "streamUsage", {
483
- enumerable: true,
484
- configurable: true,
485
- writable: true,
486
- value: true
487
- });
488
- Object.defineProperty(this, "convertSystemMessageToHumanContent", {
489
- enumerable: true,
490
- configurable: true,
491
- writable: true,
492
- value: void 0
493
- });
494
- Object.defineProperty(this, "client", {
495
- enumerable: true,
496
- configurable: true,
497
- writable: true,
498
- value: void 0
499
- });
500
- this.model = fields.model.replace(/^models\//, "");
501
- this.maxOutputTokens = fields.maxOutputTokens ?? this.maxOutputTokens;
502
- if (this.maxOutputTokens && this.maxOutputTokens < 0) {
503
- throw new Error("`maxOutputTokens` must be a positive integer");
504
- }
505
- this.temperature = fields.temperature ?? this.temperature;
506
- if (this.temperature && (this.temperature < 0 || this.temperature > 2)) {
507
- throw new Error("`temperature` must be in the range of [0.0,2.0]");
508
- }
509
- this.topP = fields.topP ?? this.topP;
510
- if (this.topP && this.topP < 0) {
511
- throw new Error("`topP` must be a positive integer");
512
- }
513
- if (this.topP && this.topP > 1) {
514
- throw new Error("`topP` must be below 1.");
515
- }
516
- this.topK = fields.topK ?? this.topK;
517
- if (this.topK && this.topK < 0) {
518
- throw new Error("`topK` must be a positive integer");
519
- }
520
- this.stopSequences = fields.stopSequences ?? this.stopSequences;
521
- this.apiKey = fields.apiKey ?? getEnvironmentVariable("GOOGLE_API_KEY");
522
- if (!this.apiKey) {
523
- throw new Error("Please set an API key for Google GenerativeAI " +
524
- "in the environment variable GOOGLE_API_KEY " +
525
- "or in the `apiKey` field of the " +
526
- "ChatGoogleGenerativeAI constructor");
527
- }
528
- this.safetySettings = fields.safetySettings ?? this.safetySettings;
529
- if (this.safetySettings && this.safetySettings.length > 0) {
530
- const safetySettingsSet = new Set(this.safetySettings.map((s) => s.category));
531
- if (safetySettingsSet.size !== this.safetySettings.length) {
532
- throw new Error("The categories in `safetySettings` array must be unique");
533
- }
534
- }
535
- this.streaming = fields.streaming ?? this.streaming;
536
- this.json = fields.json;
537
- this.client = new GenerativeAI(this.apiKey).getGenerativeModel({
538
- model: this.model,
539
- safetySettings: this.safetySettings,
540
- generationConfig: {
541
- stopSequences: this.stopSequences,
542
- maxOutputTokens: this.maxOutputTokens,
543
- temperature: this.temperature,
544
- topP: this.topP,
545
- topK: this.topK,
546
- ...(this.json ? { responseMimeType: "application/json" } : {}),
547
- },
548
- }, {
549
- apiVersion: fields.apiVersion,
550
- baseUrl: fields.baseUrl,
551
- });
552
- this.streamUsage = fields.streamUsage ?? this.streamUsage;
553
- }
554
- useCachedContent(cachedContent, modelParams, requestOptions) {
555
- if (!this.apiKey)
556
- return;
557
- this.client = new GenerativeAI(this.apiKey).getGenerativeModelFromCachedContent(cachedContent, modelParams, requestOptions);
558
- }
559
- get useSystemInstruction() {
560
- return typeof this.convertSystemMessageToHumanContent === "boolean"
561
- ? !this.convertSystemMessageToHumanContent
562
- : this.computeUseSystemInstruction;
563
- }
564
- get computeUseSystemInstruction() {
565
- // This works on models from April 2024 and later
566
- // Vertex AI: gemini-1.5-pro and gemini-1.0-002 and later
567
- // AI Studio: gemini-1.5-pro-latest
568
- if (this.model === "gemini-1.0-pro-001") {
569
- return false;
570
- }
571
- else if (this.model.startsWith("gemini-pro-vision")) {
572
- return false;
573
- }
574
- else if (this.model.startsWith("gemini-1.0-pro-vision")) {
575
- return false;
576
- }
577
- else if (this.model === "gemini-pro") {
578
- // on AI Studio gemini-pro is still pointing at gemini-1.0-pro-001
579
- return false;
580
- }
581
- return true;
582
- }
583
- getLsParams(options) {
584
- return {
585
- ls_provider: "google_genai",
586
- ls_model_name: this.model,
587
- ls_model_type: "chat",
588
- ls_temperature: this.client.generationConfig.temperature,
589
- ls_max_tokens: this.client.generationConfig.maxOutputTokens,
590
- ls_stop: options.stop,
591
- };
592
- }
593
- _combineLLMOutput() {
594
- return [];
595
- }
596
- _llmType() {
597
- return "googlegenerativeai";
598
- }
599
- bindTools(tools, kwargs) {
600
- return this.withConfig({
601
- tools: convertToolsToGenAI(tools)?.tools,
602
- ...kwargs,
603
- });
604
- }
605
- invocationParams(options) {
606
- const toolsAndConfig = options?.tools?.length
607
- ? convertToolsToGenAI(options.tools, {
608
- toolChoice: options.tool_choice,
609
- allowedFunctionNames: options.allowedFunctionNames,
610
- })
611
- : undefined;
612
- if (options?.responseSchema) {
613
- this.client.generationConfig.responseSchema = options.responseSchema;
614
- this.client.generationConfig.responseMimeType = "application/json";
615
- }
616
- else {
617
- this.client.generationConfig.responseSchema = undefined;
618
- this.client.generationConfig.responseMimeType = this.json
619
- ? "application/json"
620
- : undefined;
621
- }
622
- return {
623
- ...(toolsAndConfig?.tools ? { tools: toolsAndConfig.tools } : {}),
624
- ...(toolsAndConfig?.toolConfig
625
- ? { toolConfig: toolsAndConfig.toolConfig }
626
- : {}),
627
- };
628
- }
629
- async _generate(messages, options, runManager) {
630
- const prompt = convertBaseMessagesToContent(messages, this._isMultimodalModel, this.useSystemInstruction);
631
- let actualPrompt = prompt;
632
- if (prompt[0].role === "system") {
633
- const [systemInstruction] = prompt;
634
- this.client.systemInstruction = systemInstruction;
635
- actualPrompt = prompt.slice(1);
636
- }
637
- const parameters = this.invocationParams(options);
638
- // Handle streaming
639
- if (this.streaming) {
640
- const tokenUsage = {};
641
- const stream = this._streamResponseChunks(messages, options, runManager);
642
- const finalChunks = {};
643
- for await (const chunk of stream) {
644
- const index = chunk.generationInfo?.completion ?? 0;
645
- if (finalChunks[index] === undefined) {
646
- finalChunks[index] = chunk;
647
- }
648
- else {
649
- finalChunks[index] = finalChunks[index].concat(chunk);
650
- }
651
- }
652
- const generations = Object.entries(finalChunks)
653
- .sort(([aKey], [bKey]) => parseInt(aKey, 10) - parseInt(bKey, 10))
654
- .map(([_, value]) => value);
655
- return { generations, llmOutput: { estimatedTokenUsage: tokenUsage } };
656
- }
657
- const res = await this.completionWithRetry({
658
- ...parameters,
659
- contents: actualPrompt,
660
- });
661
- let usageMetadata;
662
- if ("usageMetadata" in res.response) {
663
- const genAIUsageMetadata = res.response.usageMetadata;
664
- usageMetadata = {
665
- input_tokens: genAIUsageMetadata.promptTokenCount ?? 0,
666
- output_tokens: genAIUsageMetadata.candidatesTokenCount ?? 0,
667
- total_tokens: genAIUsageMetadata.totalTokenCount ?? 0,
668
- };
669
- }
670
- const generationResult = mapGenerateContentResultToChatResult(res.response, {
671
- usageMetadata,
672
- });
673
- // may not have generations in output if there was a refusal for safety reasons, malformed function call, etc.
674
- if (generationResult.generations?.length > 0) {
675
- await runManager?.handleLLMNewToken(generationResult.generations[0]?.text ?? "");
676
- }
677
- return generationResult;
678
- }
679
- async *_streamResponseChunks(messages, options, runManager) {
680
- const prompt = convertBaseMessagesToContent(messages, this._isMultimodalModel, this.useSystemInstruction);
681
- let actualPrompt = prompt;
682
- if (prompt[0].role === "system") {
683
- const [systemInstruction] = prompt;
684
- this.client.systemInstruction = systemInstruction;
685
- actualPrompt = prompt.slice(1);
686
- }
687
- const parameters = this.invocationParams(options);
688
- const request = {
689
- ...parameters,
690
- contents: actualPrompt,
691
- };
692
- const stream = await this.caller.callWithOptions({ signal: options?.signal }, async () => {
693
- const { stream } = await this.client.generateContentStream(request);
694
- return stream;
695
- });
696
- let usageMetadata;
697
- let index = 0;
698
- for await (const response of stream) {
699
- if ("usageMetadata" in response &&
700
- this.streamUsage !== false &&
701
- options.streamUsage !== false) {
702
- const genAIUsageMetadata = response.usageMetadata;
703
- if (!usageMetadata) {
704
- usageMetadata = {
705
- input_tokens: genAIUsageMetadata.promptTokenCount ?? 0,
706
- output_tokens: genAIUsageMetadata.candidatesTokenCount ?? 0,
707
- total_tokens: genAIUsageMetadata.totalTokenCount ?? 0,
708
- };
709
- }
710
- else {
711
- // Under the hood, LangChain combines the prompt tokens. Google returns the updated
712
- // total each time, so we need to find the difference between the tokens.
713
- const outputTokenDiff = (genAIUsageMetadata.candidatesTokenCount ?? 0) -
714
- usageMetadata.output_tokens;
715
- usageMetadata = {
716
- input_tokens: 0,
717
- output_tokens: outputTokenDiff,
718
- total_tokens: outputTokenDiff,
719
- };
720
- }
721
- }
722
- const chunk = convertResponseContentToChatGenerationChunk(response, {
723
- usageMetadata,
724
- index,
725
- });
726
- index += 1;
727
- if (!chunk) {
728
- continue;
729
- }
730
- yield chunk;
731
- await runManager?.handleLLMNewToken(chunk.text ?? "");
732
- }
733
- }
734
- async completionWithRetry(request, options) {
735
- return this.caller.callWithOptions({ signal: options?.signal }, async () => {
736
- try {
737
- return await this.client.generateContent(request);
738
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
739
- }
740
- catch (e) {
741
- // TODO: Improve error handling
742
- if (e.message?.includes("400 Bad Request")) {
743
- e.status = 400;
744
- }
745
- throw e;
746
- }
747
- });
748
- }
749
- withStructuredOutput(outputSchema, config) {
750
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
751
- const schema = outputSchema;
752
- const name = config?.name;
753
- const method = config?.method;
754
- const includeRaw = config?.includeRaw;
755
- if (method === "jsonMode") {
756
- throw new Error(`ChatGoogleGenerativeAI only supports "jsonSchema" or "functionCalling" as a method.`);
757
- }
758
- let llm;
759
- let outputParser;
760
- if (method === "functionCalling") {
761
- let functionName = name ?? "extract";
762
- let tools;
763
- if (isInteropZodSchema(schema)) {
764
- const jsonSchema = schemaToGenerativeAIParameters(schema);
765
- tools = [
766
- {
767
- functionDeclarations: [
768
- {
769
- name: functionName,
770
- description: jsonSchema.description ?? "A function available to call.",
771
- parameters: jsonSchema,
772
- },
773
- ],
774
- },
775
- ];
776
- outputParser = new GoogleGenerativeAIToolsOutputParser({
777
- returnSingle: true,
778
- keyName: functionName,
779
- zodSchema: schema,
780
- });
781
- }
782
- else {
783
- let geminiFunctionDefinition;
784
- if (typeof schema.name === "string" &&
785
- typeof schema.parameters === "object" &&
786
- schema.parameters != null) {
787
- geminiFunctionDefinition = schema;
788
- geminiFunctionDefinition.parameters = removeAdditionalProperties(schema.parameters);
789
- functionName = schema.name;
790
- }
791
- else {
792
- geminiFunctionDefinition = {
793
- name: functionName,
794
- description: schema.description ?? "",
795
- parameters: removeAdditionalProperties(schema),
796
- };
797
- }
798
- tools = [
799
- {
800
- functionDeclarations: [geminiFunctionDefinition],
801
- },
802
- ];
803
- outputParser = new GoogleGenerativeAIToolsOutputParser({
804
- returnSingle: true,
805
- keyName: functionName,
806
- });
807
- }
808
- llm = this.bindTools(tools).withConfig({
809
- allowedFunctionNames: [functionName],
810
- });
811
- }
812
- else {
813
- const jsonSchema = schemaToGenerativeAIParameters(schema);
814
- llm = this.withConfig({
815
- responseSchema: jsonSchema,
816
- });
817
- outputParser = new JsonOutputParser();
818
- }
819
- if (!includeRaw) {
820
- return llm.pipe(outputParser).withConfig({
821
- runName: "ChatGoogleGenerativeAIStructuredOutput",
822
- });
823
- }
824
- const parserAssign = RunnablePassthrough.assign({
825
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
826
- parsed: (input, config) => outputParser.invoke(input.raw, config),
827
- });
828
- const parserNone = RunnablePassthrough.assign({
829
- parsed: () => null,
830
- });
831
- const parsedWithFallback = parserAssign.withFallbacks({
832
- fallbacks: [parserNone],
833
- });
834
- return RunnableSequence.from([
835
- {
836
- raw: llm,
837
- },
838
- parsedWithFallback,
839
- ]).withConfig({
840
- runName: "StructuredOutputRunnable",
841
- });
842
- }
843
- }
14
+ * Google Generative AI chat model integration.
15
+ *
16
+ * Setup:
17
+ * Install `@langchain/google-genai` and set an environment variable named `GOOGLE_API_KEY`.
18
+ *
19
+ * ```bash
20
+ * npm install @langchain/google-genai
21
+ * export GOOGLE_API_KEY="your-api-key"
22
+ * ```
23
+ *
24
+ * ## [Constructor args](https://api.js.langchain.com/classes/langchain_google_genai.ChatGoogleGenerativeAI.html#constructor)
25
+ *
26
+ * ## [Runtime args](https://api.js.langchain.com/interfaces/langchain_google_genai.GoogleGenerativeAIChatCallOptions.html)
27
+ *
28
+ * Runtime args can be passed as the second argument to any of the base runnable methods `.invoke`. `.stream`, `.batch`, etc.
29
+ * They can also be passed via `.withConfig`, or the second arg in `.bindTools`, like shown in the examples below:
30
+ *
31
+ * ```typescript
32
+ * // When calling `.withConfig`, call options should be passed via the first argument
33
+ * const llmWithArgsBound = llm.withConfig({
34
+ * stop: ["\n"],
35
+ * });
36
+ *
37
+ * // When calling `.bindTools`, call options should be passed via the second argument
38
+ * const llmWithTools = llm.bindTools(
39
+ * [...],
40
+ * {
41
+ * stop: ["\n"],
42
+ * }
43
+ * );
44
+ * ```
45
+ *
46
+ * ## Examples
47
+ *
48
+ * <details open>
49
+ * <summary><strong>Instantiate</strong></summary>
50
+ *
51
+ * ```typescript
52
+ * import { ChatGoogleGenerativeAI } from '@langchain/google-genai';
53
+ *
54
+ * const llm = new ChatGoogleGenerativeAI({
55
+ * model: "gemini-1.5-flash",
56
+ * temperature: 0,
57
+ * maxRetries: 2,
58
+ * // apiKey: "...",
59
+ * // other params...
60
+ * });
61
+ * ```
62
+ * </details>
63
+ *
64
+ * <br />
65
+ *
66
+ * <details>
67
+ * <summary><strong>Invoking</strong></summary>
68
+ *
69
+ * ```typescript
70
+ * const input = `Translate "I love programming" into French.`;
71
+ *
72
+ * // Models also accept a list of chat messages or a formatted prompt
73
+ * const result = await llm.invoke(input);
74
+ * console.log(result);
75
+ * ```
76
+ *
77
+ * ```txt
78
+ * AIMessage {
79
+ * "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",
80
+ * "response_metadata": {
81
+ * "finishReason": "STOP",
82
+ * "index": 0,
83
+ * "safetyRatings": [
84
+ * {
85
+ * "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
86
+ * "probability": "NEGLIGIBLE"
87
+ * },
88
+ * {
89
+ * "category": "HARM_CATEGORY_HATE_SPEECH",
90
+ * "probability": "NEGLIGIBLE"
91
+ * },
92
+ * {
93
+ * "category": "HARM_CATEGORY_HARASSMENT",
94
+ * "probability": "NEGLIGIBLE"
95
+ * },
96
+ * {
97
+ * "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
98
+ * "probability": "NEGLIGIBLE"
99
+ * }
100
+ * ]
101
+ * },
102
+ * "usage_metadata": {
103
+ * "input_tokens": 10,
104
+ * "output_tokens": 149,
105
+ * "total_tokens": 159
106
+ * }
107
+ * }
108
+ * ```
109
+ * </details>
110
+ *
111
+ * <br />
112
+ *
113
+ * <details>
114
+ * <summary><strong>Streaming Chunks</strong></summary>
115
+ *
116
+ * ```typescript
117
+ * for await (const chunk of await llm.stream(input)) {
118
+ * console.log(chunk);
119
+ * }
120
+ * ```
121
+ *
122
+ * ```txt
123
+ * AIMessageChunk {
124
+ * "content": "There",
125
+ * "response_metadata": {
126
+ * "index": 0
127
+ * }
128
+ * "usage_metadata": {
129
+ * "input_tokens": 10,
130
+ * "output_tokens": 1,
131
+ * "total_tokens": 11
132
+ * }
133
+ * }
134
+ * AIMessageChunk {
135
+ * "content": " are a few ways to translate \"I love programming\" into French, depending on",
136
+ * }
137
+ * AIMessageChunk {
138
+ * "content": " the level of formality and nuance you want to convey:\n\n**Formal:**\n\n",
139
+ * }
140
+ * AIMessageChunk {
141
+ * "content": "* **J'aime la programmation.** (This is the most literal and formal translation.)\n\n**Informal:**\n\n* **J'adore programmer.** (This",
142
+ * }
143
+ * AIMessageChunk {
144
+ * "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",
145
+ * }
146
+ * AIMessageChunk {
147
+ * "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",
148
+ * }
149
+ * AIMessageChunk {
150
+ * "content": " your intended audience. \n",
151
+ * }
152
+ * ```
153
+ * </details>
154
+ *
155
+ * <br />
156
+ *
157
+ * <details>
158
+ * <summary><strong>Aggregate Streamed Chunks</strong></summary>
159
+ *
160
+ * ```typescript
161
+ * import { AIMessageChunk } from '@langchain/core/messages';
162
+ * import { concat } from '@langchain/core/utils/stream';
163
+ *
164
+ * const stream = await llm.stream(input);
165
+ * let full: AIMessageChunk | undefined;
166
+ * for await (const chunk of stream) {
167
+ * full = !full ? chunk : concat(full, chunk);
168
+ * }
169
+ * console.log(full);
170
+ * ```
171
+ *
172
+ * ```txt
173
+ * AIMessageChunk {
174
+ * "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",
175
+ * "usage_metadata": {
176
+ * "input_tokens": 10,
177
+ * "output_tokens": 277,
178
+ * "total_tokens": 287
179
+ * }
180
+ * }
181
+ * ```
182
+ * </details>
183
+ *
184
+ * <br />
185
+ *
186
+ * <details>
187
+ * <summary><strong>Bind tools</strong></summary>
188
+ *
189
+ * ```typescript
190
+ * import { z } from 'zod';
191
+ *
192
+ * const GetWeather = {
193
+ * name: "GetWeather",
194
+ * description: "Get the current weather in a given location",
195
+ * schema: z.object({
196
+ * location: z.string().describe("The city and state, e.g. San Francisco, CA")
197
+ * }),
198
+ * }
199
+ *
200
+ * const GetPopulation = {
201
+ * name: "GetPopulation",
202
+ * description: "Get the current population in a given location",
203
+ * schema: z.object({
204
+ * location: z.string().describe("The city and state, e.g. San Francisco, CA")
205
+ * }),
206
+ * }
207
+ *
208
+ * const llmWithTools = llm.bindTools([GetWeather, GetPopulation]);
209
+ * const aiMsg = await llmWithTools.invoke(
210
+ * "Which city is hotter today and which is bigger: LA or NY?"
211
+ * );
212
+ * console.log(aiMsg.tool_calls);
213
+ * ```
214
+ *
215
+ * ```txt
216
+ * [
217
+ * {
218
+ * name: 'GetWeather',
219
+ * args: { location: 'Los Angeles, CA' },
220
+ * type: 'tool_call'
221
+ * },
222
+ * {
223
+ * name: 'GetWeather',
224
+ * args: { location: 'New York, NY' },
225
+ * type: 'tool_call'
226
+ * },
227
+ * {
228
+ * name: 'GetPopulation',
229
+ * args: { location: 'Los Angeles, CA' },
230
+ * type: 'tool_call'
231
+ * },
232
+ * {
233
+ * name: 'GetPopulation',
234
+ * args: { location: 'New York, NY' },
235
+ * type: 'tool_call'
236
+ * }
237
+ * ]
238
+ * ```
239
+ * </details>
240
+ *
241
+ * <br />
242
+ *
243
+ * <details>
244
+ * <summary><strong>Structured Output</strong></summary>
245
+ *
246
+ * ```typescript
247
+ * const Joke = z.object({
248
+ * setup: z.string().describe("The setup of the joke"),
249
+ * punchline: z.string().describe("The punchline to the joke"),
250
+ * rating: z.number().optional().describe("How funny the joke is, from 1 to 10")
251
+ * }).describe('Joke to tell user.');
252
+ *
253
+ * const structuredLlm = llm.withStructuredOutput(Joke, { name: "Joke" });
254
+ * const jokeResult = await structuredLlm.invoke("Tell me a joke about cats");
255
+ * console.log(jokeResult);
256
+ * ```
257
+ *
258
+ * ```txt
259
+ * {
260
+ * setup: "Why don\\'t cats play poker?",
261
+ * punchline: "Why don\\'t cats play poker? Because they always have an ace up their sleeve!"
262
+ * }
263
+ * ```
264
+ * </details>
265
+ *
266
+ * <br />
267
+ *
268
+ * <details>
269
+ * <summary><strong>Multimodal</strong></summary>
270
+ *
271
+ * ```typescript
272
+ * import { HumanMessage } from '@langchain/core/messages';
273
+ *
274
+ * const imageUrl = "https://example.com/image.jpg";
275
+ * const imageData = await fetch(imageUrl).then(res => res.arrayBuffer());
276
+ * const base64Image = Buffer.from(imageData).toString('base64');
277
+ *
278
+ * const message = new HumanMessage({
279
+ * content: [
280
+ * { type: "text", text: "describe the weather in this image" },
281
+ * {
282
+ * type: "image_url",
283
+ * image_url: { url: `data:image/jpeg;base64,${base64Image}` },
284
+ * },
285
+ * ]
286
+ * });
287
+ *
288
+ * const imageDescriptionAiMsg = await llm.invoke([message]);
289
+ * console.log(imageDescriptionAiMsg.content);
290
+ * ```
291
+ *
292
+ * ```txt
293
+ * 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.
294
+ * ```
295
+ * </details>
296
+ *
297
+ * <br />
298
+ *
299
+ * <details>
300
+ * <summary><strong>Usage Metadata</strong></summary>
301
+ *
302
+ * ```typescript
303
+ * const aiMsgForMetadata = await llm.invoke(input);
304
+ * console.log(aiMsgForMetadata.usage_metadata);
305
+ * ```
306
+ *
307
+ * ```txt
308
+ * { input_tokens: 10, output_tokens: 149, total_tokens: 159 }
309
+ * ```
310
+ * </details>
311
+ *
312
+ * <br />
313
+ *
314
+ * <details>
315
+ * <summary><strong>Response Metadata</strong></summary>
316
+ *
317
+ * ```typescript
318
+ * const aiMsgForResponseMetadata = await llm.invoke(input);
319
+ * console.log(aiMsgForResponseMetadata.response_metadata);
320
+ * ```
321
+ *
322
+ * ```txt
323
+ * {
324
+ * finishReason: 'STOP',
325
+ * index: 0,
326
+ * safetyRatings: [
327
+ * {
328
+ * category: 'HARM_CATEGORY_SEXUALLY_EXPLICIT',
329
+ * probability: 'NEGLIGIBLE'
330
+ * },
331
+ * {
332
+ * category: 'HARM_CATEGORY_HATE_SPEECH',
333
+ * probability: 'NEGLIGIBLE'
334
+ * },
335
+ * { category: 'HARM_CATEGORY_HARASSMENT', probability: 'NEGLIGIBLE' },
336
+ * {
337
+ * category: 'HARM_CATEGORY_DANGEROUS_CONTENT',
338
+ * probability: 'NEGLIGIBLE'
339
+ * }
340
+ * ]
341
+ * }
342
+ * ```
343
+ * </details>
344
+ *
345
+ * <br />
346
+ *
347
+ * <details>
348
+ * <summary><strong>Document Messages</strong></summary>
349
+ *
350
+ * This example will show you how to pass documents such as PDFs to Google
351
+ * Generative AI through messages.
352
+ *
353
+ * ```typescript
354
+ * const pdfPath = "/Users/my_user/Downloads/invoice.pdf";
355
+ * const pdfBase64 = await fs.readFile(pdfPath, "base64");
356
+ *
357
+ * const response = await llm.invoke([
358
+ * ["system", "Use the provided documents to answer the question"],
359
+ * [
360
+ * "user",
361
+ * [
362
+ * {
363
+ * type: "application/pdf", // If the `type` field includes a single slash (`/`), it will be treated as inline data.
364
+ * data: pdfBase64,
365
+ * },
366
+ * {
367
+ * type: "text",
368
+ * text: "Summarize the contents of this PDF",
369
+ * },
370
+ * ],
371
+ * ],
372
+ * ]);
373
+ *
374
+ * console.log(response.content);
375
+ * ```
376
+ *
377
+ * ```txt
378
+ * This is a billing invoice from Twitter Developers for X API Basic Access. The transaction date is January 7, 2025,
379
+ * 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).
380
+ * 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,
381
+ * expiring in 12/2026. The billing address is Brace Sproul, 1234 Main Street, San Francisco, CA, US 94103. The company being billed is
382
+ * X Corp, located at 865 FM 1209 Building 2, Bastrop, TX, US 78602. Terms and conditions apply.
383
+ * ```
384
+ * </details>
385
+ *
386
+ * <br />
387
+ */
388
+ var ChatGoogleGenerativeAI = class extends BaseChatModel {
389
+ static lc_name() {
390
+ return "ChatGoogleGenerativeAI";
391
+ }
392
+ lc_serializable = true;
393
+ get lc_secrets() {
394
+ return { apiKey: "GOOGLE_API_KEY" };
395
+ }
396
+ lc_namespace = [
397
+ "langchain",
398
+ "chat_models",
399
+ "google_genai"
400
+ ];
401
+ get lc_aliases() {
402
+ return { apiKey: "google_api_key" };
403
+ }
404
+ model;
405
+ temperature;
406
+ maxOutputTokens;
407
+ topP;
408
+ topK;
409
+ stopSequences = [];
410
+ safetySettings;
411
+ apiKey;
412
+ streaming = false;
413
+ json;
414
+ streamUsage = true;
415
+ convertSystemMessageToHumanContent;
416
+ client;
417
+ get _isMultimodalModel() {
418
+ return this.model.includes("vision") || this.model.startsWith("gemini-1.5") || this.model.startsWith("gemini-2") || this.model.startsWith("gemma-3-") && !this.model.startsWith("gemma-3-1b");
419
+ }
420
+ constructor(fields) {
421
+ super(fields);
422
+ this.model = fields.model.replace(/^models\//, "");
423
+ this.maxOutputTokens = fields.maxOutputTokens ?? this.maxOutputTokens;
424
+ if (this.maxOutputTokens && this.maxOutputTokens < 0) throw new Error("`maxOutputTokens` must be a positive integer");
425
+ this.temperature = fields.temperature ?? this.temperature;
426
+ if (this.temperature && (this.temperature < 0 || this.temperature > 2)) throw new Error("`temperature` must be in the range of [0.0,2.0]");
427
+ this.topP = fields.topP ?? this.topP;
428
+ if (this.topP && this.topP < 0) throw new Error("`topP` must be a positive integer");
429
+ if (this.topP && this.topP > 1) throw new Error("`topP` must be below 1.");
430
+ this.topK = fields.topK ?? this.topK;
431
+ if (this.topK && this.topK < 0) throw new Error("`topK` must be a positive integer");
432
+ this.stopSequences = fields.stopSequences ?? this.stopSequences;
433
+ this.apiKey = fields.apiKey ?? getEnvironmentVariable("GOOGLE_API_KEY");
434
+ if (!this.apiKey) throw new Error("Please set an API key for Google GenerativeAI in the environment variable GOOGLE_API_KEY or in the `apiKey` field of the ChatGoogleGenerativeAI constructor");
435
+ this.safetySettings = fields.safetySettings ?? this.safetySettings;
436
+ if (this.safetySettings && this.safetySettings.length > 0) {
437
+ const safetySettingsSet = new Set(this.safetySettings.map((s) => s.category));
438
+ if (safetySettingsSet.size !== this.safetySettings.length) throw new Error("The categories in `safetySettings` array must be unique");
439
+ }
440
+ this.streaming = fields.streaming ?? this.streaming;
441
+ this.json = fields.json;
442
+ this.client = new GoogleGenerativeAI(this.apiKey).getGenerativeModel({
443
+ model: this.model,
444
+ safetySettings: this.safetySettings,
445
+ generationConfig: {
446
+ stopSequences: this.stopSequences,
447
+ maxOutputTokens: this.maxOutputTokens,
448
+ temperature: this.temperature,
449
+ topP: this.topP,
450
+ topK: this.topK,
451
+ ...this.json ? { responseMimeType: "application/json" } : {}
452
+ }
453
+ }, {
454
+ apiVersion: fields.apiVersion,
455
+ baseUrl: fields.baseUrl
456
+ });
457
+ this.streamUsage = fields.streamUsage ?? this.streamUsage;
458
+ }
459
+ useCachedContent(cachedContent, modelParams, requestOptions) {
460
+ if (!this.apiKey) return;
461
+ this.client = new GoogleGenerativeAI(this.apiKey).getGenerativeModelFromCachedContent(cachedContent, modelParams, requestOptions);
462
+ }
463
+ get useSystemInstruction() {
464
+ return typeof this.convertSystemMessageToHumanContent === "boolean" ? !this.convertSystemMessageToHumanContent : this.computeUseSystemInstruction;
465
+ }
466
+ get computeUseSystemInstruction() {
467
+ if (this.model === "gemini-1.0-pro-001") return false;
468
+ else if (this.model.startsWith("gemini-pro-vision")) return false;
469
+ else if (this.model.startsWith("gemini-1.0-pro-vision")) return false;
470
+ else if (this.model === "gemini-pro") return false;
471
+ return true;
472
+ }
473
+ getLsParams(options) {
474
+ return {
475
+ ls_provider: "google_genai",
476
+ ls_model_name: this.model,
477
+ ls_model_type: "chat",
478
+ ls_temperature: this.client.generationConfig.temperature,
479
+ ls_max_tokens: this.client.generationConfig.maxOutputTokens,
480
+ ls_stop: options.stop
481
+ };
482
+ }
483
+ _combineLLMOutput() {
484
+ return [];
485
+ }
486
+ _llmType() {
487
+ return "googlegenerativeai";
488
+ }
489
+ bindTools(tools, kwargs) {
490
+ return this.withConfig({
491
+ tools: convertToolsToGenAI(tools)?.tools,
492
+ ...kwargs
493
+ });
494
+ }
495
+ invocationParams(options) {
496
+ const toolsAndConfig = options?.tools?.length ? convertToolsToGenAI(options.tools, {
497
+ toolChoice: options.tool_choice,
498
+ allowedFunctionNames: options.allowedFunctionNames
499
+ }) : void 0;
500
+ if (options?.responseSchema) {
501
+ this.client.generationConfig.responseSchema = options.responseSchema;
502
+ this.client.generationConfig.responseMimeType = "application/json";
503
+ } else {
504
+ this.client.generationConfig.responseSchema = void 0;
505
+ this.client.generationConfig.responseMimeType = this.json ? "application/json" : void 0;
506
+ }
507
+ return {
508
+ ...toolsAndConfig?.tools ? { tools: toolsAndConfig.tools } : {},
509
+ ...toolsAndConfig?.toolConfig ? { toolConfig: toolsAndConfig.toolConfig } : {}
510
+ };
511
+ }
512
+ async _generate(messages, options, runManager) {
513
+ const prompt = convertBaseMessagesToContent(messages, this._isMultimodalModel, this.useSystemInstruction);
514
+ let actualPrompt = prompt;
515
+ if (prompt[0].role === "system") {
516
+ const [systemInstruction] = prompt;
517
+ this.client.systemInstruction = systemInstruction;
518
+ actualPrompt = prompt.slice(1);
519
+ }
520
+ const parameters = this.invocationParams(options);
521
+ if (this.streaming) {
522
+ const tokenUsage = {};
523
+ const stream = this._streamResponseChunks(messages, options, runManager);
524
+ const finalChunks = {};
525
+ for await (const chunk of stream) {
526
+ const index = chunk.generationInfo?.completion ?? 0;
527
+ if (finalChunks[index] === void 0) finalChunks[index] = chunk;
528
+ else finalChunks[index] = finalChunks[index].concat(chunk);
529
+ }
530
+ const generations = Object.entries(finalChunks).sort(([aKey], [bKey]) => parseInt(aKey, 10) - parseInt(bKey, 10)).map(([_, value]) => value);
531
+ return {
532
+ generations,
533
+ llmOutput: { estimatedTokenUsage: tokenUsage }
534
+ };
535
+ }
536
+ const res = await this.completionWithRetry({
537
+ ...parameters,
538
+ contents: actualPrompt
539
+ });
540
+ let usageMetadata;
541
+ if ("usageMetadata" in res.response) {
542
+ const genAIUsageMetadata = res.response.usageMetadata;
543
+ usageMetadata = {
544
+ input_tokens: genAIUsageMetadata.promptTokenCount ?? 0,
545
+ output_tokens: genAIUsageMetadata.candidatesTokenCount ?? 0,
546
+ total_tokens: genAIUsageMetadata.totalTokenCount ?? 0
547
+ };
548
+ }
549
+ const generationResult = mapGenerateContentResultToChatResult(res.response, { usageMetadata });
550
+ if (generationResult.generations?.length > 0) await runManager?.handleLLMNewToken(generationResult.generations[0]?.text ?? "");
551
+ return generationResult;
552
+ }
553
+ async *_streamResponseChunks(messages, options, runManager) {
554
+ const prompt = convertBaseMessagesToContent(messages, this._isMultimodalModel, this.useSystemInstruction);
555
+ let actualPrompt = prompt;
556
+ if (prompt[0].role === "system") {
557
+ const [systemInstruction] = prompt;
558
+ this.client.systemInstruction = systemInstruction;
559
+ actualPrompt = prompt.slice(1);
560
+ }
561
+ const parameters = this.invocationParams(options);
562
+ const request = {
563
+ ...parameters,
564
+ contents: actualPrompt
565
+ };
566
+ const stream = await this.caller.callWithOptions({ signal: options?.signal }, async () => {
567
+ const { stream: stream$1 } = await this.client.generateContentStream(request);
568
+ return stream$1;
569
+ });
570
+ let usageMetadata;
571
+ let prevPromptTokenCount = 0;
572
+ let prevCandidatesTokenCount = 0;
573
+ let prevTotalTokenCount = 0;
574
+ let index = 0;
575
+ for await (const response of stream) {
576
+ if ("usageMetadata" in response && response.usageMetadata !== void 0 && this.streamUsage !== false && options.streamUsage !== false) {
577
+ usageMetadata = {
578
+ input_tokens: response.usageMetadata.promptTokenCount ?? 0,
579
+ output_tokens: response.usageMetadata.candidatesTokenCount ?? 0,
580
+ total_tokens: response.usageMetadata.totalTokenCount ?? 0
581
+ };
582
+ const newPromptTokenCount = response.usageMetadata.promptTokenCount ?? 0;
583
+ usageMetadata.input_tokens = Math.max(0, newPromptTokenCount - prevPromptTokenCount);
584
+ prevPromptTokenCount = newPromptTokenCount;
585
+ const newCandidatesTokenCount = response.usageMetadata.candidatesTokenCount ?? 0;
586
+ usageMetadata.output_tokens = Math.max(0, newCandidatesTokenCount - prevCandidatesTokenCount);
587
+ prevCandidatesTokenCount = newCandidatesTokenCount;
588
+ const newTotalTokenCount = response.usageMetadata.totalTokenCount ?? 0;
589
+ usageMetadata.total_tokens = Math.max(0, newTotalTokenCount - prevTotalTokenCount);
590
+ prevTotalTokenCount = newTotalTokenCount;
591
+ }
592
+ const chunk = convertResponseContentToChatGenerationChunk(response, {
593
+ usageMetadata,
594
+ index
595
+ });
596
+ index += 1;
597
+ if (!chunk) continue;
598
+ yield chunk;
599
+ await runManager?.handleLLMNewToken(chunk.text ?? "");
600
+ }
601
+ }
602
+ async completionWithRetry(request, options) {
603
+ return this.caller.callWithOptions({ signal: options?.signal }, async () => {
604
+ try {
605
+ return await this.client.generateContent(request);
606
+ } catch (e) {
607
+ if (e.message?.includes("400 Bad Request")) e.status = 400;
608
+ throw e;
609
+ }
610
+ });
611
+ }
612
+ withStructuredOutput(outputSchema, config) {
613
+ const schema = outputSchema;
614
+ const name = config?.name;
615
+ const method = config?.method;
616
+ const includeRaw = config?.includeRaw;
617
+ if (method === "jsonMode") throw new Error(`ChatGoogleGenerativeAI only supports "jsonSchema" or "functionCalling" as a method.`);
618
+ let llm;
619
+ let outputParser;
620
+ if (method === "functionCalling") {
621
+ let functionName = name ?? "extract";
622
+ let tools;
623
+ if (isInteropZodSchema(schema)) {
624
+ const jsonSchema = schemaToGenerativeAIParameters(schema);
625
+ tools = [{ functionDeclarations: [{
626
+ name: functionName,
627
+ description: jsonSchema.description ?? "A function available to call.",
628
+ parameters: jsonSchema
629
+ }] }];
630
+ outputParser = new GoogleGenerativeAIToolsOutputParser({
631
+ returnSingle: true,
632
+ keyName: functionName,
633
+ zodSchema: schema
634
+ });
635
+ } else {
636
+ let geminiFunctionDefinition;
637
+ if (typeof schema.name === "string" && typeof schema.parameters === "object" && schema.parameters != null) {
638
+ geminiFunctionDefinition = schema;
639
+ geminiFunctionDefinition.parameters = removeAdditionalProperties(schema.parameters);
640
+ functionName = schema.name;
641
+ } else geminiFunctionDefinition = {
642
+ name: functionName,
643
+ description: schema.description ?? "",
644
+ parameters: removeAdditionalProperties(schema)
645
+ };
646
+ tools = [{ functionDeclarations: [geminiFunctionDefinition] }];
647
+ outputParser = new GoogleGenerativeAIToolsOutputParser({
648
+ returnSingle: true,
649
+ keyName: functionName
650
+ });
651
+ }
652
+ llm = this.bindTools(tools).withConfig({ allowedFunctionNames: [functionName] });
653
+ } else {
654
+ const jsonSchema = schemaToGenerativeAIParameters(schema);
655
+ llm = this.withConfig({ responseSchema: jsonSchema });
656
+ outputParser = new JsonOutputParser();
657
+ }
658
+ if (!includeRaw) return llm.pipe(outputParser).withConfig({ runName: "ChatGoogleGenerativeAIStructuredOutput" });
659
+ const parserAssign = RunnablePassthrough.assign({ parsed: (input, config$1) => outputParser.invoke(input.raw, config$1) });
660
+ const parserNone = RunnablePassthrough.assign({ parsed: () => null });
661
+ const parsedWithFallback = parserAssign.withFallbacks({ fallbacks: [parserNone] });
662
+ return RunnableSequence.from([{ raw: llm }, parsedWithFallback]).withConfig({ runName: "StructuredOutputRunnable" });
663
+ }
664
+ };
665
+
666
+ //#endregion
667
+ export { ChatGoogleGenerativeAI };
668
+ //# sourceMappingURL=chat_models.js.map