@langchain/openai 0.6.9 → 1.0.0-alpha.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.
- package/README.md +8 -8
- package/dist/_virtual/rolldown_runtime.cjs +25 -0
- package/dist/azure/chat_models.cjs +661 -808
- package/dist/azure/chat_models.cjs.map +1 -0
- package/dist/azure/chat_models.d.cts +449 -0
- package/dist/azure/chat_models.d.cts.map +1 -0
- package/dist/azure/chat_models.d.ts +32 -28
- package/dist/azure/chat_models.d.ts.map +1 -0
- package/dist/azure/chat_models.js +660 -804
- package/dist/azure/chat_models.js.map +1 -0
- package/dist/azure/embeddings.cjs +81 -130
- package/dist/azure/embeddings.cjs.map +1 -0
- package/dist/azure/embeddings.d.cts +27 -0
- package/dist/azure/embeddings.d.cts.map +1 -0
- package/dist/azure/embeddings.d.ts +25 -20
- package/dist/azure/embeddings.d.ts.map +1 -0
- package/dist/azure/embeddings.js +80 -126
- package/dist/azure/embeddings.js.map +1 -0
- package/dist/azure/llms.cjs +109 -168
- package/dist/azure/llms.cjs.map +1 -0
- package/dist/azure/llms.d.cts +33 -0
- package/dist/azure/llms.d.cts.map +1 -0
- package/dist/azure/llms.d.ts +31 -25
- package/dist/azure/llms.d.ts.map +1 -0
- package/dist/azure/llms.js +108 -164
- package/dist/azure/llms.js.map +1 -0
- package/dist/chat_models.cjs +1951 -2774
- package/dist/chat_models.cjs.map +1 -0
- package/dist/chat_models.d.cts +930 -0
- package/dist/chat_models.d.cts.map +1 -0
- package/dist/chat_models.d.ts +365 -348
- package/dist/chat_models.d.ts.map +1 -0
- package/dist/chat_models.js +1946 -2764
- package/dist/chat_models.js.map +1 -0
- package/dist/embeddings.cjs +136 -193
- package/dist/embeddings.cjs.map +1 -0
- package/dist/embeddings.d.cts +115 -0
- package/dist/embeddings.d.cts.map +1 -0
- package/dist/embeddings.d.ts +88 -81
- package/dist/embeddings.d.ts.map +1 -0
- package/dist/embeddings.js +134 -188
- package/dist/embeddings.js.map +1 -0
- package/dist/index.cjs +47 -34
- package/dist/index.d.cts +16 -0
- package/dist/index.d.ts +16 -13
- package/dist/index.js +17 -13
- package/dist/langchain-core/dist/types/_internal.d.cts +11 -0
- package/dist/langchain-core/dist/types/_internal.d.cts.map +1 -0
- package/dist/langchain-core/dist/types/_internal.d.ts +11 -0
- package/dist/langchain-core/dist/types/_internal.d.ts.map +1 -0
- package/dist/langchain-core/dist/utils/stream.d.cts +26 -0
- package/dist/langchain-core/dist/utils/stream.d.cts.map +1 -0
- package/dist/langchain-core/dist/utils/stream.d.ts +26 -0
- package/dist/langchain-core/dist/utils/stream.d.ts.map +1 -0
- package/dist/llms.cjs +312 -468
- package/dist/llms.cjs.map +1 -0
- package/dist/llms.d.cts +123 -0
- package/dist/llms.d.cts.map +1 -0
- package/dist/llms.d.ts +93 -87
- package/dist/llms.d.ts.map +1 -0
- package/dist/llms.js +309 -462
- package/dist/llms.js.map +1 -0
- package/dist/tools/custom.cjs +25 -26
- package/dist/tools/custom.cjs.map +1 -0
- package/dist/tools/custom.d.cts +10 -0
- package/dist/tools/custom.d.cts.map +1 -0
- package/dist/tools/custom.d.ts +8 -3
- package/dist/tools/custom.d.ts.map +1 -0
- package/dist/tools/custom.js +24 -23
- package/dist/tools/custom.js.map +1 -0
- package/dist/tools/dalle.cjs +104 -194
- package/dist/tools/dalle.cjs.map +1 -0
- package/dist/tools/dalle.d.cts +123 -0
- package/dist/tools/dalle.d.cts.map +1 -0
- package/dist/tools/dalle.d.ts +111 -104
- package/dist/tools/dalle.d.ts.map +1 -0
- package/dist/tools/dalle.js +102 -189
- package/dist/tools/dalle.js.map +1 -0
- package/dist/tools/index.cjs +1 -17
- package/dist/tools/index.js +1 -1
- package/dist/types.d.cts +263 -0
- package/dist/types.d.cts.map +1 -0
- package/dist/types.d.ts +244 -237
- package/dist/types.d.ts.map +1 -0
- package/dist/utils/azure.cjs +86 -47
- package/dist/utils/azure.cjs.map +1 -0
- package/dist/utils/azure.d.cts +76 -0
- package/dist/utils/azure.d.cts.map +1 -0
- package/dist/utils/azure.d.ts +51 -9
- package/dist/utils/azure.d.ts.map +1 -0
- package/dist/utils/azure.js +85 -45
- package/dist/utils/azure.js.map +1 -0
- package/dist/utils/client.cjs +25 -0
- package/dist/utils/client.cjs.map +1 -0
- package/dist/utils/client.d.cts +5 -0
- package/dist/utils/client.d.cts.map +1 -0
- package/dist/utils/client.d.ts +5 -0
- package/dist/utils/client.d.ts.map +1 -0
- package/dist/utils/client.js +24 -0
- package/dist/utils/client.js.map +1 -0
- package/dist/utils/errors.cjs +9 -8
- package/dist/utils/errors.cjs.map +1 -0
- package/dist/utils/errors.js +9 -6
- package/dist/utils/errors.js.map +1 -0
- package/dist/utils/message_inputs.cjs +139 -0
- package/dist/utils/message_inputs.cjs.map +1 -0
- package/dist/utils/message_inputs.d.cts +9 -0
- package/dist/utils/message_inputs.d.cts.map +1 -0
- package/dist/utils/message_inputs.d.ts +9 -0
- package/dist/utils/message_inputs.d.ts.map +1 -0
- package/dist/utils/message_inputs.js +137 -0
- package/dist/utils/message_inputs.js.map +1 -0
- package/dist/utils/misc.cjs +30 -0
- package/dist/utils/misc.cjs.map +1 -0
- package/dist/utils/misc.d.cts +9 -0
- package/dist/utils/misc.d.cts.map +1 -0
- package/dist/utils/misc.d.ts +9 -0
- package/dist/utils/misc.d.ts.map +1 -0
- package/dist/utils/misc.js +27 -0
- package/dist/utils/misc.js.map +1 -0
- package/dist/utils/output.cjs +74 -0
- package/dist/utils/output.cjs.map +1 -0
- package/dist/utils/output.js +72 -0
- package/dist/utils/output.js.map +1 -0
- package/dist/utils/prompts.cjs +39 -38
- package/dist/utils/prompts.cjs.map +1 -0
- package/dist/utils/prompts.d.cts +42 -0
- package/dist/utils/prompts.d.cts.map +1 -0
- package/dist/utils/prompts.d.ts +10 -4
- package/dist/utils/prompts.d.ts.map +1 -0
- package/dist/utils/prompts.js +40 -36
- package/dist/utils/prompts.js.map +1 -0
- package/dist/utils/standard.cjs +113 -0
- package/dist/utils/standard.cjs.map +1 -0
- package/dist/utils/standard.js +111 -0
- package/dist/utils/standard.js.map +1 -0
- package/dist/utils/tools.cjs +150 -127
- package/dist/utils/tools.cjs.map +1 -0
- package/dist/utils/tools.d.cts +13 -0
- package/dist/utils/tools.d.cts.map +1 -0
- package/dist/utils/tools.d.ts +12 -42
- package/dist/utils/tools.d.ts.map +1 -0
- package/dist/utils/tools.js +146 -123
- package/dist/utils/tools.js.map +1 -0
- package/package.json +32 -40
- package/dist/tools/index.d.ts +0 -1
- package/dist/types.cjs +0 -2
- package/dist/types.js +0 -1
- package/dist/utils/errors.d.ts +0 -2
- package/dist/utils/headers.cjs +0 -39
- package/dist/utils/headers.d.ts +0 -8
- package/dist/utils/headers.js +0 -35
- package/dist/utils/openai-format-fndef.cjs +0 -80
- package/dist/utils/openai-format-fndef.d.ts +0 -44
- package/dist/utils/openai-format-fndef.js +0 -77
- package/dist/utils/openai.cjs +0 -127
- package/dist/utils/openai.d.ts +0 -15
- package/dist/utils/openai.js +0 -119
- package/index.cjs +0 -1
- package/index.d.cts +0 -1
- package/index.d.ts +0 -1
- package/index.js +0 -1
package/dist/llms.js
CHANGED
|
@@ -1,466 +1,313 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { getEndpoint } from "./utils/azure.js";
|
|
2
|
+
import { wrapOpenAIClientError } from "./utils/client.js";
|
|
3
|
+
import { OpenAI as OpenAI$1 } from "openai";
|
|
3
4
|
import { GenerationChunk } from "@langchain/core/outputs";
|
|
4
5
|
import { getEnvironmentVariable } from "@langchain/core/utils/env";
|
|
5
|
-
import {
|
|
6
|
+
import { calculateMaxTokens } from "@langchain/core/language_models/base";
|
|
7
|
+
import { BaseLLM } from "@langchain/core/language_models/llms";
|
|
6
8
|
import { chunkArray } from "@langchain/core/utils/chunk_array";
|
|
7
|
-
|
|
8
|
-
|
|
9
|
+
|
|
10
|
+
//#region src/llms.ts
|
|
9
11
|
/**
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
}
|
|
313
|
-
for (let i = 0; i < subPrompts.length; i += 1) {
|
|
314
|
-
const data = params.stream
|
|
315
|
-
? await (async () => {
|
|
316
|
-
const choices = [];
|
|
317
|
-
let response;
|
|
318
|
-
const stream = await this.completionWithRetry({
|
|
319
|
-
...params,
|
|
320
|
-
stream: true,
|
|
321
|
-
prompt: subPrompts[i],
|
|
322
|
-
}, options);
|
|
323
|
-
for await (const message of stream) {
|
|
324
|
-
// on the first message set the response properties
|
|
325
|
-
if (!response) {
|
|
326
|
-
response = {
|
|
327
|
-
id: message.id,
|
|
328
|
-
object: message.object,
|
|
329
|
-
created: message.created,
|
|
330
|
-
model: message.model,
|
|
331
|
-
};
|
|
332
|
-
}
|
|
333
|
-
// on all messages, update choice
|
|
334
|
-
for (const part of message.choices) {
|
|
335
|
-
if (!choices[part.index]) {
|
|
336
|
-
choices[part.index] = part;
|
|
337
|
-
}
|
|
338
|
-
else {
|
|
339
|
-
const choice = choices[part.index];
|
|
340
|
-
choice.text += part.text;
|
|
341
|
-
choice.finish_reason = part.finish_reason;
|
|
342
|
-
choice.logprobs = part.logprobs;
|
|
343
|
-
}
|
|
344
|
-
void runManager?.handleLLMNewToken(part.text, {
|
|
345
|
-
prompt: Math.floor(part.index / this.n),
|
|
346
|
-
completion: part.index % this.n,
|
|
347
|
-
});
|
|
348
|
-
}
|
|
349
|
-
}
|
|
350
|
-
if (options.signal?.aborted) {
|
|
351
|
-
throw new Error("AbortError");
|
|
352
|
-
}
|
|
353
|
-
return { ...response, choices };
|
|
354
|
-
})()
|
|
355
|
-
: await this.completionWithRetry({
|
|
356
|
-
...params,
|
|
357
|
-
stream: false,
|
|
358
|
-
prompt: subPrompts[i],
|
|
359
|
-
}, {
|
|
360
|
-
signal: options.signal,
|
|
361
|
-
...options.options,
|
|
362
|
-
});
|
|
363
|
-
choices.push(...data.choices);
|
|
364
|
-
const { completion_tokens: completionTokens, prompt_tokens: promptTokens, total_tokens: totalTokens, } = data.usage
|
|
365
|
-
? data.usage
|
|
366
|
-
: {
|
|
367
|
-
completion_tokens: undefined,
|
|
368
|
-
prompt_tokens: undefined,
|
|
369
|
-
total_tokens: undefined,
|
|
370
|
-
};
|
|
371
|
-
if (completionTokens) {
|
|
372
|
-
tokenUsage.completionTokens =
|
|
373
|
-
(tokenUsage.completionTokens ?? 0) + completionTokens;
|
|
374
|
-
}
|
|
375
|
-
if (promptTokens) {
|
|
376
|
-
tokenUsage.promptTokens = (tokenUsage.promptTokens ?? 0) + promptTokens;
|
|
377
|
-
}
|
|
378
|
-
if (totalTokens) {
|
|
379
|
-
tokenUsage.totalTokens = (tokenUsage.totalTokens ?? 0) + totalTokens;
|
|
380
|
-
}
|
|
381
|
-
}
|
|
382
|
-
const generations = chunkArray(choices, this.n).map((promptChoices) => promptChoices.map((choice) => ({
|
|
383
|
-
text: choice.text ?? "",
|
|
384
|
-
generationInfo: {
|
|
385
|
-
finishReason: choice.finish_reason,
|
|
386
|
-
logprobs: choice.logprobs,
|
|
387
|
-
},
|
|
388
|
-
})));
|
|
389
|
-
return {
|
|
390
|
-
generations,
|
|
391
|
-
llmOutput: { tokenUsage },
|
|
392
|
-
};
|
|
393
|
-
}
|
|
394
|
-
// TODO(jacoblee): Refactor with _generate(..., {stream: true}) implementation?
|
|
395
|
-
async *_streamResponseChunks(input, options, runManager) {
|
|
396
|
-
const params = {
|
|
397
|
-
...this.invocationParams(options),
|
|
398
|
-
prompt: input,
|
|
399
|
-
stream: true,
|
|
400
|
-
};
|
|
401
|
-
const stream = await this.completionWithRetry(params, options);
|
|
402
|
-
for await (const data of stream) {
|
|
403
|
-
const choice = data?.choices[0];
|
|
404
|
-
if (!choice) {
|
|
405
|
-
continue;
|
|
406
|
-
}
|
|
407
|
-
const chunk = new GenerationChunk({
|
|
408
|
-
text: choice.text,
|
|
409
|
-
generationInfo: {
|
|
410
|
-
finishReason: choice.finish_reason,
|
|
411
|
-
},
|
|
412
|
-
});
|
|
413
|
-
yield chunk;
|
|
414
|
-
// eslint-disable-next-line no-void
|
|
415
|
-
void runManager?.handleLLMNewToken(chunk.text ?? "");
|
|
416
|
-
}
|
|
417
|
-
if (options.signal?.aborted) {
|
|
418
|
-
throw new Error("AbortError");
|
|
419
|
-
}
|
|
420
|
-
}
|
|
421
|
-
async completionWithRetry(request, options) {
|
|
422
|
-
const requestOptions = this._getClientOptions(options);
|
|
423
|
-
return this.caller.call(async () => {
|
|
424
|
-
try {
|
|
425
|
-
const res = await this.client.completions.create(request, requestOptions);
|
|
426
|
-
return res;
|
|
427
|
-
}
|
|
428
|
-
catch (e) {
|
|
429
|
-
const error = wrapOpenAIClientError(e);
|
|
430
|
-
throw error;
|
|
431
|
-
}
|
|
432
|
-
});
|
|
433
|
-
}
|
|
434
|
-
/**
|
|
435
|
-
* Calls the OpenAI API with retry logic in case of failures.
|
|
436
|
-
* @param request The request to send to the OpenAI API.
|
|
437
|
-
* @param options Optional configuration for the API call.
|
|
438
|
-
* @returns The response from the OpenAI API.
|
|
439
|
-
*/
|
|
440
|
-
_getClientOptions(options) {
|
|
441
|
-
if (!this.client) {
|
|
442
|
-
const openAIEndpointConfig = {
|
|
443
|
-
baseURL: this.clientConfig.baseURL,
|
|
444
|
-
};
|
|
445
|
-
const endpoint = getEndpoint(openAIEndpointConfig);
|
|
446
|
-
const params = {
|
|
447
|
-
...this.clientConfig,
|
|
448
|
-
baseURL: endpoint,
|
|
449
|
-
timeout: this.timeout,
|
|
450
|
-
maxRetries: 0,
|
|
451
|
-
};
|
|
452
|
-
if (!params.baseURL) {
|
|
453
|
-
delete params.baseURL;
|
|
454
|
-
}
|
|
455
|
-
this.client = new OpenAIClient(params);
|
|
456
|
-
}
|
|
457
|
-
const requestOptions = {
|
|
458
|
-
...this.clientConfig,
|
|
459
|
-
...options,
|
|
460
|
-
};
|
|
461
|
-
return requestOptions;
|
|
462
|
-
}
|
|
463
|
-
_llmType() {
|
|
464
|
-
return "openai";
|
|
465
|
-
}
|
|
466
|
-
}
|
|
12
|
+
* Wrapper around OpenAI large language models.
|
|
13
|
+
*
|
|
14
|
+
* To use you should have the `openai` package installed, with the
|
|
15
|
+
* `OPENAI_API_KEY` environment variable set.
|
|
16
|
+
*
|
|
17
|
+
* To use with Azure, import the `AzureOpenAI` class.
|
|
18
|
+
*
|
|
19
|
+
* @remarks
|
|
20
|
+
* Any parameters that are valid to be passed to {@link
|
|
21
|
+
* https://platform.openai.com/docs/api-reference/completions/create |
|
|
22
|
+
* `openai.createCompletion`} can be passed through {@link modelKwargs}, even
|
|
23
|
+
* if not explicitly available on this class.
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* const model = new OpenAI({
|
|
27
|
+
* modelName: "gpt-4",
|
|
28
|
+
* temperature: 0.7,
|
|
29
|
+
* maxTokens: 1000,
|
|
30
|
+
* maxRetries: 5,
|
|
31
|
+
* });
|
|
32
|
+
*
|
|
33
|
+
* const res = await model.invoke(
|
|
34
|
+
* "Question: What would be a good company name for a company that makes colorful socks?\nAnswer:"
|
|
35
|
+
* );
|
|
36
|
+
* console.log({ res });
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
var OpenAI$2 = class extends BaseLLM {
|
|
40
|
+
static lc_name() {
|
|
41
|
+
return "OpenAI";
|
|
42
|
+
}
|
|
43
|
+
get callKeys() {
|
|
44
|
+
return [...super.callKeys, "options"];
|
|
45
|
+
}
|
|
46
|
+
lc_serializable = true;
|
|
47
|
+
get lc_secrets() {
|
|
48
|
+
return {
|
|
49
|
+
openAIApiKey: "OPENAI_API_KEY",
|
|
50
|
+
apiKey: "OPENAI_API_KEY",
|
|
51
|
+
organization: "OPENAI_ORGANIZATION"
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
get lc_aliases() {
|
|
55
|
+
return {
|
|
56
|
+
modelName: "model",
|
|
57
|
+
openAIApiKey: "openai_api_key",
|
|
58
|
+
apiKey: "openai_api_key"
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
temperature;
|
|
62
|
+
maxTokens;
|
|
63
|
+
topP;
|
|
64
|
+
frequencyPenalty;
|
|
65
|
+
presencePenalty;
|
|
66
|
+
n = 1;
|
|
67
|
+
bestOf;
|
|
68
|
+
logitBias;
|
|
69
|
+
model = "gpt-3.5-turbo-instruct";
|
|
70
|
+
/** @deprecated Use "model" instead */
|
|
71
|
+
modelName;
|
|
72
|
+
modelKwargs;
|
|
73
|
+
batchSize = 20;
|
|
74
|
+
timeout;
|
|
75
|
+
stop;
|
|
76
|
+
stopSequences;
|
|
77
|
+
user;
|
|
78
|
+
streaming = false;
|
|
79
|
+
openAIApiKey;
|
|
80
|
+
apiKey;
|
|
81
|
+
organization;
|
|
82
|
+
client;
|
|
83
|
+
clientConfig;
|
|
84
|
+
constructor(fields) {
|
|
85
|
+
super(fields ?? {});
|
|
86
|
+
this.openAIApiKey = fields?.apiKey ?? fields?.openAIApiKey ?? getEnvironmentVariable("OPENAI_API_KEY");
|
|
87
|
+
this.apiKey = this.openAIApiKey;
|
|
88
|
+
this.organization = fields?.configuration?.organization ?? getEnvironmentVariable("OPENAI_ORGANIZATION");
|
|
89
|
+
this.model = fields?.model ?? fields?.modelName ?? this.model;
|
|
90
|
+
if ((this.model?.startsWith("gpt-3.5-turbo") || this.model?.startsWith("gpt-4") || this.model?.startsWith("o1")) && !this.model?.includes("-instruct")) throw new Error([
|
|
91
|
+
`Your chosen OpenAI model, "${this.model}", is a chat model and not a text-in/text-out LLM.`,
|
|
92
|
+
`Passing it into the "OpenAI" class is no longer supported.`,
|
|
93
|
+
`Please use the "ChatOpenAI" class instead.`,
|
|
94
|
+
"",
|
|
95
|
+
`See this page for more information:`,
|
|
96
|
+
"|",
|
|
97
|
+
`└> https://js.langchain.com/docs/integrations/chat/openai`
|
|
98
|
+
].join("\n"));
|
|
99
|
+
this.modelName = this.model;
|
|
100
|
+
this.modelKwargs = fields?.modelKwargs ?? {};
|
|
101
|
+
this.batchSize = fields?.batchSize ?? this.batchSize;
|
|
102
|
+
this.timeout = fields?.timeout;
|
|
103
|
+
this.temperature = fields?.temperature ?? this.temperature;
|
|
104
|
+
this.maxTokens = fields?.maxTokens ?? this.maxTokens;
|
|
105
|
+
this.topP = fields?.topP ?? this.topP;
|
|
106
|
+
this.frequencyPenalty = fields?.frequencyPenalty ?? this.frequencyPenalty;
|
|
107
|
+
this.presencePenalty = fields?.presencePenalty ?? this.presencePenalty;
|
|
108
|
+
this.n = fields?.n ?? this.n;
|
|
109
|
+
this.bestOf = fields?.bestOf ?? this.bestOf;
|
|
110
|
+
this.logitBias = fields?.logitBias;
|
|
111
|
+
this.stop = fields?.stopSequences ?? fields?.stop;
|
|
112
|
+
this.stopSequences = this.stop;
|
|
113
|
+
this.user = fields?.user;
|
|
114
|
+
this.streaming = fields?.streaming ?? false;
|
|
115
|
+
if (this.streaming && this.bestOf && this.bestOf > 1) throw new Error("Cannot stream results when bestOf > 1");
|
|
116
|
+
this.clientConfig = {
|
|
117
|
+
apiKey: this.apiKey,
|
|
118
|
+
organization: this.organization,
|
|
119
|
+
dangerouslyAllowBrowser: true,
|
|
120
|
+
...fields?.configuration
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Get the parameters used to invoke the model
|
|
125
|
+
*/
|
|
126
|
+
invocationParams(options) {
|
|
127
|
+
return {
|
|
128
|
+
model: this.model,
|
|
129
|
+
temperature: this.temperature,
|
|
130
|
+
max_tokens: this.maxTokens,
|
|
131
|
+
top_p: this.topP,
|
|
132
|
+
frequency_penalty: this.frequencyPenalty,
|
|
133
|
+
presence_penalty: this.presencePenalty,
|
|
134
|
+
n: this.n,
|
|
135
|
+
best_of: this.bestOf,
|
|
136
|
+
logit_bias: this.logitBias,
|
|
137
|
+
stop: options?.stop ?? this.stopSequences,
|
|
138
|
+
user: this.user,
|
|
139
|
+
stream: this.streaming,
|
|
140
|
+
...this.modelKwargs
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
/** @ignore */
|
|
144
|
+
_identifyingParams() {
|
|
145
|
+
return {
|
|
146
|
+
model_name: this.model,
|
|
147
|
+
...this.invocationParams(),
|
|
148
|
+
...this.clientConfig
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Get the identifying parameters for the model
|
|
153
|
+
*/
|
|
154
|
+
identifyingParams() {
|
|
155
|
+
return this._identifyingParams();
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Call out to OpenAI's endpoint with k unique prompts
|
|
159
|
+
*
|
|
160
|
+
* @param [prompts] - The prompts to pass into the model.
|
|
161
|
+
* @param [options] - Optional list of stop words to use when generating.
|
|
162
|
+
* @param [runManager] - Optional callback manager to use when generating.
|
|
163
|
+
*
|
|
164
|
+
* @returns The full LLM output.
|
|
165
|
+
*
|
|
166
|
+
* @example
|
|
167
|
+
* ```ts
|
|
168
|
+
* import { OpenAI } from "langchain/llms/openai";
|
|
169
|
+
* const openai = new OpenAI();
|
|
170
|
+
* const response = await openai.generate(["Tell me a joke."]);
|
|
171
|
+
* ```
|
|
172
|
+
*/
|
|
173
|
+
async _generate(prompts, options, runManager) {
|
|
174
|
+
const subPrompts = chunkArray(prompts, this.batchSize);
|
|
175
|
+
const choices = [];
|
|
176
|
+
const tokenUsage = {};
|
|
177
|
+
const params = this.invocationParams(options);
|
|
178
|
+
if (params.max_tokens === -1) {
|
|
179
|
+
if (prompts.length !== 1) throw new Error("max_tokens set to -1 not supported for multiple inputs");
|
|
180
|
+
params.max_tokens = await calculateMaxTokens({
|
|
181
|
+
prompt: prompts[0],
|
|
182
|
+
modelName: this.model
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
for (let i = 0; i < subPrompts.length; i += 1) {
|
|
186
|
+
const data = params.stream ? await (async () => {
|
|
187
|
+
const choices$1 = [];
|
|
188
|
+
let response;
|
|
189
|
+
const stream = await this.completionWithRetry({
|
|
190
|
+
...params,
|
|
191
|
+
stream: true,
|
|
192
|
+
prompt: subPrompts[i]
|
|
193
|
+
}, options);
|
|
194
|
+
for await (const message of stream) {
|
|
195
|
+
if (!response) response = {
|
|
196
|
+
id: message.id,
|
|
197
|
+
object: message.object,
|
|
198
|
+
created: message.created,
|
|
199
|
+
model: message.model
|
|
200
|
+
};
|
|
201
|
+
for (const part of message.choices) {
|
|
202
|
+
if (!choices$1[part.index]) choices$1[part.index] = part;
|
|
203
|
+
else {
|
|
204
|
+
const choice = choices$1[part.index];
|
|
205
|
+
choice.text += part.text;
|
|
206
|
+
choice.finish_reason = part.finish_reason;
|
|
207
|
+
choice.logprobs = part.logprobs;
|
|
208
|
+
}
|
|
209
|
+
runManager?.handleLLMNewToken(part.text, {
|
|
210
|
+
prompt: Math.floor(part.index / this.n),
|
|
211
|
+
completion: part.index % this.n
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
if (options.signal?.aborted) throw new Error("AbortError");
|
|
216
|
+
return {
|
|
217
|
+
...response,
|
|
218
|
+
choices: choices$1
|
|
219
|
+
};
|
|
220
|
+
})() : await this.completionWithRetry({
|
|
221
|
+
...params,
|
|
222
|
+
stream: false,
|
|
223
|
+
prompt: subPrompts[i]
|
|
224
|
+
}, {
|
|
225
|
+
signal: options.signal,
|
|
226
|
+
...options.options
|
|
227
|
+
});
|
|
228
|
+
choices.push(...data.choices);
|
|
229
|
+
const { completion_tokens: completionTokens, prompt_tokens: promptTokens, total_tokens: totalTokens } = data.usage ? data.usage : {
|
|
230
|
+
completion_tokens: void 0,
|
|
231
|
+
prompt_tokens: void 0,
|
|
232
|
+
total_tokens: void 0
|
|
233
|
+
};
|
|
234
|
+
if (completionTokens) tokenUsage.completionTokens = (tokenUsage.completionTokens ?? 0) + completionTokens;
|
|
235
|
+
if (promptTokens) tokenUsage.promptTokens = (tokenUsage.promptTokens ?? 0) + promptTokens;
|
|
236
|
+
if (totalTokens) tokenUsage.totalTokens = (tokenUsage.totalTokens ?? 0) + totalTokens;
|
|
237
|
+
}
|
|
238
|
+
const generations = chunkArray(choices, this.n).map((promptChoices) => promptChoices.map((choice) => ({
|
|
239
|
+
text: choice.text ?? "",
|
|
240
|
+
generationInfo: {
|
|
241
|
+
finishReason: choice.finish_reason,
|
|
242
|
+
logprobs: choice.logprobs
|
|
243
|
+
}
|
|
244
|
+
})));
|
|
245
|
+
return {
|
|
246
|
+
generations,
|
|
247
|
+
llmOutput: { tokenUsage }
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
async *_streamResponseChunks(input, options, runManager) {
|
|
251
|
+
const params = {
|
|
252
|
+
...this.invocationParams(options),
|
|
253
|
+
prompt: input,
|
|
254
|
+
stream: true
|
|
255
|
+
};
|
|
256
|
+
const stream = await this.completionWithRetry(params, options);
|
|
257
|
+
for await (const data of stream) {
|
|
258
|
+
const choice = data?.choices[0];
|
|
259
|
+
if (!choice) continue;
|
|
260
|
+
const chunk = new GenerationChunk({
|
|
261
|
+
text: choice.text,
|
|
262
|
+
generationInfo: { finishReason: choice.finish_reason }
|
|
263
|
+
});
|
|
264
|
+
yield chunk;
|
|
265
|
+
runManager?.handleLLMNewToken(chunk.text ?? "");
|
|
266
|
+
}
|
|
267
|
+
if (options.signal?.aborted) throw new Error("AbortError");
|
|
268
|
+
}
|
|
269
|
+
async completionWithRetry(request, options) {
|
|
270
|
+
const requestOptions = this._getClientOptions(options);
|
|
271
|
+
return this.caller.call(async () => {
|
|
272
|
+
try {
|
|
273
|
+
const res = await this.client.completions.create(request, requestOptions);
|
|
274
|
+
return res;
|
|
275
|
+
} catch (e) {
|
|
276
|
+
const error = wrapOpenAIClientError(e);
|
|
277
|
+
throw error;
|
|
278
|
+
}
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Calls the OpenAI API with retry logic in case of failures.
|
|
283
|
+
* @param request The request to send to the OpenAI API.
|
|
284
|
+
* @param options Optional configuration for the API call.
|
|
285
|
+
* @returns The response from the OpenAI API.
|
|
286
|
+
*/
|
|
287
|
+
_getClientOptions(options) {
|
|
288
|
+
if (!this.client) {
|
|
289
|
+
const openAIEndpointConfig = { baseURL: this.clientConfig.baseURL };
|
|
290
|
+
const endpoint = getEndpoint(openAIEndpointConfig);
|
|
291
|
+
const params = {
|
|
292
|
+
...this.clientConfig,
|
|
293
|
+
baseURL: endpoint,
|
|
294
|
+
timeout: this.timeout,
|
|
295
|
+
maxRetries: 0
|
|
296
|
+
};
|
|
297
|
+
if (!params.baseURL) delete params.baseURL;
|
|
298
|
+
this.client = new OpenAI$1(params);
|
|
299
|
+
}
|
|
300
|
+
const requestOptions = {
|
|
301
|
+
...this.clientConfig,
|
|
302
|
+
...options
|
|
303
|
+
};
|
|
304
|
+
return requestOptions;
|
|
305
|
+
}
|
|
306
|
+
_llmType() {
|
|
307
|
+
return "openai";
|
|
308
|
+
}
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
//#endregion
|
|
312
|
+
export { OpenAI$2 as OpenAI };
|
|
313
|
+
//# sourceMappingURL=llms.js.map
|