@langchain/openai 0.0.28 → 0.0.29

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.
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.AzureChatOpenAI = void 0;
4
+ const openai_1 = require("openai");
4
5
  const chat_models_js_1 = require("../chat_models.cjs");
6
+ const azure_js_1 = require("../utils/azure.cjs");
5
7
  class AzureChatOpenAI extends chat_models_js_1.ChatOpenAI {
6
8
  _llmType() {
7
9
  return "azure_openai";
@@ -14,21 +16,59 @@ class AzureChatOpenAI extends chat_models_js_1.ChatOpenAI {
14
16
  };
15
17
  }
16
18
  constructor(fields) {
17
- // assume the base URL does not contain "openai" nor "deployments" prefix
18
- let basePath = fields?.openAIBasePath ?? "";
19
- if (!basePath.endsWith("/"))
20
- basePath += "/";
21
- if (!basePath.endsWith("openai/deployments"))
22
- basePath += "openai/deployments";
23
19
  const newFields = fields ? { ...fields } : fields;
24
20
  if (newFields) {
25
- newFields.azureOpenAIBasePath = basePath;
26
21
  newFields.azureOpenAIApiDeploymentName = newFields.deploymentName;
27
22
  newFields.azureOpenAIApiKey = newFields.openAIApiKey;
28
23
  newFields.azureOpenAIApiVersion = newFields.openAIApiVersion;
29
24
  }
30
25
  super(newFields);
31
26
  }
27
+ _getClientOptions(options) {
28
+ if (!this.client) {
29
+ const openAIEndpointConfig = {
30
+ azureOpenAIApiDeploymentName: this.azureOpenAIApiDeploymentName,
31
+ azureOpenAIApiInstanceName: this.azureOpenAIApiInstanceName,
32
+ azureOpenAIApiKey: this.azureOpenAIApiKey,
33
+ azureOpenAIBasePath: this.azureOpenAIBasePath,
34
+ baseURL: this.clientConfig.baseURL,
35
+ };
36
+ const endpoint = (0, azure_js_1.getEndpoint)(openAIEndpointConfig);
37
+ const params = {
38
+ ...this.clientConfig,
39
+ baseURL: endpoint,
40
+ timeout: this.timeout,
41
+ maxRetries: 0,
42
+ };
43
+ if (!this.azureADTokenProvider) {
44
+ params.apiKey = openAIEndpointConfig.azureOpenAIApiKey;
45
+ }
46
+ if (!params.baseURL) {
47
+ delete params.baseURL;
48
+ }
49
+ this.client = new openai_1.AzureOpenAI({
50
+ apiVersion: this.azureOpenAIApiVersion,
51
+ azureADTokenProvider: this.azureADTokenProvider,
52
+ deployment: this.azureOpenAIApiDeploymentName,
53
+ ...params,
54
+ });
55
+ }
56
+ const requestOptions = {
57
+ ...this.clientConfig,
58
+ ...options,
59
+ };
60
+ if (this.azureOpenAIApiKey) {
61
+ requestOptions.headers = {
62
+ "api-key": this.azureOpenAIApiKey,
63
+ ...requestOptions.headers,
64
+ };
65
+ requestOptions.query = {
66
+ "api-version": this.azureOpenAIApiVersion,
67
+ ...requestOptions.query,
68
+ };
69
+ }
70
+ return requestOptions;
71
+ }
32
72
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
33
73
  toJSON() {
34
74
  const json = super.toJSON();
@@ -1,7 +1,7 @@
1
1
  import { type ClientOptions } from "openai";
2
2
  import { type BaseChatModelParams } from "@langchain/core/language_models/chat_models";
3
3
  import { ChatOpenAI } from "../chat_models.js";
4
- import { AzureOpenAIInput, LegacyOpenAIInput, OpenAIChatInput } from "../types.js";
4
+ import { AzureOpenAIInput, LegacyOpenAIInput, OpenAIChatInput, OpenAICoreRequestOptions } from "../types.js";
5
5
  export declare class AzureChatOpenAI extends ChatOpenAI {
6
6
  _llmType(): string;
7
7
  get lc_aliases(): Record<string, string>;
@@ -13,5 +13,6 @@ export declare class AzureChatOpenAI extends ChatOpenAI {
13
13
  } & BaseChatModelParams & {
14
14
  configuration?: ClientOptions & LegacyOpenAIInput;
15
15
  });
16
+ protected _getClientOptions(options: OpenAICoreRequestOptions | undefined): OpenAICoreRequestOptions;
16
17
  toJSON(): any;
17
18
  }
@@ -1,4 +1,6 @@
1
+ import { AzureOpenAI as AzureOpenAIClient } from "openai";
1
2
  import { ChatOpenAI } from "../chat_models.js";
3
+ import { getEndpoint } from "../utils/azure.js";
2
4
  export class AzureChatOpenAI extends ChatOpenAI {
3
5
  _llmType() {
4
6
  return "azure_openai";
@@ -11,21 +13,59 @@ export class AzureChatOpenAI extends ChatOpenAI {
11
13
  };
12
14
  }
13
15
  constructor(fields) {
14
- // assume the base URL does not contain "openai" nor "deployments" prefix
15
- let basePath = fields?.openAIBasePath ?? "";
16
- if (!basePath.endsWith("/"))
17
- basePath += "/";
18
- if (!basePath.endsWith("openai/deployments"))
19
- basePath += "openai/deployments";
20
16
  const newFields = fields ? { ...fields } : fields;
21
17
  if (newFields) {
22
- newFields.azureOpenAIBasePath = basePath;
23
18
  newFields.azureOpenAIApiDeploymentName = newFields.deploymentName;
24
19
  newFields.azureOpenAIApiKey = newFields.openAIApiKey;
25
20
  newFields.azureOpenAIApiVersion = newFields.openAIApiVersion;
26
21
  }
27
22
  super(newFields);
28
23
  }
24
+ _getClientOptions(options) {
25
+ if (!this.client) {
26
+ const openAIEndpointConfig = {
27
+ azureOpenAIApiDeploymentName: this.azureOpenAIApiDeploymentName,
28
+ azureOpenAIApiInstanceName: this.azureOpenAIApiInstanceName,
29
+ azureOpenAIApiKey: this.azureOpenAIApiKey,
30
+ azureOpenAIBasePath: this.azureOpenAIBasePath,
31
+ baseURL: this.clientConfig.baseURL,
32
+ };
33
+ const endpoint = getEndpoint(openAIEndpointConfig);
34
+ const params = {
35
+ ...this.clientConfig,
36
+ baseURL: endpoint,
37
+ timeout: this.timeout,
38
+ maxRetries: 0,
39
+ };
40
+ if (!this.azureADTokenProvider) {
41
+ params.apiKey = openAIEndpointConfig.azureOpenAIApiKey;
42
+ }
43
+ if (!params.baseURL) {
44
+ delete params.baseURL;
45
+ }
46
+ this.client = new AzureOpenAIClient({
47
+ apiVersion: this.azureOpenAIApiVersion,
48
+ azureADTokenProvider: this.azureADTokenProvider,
49
+ deployment: this.azureOpenAIApiDeploymentName,
50
+ ...params,
51
+ });
52
+ }
53
+ const requestOptions = {
54
+ ...this.clientConfig,
55
+ ...options,
56
+ };
57
+ if (this.azureOpenAIApiKey) {
58
+ requestOptions.headers = {
59
+ "api-key": this.azureOpenAIApiKey,
60
+ ...requestOptions.headers,
61
+ };
62
+ requestOptions.query = {
63
+ "api-version": this.azureOpenAIApiVersion,
64
+ ...requestOptions.query,
65
+ };
66
+ }
67
+ return requestOptions;
68
+ }
29
69
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
30
70
  toJSON() {
31
71
  const json = super.toJSON();
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AzureOpenAIEmbeddings = void 0;
4
+ const openai_1 = require("openai");
5
+ const embeddings_js_1 = require("../embeddings.cjs");
6
+ const azure_js_1 = require("../utils/azure.cjs");
7
+ const openai_js_1 = require("../utils/openai.cjs");
8
+ class AzureOpenAIEmbeddings extends embeddings_js_1.OpenAIEmbeddings {
9
+ constructor(fields, configuration) {
10
+ const newFields = { ...fields };
11
+ if (Object.entries(newFields).length) {
12
+ newFields.azureOpenAIApiDeploymentName = newFields.deploymentName;
13
+ newFields.azureOpenAIApiKey = newFields.apiKey;
14
+ newFields.azureOpenAIApiVersion = newFields.openAIApiVersion;
15
+ }
16
+ super(newFields, configuration);
17
+ }
18
+ async embeddingWithRetry(request) {
19
+ if (!this.client) {
20
+ const openAIEndpointConfig = {
21
+ azureOpenAIApiDeploymentName: this.azureOpenAIApiDeploymentName,
22
+ azureOpenAIApiInstanceName: this.azureOpenAIApiInstanceName,
23
+ azureOpenAIApiKey: this.azureOpenAIApiKey,
24
+ azureOpenAIBasePath: this.azureOpenAIBasePath,
25
+ baseURL: this.clientConfig.baseURL,
26
+ };
27
+ const endpoint = (0, azure_js_1.getEndpoint)(openAIEndpointConfig);
28
+ const params = {
29
+ ...this.clientConfig,
30
+ baseURL: endpoint,
31
+ timeout: this.timeout,
32
+ maxRetries: 0,
33
+ };
34
+ if (!this.azureADTokenProvider) {
35
+ params.apiKey = openAIEndpointConfig.azureOpenAIApiKey;
36
+ }
37
+ if (!params.baseURL) {
38
+ delete params.baseURL;
39
+ }
40
+ this.client = new openai_1.AzureOpenAI({
41
+ apiVersion: this.azureOpenAIApiVersion,
42
+ azureADTokenProvider: this.azureADTokenProvider,
43
+ deployment: this.azureOpenAIApiDeploymentName,
44
+ ...params,
45
+ });
46
+ }
47
+ const requestOptions = {};
48
+ if (this.azureOpenAIApiKey) {
49
+ requestOptions.headers = {
50
+ "api-key": this.azureOpenAIApiKey,
51
+ ...requestOptions.headers,
52
+ };
53
+ requestOptions.query = {
54
+ "api-version": this.azureOpenAIApiVersion,
55
+ ...requestOptions.query,
56
+ };
57
+ }
58
+ return this.caller.call(async () => {
59
+ try {
60
+ const res = await this.client.embeddings.create(request, requestOptions);
61
+ return res;
62
+ }
63
+ catch (e) {
64
+ const error = (0, openai_js_1.wrapOpenAIClientError)(e);
65
+ throw error;
66
+ }
67
+ });
68
+ }
69
+ }
70
+ exports.AzureOpenAIEmbeddings = AzureOpenAIEmbeddings;
@@ -0,0 +1,14 @@
1
+ import { type ClientOptions, OpenAI as OpenAIClient } from "openai";
2
+ import { OpenAIEmbeddings, OpenAIEmbeddingsParams } from "../embeddings.js";
3
+ import { AzureOpenAIInput, LegacyOpenAIInput } from "../types.js";
4
+ export declare class AzureOpenAIEmbeddings extends OpenAIEmbeddings {
5
+ constructor(fields?: Partial<OpenAIEmbeddingsParams> & Partial<AzureOpenAIInput> & {
6
+ verbose?: boolean;
7
+ /** The OpenAI API key to use. */
8
+ apiKey?: string;
9
+ configuration?: ClientOptions;
10
+ deploymentName?: string;
11
+ openAIApiVersion?: string;
12
+ }, configuration?: ClientOptions & LegacyOpenAIInput);
13
+ protected embeddingWithRetry(request: OpenAIClient.EmbeddingCreateParams): Promise<OpenAIClient.Embeddings.CreateEmbeddingResponse>;
14
+ }
@@ -0,0 +1,66 @@
1
+ import { AzureOpenAI as AzureOpenAIClient, } from "openai";
2
+ import { OpenAIEmbeddings } from "../embeddings.js";
3
+ import { getEndpoint } from "../utils/azure.js";
4
+ import { wrapOpenAIClientError } from "../utils/openai.js";
5
+ export class AzureOpenAIEmbeddings extends OpenAIEmbeddings {
6
+ constructor(fields, configuration) {
7
+ const newFields = { ...fields };
8
+ if (Object.entries(newFields).length) {
9
+ newFields.azureOpenAIApiDeploymentName = newFields.deploymentName;
10
+ newFields.azureOpenAIApiKey = newFields.apiKey;
11
+ newFields.azureOpenAIApiVersion = newFields.openAIApiVersion;
12
+ }
13
+ super(newFields, configuration);
14
+ }
15
+ async embeddingWithRetry(request) {
16
+ if (!this.client) {
17
+ const openAIEndpointConfig = {
18
+ azureOpenAIApiDeploymentName: this.azureOpenAIApiDeploymentName,
19
+ azureOpenAIApiInstanceName: this.azureOpenAIApiInstanceName,
20
+ azureOpenAIApiKey: this.azureOpenAIApiKey,
21
+ azureOpenAIBasePath: this.azureOpenAIBasePath,
22
+ baseURL: this.clientConfig.baseURL,
23
+ };
24
+ const endpoint = getEndpoint(openAIEndpointConfig);
25
+ const params = {
26
+ ...this.clientConfig,
27
+ baseURL: endpoint,
28
+ timeout: this.timeout,
29
+ maxRetries: 0,
30
+ };
31
+ if (!this.azureADTokenProvider) {
32
+ params.apiKey = openAIEndpointConfig.azureOpenAIApiKey;
33
+ }
34
+ if (!params.baseURL) {
35
+ delete params.baseURL;
36
+ }
37
+ this.client = new AzureOpenAIClient({
38
+ apiVersion: this.azureOpenAIApiVersion,
39
+ azureADTokenProvider: this.azureADTokenProvider,
40
+ deployment: this.azureOpenAIApiDeploymentName,
41
+ ...params,
42
+ });
43
+ }
44
+ const requestOptions = {};
45
+ if (this.azureOpenAIApiKey) {
46
+ requestOptions.headers = {
47
+ "api-key": this.azureOpenAIApiKey,
48
+ ...requestOptions.headers,
49
+ };
50
+ requestOptions.query = {
51
+ "api-version": this.azureOpenAIApiVersion,
52
+ ...requestOptions.query,
53
+ };
54
+ }
55
+ return this.caller.call(async () => {
56
+ try {
57
+ const res = await this.client.embeddings.create(request, requestOptions);
58
+ return res;
59
+ }
60
+ catch (e) {
61
+ const error = wrapOpenAIClientError(e);
62
+ throw error;
63
+ }
64
+ });
65
+ }
66
+ }
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.AzureOpenAI = void 0;
4
+ const openai_1 = require("openai");
4
5
  const llms_js_1 = require("../llms.cjs");
6
+ const azure_js_1 = require("../utils/azure.cjs");
5
7
  class AzureOpenAI extends llms_js_1.OpenAI {
6
8
  get lc_aliases() {
7
9
  return {
@@ -11,21 +13,58 @@ class AzureOpenAI extends llms_js_1.OpenAI {
11
13
  };
12
14
  }
13
15
  constructor(fields) {
14
- // assume the base URL does not contain "openai" nor "deployments" prefix
15
- let basePath = fields?.openAIBasePath ?? "";
16
- if (!basePath.endsWith("/"))
17
- basePath += "/";
18
- if (!basePath.endsWith("openai/deployments"))
19
- basePath += "openai/deployments";
20
16
  const newFields = fields ? { ...fields } : fields;
21
17
  if (newFields) {
22
- newFields.azureOpenAIBasePath = basePath;
23
18
  newFields.azureOpenAIApiDeploymentName = newFields.deploymentName;
24
19
  newFields.azureOpenAIApiKey = newFields.openAIApiKey;
25
20
  newFields.azureOpenAIApiVersion = newFields.openAIApiVersion;
26
21
  }
27
22
  super(newFields);
28
23
  }
24
+ _getClientOptions(options) {
25
+ if (!this.client) {
26
+ const openAIEndpointConfig = {
27
+ azureOpenAIApiDeploymentName: this.azureOpenAIApiDeploymentName,
28
+ azureOpenAIApiInstanceName: this.azureOpenAIApiInstanceName,
29
+ azureOpenAIApiKey: this.azureOpenAIApiKey,
30
+ azureOpenAIBasePath: this.azureOpenAIBasePath,
31
+ baseURL: this.clientConfig.baseURL,
32
+ };
33
+ const endpoint = (0, azure_js_1.getEndpoint)(openAIEndpointConfig);
34
+ const params = {
35
+ ...this.clientConfig,
36
+ baseURL: endpoint,
37
+ timeout: this.timeout,
38
+ maxRetries: 0,
39
+ };
40
+ if (!this.azureADTokenProvider) {
41
+ params.apiKey = openAIEndpointConfig.azureOpenAIApiKey;
42
+ }
43
+ if (!params.baseURL) {
44
+ delete params.baseURL;
45
+ }
46
+ this.client = new openai_1.AzureOpenAI({
47
+ apiVersion: this.azureOpenAIApiVersion,
48
+ azureADTokenProvider: this.azureADTokenProvider,
49
+ ...params,
50
+ });
51
+ }
52
+ const requestOptions = {
53
+ ...this.clientConfig,
54
+ ...options,
55
+ };
56
+ if (this.azureOpenAIApiKey) {
57
+ requestOptions.headers = {
58
+ "api-key": this.azureOpenAIApiKey,
59
+ ...requestOptions.headers,
60
+ };
61
+ requestOptions.query = {
62
+ "api-version": this.azureOpenAIApiVersion,
63
+ ...requestOptions.query,
64
+ };
65
+ }
66
+ return requestOptions;
67
+ }
29
68
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
30
69
  toJSON() {
31
70
  const json = super.toJSON();
@@ -1,7 +1,7 @@
1
1
  import { type ClientOptions } from "openai";
2
2
  import { type BaseLLMParams } from "@langchain/core/language_models/llms";
3
3
  import { OpenAI } from "../llms.js";
4
- import type { OpenAIInput, AzureOpenAIInput, LegacyOpenAIInput } from "../types.js";
4
+ import type { OpenAIInput, AzureOpenAIInput, OpenAICoreRequestOptions, LegacyOpenAIInput } from "../types.js";
5
5
  export declare class AzureOpenAI extends OpenAI {
6
6
  get lc_aliases(): Record<string, string>;
7
7
  constructor(fields?: Partial<OpenAIInput> & {
@@ -12,5 +12,6 @@ export declare class AzureOpenAI extends OpenAI {
12
12
  } & Partial<AzureOpenAIInput> & BaseLLMParams & {
13
13
  configuration?: ClientOptions & LegacyOpenAIInput;
14
14
  });
15
+ protected _getClientOptions(options: OpenAICoreRequestOptions | undefined): OpenAICoreRequestOptions;
15
16
  toJSON(): any;
16
17
  }
@@ -1,4 +1,6 @@
1
+ import { AzureOpenAI as AzureOpenAIClient } from "openai";
1
2
  import { OpenAI } from "../llms.js";
3
+ import { getEndpoint } from "../utils/azure.js";
2
4
  export class AzureOpenAI extends OpenAI {
3
5
  get lc_aliases() {
4
6
  return {
@@ -8,21 +10,58 @@ export class AzureOpenAI extends OpenAI {
8
10
  };
9
11
  }
10
12
  constructor(fields) {
11
- // assume the base URL does not contain "openai" nor "deployments" prefix
12
- let basePath = fields?.openAIBasePath ?? "";
13
- if (!basePath.endsWith("/"))
14
- basePath += "/";
15
- if (!basePath.endsWith("openai/deployments"))
16
- basePath += "openai/deployments";
17
13
  const newFields = fields ? { ...fields } : fields;
18
14
  if (newFields) {
19
- newFields.azureOpenAIBasePath = basePath;
20
15
  newFields.azureOpenAIApiDeploymentName = newFields.deploymentName;
21
16
  newFields.azureOpenAIApiKey = newFields.openAIApiKey;
22
17
  newFields.azureOpenAIApiVersion = newFields.openAIApiVersion;
23
18
  }
24
19
  super(newFields);
25
20
  }
21
+ _getClientOptions(options) {
22
+ if (!this.client) {
23
+ const openAIEndpointConfig = {
24
+ azureOpenAIApiDeploymentName: this.azureOpenAIApiDeploymentName,
25
+ azureOpenAIApiInstanceName: this.azureOpenAIApiInstanceName,
26
+ azureOpenAIApiKey: this.azureOpenAIApiKey,
27
+ azureOpenAIBasePath: this.azureOpenAIBasePath,
28
+ baseURL: this.clientConfig.baseURL,
29
+ };
30
+ const endpoint = getEndpoint(openAIEndpointConfig);
31
+ const params = {
32
+ ...this.clientConfig,
33
+ baseURL: endpoint,
34
+ timeout: this.timeout,
35
+ maxRetries: 0,
36
+ };
37
+ if (!this.azureADTokenProvider) {
38
+ params.apiKey = openAIEndpointConfig.azureOpenAIApiKey;
39
+ }
40
+ if (!params.baseURL) {
41
+ delete params.baseURL;
42
+ }
43
+ this.client = new AzureOpenAIClient({
44
+ apiVersion: this.azureOpenAIApiVersion,
45
+ azureADTokenProvider: this.azureADTokenProvider,
46
+ ...params,
47
+ });
48
+ }
49
+ const requestOptions = {
50
+ ...this.clientConfig,
51
+ ...options,
52
+ };
53
+ if (this.azureOpenAIApiKey) {
54
+ requestOptions.headers = {
55
+ "api-key": this.azureOpenAIApiKey,
56
+ ...requestOptions.headers,
57
+ };
58
+ requestOptions.query = {
59
+ "api-version": this.azureOpenAIApiVersion,
60
+ ...requestOptions.query,
61
+ };
62
+ }
63
+ return requestOptions;
64
+ }
26
65
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
27
66
  toJSON() {
28
67
  const json = super.toJSON();
@@ -371,6 +371,12 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
371
371
  writable: true,
372
372
  value: void 0
373
373
  });
374
+ Object.defineProperty(this, "azureADTokenProvider", {
375
+ enumerable: true,
376
+ configurable: true,
377
+ writable: true,
378
+ value: void 0
379
+ });
374
380
  Object.defineProperty(this, "azureOpenAIApiInstanceName", {
375
381
  enumerable: true,
376
382
  configurable: true,
@@ -415,8 +421,9 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
415
421
  this.azureOpenAIApiKey =
416
422
  fields?.azureOpenAIApiKey ??
417
423
  (0, env_1.getEnvironmentVariable)("AZURE_OPENAI_API_KEY");
418
- if (!this.azureOpenAIApiKey && !this.apiKey) {
419
- throw new Error("OpenAI or Azure OpenAI API key not found");
424
+ this.azureADTokenProvider = fields?.azureADTokenProvider ?? undefined;
425
+ if (!this.azureOpenAIApiKey && !this.apiKey && !this.azureADTokenProvider) {
426
+ throw new Error("OpenAI or Azure OpenAI API key or Token Provider not found");
420
427
  }
421
428
  this.azureOpenAIApiInstanceName =
422
429
  fields?.azureOpenAIApiInstanceName ??
@@ -450,7 +457,7 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
450
457
  this.stopSequences = this?.stop;
451
458
  this.user = fields?.user;
452
459
  this.streaming = fields?.streaming ?? false;
453
- if (this.azureOpenAIApiKey) {
460
+ if (this.azureOpenAIApiKey || this.azureADTokenProvider) {
454
461
  if (!this.azureOpenAIApiInstanceName && !this.azureOpenAIBasePath) {
455
462
  throw new Error("Azure OpenAI API instance name not found");
456
463
  }
@@ -91,12 +91,13 @@ export declare class ChatOpenAI<CallOptions extends ChatOpenAICallOptions = Chat
91
91
  apiKey?: string;
92
92
  azureOpenAIApiVersion?: string;
93
93
  azureOpenAIApiKey?: string;
94
+ azureADTokenProvider?: () => Promise<string>;
94
95
  azureOpenAIApiInstanceName?: string;
95
96
  azureOpenAIApiDeploymentName?: string;
96
97
  azureOpenAIBasePath?: string;
97
98
  organization?: string;
98
- private client;
99
- private clientConfig;
99
+ protected client: OpenAIClient;
100
+ protected clientConfig: ClientOptions;
100
101
  constructor(fields?: Partial<OpenAIChatInput> & Partial<AzureOpenAIInput> & BaseChatModelParams & {
101
102
  configuration?: ClientOptions & LegacyOpenAIInput;
102
103
  },
@@ -142,7 +143,7 @@ export declare class ChatOpenAI<CallOptions extends ChatOpenAICallOptions = Chat
142
143
  */
143
144
  completionWithRetry(request: OpenAIClient.Chat.ChatCompletionCreateParamsStreaming, options?: OpenAICoreRequestOptions): Promise<AsyncIterable<OpenAIClient.Chat.Completions.ChatCompletionChunk>>;
144
145
  completionWithRetry(request: OpenAIClient.Chat.ChatCompletionCreateParamsNonStreaming, options?: OpenAICoreRequestOptions): Promise<OpenAIClient.Chat.Completions.ChatCompletion>;
145
- private _getClientOptions;
146
+ protected _getClientOptions(options: OpenAICoreRequestOptions | undefined): OpenAICoreRequestOptions;
146
147
  _llmType(): string;
147
148
  /** @ignore */
148
149
  _combineLLMOutput(...llmOutputs: OpenAILLMOutput[]): OpenAILLMOutput;
@@ -367,6 +367,12 @@ export class ChatOpenAI extends BaseChatModel {
367
367
  writable: true,
368
368
  value: void 0
369
369
  });
370
+ Object.defineProperty(this, "azureADTokenProvider", {
371
+ enumerable: true,
372
+ configurable: true,
373
+ writable: true,
374
+ value: void 0
375
+ });
370
376
  Object.defineProperty(this, "azureOpenAIApiInstanceName", {
371
377
  enumerable: true,
372
378
  configurable: true,
@@ -411,8 +417,9 @@ export class ChatOpenAI extends BaseChatModel {
411
417
  this.azureOpenAIApiKey =
412
418
  fields?.azureOpenAIApiKey ??
413
419
  getEnvironmentVariable("AZURE_OPENAI_API_KEY");
414
- if (!this.azureOpenAIApiKey && !this.apiKey) {
415
- throw new Error("OpenAI or Azure OpenAI API key not found");
420
+ this.azureADTokenProvider = fields?.azureADTokenProvider ?? undefined;
421
+ if (!this.azureOpenAIApiKey && !this.apiKey && !this.azureADTokenProvider) {
422
+ throw new Error("OpenAI or Azure OpenAI API key or Token Provider not found");
416
423
  }
417
424
  this.azureOpenAIApiInstanceName =
418
425
  fields?.azureOpenAIApiInstanceName ??
@@ -446,7 +453,7 @@ export class ChatOpenAI extends BaseChatModel {
446
453
  this.stopSequences = this?.stop;
447
454
  this.user = fields?.user;
448
455
  this.streaming = fields?.streaming ?? false;
449
- if (this.azureOpenAIApiKey) {
456
+ if (this.azureOpenAIApiKey || this.azureADTokenProvider) {
450
457
  if (!this.azureOpenAIApiInstanceName && !this.azureOpenAIBasePath) {
451
458
  throw new Error("Azure OpenAI API instance name not found");
452
459
  }
@@ -79,6 +79,12 @@ class OpenAIEmbeddings extends embeddings_1.Embeddings {
79
79
  writable: true,
80
80
  value: void 0
81
81
  });
82
+ Object.defineProperty(this, "azureADTokenProvider", {
83
+ enumerable: true,
84
+ configurable: true,
85
+ writable: true,
86
+ value: void 0
87
+ });
82
88
  Object.defineProperty(this, "azureOpenAIApiInstanceName", {
83
89
  enumerable: true,
84
90
  configurable: true,
@@ -120,8 +126,9 @@ class OpenAIEmbeddings extends embeddings_1.Embeddings {
120
126
  (0, env_1.getEnvironmentVariable)("OPENAI_API_KEY");
121
127
  const azureApiKey = fieldsWithDefaults?.azureOpenAIApiKey ??
122
128
  (0, env_1.getEnvironmentVariable)("AZURE_OPENAI_API_KEY");
123
- if (!azureApiKey && !apiKey) {
124
- throw new Error("OpenAI or Azure OpenAI API key not found");
129
+ this.azureADTokenProvider = fields?.azureADTokenProvider ?? undefined;
130
+ if (!azureApiKey && !apiKey && !this.azureADTokenProvider) {
131
+ throw new Error("OpenAI or Azure OpenAI API key or Token Provider not found");
125
132
  }
126
133
  const azureApiInstanceName = fieldsWithDefaults?.azureOpenAIApiInstanceName ??
127
134
  (0, env_1.getEnvironmentVariable)("AZURE_OPENAI_API_INSTANCE_NAME");
@@ -150,7 +157,7 @@ class OpenAIEmbeddings extends embeddings_1.Embeddings {
150
157
  this.azureOpenAIApiKey = azureApiKey;
151
158
  this.azureOpenAIApiInstanceName = azureApiInstanceName;
152
159
  this.azureOpenAIApiDeploymentName = azureApiDeploymentName;
153
- if (this.azureOpenAIApiKey) {
160
+ if (this.azureOpenAIApiKey || this.azureADTokenProvider) {
154
161
  if (!this.azureOpenAIApiInstanceName && !this.azureOpenAIBasePath) {
155
162
  throw new Error("Azure OpenAI API instance name not found");
156
163
  }
@@ -1,4 +1,4 @@
1
- import { type ClientOptions } from "openai";
1
+ import { type ClientOptions, OpenAI as OpenAIClient } from "openai";
2
2
  import { Embeddings, type EmbeddingsParams } from "@langchain/core/embeddings";
3
3
  import { AzureOpenAIInput, LegacyOpenAIInput } from "./types.js";
4
4
  /**
@@ -62,12 +62,13 @@ export declare class OpenAIEmbeddings extends Embeddings implements OpenAIEmbedd
62
62
  timeout?: number;
63
63
  azureOpenAIApiVersion?: string;
64
64
  azureOpenAIApiKey?: string;
65
+ azureADTokenProvider?: () => Promise<string>;
65
66
  azureOpenAIApiInstanceName?: string;
66
67
  azureOpenAIApiDeploymentName?: string;
67
68
  azureOpenAIBasePath?: string;
68
69
  organization?: string;
69
- private client;
70
- private clientConfig;
70
+ protected client: OpenAIClient;
71
+ protected clientConfig: ClientOptions;
71
72
  constructor(fields?: Partial<OpenAIEmbeddingsParams> & Partial<AzureOpenAIInput> & {
72
73
  verbose?: boolean;
73
74
  /**
@@ -101,5 +102,5 @@ export declare class OpenAIEmbeddings extends Embeddings implements OpenAIEmbedd
101
102
  * @param request Request to send to the OpenAI API.
102
103
  * @returns Promise that resolves to the response from the API.
103
104
  */
104
- private embeddingWithRetry;
105
+ protected embeddingWithRetry(request: OpenAIClient.EmbeddingCreateParams): Promise<OpenAIClient.Embeddings.CreateEmbeddingResponse>;
105
106
  }