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