@langchain/openai 0.6.1 → 0.6.3
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 +331 -161
- package/dist/azure/chat_models.d.ts +17 -28
- package/dist/azure/chat_models.js +332 -162
- package/dist/chat_models.cjs +82 -59
- package/dist/chat_models.d.ts +31 -10
- package/dist/chat_models.js +82 -60
- package/dist/types.d.ts +7 -1
- package/package.json +1 -1
|
@@ -6,6 +6,316 @@ const env_1 = require("@langchain/core/utils/env");
|
|
|
6
6
|
const chat_models_js_1 = require("../chat_models.cjs");
|
|
7
7
|
const azure_js_1 = require("../utils/azure.cjs");
|
|
8
8
|
const headers_js_1 = require("../utils/headers.cjs");
|
|
9
|
+
const AZURE_ALIASES = {
|
|
10
|
+
openAIApiKey: "openai_api_key",
|
|
11
|
+
openAIApiVersion: "openai_api_version",
|
|
12
|
+
openAIBasePath: "openai_api_base",
|
|
13
|
+
deploymentName: "deployment_name",
|
|
14
|
+
azureOpenAIEndpoint: "azure_endpoint",
|
|
15
|
+
azureOpenAIApiVersion: "openai_api_version",
|
|
16
|
+
azureOpenAIBasePath: "openai_api_base",
|
|
17
|
+
azureOpenAIApiDeploymentName: "deployment_name",
|
|
18
|
+
};
|
|
19
|
+
const AZURE_SECRETS = {
|
|
20
|
+
azureOpenAIApiKey: "AZURE_OPENAI_API_KEY",
|
|
21
|
+
};
|
|
22
|
+
const AZURE_SERIALIZABLE_KEYS = [
|
|
23
|
+
"azureOpenAIApiKey",
|
|
24
|
+
"azureOpenAIApiVersion",
|
|
25
|
+
"azureOpenAIBasePath",
|
|
26
|
+
"azureOpenAIEndpoint",
|
|
27
|
+
"azureOpenAIApiInstanceName",
|
|
28
|
+
"azureOpenAIApiDeploymentName",
|
|
29
|
+
"deploymentName",
|
|
30
|
+
"openAIApiKey",
|
|
31
|
+
"openAIApiVersion",
|
|
32
|
+
];
|
|
33
|
+
function _constructAzureFields(fields) {
|
|
34
|
+
this.azureOpenAIApiKey =
|
|
35
|
+
fields?.azureOpenAIApiKey ??
|
|
36
|
+
fields?.openAIApiKey ??
|
|
37
|
+
fields?.apiKey ??
|
|
38
|
+
(0, env_1.getEnvironmentVariable)("AZURE_OPENAI_API_KEY");
|
|
39
|
+
this.azureOpenAIApiInstanceName =
|
|
40
|
+
fields?.azureOpenAIApiInstanceName ??
|
|
41
|
+
(0, env_1.getEnvironmentVariable)("AZURE_OPENAI_API_INSTANCE_NAME");
|
|
42
|
+
this.azureOpenAIApiDeploymentName =
|
|
43
|
+
fields?.azureOpenAIApiDeploymentName ??
|
|
44
|
+
fields?.deploymentName ??
|
|
45
|
+
(0, env_1.getEnvironmentVariable)("AZURE_OPENAI_API_DEPLOYMENT_NAME");
|
|
46
|
+
this.azureOpenAIApiVersion =
|
|
47
|
+
fields?.azureOpenAIApiVersion ??
|
|
48
|
+
fields?.openAIApiVersion ??
|
|
49
|
+
(0, env_1.getEnvironmentVariable)("AZURE_OPENAI_API_VERSION");
|
|
50
|
+
this.azureOpenAIBasePath =
|
|
51
|
+
fields?.azureOpenAIBasePath ??
|
|
52
|
+
(0, env_1.getEnvironmentVariable)("AZURE_OPENAI_BASE_PATH");
|
|
53
|
+
this.azureOpenAIEndpoint =
|
|
54
|
+
fields?.azureOpenAIEndpoint ??
|
|
55
|
+
(0, env_1.getEnvironmentVariable)("AZURE_OPENAI_ENDPOINT");
|
|
56
|
+
this.azureADTokenProvider = fields?.azureADTokenProvider;
|
|
57
|
+
if (!this.azureOpenAIApiKey && !this.apiKey && !this.azureADTokenProvider) {
|
|
58
|
+
throw new Error("Azure OpenAI API key or Token Provider not found");
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function _getAzureClientOptions(options) {
|
|
62
|
+
if (!this.client) {
|
|
63
|
+
const openAIEndpointConfig = {
|
|
64
|
+
azureOpenAIApiDeploymentName: this.azureOpenAIApiDeploymentName,
|
|
65
|
+
azureOpenAIApiInstanceName: this.azureOpenAIApiInstanceName,
|
|
66
|
+
azureOpenAIApiKey: this.azureOpenAIApiKey,
|
|
67
|
+
azureOpenAIBasePath: this.azureOpenAIBasePath,
|
|
68
|
+
azureADTokenProvider: this.azureADTokenProvider,
|
|
69
|
+
baseURL: this.clientConfig.baseURL,
|
|
70
|
+
azureOpenAIEndpoint: this.azureOpenAIEndpoint,
|
|
71
|
+
};
|
|
72
|
+
const endpoint = (0, azure_js_1.getEndpoint)(openAIEndpointConfig);
|
|
73
|
+
const params = {
|
|
74
|
+
...this.clientConfig,
|
|
75
|
+
baseURL: endpoint,
|
|
76
|
+
timeout: this.timeout,
|
|
77
|
+
maxRetries: 0,
|
|
78
|
+
};
|
|
79
|
+
if (!this.azureADTokenProvider) {
|
|
80
|
+
params.apiKey = openAIEndpointConfig.azureOpenAIApiKey;
|
|
81
|
+
}
|
|
82
|
+
if (!params.baseURL) {
|
|
83
|
+
delete params.baseURL;
|
|
84
|
+
}
|
|
85
|
+
let env = (0, env_1.getEnv)();
|
|
86
|
+
if (env === "node" || env === "deno") {
|
|
87
|
+
env = `(${env}/${process.version}; ${process.platform}; ${process.arch})`;
|
|
88
|
+
}
|
|
89
|
+
const defaultHeaders = (0, headers_js_1.normalizeHeaders)(params.defaultHeaders);
|
|
90
|
+
params.defaultHeaders = {
|
|
91
|
+
...params.defaultHeaders,
|
|
92
|
+
"User-Agent": defaultHeaders["User-Agent"]
|
|
93
|
+
? `langchainjs-azure-openai/2.0.0 (${env})${defaultHeaders["User-Agent"]}`
|
|
94
|
+
: `langchainjs-azure-openai/2.0.0 (${env})`,
|
|
95
|
+
};
|
|
96
|
+
this.client = new openai_1.AzureOpenAI({
|
|
97
|
+
apiVersion: this.azureOpenAIApiVersion,
|
|
98
|
+
azureADTokenProvider: this.azureADTokenProvider,
|
|
99
|
+
deployment: this.azureOpenAIApiDeploymentName,
|
|
100
|
+
...params,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
const requestOptions = {
|
|
104
|
+
...this.clientConfig,
|
|
105
|
+
...options,
|
|
106
|
+
};
|
|
107
|
+
if (this.azureOpenAIApiKey) {
|
|
108
|
+
requestOptions.headers = {
|
|
109
|
+
"api-key": this.azureOpenAIApiKey,
|
|
110
|
+
...requestOptions.headers,
|
|
111
|
+
};
|
|
112
|
+
requestOptions.query = {
|
|
113
|
+
"api-version": this.azureOpenAIApiVersion,
|
|
114
|
+
...requestOptions.query,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
return requestOptions;
|
|
118
|
+
}
|
|
119
|
+
function _serializeAzureChat(input) {
|
|
120
|
+
const json = input;
|
|
121
|
+
function isRecord(obj) {
|
|
122
|
+
return typeof obj === "object" && obj != null;
|
|
123
|
+
}
|
|
124
|
+
if (isRecord(json) && isRecord(json.kwargs)) {
|
|
125
|
+
delete json.kwargs.azure_openai_base_path;
|
|
126
|
+
delete json.kwargs.azure_openai_api_deployment_name;
|
|
127
|
+
delete json.kwargs.azure_openai_api_key;
|
|
128
|
+
delete json.kwargs.azure_openai_api_version;
|
|
129
|
+
delete json.kwargs.azure_open_ai_base_path;
|
|
130
|
+
if (!json.kwargs.azure_endpoint && this.azureOpenAIEndpoint) {
|
|
131
|
+
json.kwargs.azure_endpoint = this.azureOpenAIEndpoint;
|
|
132
|
+
}
|
|
133
|
+
if (!json.kwargs.azure_endpoint && this.azureOpenAIBasePath) {
|
|
134
|
+
const parts = this.azureOpenAIBasePath.split("/openai/deployments/");
|
|
135
|
+
if (parts.length === 2 && parts[0].startsWith("http")) {
|
|
136
|
+
const [endpoint] = parts;
|
|
137
|
+
json.kwargs.azure_endpoint = endpoint;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
if (!json.kwargs.azure_endpoint && this.azureOpenAIApiInstanceName) {
|
|
141
|
+
json.kwargs.azure_endpoint = `https://${this.azureOpenAIApiInstanceName}.openai.azure.com/`;
|
|
142
|
+
}
|
|
143
|
+
if (!json.kwargs.deployment_name && this.azureOpenAIApiDeploymentName) {
|
|
144
|
+
json.kwargs.deployment_name = this.azureOpenAIApiDeploymentName;
|
|
145
|
+
}
|
|
146
|
+
if (!json.kwargs.deployment_name && this.azureOpenAIBasePath) {
|
|
147
|
+
const parts = this.azureOpenAIBasePath.split("/openai/deployments/");
|
|
148
|
+
if (parts.length === 2) {
|
|
149
|
+
const [, deployment] = parts;
|
|
150
|
+
json.kwargs.deployment_name = deployment;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
if (json.kwargs.azure_endpoint &&
|
|
154
|
+
json.kwargs.deployment_name &&
|
|
155
|
+
json.kwargs.openai_api_base) {
|
|
156
|
+
delete json.kwargs.openai_api_base;
|
|
157
|
+
}
|
|
158
|
+
if (json.kwargs.azure_openai_api_instance_name &&
|
|
159
|
+
json.kwargs.azure_endpoint) {
|
|
160
|
+
delete json.kwargs.azure_openai_api_instance_name;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return json;
|
|
164
|
+
}
|
|
165
|
+
class AzureChatOpenAIResponses extends chat_models_js_1.ChatOpenAIResponses {
|
|
166
|
+
_llmType() {
|
|
167
|
+
return "azure_openai";
|
|
168
|
+
}
|
|
169
|
+
get lc_aliases() {
|
|
170
|
+
return {
|
|
171
|
+
...super.lc_aliases,
|
|
172
|
+
...AZURE_ALIASES,
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
get lc_secrets() {
|
|
176
|
+
return {
|
|
177
|
+
...super.lc_secrets,
|
|
178
|
+
...AZURE_SECRETS,
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
get lc_serializable_keys() {
|
|
182
|
+
return [...super.lc_serializable_keys, ...AZURE_SERIALIZABLE_KEYS];
|
|
183
|
+
}
|
|
184
|
+
getLsParams(options) {
|
|
185
|
+
const params = super.getLsParams(options);
|
|
186
|
+
params.ls_provider = "azure";
|
|
187
|
+
return params;
|
|
188
|
+
}
|
|
189
|
+
constructor(fields) {
|
|
190
|
+
super(fields);
|
|
191
|
+
Object.defineProperty(this, "azureOpenAIApiVersion", {
|
|
192
|
+
enumerable: true,
|
|
193
|
+
configurable: true,
|
|
194
|
+
writable: true,
|
|
195
|
+
value: void 0
|
|
196
|
+
});
|
|
197
|
+
Object.defineProperty(this, "azureOpenAIApiKey", {
|
|
198
|
+
enumerable: true,
|
|
199
|
+
configurable: true,
|
|
200
|
+
writable: true,
|
|
201
|
+
value: void 0
|
|
202
|
+
});
|
|
203
|
+
Object.defineProperty(this, "azureADTokenProvider", {
|
|
204
|
+
enumerable: true,
|
|
205
|
+
configurable: true,
|
|
206
|
+
writable: true,
|
|
207
|
+
value: void 0
|
|
208
|
+
});
|
|
209
|
+
Object.defineProperty(this, "azureOpenAIApiInstanceName", {
|
|
210
|
+
enumerable: true,
|
|
211
|
+
configurable: true,
|
|
212
|
+
writable: true,
|
|
213
|
+
value: void 0
|
|
214
|
+
});
|
|
215
|
+
Object.defineProperty(this, "azureOpenAIApiDeploymentName", {
|
|
216
|
+
enumerable: true,
|
|
217
|
+
configurable: true,
|
|
218
|
+
writable: true,
|
|
219
|
+
value: void 0
|
|
220
|
+
});
|
|
221
|
+
Object.defineProperty(this, "azureOpenAIBasePath", {
|
|
222
|
+
enumerable: true,
|
|
223
|
+
configurable: true,
|
|
224
|
+
writable: true,
|
|
225
|
+
value: void 0
|
|
226
|
+
});
|
|
227
|
+
Object.defineProperty(this, "azureOpenAIEndpoint", {
|
|
228
|
+
enumerable: true,
|
|
229
|
+
configurable: true,
|
|
230
|
+
writable: true,
|
|
231
|
+
value: void 0
|
|
232
|
+
});
|
|
233
|
+
_constructAzureFields.call(this, fields);
|
|
234
|
+
}
|
|
235
|
+
_getClientOptions(options) {
|
|
236
|
+
return _getAzureClientOptions.call(this, options);
|
|
237
|
+
}
|
|
238
|
+
toJSON() {
|
|
239
|
+
return _serializeAzureChat.call(this, super.toJSON());
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
class AzureChatOpenAICompletions extends chat_models_js_1.ChatOpenAICompletions {
|
|
243
|
+
_llmType() {
|
|
244
|
+
return "azure_openai";
|
|
245
|
+
}
|
|
246
|
+
get lc_aliases() {
|
|
247
|
+
return {
|
|
248
|
+
...super.lc_aliases,
|
|
249
|
+
...AZURE_ALIASES,
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
get lc_secrets() {
|
|
253
|
+
return {
|
|
254
|
+
...super.lc_secrets,
|
|
255
|
+
...AZURE_SECRETS,
|
|
256
|
+
};
|
|
257
|
+
}
|
|
258
|
+
get lc_serializable_keys() {
|
|
259
|
+
return [...super.lc_serializable_keys, ...AZURE_SERIALIZABLE_KEYS];
|
|
260
|
+
}
|
|
261
|
+
getLsParams(options) {
|
|
262
|
+
const params = super.getLsParams(options);
|
|
263
|
+
params.ls_provider = "azure";
|
|
264
|
+
return params;
|
|
265
|
+
}
|
|
266
|
+
constructor(fields) {
|
|
267
|
+
super(fields);
|
|
268
|
+
Object.defineProperty(this, "azureOpenAIApiVersion", {
|
|
269
|
+
enumerable: true,
|
|
270
|
+
configurable: true,
|
|
271
|
+
writable: true,
|
|
272
|
+
value: void 0
|
|
273
|
+
});
|
|
274
|
+
Object.defineProperty(this, "azureOpenAIApiKey", {
|
|
275
|
+
enumerable: true,
|
|
276
|
+
configurable: true,
|
|
277
|
+
writable: true,
|
|
278
|
+
value: void 0
|
|
279
|
+
});
|
|
280
|
+
Object.defineProperty(this, "azureADTokenProvider", {
|
|
281
|
+
enumerable: true,
|
|
282
|
+
configurable: true,
|
|
283
|
+
writable: true,
|
|
284
|
+
value: void 0
|
|
285
|
+
});
|
|
286
|
+
Object.defineProperty(this, "azureOpenAIApiInstanceName", {
|
|
287
|
+
enumerable: true,
|
|
288
|
+
configurable: true,
|
|
289
|
+
writable: true,
|
|
290
|
+
value: void 0
|
|
291
|
+
});
|
|
292
|
+
Object.defineProperty(this, "azureOpenAIApiDeploymentName", {
|
|
293
|
+
enumerable: true,
|
|
294
|
+
configurable: true,
|
|
295
|
+
writable: true,
|
|
296
|
+
value: void 0
|
|
297
|
+
});
|
|
298
|
+
Object.defineProperty(this, "azureOpenAIBasePath", {
|
|
299
|
+
enumerable: true,
|
|
300
|
+
configurable: true,
|
|
301
|
+
writable: true,
|
|
302
|
+
value: void 0
|
|
303
|
+
});
|
|
304
|
+
Object.defineProperty(this, "azureOpenAIEndpoint", {
|
|
305
|
+
enumerable: true,
|
|
306
|
+
configurable: true,
|
|
307
|
+
writable: true,
|
|
308
|
+
value: void 0
|
|
309
|
+
});
|
|
310
|
+
_constructAzureFields.call(this, fields);
|
|
311
|
+
}
|
|
312
|
+
_getClientOptions(options) {
|
|
313
|
+
return _getAzureClientOptions.call(this, options);
|
|
314
|
+
}
|
|
315
|
+
toJSON() {
|
|
316
|
+
return _serializeAzureChat.call(this, super.toJSON());
|
|
317
|
+
}
|
|
318
|
+
}
|
|
9
319
|
/**
|
|
10
320
|
* Azure OpenAI chat model integration.
|
|
11
321
|
*
|
|
@@ -425,38 +735,29 @@ class AzureChatOpenAI extends chat_models_js_1.ChatOpenAI {
|
|
|
425
735
|
get lc_aliases() {
|
|
426
736
|
return {
|
|
427
737
|
...super.lc_aliases,
|
|
428
|
-
|
|
429
|
-
openAIApiVersion: "openai_api_version",
|
|
430
|
-
openAIBasePath: "openai_api_base",
|
|
431
|
-
deploymentName: "deployment_name",
|
|
432
|
-
azureOpenAIEndpoint: "azure_endpoint",
|
|
433
|
-
azureOpenAIApiVersion: "openai_api_version",
|
|
434
|
-
azureOpenAIBasePath: "openai_api_base",
|
|
435
|
-
azureOpenAIApiDeploymentName: "deployment_name",
|
|
738
|
+
...AZURE_ALIASES,
|
|
436
739
|
};
|
|
437
740
|
}
|
|
438
741
|
get lc_secrets() {
|
|
439
742
|
return {
|
|
440
743
|
...super.lc_secrets,
|
|
441
|
-
|
|
744
|
+
...AZURE_SECRETS,
|
|
442
745
|
};
|
|
443
746
|
}
|
|
444
747
|
get lc_serializable_keys() {
|
|
445
|
-
return [
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
"azureOpenAIApiInstanceName",
|
|
452
|
-
"azureOpenAIApiDeploymentName",
|
|
453
|
-
"deploymentName",
|
|
454
|
-
"openAIApiKey",
|
|
455
|
-
"openAIApiVersion",
|
|
456
|
-
];
|
|
748
|
+
return [...super.lc_serializable_keys, ...AZURE_SERIALIZABLE_KEYS];
|
|
749
|
+
}
|
|
750
|
+
getLsParams(options) {
|
|
751
|
+
const params = super.getLsParams(options);
|
|
752
|
+
params.ls_provider = "azure";
|
|
753
|
+
return params;
|
|
457
754
|
}
|
|
458
755
|
constructor(fields) {
|
|
459
|
-
super(
|
|
756
|
+
super({
|
|
757
|
+
...fields,
|
|
758
|
+
completions: new AzureChatOpenAICompletions(fields),
|
|
759
|
+
responses: new AzureChatOpenAIResponses(fields),
|
|
760
|
+
});
|
|
460
761
|
Object.defineProperty(this, "azureOpenAIApiVersion", {
|
|
461
762
|
enumerable: true,
|
|
462
763
|
configurable: true,
|
|
@@ -499,152 +800,21 @@ class AzureChatOpenAI extends chat_models_js_1.ChatOpenAI {
|
|
|
499
800
|
writable: true,
|
|
500
801
|
value: void 0
|
|
501
802
|
});
|
|
502
|
-
this
|
|
503
|
-
fields?.azureOpenAIApiKey ??
|
|
504
|
-
fields?.openAIApiKey ??
|
|
505
|
-
fields?.apiKey ??
|
|
506
|
-
(0, env_1.getEnvironmentVariable)("AZURE_OPENAI_API_KEY");
|
|
507
|
-
this.azureOpenAIApiInstanceName =
|
|
508
|
-
fields?.azureOpenAIApiInstanceName ??
|
|
509
|
-
(0, env_1.getEnvironmentVariable)("AZURE_OPENAI_API_INSTANCE_NAME");
|
|
510
|
-
this.azureOpenAIApiDeploymentName =
|
|
511
|
-
fields?.azureOpenAIApiDeploymentName ??
|
|
512
|
-
fields?.deploymentName ??
|
|
513
|
-
(0, env_1.getEnvironmentVariable)("AZURE_OPENAI_API_DEPLOYMENT_NAME");
|
|
514
|
-
this.azureOpenAIApiVersion =
|
|
515
|
-
fields?.azureOpenAIApiVersion ??
|
|
516
|
-
fields?.openAIApiVersion ??
|
|
517
|
-
(0, env_1.getEnvironmentVariable)("AZURE_OPENAI_API_VERSION");
|
|
518
|
-
this.azureOpenAIBasePath =
|
|
519
|
-
fields?.azureOpenAIBasePath ??
|
|
520
|
-
(0, env_1.getEnvironmentVariable)("AZURE_OPENAI_BASE_PATH");
|
|
521
|
-
this.azureOpenAIEndpoint =
|
|
522
|
-
fields?.azureOpenAIEndpoint ??
|
|
523
|
-
(0, env_1.getEnvironmentVariable)("AZURE_OPENAI_ENDPOINT");
|
|
524
|
-
this.azureADTokenProvider = fields?.azureADTokenProvider;
|
|
525
|
-
if (!this.azureOpenAIApiKey && !this.apiKey && !this.azureADTokenProvider) {
|
|
526
|
-
throw new Error("Azure OpenAI API key or Token Provider not found");
|
|
527
|
-
}
|
|
528
|
-
}
|
|
529
|
-
getLsParams(options) {
|
|
530
|
-
const params = super.getLsParams(options);
|
|
531
|
-
params.ls_provider = "azure";
|
|
532
|
-
return params;
|
|
803
|
+
_constructAzureFields.call(this, fields);
|
|
533
804
|
}
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
const openAIEndpointConfig = {
|
|
537
|
-
azureOpenAIApiDeploymentName: this.azureOpenAIApiDeploymentName,
|
|
538
|
-
azureOpenAIApiInstanceName: this.azureOpenAIApiInstanceName,
|
|
539
|
-
azureOpenAIApiKey: this.azureOpenAIApiKey,
|
|
540
|
-
azureOpenAIBasePath: this.azureOpenAIBasePath,
|
|
541
|
-
azureADTokenProvider: this.azureADTokenProvider,
|
|
542
|
-
baseURL: this.clientConfig.baseURL,
|
|
543
|
-
azureOpenAIEndpoint: this.azureOpenAIEndpoint,
|
|
544
|
-
};
|
|
545
|
-
const endpoint = (0, azure_js_1.getEndpoint)(openAIEndpointConfig);
|
|
546
|
-
const params = {
|
|
547
|
-
...this.clientConfig,
|
|
548
|
-
baseURL: endpoint,
|
|
549
|
-
timeout: this.timeout,
|
|
550
|
-
maxRetries: 0,
|
|
551
|
-
};
|
|
552
|
-
if (!this.azureADTokenProvider) {
|
|
553
|
-
params.apiKey = openAIEndpointConfig.azureOpenAIApiKey;
|
|
554
|
-
}
|
|
555
|
-
if (!params.baseURL) {
|
|
556
|
-
delete params.baseURL;
|
|
557
|
-
}
|
|
558
|
-
let env = (0, env_1.getEnv)();
|
|
559
|
-
if (env === "node" || env === "deno") {
|
|
560
|
-
env = `(${env}/${process.version}; ${process.platform}; ${process.arch})`;
|
|
561
|
-
}
|
|
562
|
-
const defaultHeaders = (0, headers_js_1.normalizeHeaders)(params.defaultHeaders);
|
|
563
|
-
params.defaultHeaders = {
|
|
564
|
-
...params.defaultHeaders,
|
|
565
|
-
"User-Agent": defaultHeaders["User-Agent"]
|
|
566
|
-
? `langchainjs-azure-openai/2.0.0 (${env})${defaultHeaders["User-Agent"]}`
|
|
567
|
-
: `langchainjs-azure-openai/2.0.0 (${env})`,
|
|
568
|
-
};
|
|
569
|
-
this.client = new openai_1.AzureOpenAI({
|
|
570
|
-
apiVersion: this.azureOpenAIApiVersion,
|
|
571
|
-
azureADTokenProvider: this.azureADTokenProvider,
|
|
572
|
-
deployment: this.azureOpenAIApiDeploymentName,
|
|
573
|
-
...params,
|
|
574
|
-
});
|
|
575
|
-
}
|
|
576
|
-
const requestOptions = {
|
|
577
|
-
...this.clientConfig,
|
|
578
|
-
...options,
|
|
579
|
-
};
|
|
580
|
-
if (this.azureOpenAIApiKey) {
|
|
581
|
-
requestOptions.headers = {
|
|
582
|
-
"api-key": this.azureOpenAIApiKey,
|
|
583
|
-
...requestOptions.headers,
|
|
584
|
-
};
|
|
585
|
-
requestOptions.query = {
|
|
586
|
-
"api-version": this.azureOpenAIApiVersion,
|
|
587
|
-
...requestOptions.query,
|
|
588
|
-
};
|
|
589
|
-
}
|
|
590
|
-
return requestOptions;
|
|
591
|
-
}
|
|
592
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
593
|
-
toJSON() {
|
|
594
|
-
const json = super.toJSON();
|
|
595
|
-
function isRecord(obj) {
|
|
596
|
-
return typeof obj === "object" && obj != null;
|
|
597
|
-
}
|
|
598
|
-
if (isRecord(json) && isRecord(json.kwargs)) {
|
|
599
|
-
delete json.kwargs.azure_openai_base_path;
|
|
600
|
-
delete json.kwargs.azure_openai_api_deployment_name;
|
|
601
|
-
delete json.kwargs.azure_openai_api_key;
|
|
602
|
-
delete json.kwargs.azure_openai_api_version;
|
|
603
|
-
delete json.kwargs.azure_open_ai_base_path;
|
|
604
|
-
if (!json.kwargs.azure_endpoint && this.azureOpenAIEndpoint) {
|
|
605
|
-
json.kwargs.azure_endpoint = this.azureOpenAIEndpoint;
|
|
606
|
-
}
|
|
607
|
-
if (!json.kwargs.azure_endpoint && this.azureOpenAIBasePath) {
|
|
608
|
-
const parts = this.azureOpenAIBasePath.split("/openai/deployments/");
|
|
609
|
-
if (parts.length === 2 && parts[0].startsWith("http")) {
|
|
610
|
-
const [endpoint] = parts;
|
|
611
|
-
json.kwargs.azure_endpoint = endpoint;
|
|
612
|
-
}
|
|
613
|
-
}
|
|
614
|
-
if (!json.kwargs.azure_endpoint && this.azureOpenAIApiInstanceName) {
|
|
615
|
-
json.kwargs.azure_endpoint = `https://${this.azureOpenAIApiInstanceName}.openai.azure.com/`;
|
|
616
|
-
}
|
|
617
|
-
if (!json.kwargs.deployment_name && this.azureOpenAIApiDeploymentName) {
|
|
618
|
-
json.kwargs.deployment_name = this.azureOpenAIApiDeploymentName;
|
|
619
|
-
}
|
|
620
|
-
if (!json.kwargs.deployment_name && this.azureOpenAIBasePath) {
|
|
621
|
-
const parts = this.azureOpenAIBasePath.split("/openai/deployments/");
|
|
622
|
-
if (parts.length === 2) {
|
|
623
|
-
const [, deployment] = parts;
|
|
624
|
-
json.kwargs.deployment_name = deployment;
|
|
625
|
-
}
|
|
626
|
-
}
|
|
627
|
-
if (json.kwargs.azure_endpoint &&
|
|
628
|
-
json.kwargs.deployment_name &&
|
|
629
|
-
json.kwargs.openai_api_base) {
|
|
630
|
-
delete json.kwargs.openai_api_base;
|
|
631
|
-
}
|
|
632
|
-
if (json.kwargs.azure_openai_api_instance_name &&
|
|
633
|
-
json.kwargs.azure_endpoint) {
|
|
634
|
-
delete json.kwargs.azure_openai_api_instance_name;
|
|
635
|
-
}
|
|
636
|
-
}
|
|
637
|
-
return json;
|
|
638
|
-
}
|
|
639
|
-
withStructuredOutput(outputSchema, config) {
|
|
805
|
+
/** @internal */
|
|
806
|
+
_getStructuredOutputMethod(config) {
|
|
640
807
|
const ensuredConfig = { ...config };
|
|
641
808
|
// Not all Azure gpt-4o deployments models support jsonSchema yet
|
|
642
809
|
if (this.model.startsWith("gpt-4o")) {
|
|
643
810
|
if (ensuredConfig?.method === undefined) {
|
|
644
|
-
|
|
811
|
+
return "functionCalling";
|
|
645
812
|
}
|
|
646
813
|
}
|
|
647
|
-
return super.
|
|
814
|
+
return super._getStructuredOutputMethod(ensuredConfig);
|
|
815
|
+
}
|
|
816
|
+
toJSON() {
|
|
817
|
+
return _serializeAzureChat.call(this, super.toJSON());
|
|
648
818
|
}
|
|
649
819
|
}
|
|
650
820
|
exports.AzureChatOpenAI = AzureChatOpenAI;
|
|
@@ -1,12 +1,16 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { LangSmithParams
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import { InteropZodType } from "@langchain/core/utils/types";
|
|
7
|
-
import { ChatOpenAI } from "../chat_models.js";
|
|
8
|
-
import { AzureOpenAIInput, OpenAIChatInput, OpenAICoreRequestOptions } from "../types.js";
|
|
1
|
+
import { Serialized } from "@langchain/core/load/serializable";
|
|
2
|
+
import { LangSmithParams } from "@langchain/core/language_models/chat_models";
|
|
3
|
+
import { StructuredOutputMethodOptions } from "@langchain/core/language_models/base";
|
|
4
|
+
import { BaseChatOpenAIFields, ChatOpenAI, ChatOpenAICallOptions } from "../chat_models.js";
|
|
5
|
+
import { AzureOpenAIChatInput, AzureOpenAIInput } from "../types.js";
|
|
9
6
|
export type { AzureOpenAIInput };
|
|
7
|
+
interface AzureChatOpenAIFields extends BaseChatOpenAIFields, Partial<AzureOpenAIChatInput> {
|
|
8
|
+
/**
|
|
9
|
+
* Whether to use the responses API for all requests. If `false` the responses API will be used
|
|
10
|
+
* only when required in order to fulfill the request.
|
|
11
|
+
*/
|
|
12
|
+
useResponsesApi?: boolean;
|
|
13
|
+
}
|
|
10
14
|
/**
|
|
11
15
|
* Azure OpenAI chat model integration.
|
|
12
16
|
*
|
|
@@ -419,7 +423,7 @@ export type { AzureOpenAIInput };
|
|
|
419
423
|
* ```
|
|
420
424
|
* </details>
|
|
421
425
|
*/
|
|
422
|
-
export declare class AzureChatOpenAI extends ChatOpenAI {
|
|
426
|
+
export declare class AzureChatOpenAI<CallOptions extends ChatOpenAICallOptions = ChatOpenAICallOptions> extends ChatOpenAI<CallOptions> implements Partial<AzureOpenAIChatInput> {
|
|
423
427
|
azureOpenAIApiVersion?: string;
|
|
424
428
|
azureOpenAIApiKey?: string;
|
|
425
429
|
azureADTokenProvider?: () => Promise<string>;
|
|
@@ -433,24 +437,9 @@ export declare class AzureChatOpenAI extends ChatOpenAI {
|
|
|
433
437
|
[key: string]: string;
|
|
434
438
|
} | undefined;
|
|
435
439
|
get lc_serializable_keys(): string[];
|
|
436
|
-
constructor(fields?: Partial<OpenAIChatInput> & Partial<AzureOpenAIInput> & {
|
|
437
|
-
openAIApiKey?: string;
|
|
438
|
-
openAIApiVersion?: string;
|
|
439
|
-
openAIBasePath?: string;
|
|
440
|
-
deploymentName?: string;
|
|
441
|
-
} & BaseChatModelParams & {
|
|
442
|
-
configuration?: ClientOptions;
|
|
443
|
-
});
|
|
444
440
|
getLsParams(options: this["ParsedCallOptions"]): LangSmithParams;
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
raw: BaseMessage;
|
|
450
|
-
parsed: RunOutput;
|
|
451
|
-
}>;
|
|
452
|
-
withStructuredOutput<RunOutput extends Record<string, any> = Record<string, any>>(outputSchema: InteropZodType<RunOutput> | Record<string, any>, config?: StructuredOutputMethodOptions<boolean>): Runnable<BaseLanguageModelInput, RunOutput> | Runnable<BaseLanguageModelInput, {
|
|
453
|
-
raw: BaseMessage;
|
|
454
|
-
parsed: RunOutput;
|
|
455
|
-
}>;
|
|
441
|
+
constructor(fields?: AzureChatOpenAIFields);
|
|
442
|
+
/** @internal */
|
|
443
|
+
_getStructuredOutputMethod(config: StructuredOutputMethodOptions<boolean>): string | undefined;
|
|
444
|
+
toJSON(): Serialized;
|
|
456
445
|
}
|