@langchain/openai 0.0.27 → 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();
@@ -48,12 +48,30 @@ function messageToOpenAIRole(message) {
48
48
  }
49
49
  exports.messageToOpenAIRole = messageToOpenAIRole;
50
50
  function openAIResponseToChatMessage(message) {
51
+ const rawToolCalls = message.tool_calls;
51
52
  switch (message.role) {
52
- case "assistant":
53
- return new messages_1.AIMessage(message.content || "", {
54
- function_call: message.function_call,
55
- tool_calls: message.tool_calls,
53
+ case "assistant": {
54
+ const toolCalls = [];
55
+ const invalidToolCalls = [];
56
+ for (const rawToolCall of rawToolCalls ?? []) {
57
+ try {
58
+ toolCalls.push((0, openai_tools_1.parseToolCall)(rawToolCall, { returnId: true }));
59
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
60
+ }
61
+ catch (e) {
62
+ invalidToolCalls.push((0, openai_tools_1.makeInvalidToolCall)(rawToolCall, e.message));
63
+ }
64
+ }
65
+ return new messages_1.AIMessage({
66
+ content: message.content || "",
67
+ tool_calls: toolCalls,
68
+ invalid_tool_calls: invalidToolCalls,
69
+ additional_kwargs: {
70
+ function_call: message.function_call,
71
+ tool_calls: rawToolCalls,
72
+ },
56
73
  });
74
+ }
57
75
  default:
58
76
  return new messages_1.ChatMessage(message.content || "", message.role ?? "unknown");
59
77
  }
@@ -81,7 +99,22 @@ delta, defaultRole) {
81
99
  return new messages_1.HumanMessageChunk({ content });
82
100
  }
83
101
  else if (role === "assistant") {
84
- return new messages_1.AIMessageChunk({ content, additional_kwargs });
102
+ const toolCallChunks = [];
103
+ if (Array.isArray(delta.tool_calls)) {
104
+ for (const rawToolCall of delta.tool_calls) {
105
+ toolCallChunks.push({
106
+ name: rawToolCall.function?.name,
107
+ args: rawToolCall.function?.arguments,
108
+ id: rawToolCall.id,
109
+ index: rawToolCall.index,
110
+ });
111
+ }
112
+ }
113
+ return new messages_1.AIMessageChunk({
114
+ content,
115
+ tool_call_chunks: toolCallChunks,
116
+ additional_kwargs,
117
+ });
85
118
  }
86
119
  else if (role === "system") {
87
120
  return new messages_1.SystemMessageChunk({ content });
@@ -118,11 +151,16 @@ function convertMessagesToOpenAIParams(messages) {
118
151
  if (message.additional_kwargs.function_call != null) {
119
152
  completionParam.function_call = message.additional_kwargs.function_call;
120
153
  }
121
- if (message.additional_kwargs.tool_calls != null) {
122
- completionParam.tool_calls = message.additional_kwargs.tool_calls;
154
+ if ((0, messages_1.isAIMessage)(message) && !!message.tool_calls?.length) {
155
+ completionParam.tool_calls = message.tool_calls.map(openai_tools_1.convertLangChainToolCallToOpenAI);
123
156
  }
124
- if (message.tool_call_id != null) {
125
- completionParam.tool_call_id = message.tool_call_id;
157
+ else {
158
+ if (message.additional_kwargs.tool_calls != null) {
159
+ completionParam.tool_calls = message.additional_kwargs.tool_calls;
160
+ }
161
+ if (message.tool_call_id != null) {
162
+ completionParam.tool_call_id = message.tool_call_id;
163
+ }
126
164
  }
127
165
  return completionParam;
128
166
  });
@@ -333,6 +371,12 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
333
371
  writable: true,
334
372
  value: void 0
335
373
  });
374
+ Object.defineProperty(this, "azureADTokenProvider", {
375
+ enumerable: true,
376
+ configurable: true,
377
+ writable: true,
378
+ value: void 0
379
+ });
336
380
  Object.defineProperty(this, "azureOpenAIApiInstanceName", {
337
381
  enumerable: true,
338
382
  configurable: true,
@@ -377,8 +421,9 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
377
421
  this.azureOpenAIApiKey =
378
422
  fields?.azureOpenAIApiKey ??
379
423
  (0, env_1.getEnvironmentVariable)("AZURE_OPENAI_API_KEY");
380
- if (!this.azureOpenAIApiKey && !this.apiKey) {
381
- 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");
382
427
  }
383
428
  this.azureOpenAIApiInstanceName =
384
429
  fields?.azureOpenAIApiInstanceName ??
@@ -412,7 +457,7 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
412
457
  this.stopSequences = this?.stop;
413
458
  this.user = fields?.user;
414
459
  this.streaming = fields?.streaming ?? false;
415
- if (this.azureOpenAIApiKey) {
460
+ if (this.azureOpenAIApiKey || this.azureADTokenProvider) {
416
461
  if (!this.azureOpenAIApiInstanceName && !this.azureOpenAIBasePath) {
417
462
  throw new Error("Azure OpenAI API instance name not found");
418
463
  }
@@ -437,6 +482,12 @@ class ChatOpenAI extends chat_models_1.BaseChatModel {
437
482
  ...fields?.configuration,
438
483
  };
439
484
  }
485
+ bindTools(tools, kwargs) {
486
+ return this.bind({
487
+ tools: tools.map(function_calling_1.convertToOpenAITool),
488
+ ...kwargs,
489
+ });
490
+ }
440
491
  /**
441
492
  * Get the parameters used to invoke the model
442
493
  */
@@ -1,12 +1,12 @@
1
1
  import { type ClientOptions, OpenAI as OpenAIClient } from "openai";
2
2
  import { CallbackManagerForLLMRun } from "@langchain/core/callbacks/manager";
3
- import { type BaseMessage } from "@langchain/core/messages";
3
+ import { AIMessageChunk, type BaseMessage } from "@langchain/core/messages";
4
4
  import { ChatGenerationChunk, type ChatResult } from "@langchain/core/outputs";
5
5
  import { type StructuredToolInterface } from "@langchain/core/tools";
6
6
  import { BaseChatModel, type BaseChatModelParams } from "@langchain/core/language_models/chat_models";
7
7
  import type { BaseFunctionCallOptions, BaseLanguageModelInput, StructuredOutputMethodOptions, StructuredOutputMethodParams } from "@langchain/core/language_models/base";
8
8
  import { z } from "zod";
9
- import { Runnable } from "@langchain/core/runnables";
9
+ import { Runnable, RunnableInterface } from "@langchain/core/runnables";
10
10
  import type { AzureOpenAIInput, OpenAICallOptions, OpenAIChatInput, OpenAICoreRequestOptions, LegacyOpenAIInput } from "./types.js";
11
11
  export type { AzureOpenAIInput, OpenAICallOptions, OpenAIChatInput };
12
12
  interface TokenUsage {
@@ -62,7 +62,7 @@ export interface ChatOpenAICallOptions extends OpenAICallOptions, BaseFunctionCa
62
62
  *
63
63
  * ```
64
64
  */
65
- export declare class ChatOpenAI<CallOptions extends ChatOpenAICallOptions = ChatOpenAICallOptions> extends BaseChatModel<CallOptions> implements OpenAIChatInput, AzureOpenAIInput {
65
+ export declare class ChatOpenAI<CallOptions extends ChatOpenAICallOptions = ChatOpenAICallOptions> extends BaseChatModel<CallOptions, AIMessageChunk> implements OpenAIChatInput, AzureOpenAIInput {
66
66
  static lc_name(): string;
67
67
  get callKeys(): string[];
68
68
  lc_serializable: boolean;
@@ -91,17 +91,19 @@ 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
  },
103
104
  /** @deprecated */
104
105
  configuration?: ClientOptions & LegacyOpenAIInput);
106
+ bindTools(tools: (Record<string, unknown> | StructuredToolInterface)[], kwargs?: Partial<CallOptions>): RunnableInterface<BaseLanguageModelInput, AIMessageChunk, CallOptions>;
105
107
  /**
106
108
  * Get the parameters used to invoke the model
107
109
  */
@@ -141,7 +143,7 @@ export declare class ChatOpenAI<CallOptions extends ChatOpenAICallOptions = Chat
141
143
  */
142
144
  completionWithRetry(request: OpenAIClient.Chat.ChatCompletionCreateParamsStreaming, options?: OpenAICoreRequestOptions): Promise<AsyncIterable<OpenAIClient.Chat.Completions.ChatCompletionChunk>>;
143
145
  completionWithRetry(request: OpenAIClient.Chat.ChatCompletionCreateParamsNonStreaming, options?: OpenAICoreRequestOptions): Promise<OpenAIClient.Chat.Completions.ChatCompletion>;
144
- private _getClientOptions;
146
+ protected _getClientOptions(options: OpenAICoreRequestOptions | undefined): OpenAICoreRequestOptions;
145
147
  _llmType(): string;
146
148
  /** @ignore */
147
149
  _combineLLMOutput(...llmOutputs: OpenAILLMOutput[]): OpenAILLMOutput;