@langchain/openai 0.3.17 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/legacy.js DELETED
@@ -1,454 +0,0 @@
1
- import { OpenAI as OpenAIClient } from "openai";
2
- import { GenerationChunk } from "@langchain/core/outputs";
3
- import { getEnvironmentVariable } from "@langchain/core/utils/env";
4
- import { LLM } from "@langchain/core/language_models/llms";
5
- import { getEndpoint } from "./utils/azure.js";
6
- import { wrapOpenAIClientError } from "./utils/openai.js";
7
- /**
8
- * @deprecated For legacy compatibility. Use ChatOpenAI instead.
9
- *
10
- * Wrapper around OpenAI large language models that use the Chat endpoint.
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 you should have the `openai` package installed, with the
16
- * `AZURE_OPENAI_API_KEY`,
17
- * `AZURE_OPENAI_API_INSTANCE_NAME`,
18
- * `AZURE_OPENAI_API_DEPLOYMENT_NAME`
19
- * and `AZURE_OPENAI_API_VERSION` environment variable set.
20
- *
21
- * @remarks
22
- * Any parameters that are valid to be passed to {@link
23
- * https://platform.openai.com/docs/api-reference/chat/create |
24
- * `openai.createCompletion`} can be passed through {@link modelKwargs}, even
25
- * if not explicitly available on this class.
26
- *
27
- * @augments BaseLLM
28
- * @augments OpenAIInput
29
- * @augments AzureOpenAIChatInput
30
- * @example
31
- * ```typescript
32
- * const model = new OpenAIChat({
33
- * prefixMessages: [
34
- * {
35
- * role: "system",
36
- * content: "You are a helpful assistant that answers in pirate language",
37
- * },
38
- * ],
39
- * maxTokens: 50,
40
- * });
41
- *
42
- * const res = await model.invoke(
43
- * "What would be a good company name for a company that makes colorful socks?"
44
- * );
45
- * console.log({ res });
46
- * ```
47
- */
48
- export class OpenAIChat extends LLM {
49
- static lc_name() {
50
- return "OpenAIChat";
51
- }
52
- get callKeys() {
53
- return [...super.callKeys, "options", "promptIndex"];
54
- }
55
- get lc_secrets() {
56
- return {
57
- openAIApiKey: "OPENAI_API_KEY",
58
- azureOpenAIApiKey: "AZURE_OPENAI_API_KEY",
59
- organization: "OPENAI_ORGANIZATION",
60
- };
61
- }
62
- get lc_aliases() {
63
- return {
64
- modelName: "model",
65
- openAIApiKey: "openai_api_key",
66
- azureOpenAIApiVersion: "azure_openai_api_version",
67
- azureOpenAIApiKey: "azure_openai_api_key",
68
- azureOpenAIApiInstanceName: "azure_openai_api_instance_name",
69
- azureOpenAIApiDeploymentName: "azure_openai_api_deployment_name",
70
- };
71
- }
72
- constructor(fields,
73
- /** @deprecated */
74
- configuration) {
75
- super(fields ?? {});
76
- Object.defineProperty(this, "lc_serializable", {
77
- enumerable: true,
78
- configurable: true,
79
- writable: true,
80
- value: true
81
- });
82
- Object.defineProperty(this, "temperature", {
83
- enumerable: true,
84
- configurable: true,
85
- writable: true,
86
- value: 1
87
- });
88
- Object.defineProperty(this, "topP", {
89
- enumerable: true,
90
- configurable: true,
91
- writable: true,
92
- value: 1
93
- });
94
- Object.defineProperty(this, "frequencyPenalty", {
95
- enumerable: true,
96
- configurable: true,
97
- writable: true,
98
- value: 0
99
- });
100
- Object.defineProperty(this, "presencePenalty", {
101
- enumerable: true,
102
- configurable: true,
103
- writable: true,
104
- value: 0
105
- });
106
- Object.defineProperty(this, "n", {
107
- enumerable: true,
108
- configurable: true,
109
- writable: true,
110
- value: 1
111
- });
112
- Object.defineProperty(this, "logitBias", {
113
- enumerable: true,
114
- configurable: true,
115
- writable: true,
116
- value: void 0
117
- });
118
- Object.defineProperty(this, "maxTokens", {
119
- enumerable: true,
120
- configurable: true,
121
- writable: true,
122
- value: void 0
123
- });
124
- Object.defineProperty(this, "modelName", {
125
- enumerable: true,
126
- configurable: true,
127
- writable: true,
128
- value: "gpt-3.5-turbo"
129
- });
130
- Object.defineProperty(this, "model", {
131
- enumerable: true,
132
- configurable: true,
133
- writable: true,
134
- value: "gpt-3.5-turbo"
135
- });
136
- Object.defineProperty(this, "prefixMessages", {
137
- enumerable: true,
138
- configurable: true,
139
- writable: true,
140
- value: void 0
141
- });
142
- Object.defineProperty(this, "modelKwargs", {
143
- enumerable: true,
144
- configurable: true,
145
- writable: true,
146
- value: void 0
147
- });
148
- Object.defineProperty(this, "timeout", {
149
- enumerable: true,
150
- configurable: true,
151
- writable: true,
152
- value: void 0
153
- });
154
- Object.defineProperty(this, "stop", {
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, "azureOpenAIApiVersion", {
179
- enumerable: true,
180
- configurable: true,
181
- writable: true,
182
- value: void 0
183
- });
184
- Object.defineProperty(this, "azureOpenAIApiKey", {
185
- enumerable: true,
186
- configurable: true,
187
- writable: true,
188
- value: void 0
189
- });
190
- Object.defineProperty(this, "azureOpenAIApiInstanceName", {
191
- enumerable: true,
192
- configurable: true,
193
- writable: true,
194
- value: void 0
195
- });
196
- Object.defineProperty(this, "azureOpenAIApiDeploymentName", {
197
- enumerable: true,
198
- configurable: true,
199
- writable: true,
200
- value: void 0
201
- });
202
- Object.defineProperty(this, "azureOpenAIBasePath", {
203
- enumerable: true,
204
- configurable: true,
205
- writable: true,
206
- value: void 0
207
- });
208
- Object.defineProperty(this, "organization", {
209
- enumerable: true,
210
- configurable: true,
211
- writable: true,
212
- value: void 0
213
- });
214
- Object.defineProperty(this, "client", {
215
- enumerable: true,
216
- configurable: true,
217
- writable: true,
218
- value: void 0
219
- });
220
- Object.defineProperty(this, "clientConfig", {
221
- enumerable: true,
222
- configurable: true,
223
- writable: true,
224
- value: void 0
225
- });
226
- this.openAIApiKey =
227
- fields?.apiKey ??
228
- fields?.openAIApiKey ??
229
- getEnvironmentVariable("OPENAI_API_KEY");
230
- this.azureOpenAIApiKey =
231
- fields?.azureOpenAIApiKey ??
232
- getEnvironmentVariable("AZURE_OPENAI_API_KEY");
233
- if (!this.azureOpenAIApiKey && !this.openAIApiKey) {
234
- throw new Error("OpenAI or Azure OpenAI API key not found");
235
- }
236
- this.azureOpenAIApiInstanceName =
237
- fields?.azureOpenAIApiInstanceName ??
238
- getEnvironmentVariable("AZURE_OPENAI_API_INSTANCE_NAME");
239
- this.azureOpenAIApiDeploymentName =
240
- (fields?.azureOpenAIApiCompletionsDeploymentName ||
241
- fields?.azureOpenAIApiDeploymentName) ??
242
- (getEnvironmentVariable("AZURE_OPENAI_API_COMPLETIONS_DEPLOYMENT_NAME") ||
243
- getEnvironmentVariable("AZURE_OPENAI_API_DEPLOYMENT_NAME"));
244
- this.azureOpenAIApiVersion =
245
- fields?.azureOpenAIApiVersion ??
246
- getEnvironmentVariable("AZURE_OPENAI_API_VERSION");
247
- this.azureOpenAIBasePath =
248
- fields?.azureOpenAIBasePath ??
249
- getEnvironmentVariable("AZURE_OPENAI_BASE_PATH");
250
- this.organization =
251
- fields?.configuration?.organization ??
252
- getEnvironmentVariable("OPENAI_ORGANIZATION");
253
- this.modelName = fields?.model ?? fields?.modelName ?? this.modelName;
254
- this.prefixMessages = fields?.prefixMessages ?? this.prefixMessages;
255
- this.modelKwargs = fields?.modelKwargs ?? {};
256
- this.timeout = fields?.timeout;
257
- this.temperature = fields?.temperature ?? this.temperature;
258
- this.topP = fields?.topP ?? this.topP;
259
- this.frequencyPenalty = fields?.frequencyPenalty ?? this.frequencyPenalty;
260
- this.presencePenalty = fields?.presencePenalty ?? this.presencePenalty;
261
- this.n = fields?.n ?? this.n;
262
- this.logitBias = fields?.logitBias;
263
- this.maxTokens = fields?.maxTokens;
264
- this.stop = fields?.stop;
265
- this.user = fields?.user;
266
- this.streaming = fields?.streaming ?? false;
267
- if (this.n > 1) {
268
- throw new Error("Cannot use n > 1 in OpenAIChat LLM. Use ChatOpenAI Chat Model instead.");
269
- }
270
- if (this.azureOpenAIApiKey) {
271
- if (!this.azureOpenAIApiInstanceName && !this.azureOpenAIBasePath) {
272
- throw new Error("Azure OpenAI API instance name not found");
273
- }
274
- if (!this.azureOpenAIApiDeploymentName) {
275
- throw new Error("Azure OpenAI API deployment name not found");
276
- }
277
- if (!this.azureOpenAIApiVersion) {
278
- throw new Error("Azure OpenAI API version not found");
279
- }
280
- this.openAIApiKey = this.openAIApiKey ?? "";
281
- }
282
- this.clientConfig = {
283
- apiKey: this.openAIApiKey,
284
- organization: this.organization,
285
- baseURL: configuration?.basePath ?? fields?.configuration?.basePath,
286
- dangerouslyAllowBrowser: true,
287
- defaultHeaders: configuration?.baseOptions?.headers ??
288
- fields?.configuration?.baseOptions?.headers,
289
- defaultQuery: configuration?.baseOptions?.params ??
290
- fields?.configuration?.baseOptions?.params,
291
- ...configuration,
292
- ...fields?.configuration,
293
- };
294
- }
295
- /**
296
- * Get the parameters used to invoke the model
297
- */
298
- invocationParams(options) {
299
- return {
300
- model: this.modelName,
301
- temperature: this.temperature,
302
- top_p: this.topP,
303
- frequency_penalty: this.frequencyPenalty,
304
- presence_penalty: this.presencePenalty,
305
- n: this.n,
306
- logit_bias: this.logitBias,
307
- max_tokens: this.maxTokens === -1 ? undefined : this.maxTokens,
308
- stop: options?.stop ?? this.stop,
309
- user: this.user,
310
- stream: this.streaming,
311
- ...this.modelKwargs,
312
- };
313
- }
314
- /** @ignore */
315
- _identifyingParams() {
316
- return {
317
- model_name: this.modelName,
318
- ...this.invocationParams(),
319
- ...this.clientConfig,
320
- };
321
- }
322
- /**
323
- * Get the identifying parameters for the model
324
- */
325
- identifyingParams() {
326
- return {
327
- model_name: this.modelName,
328
- ...this.invocationParams(),
329
- ...this.clientConfig,
330
- };
331
- }
332
- /**
333
- * Formats the messages for the OpenAI API.
334
- * @param prompt The prompt to be formatted.
335
- * @returns Array of formatted messages.
336
- */
337
- formatMessages(prompt) {
338
- const message = {
339
- role: "user",
340
- content: prompt,
341
- };
342
- return this.prefixMessages ? [...this.prefixMessages, message] : [message];
343
- }
344
- async *_streamResponseChunks(prompt, options, runManager) {
345
- const params = {
346
- ...this.invocationParams(options),
347
- messages: this.formatMessages(prompt),
348
- stream: true,
349
- };
350
- const stream = await this.completionWithRetry(params, options);
351
- for await (const data of stream) {
352
- const choice = data?.choices[0];
353
- if (!choice) {
354
- continue;
355
- }
356
- const { delta } = choice;
357
- const generationChunk = new GenerationChunk({
358
- text: delta.content ?? "",
359
- });
360
- yield generationChunk;
361
- const newTokenIndices = {
362
- prompt: options.promptIndex ?? 0,
363
- completion: choice.index ?? 0,
364
- };
365
- // eslint-disable-next-line no-void
366
- void runManager?.handleLLMNewToken(generationChunk.text ?? "", newTokenIndices);
367
- }
368
- if (options.signal?.aborted) {
369
- throw new Error("AbortError");
370
- }
371
- }
372
- /** @ignore */
373
- async _call(prompt, options, runManager) {
374
- const params = this.invocationParams(options);
375
- if (params.stream) {
376
- const stream = await this._streamResponseChunks(prompt, options, runManager);
377
- let finalChunk;
378
- for await (const chunk of stream) {
379
- if (finalChunk === undefined) {
380
- finalChunk = chunk;
381
- }
382
- else {
383
- finalChunk = finalChunk.concat(chunk);
384
- }
385
- }
386
- return finalChunk?.text ?? "";
387
- }
388
- else {
389
- const response = await this.completionWithRetry({
390
- ...params,
391
- stream: false,
392
- messages: this.formatMessages(prompt),
393
- }, {
394
- signal: options.signal,
395
- ...options.options,
396
- });
397
- return response?.choices[0]?.message?.content ?? "";
398
- }
399
- }
400
- async completionWithRetry(request, options) {
401
- const requestOptions = this._getClientOptions(options);
402
- return this.caller.call(async () => {
403
- try {
404
- const res = await this.client.chat.completions.create(request, requestOptions);
405
- return res;
406
- }
407
- catch (e) {
408
- const error = wrapOpenAIClientError(e);
409
- throw error;
410
- }
411
- });
412
- }
413
- /** @ignore */
414
- _getClientOptions(options) {
415
- if (!this.client) {
416
- const openAIEndpointConfig = {
417
- azureOpenAIApiDeploymentName: this.azureOpenAIApiDeploymentName,
418
- azureOpenAIApiInstanceName: this.azureOpenAIApiInstanceName,
419
- azureOpenAIApiKey: this.azureOpenAIApiKey,
420
- azureOpenAIBasePath: this.azureOpenAIBasePath,
421
- baseURL: this.clientConfig.baseURL,
422
- };
423
- const endpoint = getEndpoint(openAIEndpointConfig);
424
- const params = {
425
- ...this.clientConfig,
426
- baseURL: endpoint,
427
- timeout: this.timeout,
428
- maxRetries: 0,
429
- };
430
- if (!params.baseURL) {
431
- delete params.baseURL;
432
- }
433
- this.client = new OpenAIClient(params);
434
- }
435
- const requestOptions = {
436
- ...this.clientConfig,
437
- ...options,
438
- };
439
- if (this.azureOpenAIApiKey) {
440
- requestOptions.headers = {
441
- "api-key": this.azureOpenAIApiKey,
442
- ...requestOptions.headers,
443
- };
444
- requestOptions.query = {
445
- "api-version": this.azureOpenAIApiVersion,
446
- ...requestOptions.query,
447
- };
448
- }
449
- return requestOptions;
450
- }
451
- _llmType() {
452
- return "openai";
453
- }
454
- }