@langchain/google-genai 0.2.18 → 1.0.1

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