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