@langchain/openai 0.4.0-rc.0 → 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/azure/chat_models.cjs +67 -16
- package/dist/azure/chat_models.d.ts +14 -3
- package/dist/azure/chat_models.js +67 -16
- package/dist/azure/embeddings.cjs +60 -12
- package/dist/azure/embeddings.d.ts +8 -2
- package/dist/azure/embeddings.js +60 -12
- package/dist/azure/llms.cjs +82 -10
- package/dist/azure/llms.d.ts +12 -2
- package/dist/azure/llms.js +82 -10
- package/dist/chat_models.cjs +22 -112
- package/dist/chat_models.d.ts +14 -20
- package/dist/chat_models.js +22 -112
- package/dist/embeddings.cjs +13 -99
- package/dist/embeddings.d.ts +11 -18
- package/dist/embeddings.js +13 -99
- package/dist/llms.cjs +28 -131
- package/dist/llms.d.ts +14 -27
- package/dist/llms.js +27 -130
- package/dist/types.d.ts +1 -12
- package/package.json +1 -1
- package/dist/legacy.cjs +0 -458
- package/dist/legacy.d.ts +0 -127
- package/dist/legacy.js +0 -454
package/dist/llms.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.OpenAI =
|
|
3
|
+
exports.OpenAI = void 0;
|
|
4
4
|
const openai_1 = require("openai");
|
|
5
5
|
const base_1 = require("@langchain/core/language_models/base");
|
|
6
6
|
const outputs_1 = require("@langchain/core/outputs");
|
|
@@ -8,8 +8,6 @@ const env_1 = require("@langchain/core/utils/env");
|
|
|
8
8
|
const llms_1 = require("@langchain/core/language_models/llms");
|
|
9
9
|
const chunk_array_1 = require("@langchain/core/utils/chunk_array");
|
|
10
10
|
const azure_js_1 = require("./utils/azure.cjs");
|
|
11
|
-
const legacy_js_1 = require("./legacy.cjs");
|
|
12
|
-
Object.defineProperty(exports, "OpenAIChat", { enumerable: true, get: function () { return legacy_js_1.OpenAIChat; } });
|
|
13
11
|
const openai_js_1 = require("./utils/openai.cjs");
|
|
14
12
|
/**
|
|
15
13
|
* Wrapper around OpenAI large language models.
|
|
@@ -17,11 +15,7 @@ const openai_js_1 = require("./utils/openai.cjs");
|
|
|
17
15
|
* To use you should have the `openai` package installed, with the
|
|
18
16
|
* `OPENAI_API_KEY` environment variable set.
|
|
19
17
|
*
|
|
20
|
-
* To use with Azure
|
|
21
|
-
* `AZURE_OPENAI_API_KEY`,
|
|
22
|
-
* `AZURE_OPENAI_API_INSTANCE_NAME`,
|
|
23
|
-
* `AZURE_OPENAI_API_DEPLOYMENT_NAME`
|
|
24
|
-
* and `AZURE_OPENAI_API_VERSION` environment variable set.
|
|
18
|
+
* To use with Azure, import the `AzureOpenAI` class.
|
|
25
19
|
*
|
|
26
20
|
* @remarks
|
|
27
21
|
* Any parameters that are valid to be passed to {@link
|
|
@@ -54,7 +48,6 @@ class OpenAI extends llms_1.BaseLLM {
|
|
|
54
48
|
return {
|
|
55
49
|
openAIApiKey: "OPENAI_API_KEY",
|
|
56
50
|
apiKey: "OPENAI_API_KEY",
|
|
57
|
-
azureOpenAIApiKey: "AZURE_OPENAI_API_KEY",
|
|
58
51
|
organization: "OPENAI_ORGANIZATION",
|
|
59
52
|
};
|
|
60
53
|
}
|
|
@@ -63,30 +56,9 @@ class OpenAI extends llms_1.BaseLLM {
|
|
|
63
56
|
modelName: "model",
|
|
64
57
|
openAIApiKey: "openai_api_key",
|
|
65
58
|
apiKey: "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
59
|
};
|
|
71
60
|
}
|
|
72
|
-
constructor(fields
|
|
73
|
-
/** @deprecated */
|
|
74
|
-
configuration) {
|
|
75
|
-
let model = fields?.model ?? fields?.modelName;
|
|
76
|
-
if ((model?.startsWith("gpt-3.5-turbo") || model?.startsWith("gpt-4")) &&
|
|
77
|
-
!model?.includes("-instruct")) {
|
|
78
|
-
console.warn([
|
|
79
|
-
`Your chosen OpenAI model, "${model}", is a chat model and not a text-in/text-out LLM.`,
|
|
80
|
-
`Passing it into the "OpenAI" class is deprecated and only permitted for backwards-compatibility. You may experience odd behavior.`,
|
|
81
|
-
`Please use the "ChatOpenAI" class instead.`,
|
|
82
|
-
"",
|
|
83
|
-
`See this page for more information:`,
|
|
84
|
-
"|",
|
|
85
|
-
`└> https://js.langchain.com/docs/integrations/chat/openai`,
|
|
86
|
-
].join("\n"));
|
|
87
|
-
// eslint-disable-next-line no-constructor-return
|
|
88
|
-
return new legacy_js_1.OpenAIChat(fields, configuration);
|
|
89
|
-
}
|
|
61
|
+
constructor(fields) {
|
|
90
62
|
super(fields ?? {});
|
|
91
63
|
Object.defineProperty(this, "lc_serializable", {
|
|
92
64
|
enumerable: true,
|
|
@@ -98,31 +70,31 @@ class OpenAI extends llms_1.BaseLLM {
|
|
|
98
70
|
enumerable: true,
|
|
99
71
|
configurable: true,
|
|
100
72
|
writable: true,
|
|
101
|
-
value: 0
|
|
73
|
+
value: void 0
|
|
102
74
|
});
|
|
103
75
|
Object.defineProperty(this, "maxTokens", {
|
|
104
76
|
enumerable: true,
|
|
105
77
|
configurable: true,
|
|
106
78
|
writable: true,
|
|
107
|
-
value:
|
|
79
|
+
value: void 0
|
|
108
80
|
});
|
|
109
81
|
Object.defineProperty(this, "topP", {
|
|
110
82
|
enumerable: true,
|
|
111
83
|
configurable: true,
|
|
112
84
|
writable: true,
|
|
113
|
-
value:
|
|
85
|
+
value: void 0
|
|
114
86
|
});
|
|
115
87
|
Object.defineProperty(this, "frequencyPenalty", {
|
|
116
88
|
enumerable: true,
|
|
117
89
|
configurable: true,
|
|
118
90
|
writable: true,
|
|
119
|
-
value: 0
|
|
91
|
+
value: void 0
|
|
120
92
|
});
|
|
121
93
|
Object.defineProperty(this, "presencePenalty", {
|
|
122
94
|
enumerable: true,
|
|
123
95
|
configurable: true,
|
|
124
96
|
writable: true,
|
|
125
|
-
value: 0
|
|
97
|
+
value: void 0
|
|
126
98
|
});
|
|
127
99
|
Object.defineProperty(this, "n", {
|
|
128
100
|
enumerable: true,
|
|
@@ -142,17 +114,18 @@ class OpenAI extends llms_1.BaseLLM {
|
|
|
142
114
|
writable: true,
|
|
143
115
|
value: void 0
|
|
144
116
|
});
|
|
145
|
-
Object.defineProperty(this, "
|
|
117
|
+
Object.defineProperty(this, "model", {
|
|
146
118
|
enumerable: true,
|
|
147
119
|
configurable: true,
|
|
148
120
|
writable: true,
|
|
149
121
|
value: "gpt-3.5-turbo-instruct"
|
|
150
122
|
});
|
|
151
|
-
|
|
123
|
+
/** @deprecated Use "model" instead */
|
|
124
|
+
Object.defineProperty(this, "modelName", {
|
|
152
125
|
enumerable: true,
|
|
153
126
|
configurable: true,
|
|
154
127
|
writable: true,
|
|
155
|
-
value:
|
|
128
|
+
value: void 0
|
|
156
129
|
});
|
|
157
130
|
Object.defineProperty(this, "modelKwargs", {
|
|
158
131
|
enumerable: true,
|
|
@@ -208,42 +181,6 @@ class OpenAI extends llms_1.BaseLLM {
|
|
|
208
181
|
writable: true,
|
|
209
182
|
value: void 0
|
|
210
183
|
});
|
|
211
|
-
Object.defineProperty(this, "azureOpenAIApiVersion", {
|
|
212
|
-
enumerable: true,
|
|
213
|
-
configurable: true,
|
|
214
|
-
writable: true,
|
|
215
|
-
value: void 0
|
|
216
|
-
});
|
|
217
|
-
Object.defineProperty(this, "azureOpenAIApiKey", {
|
|
218
|
-
enumerable: true,
|
|
219
|
-
configurable: true,
|
|
220
|
-
writable: true,
|
|
221
|
-
value: void 0
|
|
222
|
-
});
|
|
223
|
-
Object.defineProperty(this, "azureADTokenProvider", {
|
|
224
|
-
enumerable: true,
|
|
225
|
-
configurable: true,
|
|
226
|
-
writable: true,
|
|
227
|
-
value: void 0
|
|
228
|
-
});
|
|
229
|
-
Object.defineProperty(this, "azureOpenAIApiInstanceName", {
|
|
230
|
-
enumerable: true,
|
|
231
|
-
configurable: true,
|
|
232
|
-
writable: true,
|
|
233
|
-
value: void 0
|
|
234
|
-
});
|
|
235
|
-
Object.defineProperty(this, "azureOpenAIApiDeploymentName", {
|
|
236
|
-
enumerable: true,
|
|
237
|
-
configurable: true,
|
|
238
|
-
writable: true,
|
|
239
|
-
value: void 0
|
|
240
|
-
});
|
|
241
|
-
Object.defineProperty(this, "azureOpenAIBasePath", {
|
|
242
|
-
enumerable: true,
|
|
243
|
-
configurable: true,
|
|
244
|
-
writable: true,
|
|
245
|
-
value: void 0
|
|
246
|
-
});
|
|
247
184
|
Object.defineProperty(this, "organization", {
|
|
248
185
|
enumerable: true,
|
|
249
186
|
configurable: true,
|
|
@@ -262,38 +199,30 @@ class OpenAI extends llms_1.BaseLLM {
|
|
|
262
199
|
writable: true,
|
|
263
200
|
value: void 0
|
|
264
201
|
});
|
|
265
|
-
model = model ?? this.model;
|
|
266
202
|
this.openAIApiKey =
|
|
267
203
|
fields?.apiKey ??
|
|
268
204
|
fields?.openAIApiKey ??
|
|
269
205
|
(0, env_1.getEnvironmentVariable)("OPENAI_API_KEY");
|
|
270
206
|
this.apiKey = this.openAIApiKey;
|
|
271
|
-
this.azureOpenAIApiKey =
|
|
272
|
-
fields?.azureOpenAIApiKey ??
|
|
273
|
-
(0, env_1.getEnvironmentVariable)("AZURE_OPENAI_API_KEY");
|
|
274
|
-
this.azureADTokenProvider = fields?.azureADTokenProvider ?? undefined;
|
|
275
|
-
if (!this.azureOpenAIApiKey && !this.apiKey && !this.azureADTokenProvider) {
|
|
276
|
-
throw new Error("OpenAI or Azure OpenAI API key or Token Provider not found");
|
|
277
|
-
}
|
|
278
|
-
this.azureOpenAIApiInstanceName =
|
|
279
|
-
fields?.azureOpenAIApiInstanceName ??
|
|
280
|
-
(0, env_1.getEnvironmentVariable)("AZURE_OPENAI_API_INSTANCE_NAME");
|
|
281
|
-
this.azureOpenAIApiDeploymentName =
|
|
282
|
-
(fields?.azureOpenAIApiCompletionsDeploymentName ||
|
|
283
|
-
fields?.azureOpenAIApiDeploymentName) ??
|
|
284
|
-
((0, env_1.getEnvironmentVariable)("AZURE_OPENAI_API_COMPLETIONS_DEPLOYMENT_NAME") ||
|
|
285
|
-
(0, env_1.getEnvironmentVariable)("AZURE_OPENAI_API_DEPLOYMENT_NAME"));
|
|
286
|
-
this.azureOpenAIApiVersion =
|
|
287
|
-
fields?.azureOpenAIApiVersion ??
|
|
288
|
-
(0, env_1.getEnvironmentVariable)("AZURE_OPENAI_API_VERSION");
|
|
289
|
-
this.azureOpenAIBasePath =
|
|
290
|
-
fields?.azureOpenAIBasePath ??
|
|
291
|
-
(0, env_1.getEnvironmentVariable)("AZURE_OPENAI_BASE_PATH");
|
|
292
207
|
this.organization =
|
|
293
208
|
fields?.configuration?.organization ??
|
|
294
209
|
(0, env_1.getEnvironmentVariable)("OPENAI_ORGANIZATION");
|
|
295
|
-
this.
|
|
296
|
-
this.model
|
|
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;
|
|
297
226
|
this.modelKwargs = fields?.modelKwargs ?? {};
|
|
298
227
|
this.batchSize = fields?.batchSize ?? this.batchSize;
|
|
299
228
|
this.timeout = fields?.timeout;
|
|
@@ -312,28 +241,10 @@ class OpenAI extends llms_1.BaseLLM {
|
|
|
312
241
|
if (this.streaming && this.bestOf && this.bestOf > 1) {
|
|
313
242
|
throw new Error("Cannot stream results when bestOf > 1");
|
|
314
243
|
}
|
|
315
|
-
if (this.azureOpenAIApiKey || this.azureADTokenProvider) {
|
|
316
|
-
if (!this.azureOpenAIApiInstanceName && !this.azureOpenAIBasePath) {
|
|
317
|
-
throw new Error("Azure OpenAI API instance name not found");
|
|
318
|
-
}
|
|
319
|
-
if (!this.azureOpenAIApiDeploymentName) {
|
|
320
|
-
throw new Error("Azure OpenAI API deployment name not found");
|
|
321
|
-
}
|
|
322
|
-
if (!this.azureOpenAIApiVersion) {
|
|
323
|
-
throw new Error("Azure OpenAI API version not found");
|
|
324
|
-
}
|
|
325
|
-
this.apiKey = this.apiKey ?? "";
|
|
326
|
-
}
|
|
327
244
|
this.clientConfig = {
|
|
328
245
|
apiKey: this.apiKey,
|
|
329
246
|
organization: this.organization,
|
|
330
|
-
baseURL: configuration?.basePath ?? fields?.configuration?.basePath,
|
|
331
247
|
dangerouslyAllowBrowser: true,
|
|
332
|
-
defaultHeaders: configuration?.baseOptions?.headers ??
|
|
333
|
-
fields?.configuration?.baseOptions?.headers,
|
|
334
|
-
defaultQuery: configuration?.baseOptions?.params ??
|
|
335
|
-
fields?.configuration?.baseOptions?.params,
|
|
336
|
-
...configuration,
|
|
337
248
|
...fields?.configuration,
|
|
338
249
|
};
|
|
339
250
|
}
|
|
@@ -532,10 +443,6 @@ class OpenAI extends llms_1.BaseLLM {
|
|
|
532
443
|
_getClientOptions(options) {
|
|
533
444
|
if (!this.client) {
|
|
534
445
|
const openAIEndpointConfig = {
|
|
535
|
-
azureOpenAIApiDeploymentName: this.azureOpenAIApiDeploymentName,
|
|
536
|
-
azureOpenAIApiInstanceName: this.azureOpenAIApiInstanceName,
|
|
537
|
-
azureOpenAIApiKey: this.azureOpenAIApiKey,
|
|
538
|
-
azureOpenAIBasePath: this.azureOpenAIBasePath,
|
|
539
446
|
baseURL: this.clientConfig.baseURL,
|
|
540
447
|
};
|
|
541
448
|
const endpoint = (0, azure_js_1.getEndpoint)(openAIEndpointConfig);
|
|
@@ -554,16 +461,6 @@ class OpenAI extends llms_1.BaseLLM {
|
|
|
554
461
|
...this.clientConfig,
|
|
555
462
|
...options,
|
|
556
463
|
};
|
|
557
|
-
if (this.azureOpenAIApiKey) {
|
|
558
|
-
requestOptions.headers = {
|
|
559
|
-
"api-key": this.azureOpenAIApiKey,
|
|
560
|
-
...requestOptions.headers,
|
|
561
|
-
};
|
|
562
|
-
requestOptions.query = {
|
|
563
|
-
"api-version": this.azureOpenAIApiVersion,
|
|
564
|
-
...requestOptions.query,
|
|
565
|
-
};
|
|
566
|
-
}
|
|
567
464
|
return requestOptions;
|
|
568
465
|
}
|
|
569
466
|
_llmType() {
|
package/dist/llms.d.ts
CHANGED
|
@@ -2,21 +2,15 @@ import { type ClientOptions, OpenAI as OpenAIClient } from "openai";
|
|
|
2
2
|
import { CallbackManagerForLLMRun } from "@langchain/core/callbacks/manager";
|
|
3
3
|
import { GenerationChunk, type LLMResult } from "@langchain/core/outputs";
|
|
4
4
|
import { BaseLLM, type BaseLLMParams } from "@langchain/core/language_models/llms";
|
|
5
|
-
import type {
|
|
6
|
-
|
|
7
|
-
export type { AzureOpenAIInput, OpenAICallOptions, OpenAIInput, OpenAIChatCallOptions, };
|
|
8
|
-
export { OpenAIChat };
|
|
5
|
+
import type { OpenAICallOptions, OpenAICoreRequestOptions, OpenAIInput } from "./types.js";
|
|
6
|
+
export type { OpenAICallOptions, OpenAIInput };
|
|
9
7
|
/**
|
|
10
8
|
* Wrapper around OpenAI large language models.
|
|
11
9
|
*
|
|
12
10
|
* To use you should have the `openai` package installed, with the
|
|
13
11
|
* `OPENAI_API_KEY` environment variable set.
|
|
14
12
|
*
|
|
15
|
-
* To use with Azure
|
|
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.
|
|
13
|
+
* To use with Azure, import the `AzureOpenAI` class.
|
|
20
14
|
*
|
|
21
15
|
* @remarks
|
|
22
16
|
* Any parameters that are valid to be passed to {@link
|
|
@@ -38,7 +32,7 @@ export { OpenAIChat };
|
|
|
38
32
|
* console.log({ res });
|
|
39
33
|
* ```
|
|
40
34
|
*/
|
|
41
|
-
export declare class OpenAI<CallOptions extends OpenAICallOptions = OpenAICallOptions> extends BaseLLM<CallOptions> implements OpenAIInput
|
|
35
|
+
export declare class OpenAI<CallOptions extends OpenAICallOptions = OpenAICallOptions> extends BaseLLM<CallOptions> implements Partial<OpenAIInput> {
|
|
42
36
|
static lc_name(): string;
|
|
43
37
|
get callKeys(): string[];
|
|
44
38
|
lc_serializable: boolean;
|
|
@@ -46,16 +40,17 @@ export declare class OpenAI<CallOptions extends OpenAICallOptions = OpenAICallOp
|
|
|
46
40
|
[key: string]: string;
|
|
47
41
|
} | undefined;
|
|
48
42
|
get lc_aliases(): Record<string, string>;
|
|
49
|
-
temperature
|
|
50
|
-
maxTokens
|
|
51
|
-
topP
|
|
52
|
-
frequencyPenalty
|
|
53
|
-
presencePenalty
|
|
43
|
+
temperature?: number;
|
|
44
|
+
maxTokens?: number;
|
|
45
|
+
topP?: number;
|
|
46
|
+
frequencyPenalty?: number;
|
|
47
|
+
presencePenalty?: number;
|
|
54
48
|
n: number;
|
|
55
49
|
bestOf?: number;
|
|
56
50
|
logitBias?: Record<string, number>;
|
|
57
|
-
modelName: string;
|
|
58
51
|
model: string;
|
|
52
|
+
/** @deprecated Use "model" instead */
|
|
53
|
+
modelName: string;
|
|
59
54
|
modelKwargs?: OpenAIInput["modelKwargs"];
|
|
60
55
|
batchSize: number;
|
|
61
56
|
timeout?: number;
|
|
@@ -65,20 +60,12 @@ export declare class OpenAI<CallOptions extends OpenAICallOptions = OpenAICallOp
|
|
|
65
60
|
streaming: boolean;
|
|
66
61
|
openAIApiKey?: string;
|
|
67
62
|
apiKey?: string;
|
|
68
|
-
azureOpenAIApiVersion?: string;
|
|
69
|
-
azureOpenAIApiKey?: string;
|
|
70
|
-
azureADTokenProvider?: () => Promise<string>;
|
|
71
|
-
azureOpenAIApiInstanceName?: string;
|
|
72
|
-
azureOpenAIApiDeploymentName?: string;
|
|
73
|
-
azureOpenAIBasePath?: string;
|
|
74
63
|
organization?: string;
|
|
75
64
|
protected client: OpenAIClient;
|
|
76
65
|
protected clientConfig: ClientOptions;
|
|
77
|
-
constructor(fields?: Partial<OpenAIInput> &
|
|
78
|
-
configuration?: ClientOptions
|
|
79
|
-
}
|
|
80
|
-
/** @deprecated */
|
|
81
|
-
configuration?: ClientOptions & LegacyOpenAIInput);
|
|
66
|
+
constructor(fields?: Partial<OpenAIInput> & BaseLLMParams & {
|
|
67
|
+
configuration?: ClientOptions;
|
|
68
|
+
});
|
|
82
69
|
/**
|
|
83
70
|
* Get the parameters used to invoke the model
|
|
84
71
|
*/
|
package/dist/llms.js
CHANGED
|
@@ -5,20 +5,14 @@ import { getEnvironmentVariable } from "@langchain/core/utils/env";
|
|
|
5
5
|
import { BaseLLM, } from "@langchain/core/language_models/llms";
|
|
6
6
|
import { chunkArray } from "@langchain/core/utils/chunk_array";
|
|
7
7
|
import { getEndpoint } from "./utils/azure.js";
|
|
8
|
-
import { OpenAIChat } from "./legacy.js";
|
|
9
8
|
import { wrapOpenAIClientError } from "./utils/openai.js";
|
|
10
|
-
export { OpenAIChat };
|
|
11
9
|
/**
|
|
12
10
|
* Wrapper around OpenAI large language models.
|
|
13
11
|
*
|
|
14
12
|
* To use you should have the `openai` package installed, with the
|
|
15
13
|
* `OPENAI_API_KEY` environment variable set.
|
|
16
14
|
*
|
|
17
|
-
* To use with Azure
|
|
18
|
-
* `AZURE_OPENAI_API_KEY`,
|
|
19
|
-
* `AZURE_OPENAI_API_INSTANCE_NAME`,
|
|
20
|
-
* `AZURE_OPENAI_API_DEPLOYMENT_NAME`
|
|
21
|
-
* and `AZURE_OPENAI_API_VERSION` environment variable set.
|
|
15
|
+
* To use with Azure, import the `AzureOpenAI` class.
|
|
22
16
|
*
|
|
23
17
|
* @remarks
|
|
24
18
|
* Any parameters that are valid to be passed to {@link
|
|
@@ -51,7 +45,6 @@ export class OpenAI extends BaseLLM {
|
|
|
51
45
|
return {
|
|
52
46
|
openAIApiKey: "OPENAI_API_KEY",
|
|
53
47
|
apiKey: "OPENAI_API_KEY",
|
|
54
|
-
azureOpenAIApiKey: "AZURE_OPENAI_API_KEY",
|
|
55
48
|
organization: "OPENAI_ORGANIZATION",
|
|
56
49
|
};
|
|
57
50
|
}
|
|
@@ -60,30 +53,9 @@ export class OpenAI extends BaseLLM {
|
|
|
60
53
|
modelName: "model",
|
|
61
54
|
openAIApiKey: "openai_api_key",
|
|
62
55
|
apiKey: "openai_api_key",
|
|
63
|
-
azureOpenAIApiVersion: "azure_openai_api_version",
|
|
64
|
-
azureOpenAIApiKey: "azure_openai_api_key",
|
|
65
|
-
azureOpenAIApiInstanceName: "azure_openai_api_instance_name",
|
|
66
|
-
azureOpenAIApiDeploymentName: "azure_openai_api_deployment_name",
|
|
67
56
|
};
|
|
68
57
|
}
|
|
69
|
-
constructor(fields
|
|
70
|
-
/** @deprecated */
|
|
71
|
-
configuration) {
|
|
72
|
-
let model = fields?.model ?? fields?.modelName;
|
|
73
|
-
if ((model?.startsWith("gpt-3.5-turbo") || model?.startsWith("gpt-4")) &&
|
|
74
|
-
!model?.includes("-instruct")) {
|
|
75
|
-
console.warn([
|
|
76
|
-
`Your chosen OpenAI model, "${model}", is a chat model and not a text-in/text-out LLM.`,
|
|
77
|
-
`Passing it into the "OpenAI" class is deprecated and only permitted for backwards-compatibility. You may experience odd behavior.`,
|
|
78
|
-
`Please use the "ChatOpenAI" class instead.`,
|
|
79
|
-
"",
|
|
80
|
-
`See this page for more information:`,
|
|
81
|
-
"|",
|
|
82
|
-
`└> https://js.langchain.com/docs/integrations/chat/openai`,
|
|
83
|
-
].join("\n"));
|
|
84
|
-
// eslint-disable-next-line no-constructor-return
|
|
85
|
-
return new OpenAIChat(fields, configuration);
|
|
86
|
-
}
|
|
58
|
+
constructor(fields) {
|
|
87
59
|
super(fields ?? {});
|
|
88
60
|
Object.defineProperty(this, "lc_serializable", {
|
|
89
61
|
enumerable: true,
|
|
@@ -95,31 +67,31 @@ export class OpenAI extends BaseLLM {
|
|
|
95
67
|
enumerable: true,
|
|
96
68
|
configurable: true,
|
|
97
69
|
writable: true,
|
|
98
|
-
value: 0
|
|
70
|
+
value: void 0
|
|
99
71
|
});
|
|
100
72
|
Object.defineProperty(this, "maxTokens", {
|
|
101
73
|
enumerable: true,
|
|
102
74
|
configurable: true,
|
|
103
75
|
writable: true,
|
|
104
|
-
value:
|
|
76
|
+
value: void 0
|
|
105
77
|
});
|
|
106
78
|
Object.defineProperty(this, "topP", {
|
|
107
79
|
enumerable: true,
|
|
108
80
|
configurable: true,
|
|
109
81
|
writable: true,
|
|
110
|
-
value:
|
|
82
|
+
value: void 0
|
|
111
83
|
});
|
|
112
84
|
Object.defineProperty(this, "frequencyPenalty", {
|
|
113
85
|
enumerable: true,
|
|
114
86
|
configurable: true,
|
|
115
87
|
writable: true,
|
|
116
|
-
value: 0
|
|
88
|
+
value: void 0
|
|
117
89
|
});
|
|
118
90
|
Object.defineProperty(this, "presencePenalty", {
|
|
119
91
|
enumerable: true,
|
|
120
92
|
configurable: true,
|
|
121
93
|
writable: true,
|
|
122
|
-
value: 0
|
|
94
|
+
value: void 0
|
|
123
95
|
});
|
|
124
96
|
Object.defineProperty(this, "n", {
|
|
125
97
|
enumerable: true,
|
|
@@ -139,17 +111,18 @@ export class OpenAI extends BaseLLM {
|
|
|
139
111
|
writable: true,
|
|
140
112
|
value: void 0
|
|
141
113
|
});
|
|
142
|
-
Object.defineProperty(this, "
|
|
114
|
+
Object.defineProperty(this, "model", {
|
|
143
115
|
enumerable: true,
|
|
144
116
|
configurable: true,
|
|
145
117
|
writable: true,
|
|
146
118
|
value: "gpt-3.5-turbo-instruct"
|
|
147
119
|
});
|
|
148
|
-
|
|
120
|
+
/** @deprecated Use "model" instead */
|
|
121
|
+
Object.defineProperty(this, "modelName", {
|
|
149
122
|
enumerable: true,
|
|
150
123
|
configurable: true,
|
|
151
124
|
writable: true,
|
|
152
|
-
value:
|
|
125
|
+
value: void 0
|
|
153
126
|
});
|
|
154
127
|
Object.defineProperty(this, "modelKwargs", {
|
|
155
128
|
enumerable: true,
|
|
@@ -205,42 +178,6 @@ export class OpenAI extends BaseLLM {
|
|
|
205
178
|
writable: true,
|
|
206
179
|
value: void 0
|
|
207
180
|
});
|
|
208
|
-
Object.defineProperty(this, "azureOpenAIApiVersion", {
|
|
209
|
-
enumerable: true,
|
|
210
|
-
configurable: true,
|
|
211
|
-
writable: true,
|
|
212
|
-
value: void 0
|
|
213
|
-
});
|
|
214
|
-
Object.defineProperty(this, "azureOpenAIApiKey", {
|
|
215
|
-
enumerable: true,
|
|
216
|
-
configurable: true,
|
|
217
|
-
writable: true,
|
|
218
|
-
value: void 0
|
|
219
|
-
});
|
|
220
|
-
Object.defineProperty(this, "azureADTokenProvider", {
|
|
221
|
-
enumerable: true,
|
|
222
|
-
configurable: true,
|
|
223
|
-
writable: true,
|
|
224
|
-
value: void 0
|
|
225
|
-
});
|
|
226
|
-
Object.defineProperty(this, "azureOpenAIApiInstanceName", {
|
|
227
|
-
enumerable: true,
|
|
228
|
-
configurable: true,
|
|
229
|
-
writable: true,
|
|
230
|
-
value: void 0
|
|
231
|
-
});
|
|
232
|
-
Object.defineProperty(this, "azureOpenAIApiDeploymentName", {
|
|
233
|
-
enumerable: true,
|
|
234
|
-
configurable: true,
|
|
235
|
-
writable: true,
|
|
236
|
-
value: void 0
|
|
237
|
-
});
|
|
238
|
-
Object.defineProperty(this, "azureOpenAIBasePath", {
|
|
239
|
-
enumerable: true,
|
|
240
|
-
configurable: true,
|
|
241
|
-
writable: true,
|
|
242
|
-
value: void 0
|
|
243
|
-
});
|
|
244
181
|
Object.defineProperty(this, "organization", {
|
|
245
182
|
enumerable: true,
|
|
246
183
|
configurable: true,
|
|
@@ -259,38 +196,30 @@ export class OpenAI extends BaseLLM {
|
|
|
259
196
|
writable: true,
|
|
260
197
|
value: void 0
|
|
261
198
|
});
|
|
262
|
-
model = model ?? this.model;
|
|
263
199
|
this.openAIApiKey =
|
|
264
200
|
fields?.apiKey ??
|
|
265
201
|
fields?.openAIApiKey ??
|
|
266
202
|
getEnvironmentVariable("OPENAI_API_KEY");
|
|
267
203
|
this.apiKey = this.openAIApiKey;
|
|
268
|
-
this.azureOpenAIApiKey =
|
|
269
|
-
fields?.azureOpenAIApiKey ??
|
|
270
|
-
getEnvironmentVariable("AZURE_OPENAI_API_KEY");
|
|
271
|
-
this.azureADTokenProvider = fields?.azureADTokenProvider ?? undefined;
|
|
272
|
-
if (!this.azureOpenAIApiKey && !this.apiKey && !this.azureADTokenProvider) {
|
|
273
|
-
throw new Error("OpenAI or Azure OpenAI API key or Token Provider not found");
|
|
274
|
-
}
|
|
275
|
-
this.azureOpenAIApiInstanceName =
|
|
276
|
-
fields?.azureOpenAIApiInstanceName ??
|
|
277
|
-
getEnvironmentVariable("AZURE_OPENAI_API_INSTANCE_NAME");
|
|
278
|
-
this.azureOpenAIApiDeploymentName =
|
|
279
|
-
(fields?.azureOpenAIApiCompletionsDeploymentName ||
|
|
280
|
-
fields?.azureOpenAIApiDeploymentName) ??
|
|
281
|
-
(getEnvironmentVariable("AZURE_OPENAI_API_COMPLETIONS_DEPLOYMENT_NAME") ||
|
|
282
|
-
getEnvironmentVariable("AZURE_OPENAI_API_DEPLOYMENT_NAME"));
|
|
283
|
-
this.azureOpenAIApiVersion =
|
|
284
|
-
fields?.azureOpenAIApiVersion ??
|
|
285
|
-
getEnvironmentVariable("AZURE_OPENAI_API_VERSION");
|
|
286
|
-
this.azureOpenAIBasePath =
|
|
287
|
-
fields?.azureOpenAIBasePath ??
|
|
288
|
-
getEnvironmentVariable("AZURE_OPENAI_BASE_PATH");
|
|
289
204
|
this.organization =
|
|
290
205
|
fields?.configuration?.organization ??
|
|
291
206
|
getEnvironmentVariable("OPENAI_ORGANIZATION");
|
|
292
|
-
this.
|
|
293
|
-
this.model
|
|
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;
|
|
294
223
|
this.modelKwargs = fields?.modelKwargs ?? {};
|
|
295
224
|
this.batchSize = fields?.batchSize ?? this.batchSize;
|
|
296
225
|
this.timeout = fields?.timeout;
|
|
@@ -309,28 +238,10 @@ export class OpenAI extends BaseLLM {
|
|
|
309
238
|
if (this.streaming && this.bestOf && this.bestOf > 1) {
|
|
310
239
|
throw new Error("Cannot stream results when bestOf > 1");
|
|
311
240
|
}
|
|
312
|
-
if (this.azureOpenAIApiKey || this.azureADTokenProvider) {
|
|
313
|
-
if (!this.azureOpenAIApiInstanceName && !this.azureOpenAIBasePath) {
|
|
314
|
-
throw new Error("Azure OpenAI API instance name not found");
|
|
315
|
-
}
|
|
316
|
-
if (!this.azureOpenAIApiDeploymentName) {
|
|
317
|
-
throw new Error("Azure OpenAI API deployment name not found");
|
|
318
|
-
}
|
|
319
|
-
if (!this.azureOpenAIApiVersion) {
|
|
320
|
-
throw new Error("Azure OpenAI API version not found");
|
|
321
|
-
}
|
|
322
|
-
this.apiKey = this.apiKey ?? "";
|
|
323
|
-
}
|
|
324
241
|
this.clientConfig = {
|
|
325
242
|
apiKey: this.apiKey,
|
|
326
243
|
organization: this.organization,
|
|
327
|
-
baseURL: configuration?.basePath ?? fields?.configuration?.basePath,
|
|
328
244
|
dangerouslyAllowBrowser: true,
|
|
329
|
-
defaultHeaders: configuration?.baseOptions?.headers ??
|
|
330
|
-
fields?.configuration?.baseOptions?.headers,
|
|
331
|
-
defaultQuery: configuration?.baseOptions?.params ??
|
|
332
|
-
fields?.configuration?.baseOptions?.params,
|
|
333
|
-
...configuration,
|
|
334
245
|
...fields?.configuration,
|
|
335
246
|
};
|
|
336
247
|
}
|
|
@@ -529,10 +440,6 @@ export class OpenAI extends BaseLLM {
|
|
|
529
440
|
_getClientOptions(options) {
|
|
530
441
|
if (!this.client) {
|
|
531
442
|
const openAIEndpointConfig = {
|
|
532
|
-
azureOpenAIApiDeploymentName: this.azureOpenAIApiDeploymentName,
|
|
533
|
-
azureOpenAIApiInstanceName: this.azureOpenAIApiInstanceName,
|
|
534
|
-
azureOpenAIApiKey: this.azureOpenAIApiKey,
|
|
535
|
-
azureOpenAIBasePath: this.azureOpenAIBasePath,
|
|
536
443
|
baseURL: this.clientConfig.baseURL,
|
|
537
444
|
};
|
|
538
445
|
const endpoint = getEndpoint(openAIEndpointConfig);
|
|
@@ -551,16 +458,6 @@ export class OpenAI extends BaseLLM {
|
|
|
551
458
|
...this.clientConfig,
|
|
552
459
|
...options,
|
|
553
460
|
};
|
|
554
|
-
if (this.azureOpenAIApiKey) {
|
|
555
|
-
requestOptions.headers = {
|
|
556
|
-
"api-key": this.azureOpenAIApiKey,
|
|
557
|
-
...requestOptions.headers,
|
|
558
|
-
};
|
|
559
|
-
requestOptions.query = {
|
|
560
|
-
"api-version": this.azureOpenAIApiVersion,
|
|
561
|
-
...requestOptions.query,
|
|
562
|
-
};
|
|
563
|
-
}
|
|
564
461
|
return requestOptions;
|
|
565
462
|
}
|
|
566
463
|
_llmType() {
|
package/dist/types.d.ts
CHANGED
|
@@ -34,6 +34,7 @@ export declare interface OpenAIBaseInput {
|
|
|
34
34
|
/**
|
|
35
35
|
* Model name to use
|
|
36
36
|
* Alias for `model`
|
|
37
|
+
* @deprecated Use "model" instead.
|
|
37
38
|
*/
|
|
38
39
|
modelName: string;
|
|
39
40
|
/** Model name to use */
|
|
@@ -93,18 +94,6 @@ export declare interface OpenAIInput extends OpenAIBaseInput {
|
|
|
93
94
|
/** Batch size to use when passing multiple documents to generate */
|
|
94
95
|
batchSize: number;
|
|
95
96
|
}
|
|
96
|
-
/**
|
|
97
|
-
* @deprecated Use "baseURL", "defaultHeaders", and "defaultParams" instead.
|
|
98
|
-
*/
|
|
99
|
-
export interface LegacyOpenAIInput {
|
|
100
|
-
/** @deprecated Use baseURL instead */
|
|
101
|
-
basePath?: string;
|
|
102
|
-
/** @deprecated Use defaultHeaders and defaultQuery instead */
|
|
103
|
-
baseOptions?: {
|
|
104
|
-
headers?: Record<string, string>;
|
|
105
|
-
params?: Record<string, string>;
|
|
106
|
-
};
|
|
107
|
-
}
|
|
108
97
|
export interface OpenAIChatInput extends OpenAIBaseInput {
|
|
109
98
|
/**
|
|
110
99
|
* Whether to return log probabilities of the output tokens or not.
|