@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.
Files changed (162) hide show
  1. package/README.md +8 -8
  2. package/dist/_virtual/rolldown_runtime.cjs +25 -0
  3. package/dist/azure/chat_models.cjs +661 -808
  4. package/dist/azure/chat_models.cjs.map +1 -0
  5. package/dist/azure/chat_models.d.cts +449 -0
  6. package/dist/azure/chat_models.d.cts.map +1 -0
  7. package/dist/azure/chat_models.d.ts +32 -28
  8. package/dist/azure/chat_models.d.ts.map +1 -0
  9. package/dist/azure/chat_models.js +660 -804
  10. package/dist/azure/chat_models.js.map +1 -0
  11. package/dist/azure/embeddings.cjs +81 -130
  12. package/dist/azure/embeddings.cjs.map +1 -0
  13. package/dist/azure/embeddings.d.cts +27 -0
  14. package/dist/azure/embeddings.d.cts.map +1 -0
  15. package/dist/azure/embeddings.d.ts +25 -20
  16. package/dist/azure/embeddings.d.ts.map +1 -0
  17. package/dist/azure/embeddings.js +80 -126
  18. package/dist/azure/embeddings.js.map +1 -0
  19. package/dist/azure/llms.cjs +109 -168
  20. package/dist/azure/llms.cjs.map +1 -0
  21. package/dist/azure/llms.d.cts +33 -0
  22. package/dist/azure/llms.d.cts.map +1 -0
  23. package/dist/azure/llms.d.ts +31 -25
  24. package/dist/azure/llms.d.ts.map +1 -0
  25. package/dist/azure/llms.js +108 -164
  26. package/dist/azure/llms.js.map +1 -0
  27. package/dist/chat_models.cjs +1951 -2774
  28. package/dist/chat_models.cjs.map +1 -0
  29. package/dist/chat_models.d.cts +930 -0
  30. package/dist/chat_models.d.cts.map +1 -0
  31. package/dist/chat_models.d.ts +365 -348
  32. package/dist/chat_models.d.ts.map +1 -0
  33. package/dist/chat_models.js +1946 -2764
  34. package/dist/chat_models.js.map +1 -0
  35. package/dist/embeddings.cjs +136 -193
  36. package/dist/embeddings.cjs.map +1 -0
  37. package/dist/embeddings.d.cts +115 -0
  38. package/dist/embeddings.d.cts.map +1 -0
  39. package/dist/embeddings.d.ts +88 -81
  40. package/dist/embeddings.d.ts.map +1 -0
  41. package/dist/embeddings.js +134 -188
  42. package/dist/embeddings.js.map +1 -0
  43. package/dist/index.cjs +47 -34
  44. package/dist/index.d.cts +16 -0
  45. package/dist/index.d.ts +16 -13
  46. package/dist/index.js +17 -13
  47. package/dist/langchain-core/dist/types/_internal.d.cts +11 -0
  48. package/dist/langchain-core/dist/types/_internal.d.cts.map +1 -0
  49. package/dist/langchain-core/dist/types/_internal.d.ts +11 -0
  50. package/dist/langchain-core/dist/types/_internal.d.ts.map +1 -0
  51. package/dist/langchain-core/dist/utils/stream.d.cts +26 -0
  52. package/dist/langchain-core/dist/utils/stream.d.cts.map +1 -0
  53. package/dist/langchain-core/dist/utils/stream.d.ts +26 -0
  54. package/dist/langchain-core/dist/utils/stream.d.ts.map +1 -0
  55. package/dist/llms.cjs +312 -468
  56. package/dist/llms.cjs.map +1 -0
  57. package/dist/llms.d.cts +123 -0
  58. package/dist/llms.d.cts.map +1 -0
  59. package/dist/llms.d.ts +93 -87
  60. package/dist/llms.d.ts.map +1 -0
  61. package/dist/llms.js +309 -462
  62. package/dist/llms.js.map +1 -0
  63. package/dist/tools/custom.cjs +25 -26
  64. package/dist/tools/custom.cjs.map +1 -0
  65. package/dist/tools/custom.d.cts +10 -0
  66. package/dist/tools/custom.d.cts.map +1 -0
  67. package/dist/tools/custom.d.ts +8 -3
  68. package/dist/tools/custom.d.ts.map +1 -0
  69. package/dist/tools/custom.js +24 -23
  70. package/dist/tools/custom.js.map +1 -0
  71. package/dist/tools/dalle.cjs +104 -194
  72. package/dist/tools/dalle.cjs.map +1 -0
  73. package/dist/tools/dalle.d.cts +123 -0
  74. package/dist/tools/dalle.d.cts.map +1 -0
  75. package/dist/tools/dalle.d.ts +111 -104
  76. package/dist/tools/dalle.d.ts.map +1 -0
  77. package/dist/tools/dalle.js +102 -189
  78. package/dist/tools/dalle.js.map +1 -0
  79. package/dist/tools/index.cjs +1 -17
  80. package/dist/tools/index.js +1 -1
  81. package/dist/types.d.cts +263 -0
  82. package/dist/types.d.cts.map +1 -0
  83. package/dist/types.d.ts +244 -237
  84. package/dist/types.d.ts.map +1 -0
  85. package/dist/utils/azure.cjs +86 -47
  86. package/dist/utils/azure.cjs.map +1 -0
  87. package/dist/utils/azure.d.cts +76 -0
  88. package/dist/utils/azure.d.cts.map +1 -0
  89. package/dist/utils/azure.d.ts +51 -9
  90. package/dist/utils/azure.d.ts.map +1 -0
  91. package/dist/utils/azure.js +85 -45
  92. package/dist/utils/azure.js.map +1 -0
  93. package/dist/utils/client.cjs +25 -0
  94. package/dist/utils/client.cjs.map +1 -0
  95. package/dist/utils/client.d.cts +5 -0
  96. package/dist/utils/client.d.cts.map +1 -0
  97. package/dist/utils/client.d.ts +5 -0
  98. package/dist/utils/client.d.ts.map +1 -0
  99. package/dist/utils/client.js +24 -0
  100. package/dist/utils/client.js.map +1 -0
  101. package/dist/utils/errors.cjs +9 -8
  102. package/dist/utils/errors.cjs.map +1 -0
  103. package/dist/utils/errors.js +9 -6
  104. package/dist/utils/errors.js.map +1 -0
  105. package/dist/utils/message_inputs.cjs +139 -0
  106. package/dist/utils/message_inputs.cjs.map +1 -0
  107. package/dist/utils/message_inputs.d.cts +9 -0
  108. package/dist/utils/message_inputs.d.cts.map +1 -0
  109. package/dist/utils/message_inputs.d.ts +9 -0
  110. package/dist/utils/message_inputs.d.ts.map +1 -0
  111. package/dist/utils/message_inputs.js +137 -0
  112. package/dist/utils/message_inputs.js.map +1 -0
  113. package/dist/utils/misc.cjs +30 -0
  114. package/dist/utils/misc.cjs.map +1 -0
  115. package/dist/utils/misc.d.cts +9 -0
  116. package/dist/utils/misc.d.cts.map +1 -0
  117. package/dist/utils/misc.d.ts +9 -0
  118. package/dist/utils/misc.d.ts.map +1 -0
  119. package/dist/utils/misc.js +27 -0
  120. package/dist/utils/misc.js.map +1 -0
  121. package/dist/utils/output.cjs +74 -0
  122. package/dist/utils/output.cjs.map +1 -0
  123. package/dist/utils/output.js +72 -0
  124. package/dist/utils/output.js.map +1 -0
  125. package/dist/utils/prompts.cjs +39 -38
  126. package/dist/utils/prompts.cjs.map +1 -0
  127. package/dist/utils/prompts.d.cts +42 -0
  128. package/dist/utils/prompts.d.cts.map +1 -0
  129. package/dist/utils/prompts.d.ts +10 -4
  130. package/dist/utils/prompts.d.ts.map +1 -0
  131. package/dist/utils/prompts.js +40 -36
  132. package/dist/utils/prompts.js.map +1 -0
  133. package/dist/utils/standard.cjs +113 -0
  134. package/dist/utils/standard.cjs.map +1 -0
  135. package/dist/utils/standard.js +111 -0
  136. package/dist/utils/standard.js.map +1 -0
  137. package/dist/utils/tools.cjs +150 -127
  138. package/dist/utils/tools.cjs.map +1 -0
  139. package/dist/utils/tools.d.cts +13 -0
  140. package/dist/utils/tools.d.cts.map +1 -0
  141. package/dist/utils/tools.d.ts +12 -42
  142. package/dist/utils/tools.d.ts.map +1 -0
  143. package/dist/utils/tools.js +146 -123
  144. package/dist/utils/tools.js.map +1 -0
  145. package/package.json +32 -40
  146. package/dist/tools/index.d.ts +0 -1
  147. package/dist/types.cjs +0 -2
  148. package/dist/types.js +0 -1
  149. package/dist/utils/errors.d.ts +0 -2
  150. package/dist/utils/headers.cjs +0 -39
  151. package/dist/utils/headers.d.ts +0 -8
  152. package/dist/utils/headers.js +0 -35
  153. package/dist/utils/openai-format-fndef.cjs +0 -80
  154. package/dist/utils/openai-format-fndef.d.ts +0 -44
  155. package/dist/utils/openai-format-fndef.js +0 -77
  156. package/dist/utils/openai.cjs +0 -127
  157. package/dist/utils/openai.d.ts +0 -15
  158. package/dist/utils/openai.js +0 -119
  159. package/index.cjs +0 -1
  160. package/index.d.cts +0 -1
  161. package/index.d.ts +0 -1
  162. package/index.js +0 -1
package/dist/llms.cjs CHANGED
@@ -1,470 +1,314 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.OpenAI = void 0;
4
- const openai_1 = require("openai");
5
- const base_1 = require("@langchain/core/language_models/base");
6
- const outputs_1 = require("@langchain/core/outputs");
7
- const env_1 = require("@langchain/core/utils/env");
8
- const llms_1 = require("@langchain/core/language_models/llms");
9
- const chunk_array_1 = require("@langchain/core/utils/chunk_array");
10
- const azure_js_1 = require("./utils/azure.cjs");
11
- const openai_js_1 = require("./utils/openai.cjs");
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
- * 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
- class OpenAI extends llms_1.BaseLLM {
41
- static lc_name() {
42
- return "OpenAI";
43
- }
44
- get callKeys() {
45
- return [...super.callKeys, "options"];
46
- }
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
- constructor(fields) {
62
- super(fields ?? {});
63
- Object.defineProperty(this, "lc_serializable", {
64
- enumerable: true,
65
- configurable: true,
66
- writable: true,
67
- value: true
68
- });
69
- Object.defineProperty(this, "temperature", {
70
- enumerable: true,
71
- configurable: true,
72
- writable: true,
73
- value: void 0
74
- });
75
- Object.defineProperty(this, "maxTokens", {
76
- enumerable: true,
77
- configurable: true,
78
- writable: true,
79
- value: void 0
80
- });
81
- Object.defineProperty(this, "topP", {
82
- enumerable: true,
83
- configurable: true,
84
- writable: true,
85
- value: void 0
86
- });
87
- Object.defineProperty(this, "frequencyPenalty", {
88
- enumerable: true,
89
- configurable: true,
90
- writable: true,
91
- value: void 0
92
- });
93
- Object.defineProperty(this, "presencePenalty", {
94
- enumerable: true,
95
- configurable: true,
96
- writable: true,
97
- value: void 0
98
- });
99
- Object.defineProperty(this, "n", {
100
- enumerable: true,
101
- configurable: true,
102
- writable: true,
103
- value: 1
104
- });
105
- Object.defineProperty(this, "bestOf", {
106
- enumerable: true,
107
- configurable: true,
108
- writable: true,
109
- value: void 0
110
- });
111
- Object.defineProperty(this, "logitBias", {
112
- enumerable: true,
113
- configurable: true,
114
- writable: true,
115
- value: void 0
116
- });
117
- Object.defineProperty(this, "model", {
118
- enumerable: true,
119
- configurable: true,
120
- writable: true,
121
- value: "gpt-3.5-turbo-instruct"
122
- });
123
- /** @deprecated Use "model" instead */
124
- Object.defineProperty(this, "modelName", {
125
- enumerable: true,
126
- configurable: true,
127
- writable: true,
128
- value: void 0
129
- });
130
- Object.defineProperty(this, "modelKwargs", {
131
- enumerable: true,
132
- configurable: true,
133
- writable: true,
134
- value: void 0
135
- });
136
- Object.defineProperty(this, "batchSize", {
137
- enumerable: true,
138
- configurable: true,
139
- writable: true,
140
- value: 20
141
- });
142
- Object.defineProperty(this, "timeout", {
143
- enumerable: true,
144
- configurable: true,
145
- writable: true,
146
- value: void 0
147
- });
148
- Object.defineProperty(this, "stop", {
149
- enumerable: true,
150
- configurable: true,
151
- writable: true,
152
- value: void 0
153
- });
154
- Object.defineProperty(this, "stopSequences", {
155
- enumerable: true,
156
- configurable: true,
157
- writable: true,
158
- value: void 0
159
- });
160
- Object.defineProperty(this, "user", {
161
- enumerable: true,
162
- configurable: true,
163
- writable: true,
164
- value: void 0
165
- });
166
- Object.defineProperty(this, "streaming", {
167
- enumerable: true,
168
- configurable: true,
169
- writable: true,
170
- value: false
171
- });
172
- Object.defineProperty(this, "openAIApiKey", {
173
- enumerable: true,
174
- configurable: true,
175
- writable: true,
176
- value: void 0
177
- });
178
- Object.defineProperty(this, "apiKey", {
179
- enumerable: true,
180
- configurable: true,
181
- writable: true,
182
- value: void 0
183
- });
184
- Object.defineProperty(this, "organization", {
185
- enumerable: true,
186
- configurable: true,
187
- writable: true,
188
- value: void 0
189
- });
190
- Object.defineProperty(this, "client", {
191
- enumerable: true,
192
- configurable: true,
193
- writable: true,
194
- value: void 0
195
- });
196
- Object.defineProperty(this, "clientConfig", {
197
- enumerable: true,
198
- configurable: true,
199
- writable: true,
200
- value: void 0
201
- });
202
- this.openAIApiKey =
203
- fields?.apiKey ??
204
- fields?.openAIApiKey ??
205
- (0, env_1.getEnvironmentVariable)("OPENAI_API_KEY");
206
- this.apiKey = this.openAIApiKey;
207
- this.organization =
208
- fields?.configuration?.organization ??
209
- (0, env_1.getEnvironmentVariable)("OPENAI_ORGANIZATION");
210
- this.model = fields?.model ?? fields?.modelName ?? this.model;
211
- if ((this.model?.startsWith("gpt-3.5-turbo") ||
212
- this.model?.startsWith("gpt-4") ||
213
- this.model?.startsWith("o1")) &&
214
- !this.model?.includes("-instruct")) {
215
- throw new Error([
216
- `Your chosen OpenAI model, "${this.model}", is a chat model and not a text-in/text-out LLM.`,
217
- `Passing it into the "OpenAI" class is no longer supported.`,
218
- `Please use the "ChatOpenAI" class instead.`,
219
- "",
220
- `See this page for more information:`,
221
- "|",
222
- `└> https://js.langchain.com/docs/integrations/chat/openai`,
223
- ].join("\n"));
224
- }
225
- this.modelName = this.model;
226
- this.modelKwargs = fields?.modelKwargs ?? {};
227
- this.batchSize = fields?.batchSize ?? this.batchSize;
228
- this.timeout = fields?.timeout;
229
- this.temperature = fields?.temperature ?? this.temperature;
230
- this.maxTokens = fields?.maxTokens ?? this.maxTokens;
231
- this.topP = fields?.topP ?? this.topP;
232
- this.frequencyPenalty = fields?.frequencyPenalty ?? this.frequencyPenalty;
233
- this.presencePenalty = fields?.presencePenalty ?? this.presencePenalty;
234
- this.n = fields?.n ?? this.n;
235
- this.bestOf = fields?.bestOf ?? this.bestOf;
236
- this.logitBias = fields?.logitBias;
237
- this.stop = fields?.stopSequences ?? fields?.stop;
238
- this.stopSequences = this.stop;
239
- this.user = fields?.user;
240
- this.streaming = fields?.streaming ?? false;
241
- if (this.streaming && this.bestOf && this.bestOf > 1) {
242
- throw new Error("Cannot stream results when bestOf > 1");
243
- }
244
- this.clientConfig = {
245
- apiKey: this.apiKey,
246
- organization: this.organization,
247
- dangerouslyAllowBrowser: true,
248
- ...fields?.configuration,
249
- };
250
- }
251
- /**
252
- * Get the parameters used to invoke the model
253
- */
254
- invocationParams(options) {
255
- return {
256
- model: this.model,
257
- temperature: this.temperature,
258
- max_tokens: this.maxTokens,
259
- top_p: this.topP,
260
- frequency_penalty: this.frequencyPenalty,
261
- presence_penalty: this.presencePenalty,
262
- n: this.n,
263
- best_of: this.bestOf,
264
- logit_bias: this.logitBias,
265
- stop: options?.stop ?? this.stopSequences,
266
- user: this.user,
267
- stream: this.streaming,
268
- ...this.modelKwargs,
269
- };
270
- }
271
- /** @ignore */
272
- _identifyingParams() {
273
- return {
274
- model_name: this.model,
275
- ...this.invocationParams(),
276
- ...this.clientConfig,
277
- };
278
- }
279
- /**
280
- * Get the identifying parameters for the model
281
- */
282
- identifyingParams() {
283
- return this._identifyingParams();
284
- }
285
- /**
286
- * Call out to OpenAI's endpoint with k unique prompts
287
- *
288
- * @param [prompts] - The prompts to pass into the model.
289
- * @param [options] - Optional list of stop words to use when generating.
290
- * @param [runManager] - Optional callback manager to use when generating.
291
- *
292
- * @returns The full LLM output.
293
- *
294
- * @example
295
- * ```ts
296
- * import { OpenAI } from "langchain/llms/openai";
297
- * const openai = new OpenAI();
298
- * const response = await openai.generate(["Tell me a joke."]);
299
- * ```
300
- */
301
- async _generate(prompts, options, runManager) {
302
- const subPrompts = (0, chunk_array_1.chunkArray)(prompts, this.batchSize);
303
- const choices = [];
304
- const tokenUsage = {};
305
- const params = this.invocationParams(options);
306
- if (params.max_tokens === -1) {
307
- if (prompts.length !== 1) {
308
- throw new Error("max_tokens set to -1 not supported for multiple inputs");
309
- }
310
- params.max_tokens = await (0, base_1.calculateMaxTokens)({
311
- prompt: prompts[0],
312
- // Cast here to allow for other models that may not fit the union
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