@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/azure/chat_models.cjs +77 -11
- package/dist/azure/chat_models.d.ts +14 -3
- package/dist/azure/chat_models.js +77 -11
- 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 -124
- package/dist/chat_models.d.ts +14 -20
- package/dist/chat_models.js +22 -124
- 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.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.
|