@langchain/google-genai 0.0.25 → 0.1.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.
- package/README.md +6 -6
- package/dist/chat_models.cjs +327 -20
- package/dist/chat_models.d.ts +333 -19
- package/dist/chat_models.js +327 -20
- package/package.json +7 -4
package/README.md
CHANGED
|
@@ -5,7 +5,7 @@ This package contains the LangChain.js integrations for Gemini through their gen
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
7
7
|
```bash npm2yarn
|
|
8
|
-
npm install @langchain/google-genai
|
|
8
|
+
npm install @langchain/google-genai @langchain/core
|
|
9
9
|
```
|
|
10
10
|
|
|
11
11
|
This package, along with the main LangChain package, depends on [`@langchain/core`](https://npmjs.com/package/@langchain/core/).
|
|
@@ -17,18 +17,18 @@ You can do so by adding appropriate field to your project's `package.json` like
|
|
|
17
17
|
"name": "your-project",
|
|
18
18
|
"version": "0.0.0",
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@langchain/
|
|
21
|
-
"langchain": "0.0.
|
|
20
|
+
"@langchain/core": "^0.3.0",
|
|
21
|
+
"@langchain/google-genai": "^0.0.0"
|
|
22
22
|
},
|
|
23
23
|
"resolutions": {
|
|
24
|
-
"@langchain/core": "0.
|
|
24
|
+
"@langchain/core": "^0.3.0"
|
|
25
25
|
},
|
|
26
26
|
"overrides": {
|
|
27
|
-
"@langchain/core": "0.
|
|
27
|
+
"@langchain/core": "^0.3.0"
|
|
28
28
|
},
|
|
29
29
|
"pnpm": {
|
|
30
30
|
"overrides": {
|
|
31
|
-
"@langchain/core": "0.
|
|
31
|
+
"@langchain/core": "^0.3.0"
|
|
32
32
|
}
|
|
33
33
|
}
|
|
34
34
|
}
|
package/dist/chat_models.cjs
CHANGED
|
@@ -10,33 +10,339 @@ const zod_to_genai_parameters_js_1 = require("./utils/zod_to_genai_parameters.cj
|
|
|
10
10
|
const common_js_1 = require("./utils/common.cjs");
|
|
11
11
|
const output_parsers_js_1 = require("./output_parsers.cjs");
|
|
12
12
|
/**
|
|
13
|
-
*
|
|
14
|
-
*
|
|
13
|
+
* Google Generative AI chat model integration.
|
|
14
|
+
*
|
|
15
|
+
* Setup:
|
|
16
|
+
* Install `@langchain/google-genai` and set an environment variable named `GOOGLE_API_KEY`.
|
|
17
|
+
*
|
|
18
|
+
* ```bash
|
|
19
|
+
* npm install @langchain/google-genai
|
|
20
|
+
* export GOOGLE_API_KEY="your-api-key"
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* ## [Constructor args](https://api.js.langchain.com/classes/langchain_google_genai.ChatGoogleGenerativeAI.html#constructor)
|
|
24
|
+
*
|
|
25
|
+
* ## [Runtime args](https://api.js.langchain.com/interfaces/langchain_google_genai.GoogleGenerativeAIChatCallOptions.html)
|
|
26
|
+
*
|
|
27
|
+
* Runtime args can be passed as the second argument to any of the base runnable methods `.invoke`. `.stream`, `.batch`, etc.
|
|
28
|
+
* They can also be passed via `.bind`, or the second arg in `.bindTools`, like shown in the examples below:
|
|
29
|
+
*
|
|
30
|
+
* ```typescript
|
|
31
|
+
* // When calling `.bind`, call options should be passed via the first argument
|
|
32
|
+
* const llmWithArgsBound = llm.bind({
|
|
33
|
+
* stop: ["\n"],
|
|
34
|
+
* tools: [...],
|
|
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
|
+
*
|
|
15
51
|
* ```typescript
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
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...
|
|
22
60
|
* });
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
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
|
+
* },
|
|
26
88
|
* {
|
|
27
|
-
*
|
|
28
|
-
*
|
|
89
|
+
* "category": "HARM_CATEGORY_HATE_SPEECH",
|
|
90
|
+
* "probability": "NEGLIGIBLE"
|
|
29
91
|
* },
|
|
30
92
|
* {
|
|
31
|
-
*
|
|
32
|
-
*
|
|
93
|
+
* "category": "HARM_CATEGORY_HARASSMENT",
|
|
94
|
+
* "probability": "NEGLIGIBLE"
|
|
33
95
|
* },
|
|
96
|
+
* {
|
|
97
|
+
* "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
|
|
98
|
+
* "probability": "NEGLIGIBLE"
|
|
99
|
+
* }
|
|
34
100
|
* ]
|
|
35
|
-
* }
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
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
|
+
* }
|
|
39
342
|
* ```
|
|
343
|
+
* </details>
|
|
344
|
+
*
|
|
345
|
+
* <br />
|
|
40
346
|
*/
|
|
41
347
|
class ChatGoogleGenerativeAI extends chat_models_1.BaseChatModel {
|
|
42
348
|
static lc_name() {
|
|
@@ -191,6 +497,7 @@ class ChatGoogleGenerativeAI extends chat_models_1.BaseChatModel {
|
|
|
191
497
|
temperature: this.temperature,
|
|
192
498
|
topP: this.topP,
|
|
193
499
|
topK: this.topK,
|
|
500
|
+
...(fields?.json ? { responseMimeType: "application/json" } : {}),
|
|
194
501
|
},
|
|
195
502
|
}, {
|
|
196
503
|
apiVersion: fields?.apiVersion,
|
|
@@ -328,7 +635,7 @@ class ChatGoogleGenerativeAI extends chat_models_1.BaseChatModel {
|
|
|
328
635
|
async completionWithRetry(request, options) {
|
|
329
636
|
return this.caller.callWithOptions({ signal: options?.signal }, async () => {
|
|
330
637
|
try {
|
|
331
|
-
return this.client.generateContent(request);
|
|
638
|
+
return await this.client.generateContent(request);
|
|
332
639
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
333
640
|
}
|
|
334
641
|
catch (e) {
|
package/dist/chat_models.d.ts
CHANGED
|
@@ -25,6 +25,8 @@ export interface GoogleGenerativeAIChatCallOptions extends BaseChatModelCallOpti
|
|
|
25
25
|
*/
|
|
26
26
|
export interface GoogleGenerativeAIChatInput extends BaseChatModelParams, Pick<GoogleGenerativeAIChatCallOptions, "streamUsage"> {
|
|
27
27
|
/**
|
|
28
|
+
* @deprecated Use "model" instead.
|
|
29
|
+
*
|
|
28
30
|
* Model Name to use
|
|
29
31
|
*
|
|
30
32
|
* Alias for `model`
|
|
@@ -107,35 +109,347 @@ export interface GoogleGenerativeAIChatInput extends BaseChatModelParams, Pick<G
|
|
|
107
109
|
baseUrl?: string;
|
|
108
110
|
/** Whether to stream the results or not */
|
|
109
111
|
streaming?: boolean;
|
|
112
|
+
/**
|
|
113
|
+
* Whether or not to force the model to respond with JSON.
|
|
114
|
+
* Available for `gemini-1.5` models and later.
|
|
115
|
+
* @default false
|
|
116
|
+
*/
|
|
117
|
+
json?: boolean;
|
|
110
118
|
}
|
|
111
119
|
/**
|
|
112
|
-
*
|
|
113
|
-
*
|
|
120
|
+
* Google Generative AI chat model integration.
|
|
121
|
+
*
|
|
122
|
+
* Setup:
|
|
123
|
+
* Install `@langchain/google-genai` and set an environment variable named `GOOGLE_API_KEY`.
|
|
124
|
+
*
|
|
125
|
+
* ```bash
|
|
126
|
+
* npm install @langchain/google-genai
|
|
127
|
+
* export GOOGLE_API_KEY="your-api-key"
|
|
128
|
+
* ```
|
|
129
|
+
*
|
|
130
|
+
* ## [Constructor args](https://api.js.langchain.com/classes/langchain_google_genai.ChatGoogleGenerativeAI.html#constructor)
|
|
131
|
+
*
|
|
132
|
+
* ## [Runtime args](https://api.js.langchain.com/interfaces/langchain_google_genai.GoogleGenerativeAIChatCallOptions.html)
|
|
133
|
+
*
|
|
134
|
+
* Runtime args can be passed as the second argument to any of the base runnable methods `.invoke`. `.stream`, `.batch`, etc.
|
|
135
|
+
* They can also be passed via `.bind`, or the second arg in `.bindTools`, like shown in the examples below:
|
|
136
|
+
*
|
|
114
137
|
* ```typescript
|
|
115
|
-
*
|
|
116
|
-
*
|
|
117
|
-
*
|
|
118
|
-
*
|
|
119
|
-
* topK: 40,
|
|
120
|
-
* topP: 1,
|
|
138
|
+
* // When calling `.bind`, call options should be passed via the first argument
|
|
139
|
+
* const llmWithArgsBound = llm.bind({
|
|
140
|
+
* stop: ["\n"],
|
|
141
|
+
* tools: [...],
|
|
121
142
|
* });
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
*
|
|
143
|
+
*
|
|
144
|
+
* // When calling `.bindTools`, call options should be passed via the second argument
|
|
145
|
+
* const llmWithTools = llm.bindTools(
|
|
146
|
+
* [...],
|
|
147
|
+
* {
|
|
148
|
+
* stop: ["\n"],
|
|
149
|
+
* }
|
|
150
|
+
* );
|
|
151
|
+
* ```
|
|
152
|
+
*
|
|
153
|
+
* ## Examples
|
|
154
|
+
*
|
|
155
|
+
* <details open>
|
|
156
|
+
* <summary><strong>Instantiate</strong></summary>
|
|
157
|
+
*
|
|
158
|
+
* ```typescript
|
|
159
|
+
* import { ChatGoogleGenerativeAI } from '@langchain/google-genai';
|
|
160
|
+
*
|
|
161
|
+
* const llm = new ChatGoogleGenerativeAI({
|
|
162
|
+
* model: "gemini-1.5-flash",
|
|
163
|
+
* temperature: 0,
|
|
164
|
+
* maxRetries: 2,
|
|
165
|
+
* // apiKey: "...",
|
|
166
|
+
* // other params...
|
|
167
|
+
* });
|
|
168
|
+
* ```
|
|
169
|
+
* </details>
|
|
170
|
+
*
|
|
171
|
+
* <br />
|
|
172
|
+
*
|
|
173
|
+
* <details>
|
|
174
|
+
* <summary><strong>Invoking</strong></summary>
|
|
175
|
+
*
|
|
176
|
+
* ```typescript
|
|
177
|
+
* const input = `Translate "I love programming" into French.`;
|
|
178
|
+
*
|
|
179
|
+
* // Models also accept a list of chat messages or a formatted prompt
|
|
180
|
+
* const result = await llm.invoke(input);
|
|
181
|
+
* console.log(result);
|
|
182
|
+
* ```
|
|
183
|
+
*
|
|
184
|
+
* ```txt
|
|
185
|
+
* AIMessage {
|
|
186
|
+
* "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",
|
|
187
|
+
* "response_metadata": {
|
|
188
|
+
* "finishReason": "STOP",
|
|
189
|
+
* "index": 0,
|
|
190
|
+
* "safetyRatings": [
|
|
191
|
+
* {
|
|
192
|
+
* "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
|
193
|
+
* "probability": "NEGLIGIBLE"
|
|
194
|
+
* },
|
|
125
195
|
* {
|
|
126
|
-
*
|
|
127
|
-
*
|
|
196
|
+
* "category": "HARM_CATEGORY_HATE_SPEECH",
|
|
197
|
+
* "probability": "NEGLIGIBLE"
|
|
128
198
|
* },
|
|
129
199
|
* {
|
|
130
|
-
*
|
|
131
|
-
*
|
|
200
|
+
* "category": "HARM_CATEGORY_HARASSMENT",
|
|
201
|
+
* "probability": "NEGLIGIBLE"
|
|
132
202
|
* },
|
|
203
|
+
* {
|
|
204
|
+
* "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
|
|
205
|
+
* "probability": "NEGLIGIBLE"
|
|
206
|
+
* }
|
|
133
207
|
* ]
|
|
134
|
-
* }
|
|
135
|
-
*
|
|
136
|
-
*
|
|
137
|
-
*
|
|
208
|
+
* },
|
|
209
|
+
* "usage_metadata": {
|
|
210
|
+
* "input_tokens": 10,
|
|
211
|
+
* "output_tokens": 149,
|
|
212
|
+
* "total_tokens": 159
|
|
213
|
+
* }
|
|
214
|
+
* }
|
|
215
|
+
* ```
|
|
216
|
+
* </details>
|
|
217
|
+
*
|
|
218
|
+
* <br />
|
|
219
|
+
*
|
|
220
|
+
* <details>
|
|
221
|
+
* <summary><strong>Streaming Chunks</strong></summary>
|
|
222
|
+
*
|
|
223
|
+
* ```typescript
|
|
224
|
+
* for await (const chunk of await llm.stream(input)) {
|
|
225
|
+
* console.log(chunk);
|
|
226
|
+
* }
|
|
227
|
+
* ```
|
|
228
|
+
*
|
|
229
|
+
* ```txt
|
|
230
|
+
* AIMessageChunk {
|
|
231
|
+
* "content": "There",
|
|
232
|
+
* "response_metadata": {
|
|
233
|
+
* "index": 0
|
|
234
|
+
* }
|
|
235
|
+
* "usage_metadata": {
|
|
236
|
+
* "input_tokens": 10,
|
|
237
|
+
* "output_tokens": 1,
|
|
238
|
+
* "total_tokens": 11
|
|
239
|
+
* }
|
|
240
|
+
* }
|
|
241
|
+
* AIMessageChunk {
|
|
242
|
+
* "content": " are a few ways to translate \"I love programming\" into French, depending on",
|
|
243
|
+
* }
|
|
244
|
+
* AIMessageChunk {
|
|
245
|
+
* "content": " the level of formality and nuance you want to convey:\n\n**Formal:**\n\n",
|
|
246
|
+
* }
|
|
247
|
+
* AIMessageChunk {
|
|
248
|
+
* "content": "* **J'aime la programmation.** (This is the most literal and formal translation.)\n\n**Informal:**\n\n* **J'adore programmer.** (This",
|
|
249
|
+
* }
|
|
250
|
+
* AIMessageChunk {
|
|
251
|
+
* "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",
|
|
252
|
+
* }
|
|
253
|
+
* AIMessageChunk {
|
|
254
|
+
* "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",
|
|
255
|
+
* }
|
|
256
|
+
* AIMessageChunk {
|
|
257
|
+
* "content": " your intended audience. \n",
|
|
258
|
+
* }
|
|
259
|
+
* ```
|
|
260
|
+
* </details>
|
|
261
|
+
*
|
|
262
|
+
* <br />
|
|
263
|
+
*
|
|
264
|
+
* <details>
|
|
265
|
+
* <summary><strong>Aggregate Streamed Chunks</strong></summary>
|
|
266
|
+
*
|
|
267
|
+
* ```typescript
|
|
268
|
+
* import { AIMessageChunk } from '@langchain/core/messages';
|
|
269
|
+
* import { concat } from '@langchain/core/utils/stream';
|
|
270
|
+
*
|
|
271
|
+
* const stream = await llm.stream(input);
|
|
272
|
+
* let full: AIMessageChunk | undefined;
|
|
273
|
+
* for await (const chunk of stream) {
|
|
274
|
+
* full = !full ? chunk : concat(full, chunk);
|
|
275
|
+
* }
|
|
276
|
+
* console.log(full);
|
|
277
|
+
* ```
|
|
278
|
+
*
|
|
279
|
+
* ```txt
|
|
280
|
+
* AIMessageChunk {
|
|
281
|
+
* "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",
|
|
282
|
+
* "usage_metadata": {
|
|
283
|
+
* "input_tokens": 10,
|
|
284
|
+
* "output_tokens": 277,
|
|
285
|
+
* "total_tokens": 287
|
|
286
|
+
* }
|
|
287
|
+
* }
|
|
288
|
+
* ```
|
|
289
|
+
* </details>
|
|
290
|
+
*
|
|
291
|
+
* <br />
|
|
292
|
+
*
|
|
293
|
+
* <details>
|
|
294
|
+
* <summary><strong>Bind tools</strong></summary>
|
|
295
|
+
*
|
|
296
|
+
* ```typescript
|
|
297
|
+
* import { z } from 'zod';
|
|
298
|
+
*
|
|
299
|
+
* const GetWeather = {
|
|
300
|
+
* name: "GetWeather",
|
|
301
|
+
* description: "Get the current weather in a given location",
|
|
302
|
+
* schema: z.object({
|
|
303
|
+
* location: z.string().describe("The city and state, e.g. San Francisco, CA")
|
|
304
|
+
* }),
|
|
305
|
+
* }
|
|
306
|
+
*
|
|
307
|
+
* const GetPopulation = {
|
|
308
|
+
* name: "GetPopulation",
|
|
309
|
+
* description: "Get the current population in a given location",
|
|
310
|
+
* schema: z.object({
|
|
311
|
+
* location: z.string().describe("The city and state, e.g. San Francisco, CA")
|
|
312
|
+
* }),
|
|
313
|
+
* }
|
|
314
|
+
*
|
|
315
|
+
* const llmWithTools = llm.bindTools([GetWeather, GetPopulation]);
|
|
316
|
+
* const aiMsg = await llmWithTools.invoke(
|
|
317
|
+
* "Which city is hotter today and which is bigger: LA or NY?"
|
|
318
|
+
* );
|
|
319
|
+
* console.log(aiMsg.tool_calls);
|
|
320
|
+
* ```
|
|
321
|
+
*
|
|
322
|
+
* ```txt
|
|
323
|
+
* [
|
|
324
|
+
* {
|
|
325
|
+
* name: 'GetWeather',
|
|
326
|
+
* args: { location: 'Los Angeles, CA' },
|
|
327
|
+
* type: 'tool_call'
|
|
328
|
+
* },
|
|
329
|
+
* {
|
|
330
|
+
* name: 'GetWeather',
|
|
331
|
+
* args: { location: 'New York, NY' },
|
|
332
|
+
* type: 'tool_call'
|
|
333
|
+
* },
|
|
334
|
+
* {
|
|
335
|
+
* name: 'GetPopulation',
|
|
336
|
+
* args: { location: 'Los Angeles, CA' },
|
|
337
|
+
* type: 'tool_call'
|
|
338
|
+
* },
|
|
339
|
+
* {
|
|
340
|
+
* name: 'GetPopulation',
|
|
341
|
+
* args: { location: 'New York, NY' },
|
|
342
|
+
* type: 'tool_call'
|
|
343
|
+
* }
|
|
344
|
+
* ]
|
|
345
|
+
* ```
|
|
346
|
+
* </details>
|
|
347
|
+
*
|
|
348
|
+
* <br />
|
|
349
|
+
*
|
|
350
|
+
* <details>
|
|
351
|
+
* <summary><strong>Structured Output</strong></summary>
|
|
352
|
+
*
|
|
353
|
+
* ```typescript
|
|
354
|
+
* const Joke = z.object({
|
|
355
|
+
* setup: z.string().describe("The setup of the joke"),
|
|
356
|
+
* punchline: z.string().describe("The punchline to the joke"),
|
|
357
|
+
* rating: z.number().optional().describe("How funny the joke is, from 1 to 10")
|
|
358
|
+
* }).describe('Joke to tell user.');
|
|
359
|
+
*
|
|
360
|
+
* const structuredLlm = llm.withStructuredOutput(Joke, { name: "Joke" });
|
|
361
|
+
* const jokeResult = await structuredLlm.invoke("Tell me a joke about cats");
|
|
362
|
+
* console.log(jokeResult);
|
|
363
|
+
* ```
|
|
364
|
+
*
|
|
365
|
+
* ```txt
|
|
366
|
+
* {
|
|
367
|
+
* setup: "Why don\\'t cats play poker?",
|
|
368
|
+
* punchline: "Why don\\'t cats play poker? Because they always have an ace up their sleeve!"
|
|
369
|
+
* }
|
|
370
|
+
* ```
|
|
371
|
+
* </details>
|
|
372
|
+
*
|
|
373
|
+
* <br />
|
|
374
|
+
*
|
|
375
|
+
* <details>
|
|
376
|
+
* <summary><strong>Multimodal</strong></summary>
|
|
377
|
+
*
|
|
378
|
+
* ```typescript
|
|
379
|
+
* import { HumanMessage } from '@langchain/core/messages';
|
|
380
|
+
*
|
|
381
|
+
* const imageUrl = "https://example.com/image.jpg";
|
|
382
|
+
* const imageData = await fetch(imageUrl).then(res => res.arrayBuffer());
|
|
383
|
+
* const base64Image = Buffer.from(imageData).toString('base64');
|
|
384
|
+
*
|
|
385
|
+
* const message = new HumanMessage({
|
|
386
|
+
* content: [
|
|
387
|
+
* { type: "text", text: "describe the weather in this image" },
|
|
388
|
+
* {
|
|
389
|
+
* type: "image_url",
|
|
390
|
+
* image_url: { url: `data:image/jpeg;base64,${base64Image}` },
|
|
391
|
+
* },
|
|
392
|
+
* ]
|
|
393
|
+
* });
|
|
394
|
+
*
|
|
395
|
+
* const imageDescriptionAiMsg = await llm.invoke([message]);
|
|
396
|
+
* console.log(imageDescriptionAiMsg.content);
|
|
397
|
+
* ```
|
|
398
|
+
*
|
|
399
|
+
* ```txt
|
|
400
|
+
* 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.
|
|
401
|
+
* ```
|
|
402
|
+
* </details>
|
|
403
|
+
*
|
|
404
|
+
* <br />
|
|
405
|
+
*
|
|
406
|
+
* <details>
|
|
407
|
+
* <summary><strong>Usage Metadata</strong></summary>
|
|
408
|
+
*
|
|
409
|
+
* ```typescript
|
|
410
|
+
* const aiMsgForMetadata = await llm.invoke(input);
|
|
411
|
+
* console.log(aiMsgForMetadata.usage_metadata);
|
|
412
|
+
* ```
|
|
413
|
+
*
|
|
414
|
+
* ```txt
|
|
415
|
+
* { input_tokens: 10, output_tokens: 149, total_tokens: 159 }
|
|
416
|
+
* ```
|
|
417
|
+
* </details>
|
|
418
|
+
*
|
|
419
|
+
* <br />
|
|
420
|
+
*
|
|
421
|
+
* <details>
|
|
422
|
+
* <summary><strong>Response Metadata</strong></summary>
|
|
423
|
+
*
|
|
424
|
+
* ```typescript
|
|
425
|
+
* const aiMsgForResponseMetadata = await llm.invoke(input);
|
|
426
|
+
* console.log(aiMsgForResponseMetadata.response_metadata);
|
|
427
|
+
* ```
|
|
428
|
+
*
|
|
429
|
+
* ```txt
|
|
430
|
+
* {
|
|
431
|
+
* finishReason: 'STOP',
|
|
432
|
+
* index: 0,
|
|
433
|
+
* safetyRatings: [
|
|
434
|
+
* {
|
|
435
|
+
* category: 'HARM_CATEGORY_SEXUALLY_EXPLICIT',
|
|
436
|
+
* probability: 'NEGLIGIBLE'
|
|
437
|
+
* },
|
|
438
|
+
* {
|
|
439
|
+
* category: 'HARM_CATEGORY_HATE_SPEECH',
|
|
440
|
+
* probability: 'NEGLIGIBLE'
|
|
441
|
+
* },
|
|
442
|
+
* { category: 'HARM_CATEGORY_HARASSMENT', probability: 'NEGLIGIBLE' },
|
|
443
|
+
* {
|
|
444
|
+
* category: 'HARM_CATEGORY_DANGEROUS_CONTENT',
|
|
445
|
+
* probability: 'NEGLIGIBLE'
|
|
446
|
+
* }
|
|
447
|
+
* ]
|
|
448
|
+
* }
|
|
138
449
|
* ```
|
|
450
|
+
* </details>
|
|
451
|
+
*
|
|
452
|
+
* <br />
|
|
139
453
|
*/
|
|
140
454
|
export declare class ChatGoogleGenerativeAI extends BaseChatModel<GoogleGenerativeAIChatCallOptions, AIMessageChunk> implements GoogleGenerativeAIChatInput {
|
|
141
455
|
static lc_name(): string;
|
package/dist/chat_models.js
CHANGED
|
@@ -7,33 +7,339 @@ import { zodToGenerativeAIParameters } from "./utils/zod_to_genai_parameters.js"
|
|
|
7
7
|
import { convertBaseMessagesToContent, convertResponseContentToChatGenerationChunk, convertToGenerativeAITools, mapGenerateContentResultToChatResult, } from "./utils/common.js";
|
|
8
8
|
import { GoogleGenerativeAIToolsOutputParser } from "./output_parsers.js";
|
|
9
9
|
/**
|
|
10
|
-
*
|
|
11
|
-
*
|
|
10
|
+
* Google Generative AI chat model integration.
|
|
11
|
+
*
|
|
12
|
+
* Setup:
|
|
13
|
+
* Install `@langchain/google-genai` and set an environment variable named `GOOGLE_API_KEY`.
|
|
14
|
+
*
|
|
15
|
+
* ```bash
|
|
16
|
+
* npm install @langchain/google-genai
|
|
17
|
+
* export GOOGLE_API_KEY="your-api-key"
|
|
18
|
+
* ```
|
|
19
|
+
*
|
|
20
|
+
* ## [Constructor args](https://api.js.langchain.com/classes/langchain_google_genai.ChatGoogleGenerativeAI.html#constructor)
|
|
21
|
+
*
|
|
22
|
+
* ## [Runtime args](https://api.js.langchain.com/interfaces/langchain_google_genai.GoogleGenerativeAIChatCallOptions.html)
|
|
23
|
+
*
|
|
24
|
+
* Runtime args can be passed as the second argument to any of the base runnable methods `.invoke`. `.stream`, `.batch`, etc.
|
|
25
|
+
* They can also be passed via `.bind`, or the second arg in `.bindTools`, like shown in the examples below:
|
|
26
|
+
*
|
|
27
|
+
* ```typescript
|
|
28
|
+
* // When calling `.bind`, call options should be passed via the first argument
|
|
29
|
+
* const llmWithArgsBound = llm.bind({
|
|
30
|
+
* stop: ["\n"],
|
|
31
|
+
* tools: [...],
|
|
32
|
+
* });
|
|
33
|
+
*
|
|
34
|
+
* // When calling `.bindTools`, call options should be passed via the second argument
|
|
35
|
+
* const llmWithTools = llm.bindTools(
|
|
36
|
+
* [...],
|
|
37
|
+
* {
|
|
38
|
+
* stop: ["\n"],
|
|
39
|
+
* }
|
|
40
|
+
* );
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* ## Examples
|
|
44
|
+
*
|
|
45
|
+
* <details open>
|
|
46
|
+
* <summary><strong>Instantiate</strong></summary>
|
|
47
|
+
*
|
|
12
48
|
* ```typescript
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
49
|
+
* import { ChatGoogleGenerativeAI } from '@langchain/google-genai';
|
|
50
|
+
*
|
|
51
|
+
* const llm = new ChatGoogleGenerativeAI({
|
|
52
|
+
* model: "gemini-1.5-flash",
|
|
53
|
+
* temperature: 0,
|
|
54
|
+
* maxRetries: 2,
|
|
55
|
+
* // apiKey: "...",
|
|
56
|
+
* // other params...
|
|
19
57
|
* });
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
58
|
+
* ```
|
|
59
|
+
* </details>
|
|
60
|
+
*
|
|
61
|
+
* <br />
|
|
62
|
+
*
|
|
63
|
+
* <details>
|
|
64
|
+
* <summary><strong>Invoking</strong></summary>
|
|
65
|
+
*
|
|
66
|
+
* ```typescript
|
|
67
|
+
* const input = `Translate "I love programming" into French.`;
|
|
68
|
+
*
|
|
69
|
+
* // Models also accept a list of chat messages or a formatted prompt
|
|
70
|
+
* const result = await llm.invoke(input);
|
|
71
|
+
* console.log(result);
|
|
72
|
+
* ```
|
|
73
|
+
*
|
|
74
|
+
* ```txt
|
|
75
|
+
* AIMessage {
|
|
76
|
+
* "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",
|
|
77
|
+
* "response_metadata": {
|
|
78
|
+
* "finishReason": "STOP",
|
|
79
|
+
* "index": 0,
|
|
80
|
+
* "safetyRatings": [
|
|
81
|
+
* {
|
|
82
|
+
* "category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
|
83
|
+
* "probability": "NEGLIGIBLE"
|
|
84
|
+
* },
|
|
23
85
|
* {
|
|
24
|
-
*
|
|
25
|
-
*
|
|
86
|
+
* "category": "HARM_CATEGORY_HATE_SPEECH",
|
|
87
|
+
* "probability": "NEGLIGIBLE"
|
|
26
88
|
* },
|
|
27
89
|
* {
|
|
28
|
-
*
|
|
29
|
-
*
|
|
90
|
+
* "category": "HARM_CATEGORY_HARASSMENT",
|
|
91
|
+
* "probability": "NEGLIGIBLE"
|
|
30
92
|
* },
|
|
93
|
+
* {
|
|
94
|
+
* "category": "HARM_CATEGORY_DANGEROUS_CONTENT",
|
|
95
|
+
* "probability": "NEGLIGIBLE"
|
|
96
|
+
* }
|
|
31
97
|
* ]
|
|
32
|
-
* }
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
98
|
+
* },
|
|
99
|
+
* "usage_metadata": {
|
|
100
|
+
* "input_tokens": 10,
|
|
101
|
+
* "output_tokens": 149,
|
|
102
|
+
* "total_tokens": 159
|
|
103
|
+
* }
|
|
104
|
+
* }
|
|
105
|
+
* ```
|
|
106
|
+
* </details>
|
|
107
|
+
*
|
|
108
|
+
* <br />
|
|
109
|
+
*
|
|
110
|
+
* <details>
|
|
111
|
+
* <summary><strong>Streaming Chunks</strong></summary>
|
|
112
|
+
*
|
|
113
|
+
* ```typescript
|
|
114
|
+
* for await (const chunk of await llm.stream(input)) {
|
|
115
|
+
* console.log(chunk);
|
|
116
|
+
* }
|
|
117
|
+
* ```
|
|
118
|
+
*
|
|
119
|
+
* ```txt
|
|
120
|
+
* AIMessageChunk {
|
|
121
|
+
* "content": "There",
|
|
122
|
+
* "response_metadata": {
|
|
123
|
+
* "index": 0
|
|
124
|
+
* }
|
|
125
|
+
* "usage_metadata": {
|
|
126
|
+
* "input_tokens": 10,
|
|
127
|
+
* "output_tokens": 1,
|
|
128
|
+
* "total_tokens": 11
|
|
129
|
+
* }
|
|
130
|
+
* }
|
|
131
|
+
* AIMessageChunk {
|
|
132
|
+
* "content": " are a few ways to translate \"I love programming\" into French, depending on",
|
|
133
|
+
* }
|
|
134
|
+
* AIMessageChunk {
|
|
135
|
+
* "content": " the level of formality and nuance you want to convey:\n\n**Formal:**\n\n",
|
|
136
|
+
* }
|
|
137
|
+
* AIMessageChunk {
|
|
138
|
+
* "content": "* **J'aime la programmation.** (This is the most literal and formal translation.)\n\n**Informal:**\n\n* **J'adore programmer.** (This",
|
|
139
|
+
* }
|
|
140
|
+
* AIMessageChunk {
|
|
141
|
+
* "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",
|
|
142
|
+
* }
|
|
143
|
+
* AIMessageChunk {
|
|
144
|
+
* "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",
|
|
145
|
+
* }
|
|
146
|
+
* AIMessageChunk {
|
|
147
|
+
* "content": " your intended audience. \n",
|
|
148
|
+
* }
|
|
149
|
+
* ```
|
|
150
|
+
* </details>
|
|
151
|
+
*
|
|
152
|
+
* <br />
|
|
153
|
+
*
|
|
154
|
+
* <details>
|
|
155
|
+
* <summary><strong>Aggregate Streamed Chunks</strong></summary>
|
|
156
|
+
*
|
|
157
|
+
* ```typescript
|
|
158
|
+
* import { AIMessageChunk } from '@langchain/core/messages';
|
|
159
|
+
* import { concat } from '@langchain/core/utils/stream';
|
|
160
|
+
*
|
|
161
|
+
* const stream = await llm.stream(input);
|
|
162
|
+
* let full: AIMessageChunk | undefined;
|
|
163
|
+
* for await (const chunk of stream) {
|
|
164
|
+
* full = !full ? chunk : concat(full, chunk);
|
|
165
|
+
* }
|
|
166
|
+
* console.log(full);
|
|
167
|
+
* ```
|
|
168
|
+
*
|
|
169
|
+
* ```txt
|
|
170
|
+
* AIMessageChunk {
|
|
171
|
+
* "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",
|
|
172
|
+
* "usage_metadata": {
|
|
173
|
+
* "input_tokens": 10,
|
|
174
|
+
* "output_tokens": 277,
|
|
175
|
+
* "total_tokens": 287
|
|
176
|
+
* }
|
|
177
|
+
* }
|
|
178
|
+
* ```
|
|
179
|
+
* </details>
|
|
180
|
+
*
|
|
181
|
+
* <br />
|
|
182
|
+
*
|
|
183
|
+
* <details>
|
|
184
|
+
* <summary><strong>Bind tools</strong></summary>
|
|
185
|
+
*
|
|
186
|
+
* ```typescript
|
|
187
|
+
* import { z } from 'zod';
|
|
188
|
+
*
|
|
189
|
+
* const GetWeather = {
|
|
190
|
+
* name: "GetWeather",
|
|
191
|
+
* description: "Get the current weather in a given location",
|
|
192
|
+
* schema: z.object({
|
|
193
|
+
* location: z.string().describe("The city and state, e.g. San Francisco, CA")
|
|
194
|
+
* }),
|
|
195
|
+
* }
|
|
196
|
+
*
|
|
197
|
+
* const GetPopulation = {
|
|
198
|
+
* name: "GetPopulation",
|
|
199
|
+
* description: "Get the current population in a given location",
|
|
200
|
+
* schema: z.object({
|
|
201
|
+
* location: z.string().describe("The city and state, e.g. San Francisco, CA")
|
|
202
|
+
* }),
|
|
203
|
+
* }
|
|
204
|
+
*
|
|
205
|
+
* const llmWithTools = llm.bindTools([GetWeather, GetPopulation]);
|
|
206
|
+
* const aiMsg = await llmWithTools.invoke(
|
|
207
|
+
* "Which city is hotter today and which is bigger: LA or NY?"
|
|
208
|
+
* );
|
|
209
|
+
* console.log(aiMsg.tool_calls);
|
|
210
|
+
* ```
|
|
211
|
+
*
|
|
212
|
+
* ```txt
|
|
213
|
+
* [
|
|
214
|
+
* {
|
|
215
|
+
* name: 'GetWeather',
|
|
216
|
+
* args: { location: 'Los Angeles, CA' },
|
|
217
|
+
* type: 'tool_call'
|
|
218
|
+
* },
|
|
219
|
+
* {
|
|
220
|
+
* name: 'GetWeather',
|
|
221
|
+
* args: { location: 'New York, NY' },
|
|
222
|
+
* type: 'tool_call'
|
|
223
|
+
* },
|
|
224
|
+
* {
|
|
225
|
+
* name: 'GetPopulation',
|
|
226
|
+
* args: { location: 'Los Angeles, CA' },
|
|
227
|
+
* type: 'tool_call'
|
|
228
|
+
* },
|
|
229
|
+
* {
|
|
230
|
+
* name: 'GetPopulation',
|
|
231
|
+
* args: { location: 'New York, NY' },
|
|
232
|
+
* type: 'tool_call'
|
|
233
|
+
* }
|
|
234
|
+
* ]
|
|
235
|
+
* ```
|
|
236
|
+
* </details>
|
|
237
|
+
*
|
|
238
|
+
* <br />
|
|
239
|
+
*
|
|
240
|
+
* <details>
|
|
241
|
+
* <summary><strong>Structured Output</strong></summary>
|
|
242
|
+
*
|
|
243
|
+
* ```typescript
|
|
244
|
+
* const Joke = z.object({
|
|
245
|
+
* setup: z.string().describe("The setup of the joke"),
|
|
246
|
+
* punchline: z.string().describe("The punchline to the joke"),
|
|
247
|
+
* rating: z.number().optional().describe("How funny the joke is, from 1 to 10")
|
|
248
|
+
* }).describe('Joke to tell user.');
|
|
249
|
+
*
|
|
250
|
+
* const structuredLlm = llm.withStructuredOutput(Joke, { name: "Joke" });
|
|
251
|
+
* const jokeResult = await structuredLlm.invoke("Tell me a joke about cats");
|
|
252
|
+
* console.log(jokeResult);
|
|
253
|
+
* ```
|
|
254
|
+
*
|
|
255
|
+
* ```txt
|
|
256
|
+
* {
|
|
257
|
+
* setup: "Why don\\'t cats play poker?",
|
|
258
|
+
* punchline: "Why don\\'t cats play poker? Because they always have an ace up their sleeve!"
|
|
259
|
+
* }
|
|
260
|
+
* ```
|
|
261
|
+
* </details>
|
|
262
|
+
*
|
|
263
|
+
* <br />
|
|
264
|
+
*
|
|
265
|
+
* <details>
|
|
266
|
+
* <summary><strong>Multimodal</strong></summary>
|
|
267
|
+
*
|
|
268
|
+
* ```typescript
|
|
269
|
+
* import { HumanMessage } from '@langchain/core/messages';
|
|
270
|
+
*
|
|
271
|
+
* const imageUrl = "https://example.com/image.jpg";
|
|
272
|
+
* const imageData = await fetch(imageUrl).then(res => res.arrayBuffer());
|
|
273
|
+
* const base64Image = Buffer.from(imageData).toString('base64');
|
|
274
|
+
*
|
|
275
|
+
* const message = new HumanMessage({
|
|
276
|
+
* content: [
|
|
277
|
+
* { type: "text", text: "describe the weather in this image" },
|
|
278
|
+
* {
|
|
279
|
+
* type: "image_url",
|
|
280
|
+
* image_url: { url: `data:image/jpeg;base64,${base64Image}` },
|
|
281
|
+
* },
|
|
282
|
+
* ]
|
|
283
|
+
* });
|
|
284
|
+
*
|
|
285
|
+
* const imageDescriptionAiMsg = await llm.invoke([message]);
|
|
286
|
+
* console.log(imageDescriptionAiMsg.content);
|
|
287
|
+
* ```
|
|
288
|
+
*
|
|
289
|
+
* ```txt
|
|
290
|
+
* 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.
|
|
291
|
+
* ```
|
|
292
|
+
* </details>
|
|
293
|
+
*
|
|
294
|
+
* <br />
|
|
295
|
+
*
|
|
296
|
+
* <details>
|
|
297
|
+
* <summary><strong>Usage Metadata</strong></summary>
|
|
298
|
+
*
|
|
299
|
+
* ```typescript
|
|
300
|
+
* const aiMsgForMetadata = await llm.invoke(input);
|
|
301
|
+
* console.log(aiMsgForMetadata.usage_metadata);
|
|
302
|
+
* ```
|
|
303
|
+
*
|
|
304
|
+
* ```txt
|
|
305
|
+
* { input_tokens: 10, output_tokens: 149, total_tokens: 159 }
|
|
306
|
+
* ```
|
|
307
|
+
* </details>
|
|
308
|
+
*
|
|
309
|
+
* <br />
|
|
310
|
+
*
|
|
311
|
+
* <details>
|
|
312
|
+
* <summary><strong>Response Metadata</strong></summary>
|
|
313
|
+
*
|
|
314
|
+
* ```typescript
|
|
315
|
+
* const aiMsgForResponseMetadata = await llm.invoke(input);
|
|
316
|
+
* console.log(aiMsgForResponseMetadata.response_metadata);
|
|
317
|
+
* ```
|
|
318
|
+
*
|
|
319
|
+
* ```txt
|
|
320
|
+
* {
|
|
321
|
+
* finishReason: 'STOP',
|
|
322
|
+
* index: 0,
|
|
323
|
+
* safetyRatings: [
|
|
324
|
+
* {
|
|
325
|
+
* category: 'HARM_CATEGORY_SEXUALLY_EXPLICIT',
|
|
326
|
+
* probability: 'NEGLIGIBLE'
|
|
327
|
+
* },
|
|
328
|
+
* {
|
|
329
|
+
* category: 'HARM_CATEGORY_HATE_SPEECH',
|
|
330
|
+
* probability: 'NEGLIGIBLE'
|
|
331
|
+
* },
|
|
332
|
+
* { category: 'HARM_CATEGORY_HARASSMENT', probability: 'NEGLIGIBLE' },
|
|
333
|
+
* {
|
|
334
|
+
* category: 'HARM_CATEGORY_DANGEROUS_CONTENT',
|
|
335
|
+
* probability: 'NEGLIGIBLE'
|
|
336
|
+
* }
|
|
337
|
+
* ]
|
|
338
|
+
* }
|
|
36
339
|
* ```
|
|
340
|
+
* </details>
|
|
341
|
+
*
|
|
342
|
+
* <br />
|
|
37
343
|
*/
|
|
38
344
|
export class ChatGoogleGenerativeAI extends BaseChatModel {
|
|
39
345
|
static lc_name() {
|
|
@@ -188,6 +494,7 @@ export class ChatGoogleGenerativeAI extends BaseChatModel {
|
|
|
188
494
|
temperature: this.temperature,
|
|
189
495
|
topP: this.topP,
|
|
190
496
|
topK: this.topK,
|
|
497
|
+
...(fields?.json ? { responseMimeType: "application/json" } : {}),
|
|
191
498
|
},
|
|
192
499
|
}, {
|
|
193
500
|
apiVersion: fields?.apiVersion,
|
|
@@ -325,7 +632,7 @@ export class ChatGoogleGenerativeAI extends BaseChatModel {
|
|
|
325
632
|
async completionWithRetry(request, options) {
|
|
326
633
|
return this.caller.callWithOptions({ signal: options?.signal }, async () => {
|
|
327
634
|
try {
|
|
328
|
-
return this.client.generateContent(request);
|
|
635
|
+
return await this.client.generateContent(request);
|
|
329
636
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
330
637
|
}
|
|
331
638
|
catch (e) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@langchain/google-genai",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"description": "Google Generative AI integration for LangChain.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"homepage": "https://github.com/langchain-ai/langchainjs/tree/main/libs/langchain-google-genai/",
|
|
16
16
|
"scripts": {
|
|
17
17
|
"build": "yarn turbo:command build:internal --filter=@langchain/google-genai",
|
|
18
|
-
"build:internal": "yarn
|
|
18
|
+
"build:internal": "yarn lc_build --create-entrypoints --pre --tree-shaking",
|
|
19
19
|
"lint:eslint": "NODE_OPTIONS=--max-old-space-size=4096 eslint --cache --ext .ts,.js src/",
|
|
20
20
|
"lint:dpdm": "dpdm --exit-code circular:1 --no-warning --no-tree src/*.ts src/**/*.ts",
|
|
21
21
|
"lint": "yarn lint:eslint && yarn lint:dpdm",
|
|
@@ -36,12 +36,15 @@
|
|
|
36
36
|
"license": "MIT",
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"@google/generative-ai": "^0.7.0",
|
|
39
|
-
"@langchain/core": ">=0.2.21 <0.3.0",
|
|
40
39
|
"zod-to-json-schema": "^3.22.4"
|
|
41
40
|
},
|
|
41
|
+
"peerDependencies": {
|
|
42
|
+
"@langchain/core": ">=0.2.21 <0.4.0"
|
|
43
|
+
},
|
|
42
44
|
"devDependencies": {
|
|
43
45
|
"@jest/globals": "^29.5.0",
|
|
44
|
-
"@langchain/
|
|
46
|
+
"@langchain/core": "workspace:*",
|
|
47
|
+
"@langchain/scripts": ">=0.1.0 <0.2.0",
|
|
45
48
|
"@langchain/standard-tests": "0.0.0",
|
|
46
49
|
"@swc/core": "^1.3.90",
|
|
47
50
|
"@swc/jest": "^0.2.29",
|