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