@langchain/openai 0.3.2 → 0.3.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.
@@ -425,6 +425,11 @@ class AzureChatOpenAI extends chat_models_js_1.ChatOpenAI {
425
425
  openAIApiKey: "openai_api_key",
426
426
  openAIApiVersion: "openai_api_version",
427
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",
428
433
  };
429
434
  }
430
435
  constructor(fields) {
@@ -454,6 +459,7 @@ class AzureChatOpenAI extends chat_models_js_1.ChatOpenAI {
454
459
  azureOpenAIBasePath: this.azureOpenAIBasePath,
455
460
  azureADTokenProvider: this.azureADTokenProvider,
456
461
  baseURL: this.clientConfig.baseURL,
462
+ azureOpenAIEndpoint: this.azureOpenAIEndpoint,
457
463
  };
458
464
  const endpoint = (0, azure_js_1.getEndpoint)(openAIEndpointConfig);
459
465
  const params = {
@@ -509,6 +515,38 @@ class AzureChatOpenAI extends chat_models_js_1.ChatOpenAI {
509
515
  delete json.kwargs.azure_openai_api_key;
510
516
  delete json.kwargs.azure_openai_api_version;
511
517
  delete json.kwargs.azure_open_ai_base_path;
518
+ if (!json.kwargs.azure_endpoint && this.azureOpenAIEndpoint) {
519
+ json.kwargs.azure_endpoint = this.azureOpenAIEndpoint;
520
+ }
521
+ if (!json.kwargs.azure_endpoint && this.azureOpenAIBasePath) {
522
+ const parts = this.azureOpenAIBasePath.split("/openai/deployments/");
523
+ if (parts.length === 2 && parts[0].startsWith("http")) {
524
+ const [endpoint] = parts;
525
+ json.kwargs.azure_endpoint = endpoint;
526
+ }
527
+ }
528
+ if (!json.kwargs.azure_endpoint && this.azureOpenAIApiInstanceName) {
529
+ json.kwargs.azure_endpoint = `https://${this.azureOpenAIApiInstanceName}.openai.azure.com/`;
530
+ }
531
+ if (!json.kwargs.deployment_name && this.azureOpenAIApiDeploymentName) {
532
+ json.kwargs.deployment_name = this.azureOpenAIApiDeploymentName;
533
+ }
534
+ if (!json.kwargs.deployment_name && this.azureOpenAIBasePath) {
535
+ const parts = this.azureOpenAIBasePath.split("/openai/deployments/");
536
+ if (parts.length === 2) {
537
+ const [, deployment] = parts;
538
+ json.kwargs.deployment_name = deployment;
539
+ }
540
+ }
541
+ if (json.kwargs.azure_endpoint &&
542
+ json.kwargs.deployment_name &&
543
+ json.kwargs.openai_api_base) {
544
+ delete json.kwargs.openai_api_base;
545
+ }
546
+ if (json.kwargs.azure_openai_api_instance_name &&
547
+ json.kwargs.azure_endpoint) {
548
+ delete json.kwargs.azure_openai_api_instance_name;
549
+ }
512
550
  }
513
551
  return json;
514
552
  }
@@ -422,6 +422,11 @@ export class AzureChatOpenAI extends ChatOpenAI {
422
422
  openAIApiKey: "openai_api_key",
423
423
  openAIApiVersion: "openai_api_version",
424
424
  openAIBasePath: "openai_api_base",
425
+ deploymentName: "deployment_name",
426
+ azureOpenAIEndpoint: "azure_endpoint",
427
+ azureOpenAIApiVersion: "openai_api_version",
428
+ azureOpenAIBasePath: "openai_api_base",
429
+ azureOpenAIApiDeploymentName: "deployment_name",
425
430
  };
426
431
  }
427
432
  constructor(fields) {
@@ -451,6 +456,7 @@ export class AzureChatOpenAI extends ChatOpenAI {
451
456
  azureOpenAIBasePath: this.azureOpenAIBasePath,
452
457
  azureADTokenProvider: this.azureADTokenProvider,
453
458
  baseURL: this.clientConfig.baseURL,
459
+ azureOpenAIEndpoint: this.azureOpenAIEndpoint,
454
460
  };
455
461
  const endpoint = getEndpoint(openAIEndpointConfig);
456
462
  const params = {
@@ -506,6 +512,38 @@ export class AzureChatOpenAI extends ChatOpenAI {
506
512
  delete json.kwargs.azure_openai_api_key;
507
513
  delete json.kwargs.azure_openai_api_version;
508
514
  delete json.kwargs.azure_open_ai_base_path;
515
+ if (!json.kwargs.azure_endpoint && this.azureOpenAIEndpoint) {
516
+ json.kwargs.azure_endpoint = this.azureOpenAIEndpoint;
517
+ }
518
+ if (!json.kwargs.azure_endpoint && this.azureOpenAIBasePath) {
519
+ const parts = this.azureOpenAIBasePath.split("/openai/deployments/");
520
+ if (parts.length === 2 && parts[0].startsWith("http")) {
521
+ const [endpoint] = parts;
522
+ json.kwargs.azure_endpoint = endpoint;
523
+ }
524
+ }
525
+ if (!json.kwargs.azure_endpoint && this.azureOpenAIApiInstanceName) {
526
+ json.kwargs.azure_endpoint = `https://${this.azureOpenAIApiInstanceName}.openai.azure.com/`;
527
+ }
528
+ if (!json.kwargs.deployment_name && this.azureOpenAIApiDeploymentName) {
529
+ json.kwargs.deployment_name = this.azureOpenAIApiDeploymentName;
530
+ }
531
+ if (!json.kwargs.deployment_name && this.azureOpenAIBasePath) {
532
+ const parts = this.azureOpenAIBasePath.split("/openai/deployments/");
533
+ if (parts.length === 2) {
534
+ const [, deployment] = parts;
535
+ json.kwargs.deployment_name = deployment;
536
+ }
537
+ }
538
+ if (json.kwargs.azure_endpoint &&
539
+ json.kwargs.deployment_name &&
540
+ json.kwargs.openai_api_base) {
541
+ delete json.kwargs.openai_api_base;
542
+ }
543
+ if (json.kwargs.azure_openai_api_instance_name &&
544
+ json.kwargs.azure_endpoint) {
545
+ delete json.kwargs.azure_openai_api_instance_name;
546
+ }
509
547
  }
510
548
  return json;
511
549
  }
@@ -864,6 +864,12 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
864
864
  writable: true,
865
865
  value: void 0
866
866
  });
867
+ Object.defineProperty(this, "azureOpenAIEndpoint", {
868
+ enumerable: true,
869
+ configurable: true,
870
+ writable: true,
871
+ value: void 0
872
+ });
867
873
  Object.defineProperty(this, "organization", {
868
874
  enumerable: true,
869
875
  configurable: true,
@@ -926,6 +932,9 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
926
932
  this.organization =
927
933
  fields?.configuration?.organization ??
928
934
  (0, env_1.getEnvironmentVariable)("OPENAI_ORGANIZATION");
935
+ this.azureOpenAIEndpoint =
936
+ fields?.azureOpenAIEndpoint ??
937
+ (0, env_1.getEnvironmentVariable)("AZURE_OPENAI_ENDPOINT");
929
938
  this.modelName = fields?.model ?? fields?.modelName ?? this.model;
930
939
  this.model = this.modelName;
931
940
  this.modelKwargs = fields?.modelKwargs ?? {};
@@ -944,9 +953,18 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
944
953
  this.user = fields?.user;
945
954
  this.__includeRawResponse = fields?.__includeRawResponse;
946
955
  if (this.azureOpenAIApiKey || this.azureADTokenProvider) {
947
- if (!this.azureOpenAIApiInstanceName && !this.azureOpenAIBasePath) {
956
+ if (!this.azureOpenAIApiInstanceName &&
957
+ !this.azureOpenAIBasePath &&
958
+ !this.azureOpenAIEndpoint) {
948
959
  throw new Error("Azure OpenAI API instance name not found");
949
960
  }
961
+ if (!this.azureOpenAIApiDeploymentName && this.azureOpenAIBasePath) {
962
+ const parts = this.azureOpenAIBasePath.split("/openai/deployments/");
963
+ if (parts.length === 2) {
964
+ const [, deployment] = parts;
965
+ this.azureOpenAIApiDeploymentName = deployment;
966
+ }
967
+ }
950
968
  if (!this.azureOpenAIApiDeploymentName) {
951
969
  throw new Error("Azure OpenAI API deployment name not found");
952
970
  }
@@ -1399,6 +1417,7 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
1399
1417
  azureOpenAIApiKey: this.azureOpenAIApiKey,
1400
1418
  azureOpenAIBasePath: this.azureOpenAIBasePath,
1401
1419
  baseURL: this.clientConfig.baseURL,
1420
+ azureOpenAIEndpoint: this.azureOpenAIEndpoint,
1402
1421
  };
1403
1422
  const endpoint = (0, azure_js_1.getEndpoint)(openAIEndpointConfig);
1404
1423
  const params = {
@@ -572,6 +572,7 @@ export declare class ChatOpenAI<CallOptions extends ChatOpenAICallOptions = Chat
572
572
  azureOpenAIApiInstanceName?: string;
573
573
  azureOpenAIApiDeploymentName?: string;
574
574
  azureOpenAIBasePath?: string;
575
+ azureOpenAIEndpoint?: string;
575
576
  organization?: string;
576
577
  __includeRawResponse?: boolean;
577
578
  protected client: OpenAIClient;
@@ -859,6 +859,12 @@ export class ChatOpenAI extends BaseChatModel {
859
859
  writable: true,
860
860
  value: void 0
861
861
  });
862
+ Object.defineProperty(this, "azureOpenAIEndpoint", {
863
+ enumerable: true,
864
+ configurable: true,
865
+ writable: true,
866
+ value: void 0
867
+ });
862
868
  Object.defineProperty(this, "organization", {
863
869
  enumerable: true,
864
870
  configurable: true,
@@ -921,6 +927,9 @@ export class ChatOpenAI extends BaseChatModel {
921
927
  this.organization =
922
928
  fields?.configuration?.organization ??
923
929
  getEnvironmentVariable("OPENAI_ORGANIZATION");
930
+ this.azureOpenAIEndpoint =
931
+ fields?.azureOpenAIEndpoint ??
932
+ getEnvironmentVariable("AZURE_OPENAI_ENDPOINT");
924
933
  this.modelName = fields?.model ?? fields?.modelName ?? this.model;
925
934
  this.model = this.modelName;
926
935
  this.modelKwargs = fields?.modelKwargs ?? {};
@@ -939,9 +948,18 @@ export class ChatOpenAI extends BaseChatModel {
939
948
  this.user = fields?.user;
940
949
  this.__includeRawResponse = fields?.__includeRawResponse;
941
950
  if (this.azureOpenAIApiKey || this.azureADTokenProvider) {
942
- if (!this.azureOpenAIApiInstanceName && !this.azureOpenAIBasePath) {
951
+ if (!this.azureOpenAIApiInstanceName &&
952
+ !this.azureOpenAIBasePath &&
953
+ !this.azureOpenAIEndpoint) {
943
954
  throw new Error("Azure OpenAI API instance name not found");
944
955
  }
956
+ if (!this.azureOpenAIApiDeploymentName && this.azureOpenAIBasePath) {
957
+ const parts = this.azureOpenAIBasePath.split("/openai/deployments/");
958
+ if (parts.length === 2) {
959
+ const [, deployment] = parts;
960
+ this.azureOpenAIApiDeploymentName = deployment;
961
+ }
962
+ }
945
963
  if (!this.azureOpenAIApiDeploymentName) {
946
964
  throw new Error("Azure OpenAI API deployment name not found");
947
965
  }
@@ -1394,6 +1412,7 @@ export class ChatOpenAI extends BaseChatModel {
1394
1412
  azureOpenAIApiKey: this.azureOpenAIApiKey,
1395
1413
  azureOpenAIBasePath: this.azureOpenAIBasePath,
1396
1414
  baseURL: this.clientConfig.baseURL,
1415
+ azureOpenAIEndpoint: this.azureOpenAIEndpoint,
1397
1416
  };
1398
1417
  const endpoint = getEndpoint(openAIEndpointConfig);
1399
1418
  const params = {
package/dist/types.d.ts CHANGED
@@ -170,11 +170,17 @@ export declare interface AzureOpenAIInput {
170
170
  */
171
171
  azureOpenAIApiCompletionsDeploymentName?: string;
172
172
  /**
173
- * Custom endpoint for Azure OpenAI API. This is useful in case you have a deployment in another region.
173
+ * Custom base url for Azure OpenAI API. This is useful in case you have a deployment in another region.
174
174
  * e.g. setting this value to "https://westeurope.api.cognitive.microsoft.com/openai/deployments"
175
175
  * will be result in the endpoint URL: https://westeurope.api.cognitive.microsoft.com/openai/deployments/{DeploymentName}/
176
176
  */
177
177
  azureOpenAIBasePath?: string;
178
+ /**
179
+ * Custom endpoint for Azure OpenAI API. This is useful in case you have a deployment in another region.
180
+ * e.g. setting this value to "https://westeurope.api.cognitive.microsoft.com/"
181
+ * will be result in the endpoint URL: https://westeurope.api.cognitive.microsoft.com/openai/deployments/{DeploymentName}/
182
+ */
183
+ azureOpenAIEndpoint?: string;
178
184
  /**
179
185
  * A function that returns an access token for Microsoft Entra (formerly known as Azure Active Directory),
180
186
  * which will be invoked on every request.
@@ -8,13 +8,15 @@ exports.getEndpoint = void 0;
8
8
  * @param {OpenAIEndpointConfig} config - The configuration object for the (Azure) endpoint.
9
9
  *
10
10
  * @property {string} config.azureOpenAIApiDeploymentName - The deployment name of Azure OpenAI.
11
- * @property {string} config.azureOpenAIApiInstanceName - The instance name of Azure OpenAI.
11
+ * @property {string} config.azureOpenAIApiInstanceName - The instance name of Azure OpenAI, e.g. `example-resource`.
12
12
  * @property {string} config.azureOpenAIApiKey - The API Key for Azure OpenAI.
13
- * @property {string} config.azureOpenAIBasePath - The base path for Azure OpenAI.
13
+ * @property {string} config.azureOpenAIBasePath - The base path for Azure OpenAI, e.g. `https://example-resource.azure.openai.com/openai/deployments/`.
14
14
  * @property {string} config.baseURL - Some other custom base path URL.
15
+ * @property {string} config.azureOpenAIEndpoint - The endpoint for the Azure OpenAI instance, e.g. `https://example-resource.azure.openai.com/`.
15
16
  *
16
17
  * The function operates as follows:
17
18
  * - If both `azureOpenAIBasePath` and `azureOpenAIApiDeploymentName` (plus `azureOpenAIApiKey`) are provided, it returns an URL combining these two parameters (`${azureOpenAIBasePath}/${azureOpenAIApiDeploymentName}`).
19
+ * - If both `azureOpenAIEndpoint` and `azureOpenAIApiDeploymentName` (plus `azureOpenAIApiKey`) are provided, it returns an URL combining these two parameters (`${azureOpenAIEndpoint}/openai/deployments/${azureOpenAIApiDeploymentName}`).
18
20
  * - If `azureOpenAIApiKey` is provided, it checks for `azureOpenAIApiInstanceName` and `azureOpenAIApiDeploymentName` and throws an error if any of these is missing. If both are provided, it generates an URL incorporating these parameters.
19
21
  * - If none of the above conditions are met, return any custom `baseURL`.
20
22
  * - The function returns the generated URL as a string, or undefined if no custom paths are specified.
@@ -24,12 +26,17 @@ exports.getEndpoint = void 0;
24
26
  * @returns {string | undefined} The generated (Azure) OpenAI endpoint URL.
25
27
  */
26
28
  function getEndpoint(config) {
27
- const { azureOpenAIApiDeploymentName, azureOpenAIApiInstanceName, azureOpenAIApiKey, azureOpenAIBasePath, baseURL, azureADTokenProvider, } = config;
29
+ const { azureOpenAIApiDeploymentName, azureOpenAIApiInstanceName, azureOpenAIApiKey, azureOpenAIBasePath, baseURL, azureADTokenProvider, azureOpenAIEndpoint, } = config;
28
30
  if ((azureOpenAIApiKey || azureADTokenProvider) &&
29
31
  azureOpenAIBasePath &&
30
32
  azureOpenAIApiDeploymentName) {
31
33
  return `${azureOpenAIBasePath}/${azureOpenAIApiDeploymentName}`;
32
34
  }
35
+ if ((azureOpenAIApiKey || azureADTokenProvider) &&
36
+ azureOpenAIEndpoint &&
37
+ azureOpenAIApiDeploymentName) {
38
+ return `${azureOpenAIEndpoint}/openai/deployments/${azureOpenAIApiDeploymentName}`;
39
+ }
33
40
  if (azureOpenAIApiKey || azureADTokenProvider) {
34
41
  if (!azureOpenAIApiInstanceName) {
35
42
  throw new Error("azureOpenAIApiInstanceName is required when using azureOpenAIApiKey");
@@ -5,6 +5,7 @@ export interface OpenAIEndpointConfig {
5
5
  azureADTokenProvider?: () => Promise<string>;
6
6
  azureOpenAIBasePath?: string;
7
7
  baseURL?: string | null;
8
+ azureOpenAIEndpoint?: string;
8
9
  }
9
10
  /**
10
11
  * This function generates an endpoint URL for (Azure) OpenAI
@@ -13,13 +14,15 @@ export interface OpenAIEndpointConfig {
13
14
  * @param {OpenAIEndpointConfig} config - The configuration object for the (Azure) endpoint.
14
15
  *
15
16
  * @property {string} config.azureOpenAIApiDeploymentName - The deployment name of Azure OpenAI.
16
- * @property {string} config.azureOpenAIApiInstanceName - The instance name of Azure OpenAI.
17
+ * @property {string} config.azureOpenAIApiInstanceName - The instance name of Azure OpenAI, e.g. `example-resource`.
17
18
  * @property {string} config.azureOpenAIApiKey - The API Key for Azure OpenAI.
18
- * @property {string} config.azureOpenAIBasePath - The base path for Azure OpenAI.
19
+ * @property {string} config.azureOpenAIBasePath - The base path for Azure OpenAI, e.g. `https://example-resource.azure.openai.com/openai/deployments/`.
19
20
  * @property {string} config.baseURL - Some other custom base path URL.
21
+ * @property {string} config.azureOpenAIEndpoint - The endpoint for the Azure OpenAI instance, e.g. `https://example-resource.azure.openai.com/`.
20
22
  *
21
23
  * The function operates as follows:
22
24
  * - If both `azureOpenAIBasePath` and `azureOpenAIApiDeploymentName` (plus `azureOpenAIApiKey`) are provided, it returns an URL combining these two parameters (`${azureOpenAIBasePath}/${azureOpenAIApiDeploymentName}`).
25
+ * - If both `azureOpenAIEndpoint` and `azureOpenAIApiDeploymentName` (plus `azureOpenAIApiKey`) are provided, it returns an URL combining these two parameters (`${azureOpenAIEndpoint}/openai/deployments/${azureOpenAIApiDeploymentName}`).
23
26
  * - If `azureOpenAIApiKey` is provided, it checks for `azureOpenAIApiInstanceName` and `azureOpenAIApiDeploymentName` and throws an error if any of these is missing. If both are provided, it generates an URL incorporating these parameters.
24
27
  * - If none of the above conditions are met, return any custom `baseURL`.
25
28
  * - The function returns the generated URL as a string, or undefined if no custom paths are specified.
@@ -5,13 +5,15 @@
5
5
  * @param {OpenAIEndpointConfig} config - The configuration object for the (Azure) endpoint.
6
6
  *
7
7
  * @property {string} config.azureOpenAIApiDeploymentName - The deployment name of Azure OpenAI.
8
- * @property {string} config.azureOpenAIApiInstanceName - The instance name of Azure OpenAI.
8
+ * @property {string} config.azureOpenAIApiInstanceName - The instance name of Azure OpenAI, e.g. `example-resource`.
9
9
  * @property {string} config.azureOpenAIApiKey - The API Key for Azure OpenAI.
10
- * @property {string} config.azureOpenAIBasePath - The base path for Azure OpenAI.
10
+ * @property {string} config.azureOpenAIBasePath - The base path for Azure OpenAI, e.g. `https://example-resource.azure.openai.com/openai/deployments/`.
11
11
  * @property {string} config.baseURL - Some other custom base path URL.
12
+ * @property {string} config.azureOpenAIEndpoint - The endpoint for the Azure OpenAI instance, e.g. `https://example-resource.azure.openai.com/`.
12
13
  *
13
14
  * The function operates as follows:
14
15
  * - If both `azureOpenAIBasePath` and `azureOpenAIApiDeploymentName` (plus `azureOpenAIApiKey`) are provided, it returns an URL combining these two parameters (`${azureOpenAIBasePath}/${azureOpenAIApiDeploymentName}`).
16
+ * - If both `azureOpenAIEndpoint` and `azureOpenAIApiDeploymentName` (plus `azureOpenAIApiKey`) are provided, it returns an URL combining these two parameters (`${azureOpenAIEndpoint}/openai/deployments/${azureOpenAIApiDeploymentName}`).
15
17
  * - If `azureOpenAIApiKey` is provided, it checks for `azureOpenAIApiInstanceName` and `azureOpenAIApiDeploymentName` and throws an error if any of these is missing. If both are provided, it generates an URL incorporating these parameters.
16
18
  * - If none of the above conditions are met, return any custom `baseURL`.
17
19
  * - The function returns the generated URL as a string, or undefined if no custom paths are specified.
@@ -21,12 +23,17 @@
21
23
  * @returns {string | undefined} The generated (Azure) OpenAI endpoint URL.
22
24
  */
23
25
  export function getEndpoint(config) {
24
- const { azureOpenAIApiDeploymentName, azureOpenAIApiInstanceName, azureOpenAIApiKey, azureOpenAIBasePath, baseURL, azureADTokenProvider, } = config;
26
+ const { azureOpenAIApiDeploymentName, azureOpenAIApiInstanceName, azureOpenAIApiKey, azureOpenAIBasePath, baseURL, azureADTokenProvider, azureOpenAIEndpoint, } = config;
25
27
  if ((azureOpenAIApiKey || azureADTokenProvider) &&
26
28
  azureOpenAIBasePath &&
27
29
  azureOpenAIApiDeploymentName) {
28
30
  return `${azureOpenAIBasePath}/${azureOpenAIApiDeploymentName}`;
29
31
  }
32
+ if ((azureOpenAIApiKey || azureADTokenProvider) &&
33
+ azureOpenAIEndpoint &&
34
+ azureOpenAIApiDeploymentName) {
35
+ return `${azureOpenAIEndpoint}/openai/deployments/${azureOpenAIApiDeploymentName}`;
36
+ }
30
37
  if (azureOpenAIApiKey || azureADTokenProvider) {
31
38
  if (!azureOpenAIApiInstanceName) {
32
39
  throw new Error("azureOpenAIApiInstanceName is required when using azureOpenAIApiKey");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/openai",
3
- "version": "0.3.2",
3
+ "version": "0.3.3",
4
4
  "description": "OpenAI integrations for LangChain.js",
5
5
  "type": "module",
6
6
  "engines": {