@langchain/openai 0.3.2 → 0.3.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.
@@ -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
  }
@@ -458,7 +458,10 @@ function _convertChatOpenAIToolTypeToOpenAITool(tool, fields) {
458
458
  * rating: z.number().optional().describe("How funny the joke is, from 1 to 10")
459
459
  * }).describe('Joke to tell user.');
460
460
  *
461
- * const structuredLlm = llm.withStructuredOutput(Joke, { name: "Joke" });
461
+ * const structuredLlm = llm.withStructuredOutput(Joke, {
462
+ * name: "Joke",
463
+ * strict: true, // Optionally enable OpenAI structured outputs
464
+ * });
462
465
  * const jokeResult = await structuredLlm.invoke("Tell me a joke about cats");
463
466
  * console.log(jokeResult);
464
467
  * ```
@@ -864,6 +867,12 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
864
867
  writable: true,
865
868
  value: void 0
866
869
  });
870
+ Object.defineProperty(this, "azureOpenAIEndpoint", {
871
+ enumerable: true,
872
+ configurable: true,
873
+ writable: true,
874
+ value: void 0
875
+ });
867
876
  Object.defineProperty(this, "organization", {
868
877
  enumerable: true,
869
878
  configurable: true,
@@ -926,6 +935,9 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
926
935
  this.organization =
927
936
  fields?.configuration?.organization ??
928
937
  (0, env_1.getEnvironmentVariable)("OPENAI_ORGANIZATION");
938
+ this.azureOpenAIEndpoint =
939
+ fields?.azureOpenAIEndpoint ??
940
+ (0, env_1.getEnvironmentVariable)("AZURE_OPENAI_ENDPOINT");
929
941
  this.modelName = fields?.model ?? fields?.modelName ?? this.model;
930
942
  this.model = this.modelName;
931
943
  this.modelKwargs = fields?.modelKwargs ?? {};
@@ -944,9 +956,18 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
944
956
  this.user = fields?.user;
945
957
  this.__includeRawResponse = fields?.__includeRawResponse;
946
958
  if (this.azureOpenAIApiKey || this.azureADTokenProvider) {
947
- if (!this.azureOpenAIApiInstanceName && !this.azureOpenAIBasePath) {
959
+ if (!this.azureOpenAIApiInstanceName &&
960
+ !this.azureOpenAIBasePath &&
961
+ !this.azureOpenAIEndpoint) {
948
962
  throw new Error("Azure OpenAI API instance name not found");
949
963
  }
964
+ if (!this.azureOpenAIApiDeploymentName && this.azureOpenAIBasePath) {
965
+ const parts = this.azureOpenAIBasePath.split("/openai/deployments/");
966
+ if (parts.length === 2) {
967
+ const [, deployment] = parts;
968
+ this.azureOpenAIApiDeploymentName = deployment;
969
+ }
970
+ }
950
971
  if (!this.azureOpenAIApiDeploymentName) {
951
972
  throw new Error("Azure OpenAI API deployment name not found");
952
973
  }
@@ -1087,20 +1108,6 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
1087
1108
  stream: true,
1088
1109
  };
1089
1110
  let defaultRole;
1090
- if (params.response_format &&
1091
- params.response_format.type === "json_schema") {
1092
- console.warn(`OpenAI does not yet support streaming with "response_format" set to "json_schema". Falling back to non-streaming mode.`);
1093
- const res = await this._generate(messages, options, runManager);
1094
- const chunk = new outputs_1.ChatGenerationChunk({
1095
- message: new messages_1.AIMessageChunk({
1096
- ...res.generations[0].message,
1097
- }),
1098
- text: res.generations[0].text,
1099
- generationInfo: res.generations[0].generationInfo,
1100
- });
1101
- yield chunk;
1102
- return runManager?.handleLLMNewToken(res.generations[0].text ?? "", undefined, undefined, undefined, undefined, { chunk });
1103
- }
1104
1111
  const streamIterable = await this.completionWithRetry(params, options);
1105
1112
  let usage;
1106
1113
  for await (const data of streamIterable) {
@@ -1399,6 +1406,7 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
1399
1406
  azureOpenAIApiKey: this.azureOpenAIApiKey,
1400
1407
  azureOpenAIBasePath: this.azureOpenAIBasePath,
1401
1408
  baseURL: this.clientConfig.baseURL,
1409
+ azureOpenAIEndpoint: this.azureOpenAIEndpoint,
1402
1410
  };
1403
1411
  const endpoint = (0, azure_js_1.getEndpoint)(openAIEndpointConfig);
1404
1412
  const params = {
@@ -334,7 +334,10 @@ export interface ChatOpenAIFields extends Partial<OpenAIChatInput>, Partial<Azur
334
334
  * rating: z.number().optional().describe("How funny the joke is, from 1 to 10")
335
335
  * }).describe('Joke to tell user.');
336
336
  *
337
- * const structuredLlm = llm.withStructuredOutput(Joke, { name: "Joke" });
337
+ * const structuredLlm = llm.withStructuredOutput(Joke, {
338
+ * name: "Joke",
339
+ * strict: true, // Optionally enable OpenAI structured outputs
340
+ * });
338
341
  * const jokeResult = await structuredLlm.invoke("Tell me a joke about cats");
339
342
  * console.log(jokeResult);
340
343
  * ```
@@ -572,6 +575,7 @@ export declare class ChatOpenAI<CallOptions extends ChatOpenAICallOptions = Chat
572
575
  azureOpenAIApiInstanceName?: string;
573
576
  azureOpenAIApiDeploymentName?: string;
574
577
  azureOpenAIBasePath?: string;
578
+ azureOpenAIEndpoint?: string;
575
579
  organization?: string;
576
580
  __includeRawResponse?: boolean;
577
581
  protected client: OpenAIClient;
@@ -453,7 +453,10 @@ function _convertChatOpenAIToolTypeToOpenAITool(tool, fields) {
453
453
  * rating: z.number().optional().describe("How funny the joke is, from 1 to 10")
454
454
  * }).describe('Joke to tell user.');
455
455
  *
456
- * const structuredLlm = llm.withStructuredOutput(Joke, { name: "Joke" });
456
+ * const structuredLlm = llm.withStructuredOutput(Joke, {
457
+ * name: "Joke",
458
+ * strict: true, // Optionally enable OpenAI structured outputs
459
+ * });
457
460
  * const jokeResult = await structuredLlm.invoke("Tell me a joke about cats");
458
461
  * console.log(jokeResult);
459
462
  * ```
@@ -859,6 +862,12 @@ export class ChatOpenAI extends BaseChatModel {
859
862
  writable: true,
860
863
  value: void 0
861
864
  });
865
+ Object.defineProperty(this, "azureOpenAIEndpoint", {
866
+ enumerable: true,
867
+ configurable: true,
868
+ writable: true,
869
+ value: void 0
870
+ });
862
871
  Object.defineProperty(this, "organization", {
863
872
  enumerable: true,
864
873
  configurable: true,
@@ -921,6 +930,9 @@ export class ChatOpenAI extends BaseChatModel {
921
930
  this.organization =
922
931
  fields?.configuration?.organization ??
923
932
  getEnvironmentVariable("OPENAI_ORGANIZATION");
933
+ this.azureOpenAIEndpoint =
934
+ fields?.azureOpenAIEndpoint ??
935
+ getEnvironmentVariable("AZURE_OPENAI_ENDPOINT");
924
936
  this.modelName = fields?.model ?? fields?.modelName ?? this.model;
925
937
  this.model = this.modelName;
926
938
  this.modelKwargs = fields?.modelKwargs ?? {};
@@ -939,9 +951,18 @@ export class ChatOpenAI extends BaseChatModel {
939
951
  this.user = fields?.user;
940
952
  this.__includeRawResponse = fields?.__includeRawResponse;
941
953
  if (this.azureOpenAIApiKey || this.azureADTokenProvider) {
942
- if (!this.azureOpenAIApiInstanceName && !this.azureOpenAIBasePath) {
954
+ if (!this.azureOpenAIApiInstanceName &&
955
+ !this.azureOpenAIBasePath &&
956
+ !this.azureOpenAIEndpoint) {
943
957
  throw new Error("Azure OpenAI API instance name not found");
944
958
  }
959
+ if (!this.azureOpenAIApiDeploymentName && this.azureOpenAIBasePath) {
960
+ const parts = this.azureOpenAIBasePath.split("/openai/deployments/");
961
+ if (parts.length === 2) {
962
+ const [, deployment] = parts;
963
+ this.azureOpenAIApiDeploymentName = deployment;
964
+ }
965
+ }
945
966
  if (!this.azureOpenAIApiDeploymentName) {
946
967
  throw new Error("Azure OpenAI API deployment name not found");
947
968
  }
@@ -1082,20 +1103,6 @@ export class ChatOpenAI extends BaseChatModel {
1082
1103
  stream: true,
1083
1104
  };
1084
1105
  let defaultRole;
1085
- if (params.response_format &&
1086
- params.response_format.type === "json_schema") {
1087
- console.warn(`OpenAI does not yet support streaming with "response_format" set to "json_schema". Falling back to non-streaming mode.`);
1088
- const res = await this._generate(messages, options, runManager);
1089
- const chunk = new ChatGenerationChunk({
1090
- message: new AIMessageChunk({
1091
- ...res.generations[0].message,
1092
- }),
1093
- text: res.generations[0].text,
1094
- generationInfo: res.generations[0].generationInfo,
1095
- });
1096
- yield chunk;
1097
- return runManager?.handleLLMNewToken(res.generations[0].text ?? "", undefined, undefined, undefined, undefined, { chunk });
1098
- }
1099
1106
  const streamIterable = await this.completionWithRetry(params, options);
1100
1107
  let usage;
1101
1108
  for await (const data of streamIterable) {
@@ -1394,6 +1401,7 @@ export class ChatOpenAI extends BaseChatModel {
1394
1401
  azureOpenAIApiKey: this.azureOpenAIApiKey,
1395
1402
  azureOpenAIBasePath: this.azureOpenAIBasePath,
1396
1403
  baseURL: this.clientConfig.baseURL,
1404
+ azureOpenAIEndpoint: this.azureOpenAIEndpoint,
1397
1405
  };
1398
1406
  const endpoint = getEndpoint(openAIEndpointConfig);
1399
1407
  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.4",
4
4
  "description": "OpenAI integrations for LangChain.js",
5
5
  "type": "module",
6
6
  "engines": {