@langchain/openai 0.2.6 → 0.2.8
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/dist/azure/chat_models.cjs +412 -0
- package/dist/azure/chat_models.d.ts +412 -0
- package/dist/azure/chat_models.js +412 -0
- package/dist/chat_models.cjs +541 -37
- package/dist/chat_models.d.ts +462 -29
- package/dist/chat_models.js +541 -37
- package/dist/types.d.ts +12 -0
- package/dist/utils/tools.cjs +54 -0
- package/dist/utils/tools.d.ts +20 -0
- package/dist/utils/tools.js +50 -0
- package/package.json +4 -4
|
@@ -4,6 +4,418 @@ exports.AzureChatOpenAI = void 0;
|
|
|
4
4
|
const openai_1 = require("openai");
|
|
5
5
|
const chat_models_js_1 = require("../chat_models.cjs");
|
|
6
6
|
const azure_js_1 = require("../utils/azure.cjs");
|
|
7
|
+
/**
|
|
8
|
+
* Azure OpenAI chat model integration.
|
|
9
|
+
*
|
|
10
|
+
* Setup:
|
|
11
|
+
* Install `@langchain/openai` and set the following environment variables:
|
|
12
|
+
*
|
|
13
|
+
* ```bash
|
|
14
|
+
* npm install @langchain/openai
|
|
15
|
+
* export AZURE_OPENAI_API_KEY="your-api-key"
|
|
16
|
+
* export AZURE_OPENAI_API_DEPLOYMENT_NAME="your-deployment-name"
|
|
17
|
+
* export AZURE_OPENAI_API_VERSION="your-version"
|
|
18
|
+
* export AZURE_OPENAI_BASE_PATH="your-base-path"
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* ## [Constructor args](https://api.js.langchain.com/classes/langchain_openai.AzureChatOpenAI.html#constructor)
|
|
22
|
+
*
|
|
23
|
+
* ## [Runtime args](https://api.js.langchain.com/interfaces/langchain_openai.ChatOpenAICallOptions.html)
|
|
24
|
+
*
|
|
25
|
+
* Runtime args can be passed as the second argument to any of the base runnable methods `.invoke`. `.stream`, `.batch`, etc.
|
|
26
|
+
* They can also be passed via `.bind`, or the second arg in `.bindTools`, like shown in the examples below:
|
|
27
|
+
*
|
|
28
|
+
* ```typescript
|
|
29
|
+
* // When calling `.bind`, call options should be passed via the first argument
|
|
30
|
+
* const llmWithArgsBound = llm.bind({
|
|
31
|
+
* stop: ["\n"],
|
|
32
|
+
* tools: [...],
|
|
33
|
+
* });
|
|
34
|
+
*
|
|
35
|
+
* // When calling `.bindTools`, call options should be passed via the second argument
|
|
36
|
+
* const llmWithTools = llm.bindTools(
|
|
37
|
+
* [...],
|
|
38
|
+
* {
|
|
39
|
+
* tool_choice: "auto",
|
|
40
|
+
* }
|
|
41
|
+
* );
|
|
42
|
+
* ```
|
|
43
|
+
*
|
|
44
|
+
* ## Examples
|
|
45
|
+
*
|
|
46
|
+
* <details open>
|
|
47
|
+
* <summary><strong>Instantiate</strong></summary>
|
|
48
|
+
*
|
|
49
|
+
* ```typescript
|
|
50
|
+
* import { AzureChatOpenAI } from '@langchain/openai';
|
|
51
|
+
*
|
|
52
|
+
* const llm = new AzureChatOpenAI({
|
|
53
|
+
* azureOpenAIApiKey: process.env.AZURE_OPENAI_API_KEY, // In Node.js defaults to process.env.AZURE_OPENAI_API_KEY
|
|
54
|
+
* azureOpenAIApiInstanceName: process.env.AZURE_OPENAI_API_INSTANCE_NAME, // In Node.js defaults to process.env.AZURE_OPENAI_API_INSTANCE_NAME
|
|
55
|
+
* azureOpenAIApiDeploymentName: process.env.AZURE_OPENAI_API_DEPLOYMENT_NAME, // In Node.js defaults to process.env.AZURE_OPENAI_API_DEPLOYMENT_NAME
|
|
56
|
+
* azureOpenAIApiVersion: process.env.AZURE_OPENAI_API_VERSION, // In Node.js defaults to process.env.AZURE_OPENAI_API_VERSION
|
|
57
|
+
* temperature: 0,
|
|
58
|
+
* maxTokens: undefined,
|
|
59
|
+
* timeout: undefined,
|
|
60
|
+
* maxRetries: 2,
|
|
61
|
+
* // apiKey: "...",
|
|
62
|
+
* // baseUrl: "...",
|
|
63
|
+
* // other params...
|
|
64
|
+
* });
|
|
65
|
+
* ```
|
|
66
|
+
* </details>
|
|
67
|
+
*
|
|
68
|
+
* <br />
|
|
69
|
+
*
|
|
70
|
+
* <details>
|
|
71
|
+
* <summary><strong>Invoking</strong></summary>
|
|
72
|
+
*
|
|
73
|
+
* ```typescript
|
|
74
|
+
* const input = `Translate "I love programming" into French.`;
|
|
75
|
+
*
|
|
76
|
+
* // Models also accept a list of chat messages or a formatted prompt
|
|
77
|
+
* const result = await llm.invoke(input);
|
|
78
|
+
* console.log(result);
|
|
79
|
+
* ```
|
|
80
|
+
*
|
|
81
|
+
* ```txt
|
|
82
|
+
* AIMessage {
|
|
83
|
+
* "id": "chatcmpl-9u4Mpu44CbPjwYFkTbeoZgvzB00Tz",
|
|
84
|
+
* "content": "J'adore la programmation.",
|
|
85
|
+
* "response_metadata": {
|
|
86
|
+
* "tokenUsage": {
|
|
87
|
+
* "completionTokens": 5,
|
|
88
|
+
* "promptTokens": 28,
|
|
89
|
+
* "totalTokens": 33
|
|
90
|
+
* },
|
|
91
|
+
* "finish_reason": "stop",
|
|
92
|
+
* "system_fingerprint": "fp_3aa7262c27"
|
|
93
|
+
* },
|
|
94
|
+
* "usage_metadata": {
|
|
95
|
+
* "input_tokens": 28,
|
|
96
|
+
* "output_tokens": 5,
|
|
97
|
+
* "total_tokens": 33
|
|
98
|
+
* }
|
|
99
|
+
* }
|
|
100
|
+
* ```
|
|
101
|
+
* </details>
|
|
102
|
+
*
|
|
103
|
+
* <br />
|
|
104
|
+
*
|
|
105
|
+
* <details>
|
|
106
|
+
* <summary><strong>Streaming Chunks</strong></summary>
|
|
107
|
+
*
|
|
108
|
+
* ```typescript
|
|
109
|
+
* for await (const chunk of await llm.stream(input)) {
|
|
110
|
+
* console.log(chunk);
|
|
111
|
+
* }
|
|
112
|
+
* ```
|
|
113
|
+
*
|
|
114
|
+
* ```txt
|
|
115
|
+
* AIMessageChunk {
|
|
116
|
+
* "id": "chatcmpl-9u4NWB7yUeHCKdLr6jP3HpaOYHTqs",
|
|
117
|
+
* "content": ""
|
|
118
|
+
* }
|
|
119
|
+
* AIMessageChunk {
|
|
120
|
+
* "content": "J"
|
|
121
|
+
* }
|
|
122
|
+
* AIMessageChunk {
|
|
123
|
+
* "content": "'adore"
|
|
124
|
+
* }
|
|
125
|
+
* AIMessageChunk {
|
|
126
|
+
* "content": " la"
|
|
127
|
+
* }
|
|
128
|
+
* AIMessageChunk {
|
|
129
|
+
* "content": " programmation",,
|
|
130
|
+
* }
|
|
131
|
+
* AIMessageChunk {
|
|
132
|
+
* "content": ".",,
|
|
133
|
+
* }
|
|
134
|
+
* AIMessageChunk {
|
|
135
|
+
* "content": "",
|
|
136
|
+
* "response_metadata": {
|
|
137
|
+
* "finish_reason": "stop",
|
|
138
|
+
* "system_fingerprint": "fp_c9aa9c0491"
|
|
139
|
+
* },
|
|
140
|
+
* }
|
|
141
|
+
* AIMessageChunk {
|
|
142
|
+
* "content": "",
|
|
143
|
+
* "usage_metadata": {
|
|
144
|
+
* "input_tokens": 28,
|
|
145
|
+
* "output_tokens": 5,
|
|
146
|
+
* "total_tokens": 33
|
|
147
|
+
* }
|
|
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
|
+
* "id": "chatcmpl-9u4PnX6Fy7OmK46DASy0bH6cxn5Xu",
|
|
172
|
+
* "content": "J'adore la programmation.",
|
|
173
|
+
* "response_metadata": {
|
|
174
|
+
* "prompt": 0,
|
|
175
|
+
* "completion": 0,
|
|
176
|
+
* "finish_reason": "stop",
|
|
177
|
+
* },
|
|
178
|
+
* "usage_metadata": {
|
|
179
|
+
* "input_tokens": 28,
|
|
180
|
+
* "output_tokens": 5,
|
|
181
|
+
* "total_tokens": 33
|
|
182
|
+
* }
|
|
183
|
+
* }
|
|
184
|
+
* ```
|
|
185
|
+
* </details>
|
|
186
|
+
*
|
|
187
|
+
* <br />
|
|
188
|
+
*
|
|
189
|
+
* <details>
|
|
190
|
+
* <summary><strong>Bind tools</strong></summary>
|
|
191
|
+
*
|
|
192
|
+
* ```typescript
|
|
193
|
+
* import { z } from 'zod';
|
|
194
|
+
*
|
|
195
|
+
* const GetWeather = {
|
|
196
|
+
* name: "GetWeather",
|
|
197
|
+
* description: "Get the current weather in a given location",
|
|
198
|
+
* schema: z.object({
|
|
199
|
+
* location: z.string().describe("The city and state, e.g. San Francisco, CA")
|
|
200
|
+
* }),
|
|
201
|
+
* }
|
|
202
|
+
*
|
|
203
|
+
* const GetPopulation = {
|
|
204
|
+
* name: "GetPopulation",
|
|
205
|
+
* description: "Get the current population in a given location",
|
|
206
|
+
* schema: z.object({
|
|
207
|
+
* location: z.string().describe("The city and state, e.g. San Francisco, CA")
|
|
208
|
+
* }),
|
|
209
|
+
* }
|
|
210
|
+
*
|
|
211
|
+
* const llmWithTools = llm.bindTools([GetWeather, GetPopulation]);
|
|
212
|
+
* const aiMsg = await llmWithTools.invoke(
|
|
213
|
+
* "Which city is hotter today and which is bigger: LA or NY?"
|
|
214
|
+
* );
|
|
215
|
+
* console.log(aiMsg.tool_calls);
|
|
216
|
+
* ```
|
|
217
|
+
*
|
|
218
|
+
* ```txt
|
|
219
|
+
* [
|
|
220
|
+
* {
|
|
221
|
+
* name: 'GetWeather',
|
|
222
|
+
* args: { location: 'Los Angeles, CA' },
|
|
223
|
+
* type: 'tool_call',
|
|
224
|
+
* id: 'call_uPU4FiFzoKAtMxfmPnfQL6UK'
|
|
225
|
+
* },
|
|
226
|
+
* {
|
|
227
|
+
* name: 'GetWeather',
|
|
228
|
+
* args: { location: 'New York, NY' },
|
|
229
|
+
* type: 'tool_call',
|
|
230
|
+
* id: 'call_UNkEwuQsHrGYqgDQuH9nPAtX'
|
|
231
|
+
* },
|
|
232
|
+
* {
|
|
233
|
+
* name: 'GetPopulation',
|
|
234
|
+
* args: { location: 'Los Angeles, CA' },
|
|
235
|
+
* type: 'tool_call',
|
|
236
|
+
* id: 'call_kL3OXxaq9OjIKqRTpvjaCH14'
|
|
237
|
+
* },
|
|
238
|
+
* {
|
|
239
|
+
* name: 'GetPopulation',
|
|
240
|
+
* args: { location: 'New York, NY' },
|
|
241
|
+
* type: 'tool_call',
|
|
242
|
+
* id: 'call_s9KQB1UWj45LLGaEnjz0179q'
|
|
243
|
+
* }
|
|
244
|
+
* ]
|
|
245
|
+
* ```
|
|
246
|
+
* </details>
|
|
247
|
+
*
|
|
248
|
+
* <br />
|
|
249
|
+
*
|
|
250
|
+
* <details>
|
|
251
|
+
* <summary><strong>Structured Output</strong></summary>
|
|
252
|
+
*
|
|
253
|
+
* ```typescript
|
|
254
|
+
* import { z } from 'zod';
|
|
255
|
+
*
|
|
256
|
+
* const Joke = z.object({
|
|
257
|
+
* setup: z.string().describe("The setup of the joke"),
|
|
258
|
+
* punchline: z.string().describe("The punchline to the joke"),
|
|
259
|
+
* rating: z.number().optional().describe("How funny the joke is, from 1 to 10")
|
|
260
|
+
* }).describe('Joke to tell user.');
|
|
261
|
+
*
|
|
262
|
+
* const structuredLlm = llm.withStructuredOutput(Joke, { name: "Joke" });
|
|
263
|
+
* const jokeResult = await structuredLlm.invoke("Tell me a joke about cats");
|
|
264
|
+
* console.log(jokeResult);
|
|
265
|
+
* ```
|
|
266
|
+
*
|
|
267
|
+
* ```txt
|
|
268
|
+
* {
|
|
269
|
+
* setup: 'Why was the cat sitting on the computer?',
|
|
270
|
+
* punchline: 'Because it wanted to keep an eye on the mouse!',
|
|
271
|
+
* rating: 7
|
|
272
|
+
* }
|
|
273
|
+
* ```
|
|
274
|
+
* </details>
|
|
275
|
+
*
|
|
276
|
+
* <br />
|
|
277
|
+
*
|
|
278
|
+
* <details>
|
|
279
|
+
* <summary><strong>JSON Object Response Format</strong></summary>
|
|
280
|
+
*
|
|
281
|
+
* ```typescript
|
|
282
|
+
* const jsonLlm = llm.bind({ response_format: { type: "json_object" } });
|
|
283
|
+
* const jsonLlmAiMsg = await jsonLlm.invoke(
|
|
284
|
+
* "Return a JSON object with key 'randomInts' and a value of 10 random ints in [0-99]"
|
|
285
|
+
* );
|
|
286
|
+
* console.log(jsonLlmAiMsg.content);
|
|
287
|
+
* ```
|
|
288
|
+
*
|
|
289
|
+
* ```txt
|
|
290
|
+
* {
|
|
291
|
+
* "randomInts": [23, 87, 45, 12, 78, 34, 56, 90, 11, 67]
|
|
292
|
+
* }
|
|
293
|
+
* ```
|
|
294
|
+
* </details>
|
|
295
|
+
*
|
|
296
|
+
* <br />
|
|
297
|
+
*
|
|
298
|
+
* <details>
|
|
299
|
+
* <summary><strong>Multimodal</strong></summary>
|
|
300
|
+
*
|
|
301
|
+
* ```typescript
|
|
302
|
+
* import { HumanMessage } from '@langchain/core/messages';
|
|
303
|
+
*
|
|
304
|
+
* const imageUrl = "https://example.com/image.jpg";
|
|
305
|
+
* const imageData = await fetch(imageUrl).then(res => res.arrayBuffer());
|
|
306
|
+
* const base64Image = Buffer.from(imageData).toString('base64');
|
|
307
|
+
*
|
|
308
|
+
* const message = new HumanMessage({
|
|
309
|
+
* content: [
|
|
310
|
+
* { type: "text", text: "describe the weather in this image" },
|
|
311
|
+
* {
|
|
312
|
+
* type: "image_url",
|
|
313
|
+
* image_url: { url: `data:image/jpeg;base64,${base64Image}` },
|
|
314
|
+
* },
|
|
315
|
+
* ]
|
|
316
|
+
* });
|
|
317
|
+
*
|
|
318
|
+
* const imageDescriptionAiMsg = await llm.invoke([message]);
|
|
319
|
+
* console.log(imageDescriptionAiMsg.content);
|
|
320
|
+
* ```
|
|
321
|
+
*
|
|
322
|
+
* ```txt
|
|
323
|
+
* 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.
|
|
324
|
+
* ```
|
|
325
|
+
* </details>
|
|
326
|
+
*
|
|
327
|
+
* <br />
|
|
328
|
+
*
|
|
329
|
+
* <details>
|
|
330
|
+
* <summary><strong>Usage Metadata</strong></summary>
|
|
331
|
+
*
|
|
332
|
+
* ```typescript
|
|
333
|
+
* const aiMsgForMetadata = await llm.invoke(input);
|
|
334
|
+
* console.log(aiMsgForMetadata.usage_metadata);
|
|
335
|
+
* ```
|
|
336
|
+
*
|
|
337
|
+
* ```txt
|
|
338
|
+
* { input_tokens: 28, output_tokens: 5, total_tokens: 33 }
|
|
339
|
+
* ```
|
|
340
|
+
* </details>
|
|
341
|
+
*
|
|
342
|
+
* <br />
|
|
343
|
+
*
|
|
344
|
+
* <details>
|
|
345
|
+
* <summary><strong>Logprobs</strong></summary>
|
|
346
|
+
*
|
|
347
|
+
* ```typescript
|
|
348
|
+
* const logprobsLlm = new ChatOpenAI({ logprobs: true });
|
|
349
|
+
* const aiMsgForLogprobs = await logprobsLlm.invoke(input);
|
|
350
|
+
* console.log(aiMsgForLogprobs.response_metadata.logprobs);
|
|
351
|
+
* ```
|
|
352
|
+
*
|
|
353
|
+
* ```txt
|
|
354
|
+
* {
|
|
355
|
+
* content: [
|
|
356
|
+
* {
|
|
357
|
+
* token: 'J',
|
|
358
|
+
* logprob: -0.000050616763,
|
|
359
|
+
* bytes: [Array],
|
|
360
|
+
* top_logprobs: []
|
|
361
|
+
* },
|
|
362
|
+
* {
|
|
363
|
+
* token: "'",
|
|
364
|
+
* logprob: -0.01868736,
|
|
365
|
+
* bytes: [Array],
|
|
366
|
+
* top_logprobs: []
|
|
367
|
+
* },
|
|
368
|
+
* {
|
|
369
|
+
* token: 'ad',
|
|
370
|
+
* logprob: -0.0000030545007,
|
|
371
|
+
* bytes: [Array],
|
|
372
|
+
* top_logprobs: []
|
|
373
|
+
* },
|
|
374
|
+
* { token: 'ore', logprob: 0, bytes: [Array], top_logprobs: [] },
|
|
375
|
+
* {
|
|
376
|
+
* token: ' la',
|
|
377
|
+
* logprob: -0.515404,
|
|
378
|
+
* bytes: [Array],
|
|
379
|
+
* top_logprobs: []
|
|
380
|
+
* },
|
|
381
|
+
* {
|
|
382
|
+
* token: ' programm',
|
|
383
|
+
* logprob: -0.0000118755715,
|
|
384
|
+
* bytes: [Array],
|
|
385
|
+
* top_logprobs: []
|
|
386
|
+
* },
|
|
387
|
+
* { token: 'ation', logprob: 0, bytes: [Array], top_logprobs: [] },
|
|
388
|
+
* {
|
|
389
|
+
* token: '.',
|
|
390
|
+
* logprob: -0.0000037697225,
|
|
391
|
+
* bytes: [Array],
|
|
392
|
+
* top_logprobs: []
|
|
393
|
+
* }
|
|
394
|
+
* ],
|
|
395
|
+
* refusal: null
|
|
396
|
+
* }
|
|
397
|
+
* ```
|
|
398
|
+
* </details>
|
|
399
|
+
*
|
|
400
|
+
* <br />
|
|
401
|
+
*
|
|
402
|
+
* <details>
|
|
403
|
+
* <summary><strong>Response Metadata</strong></summary>
|
|
404
|
+
*
|
|
405
|
+
* ```typescript
|
|
406
|
+
* const aiMsgForResponseMetadata = await llm.invoke(input);
|
|
407
|
+
* console.log(aiMsgForResponseMetadata.response_metadata);
|
|
408
|
+
* ```
|
|
409
|
+
*
|
|
410
|
+
* ```txt
|
|
411
|
+
* {
|
|
412
|
+
* tokenUsage: { completionTokens: 5, promptTokens: 28, totalTokens: 33 },
|
|
413
|
+
* finish_reason: 'stop',
|
|
414
|
+
* system_fingerprint: 'fp_3aa7262c27'
|
|
415
|
+
* }
|
|
416
|
+
* ```
|
|
417
|
+
* </details>
|
|
418
|
+
*/
|
|
7
419
|
class AzureChatOpenAI extends chat_models_js_1.ChatOpenAI {
|
|
8
420
|
_llmType() {
|
|
9
421
|
return "azure_openai";
|