@librechat/agents 2.4.16 → 2.4.18

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,10 +1,12 @@
1
1
  'use strict';
2
2
 
3
+ var openai$1 = require('openai');
3
4
  var xai = require('@langchain/xai');
4
5
  var deepseek = require('@langchain/deepseek');
5
6
  var openai = require('@langchain/openai');
6
7
 
7
8
  class CustomOpenAIClient extends openai.OpenAIClient {
9
+ abortHandler;
8
10
  async fetchWithTimeout(url, init, ms, controller) {
9
11
  const { signal, ...options } = init || {};
10
12
  const handler = () => controller.abort();
@@ -25,8 +27,33 @@ class CustomOpenAIClient extends openai.OpenAIClient {
25
27
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
26
28
  /** @ts-ignore */
27
29
  this.fetch.call(undefined, url, fetchOptions).finally(() => {
28
- if (signal)
29
- signal.removeEventListener('abort', handler);
30
+ clearTimeout(timeout);
31
+ }));
32
+ }
33
+ }
34
+ class CustomAzureOpenAIClient extends openai$1.AzureOpenAI {
35
+ abortHandler;
36
+ async fetchWithTimeout(url, init, ms, controller) {
37
+ const { signal, ...options } = init || {};
38
+ const handler = () => controller.abort();
39
+ this.abortHandler = handler;
40
+ if (signal)
41
+ signal.addEventListener('abort', handler);
42
+ const timeout = setTimeout(() => handler, ms);
43
+ const fetchOptions = {
44
+ signal: controller.signal,
45
+ ...options,
46
+ };
47
+ if (fetchOptions.method != null) {
48
+ // Custom methods like 'patch' need to be uppercased
49
+ // See https://github.com/nodejs/undici/issues/2294
50
+ fetchOptions.method = fetchOptions.method.toUpperCase();
51
+ }
52
+ return (
53
+ // use undefined this binding; fetch errors if bound to something else in browser/cloudflare
54
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
55
+ /** @ts-ignore */
56
+ this.fetch.call(undefined, url, fetchOptions).finally(() => {
30
57
  clearTimeout(timeout);
31
58
  }));
32
59
  }
@@ -60,6 +87,11 @@ class AzureChatOpenAI extends openai.AzureChatOpenAI {
60
87
  _getClientOptions(options) {
61
88
  if (!this.client) {
62
89
  const openAIEndpointConfig = {
90
+ azureOpenAIApiDeploymentName: this.azureOpenAIApiDeploymentName,
91
+ azureOpenAIApiInstanceName: this.azureOpenAIApiInstanceName,
92
+ azureOpenAIApiKey: this.azureOpenAIApiKey,
93
+ azureOpenAIBasePath: this.azureOpenAIBasePath,
94
+ azureADTokenProvider: this.azureADTokenProvider,
63
95
  baseURL: this.clientConfig.baseURL,
64
96
  };
65
97
  const endpoint = openai.getEndpoint(openAIEndpointConfig);
@@ -69,15 +101,38 @@ class AzureChatOpenAI extends openai.AzureChatOpenAI {
69
101
  timeout: this.timeout,
70
102
  maxRetries: 0,
71
103
  };
104
+ if (!this.azureADTokenProvider) {
105
+ params.apiKey = openAIEndpointConfig.azureOpenAIApiKey;
106
+ }
72
107
  if (params.baseURL == null) {
73
108
  delete params.baseURL;
74
109
  }
75
- this.client = new CustomOpenAIClient(params);
110
+ params.defaultHeaders = {
111
+ ...params.defaultHeaders,
112
+ 'User-Agent': params.defaultHeaders?.['User-Agent'] != null
113
+ ? `${params.defaultHeaders['User-Agent']}: langchainjs-azure-openai-v2`
114
+ : 'langchainjs-azure-openai-v2',
115
+ };
116
+ this.client = new CustomAzureOpenAIClient({
117
+ apiVersion: this.azureOpenAIApiVersion,
118
+ azureADTokenProvider: this.azureADTokenProvider,
119
+ ...params,
120
+ });
76
121
  }
77
122
  const requestOptions = {
78
123
  ...this.clientConfig,
79
124
  ...options,
80
125
  };
126
+ if (this.azureOpenAIApiKey != null) {
127
+ requestOptions.headers = {
128
+ 'api-key': this.azureOpenAIApiKey,
129
+ ...requestOptions.headers,
130
+ };
131
+ requestOptions.query = {
132
+ 'api-version': this.azureOpenAIApiVersion,
133
+ ...requestOptions.query,
134
+ };
135
+ }
81
136
  return requestOptions;
82
137
  }
83
138
  }
@@ -136,5 +191,6 @@ exports.AzureChatOpenAI = AzureChatOpenAI;
136
191
  exports.ChatDeepSeek = ChatDeepSeek;
137
192
  exports.ChatOpenAI = ChatOpenAI;
138
193
  exports.ChatXAI = ChatXAI;
194
+ exports.CustomAzureOpenAIClient = CustomAzureOpenAIClient;
139
195
  exports.CustomOpenAIClient = CustomOpenAIClient;
140
196
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../../../../src/llm/openai/index.ts"],"sourcesContent":["import { ChatXAI as OriginalChatXAI } from '@langchain/xai';\nimport { ChatDeepSeek as OriginalChatDeepSeek } from '@langchain/deepseek';\nimport {\n getEndpoint,\n OpenAIClient,\n ChatOpenAI as OriginalChatOpenAI,\n AzureChatOpenAI as OriginalAzureChatOpenAI,\n} from '@langchain/openai';\nimport type * as t from '@langchain/openai';\n\nexport class CustomOpenAIClient extends OpenAIClient {\n async fetchWithTimeout(\n url: RequestInfo,\n init: RequestInit | undefined,\n ms: number,\n controller: AbortController\n ): Promise<Response> {\n const { signal, ...options } = init || {};\n const handler = (): void => controller.abort();\n if (signal) signal.addEventListener('abort', handler);\n\n const timeout = setTimeout(() => handler, ms);\n\n const fetchOptions = {\n signal: controller.signal as AbortSignal,\n ...options,\n };\n if (fetchOptions.method != null) {\n // Custom methods like 'patch' need to be uppercased\n // See https://github.com/nodejs/undici/issues/2294\n fetchOptions.method = fetchOptions.method.toUpperCase();\n }\n\n return (\n // use undefined this binding; fetch errors if bound to something else in browser/cloudflare\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n /** @ts-ignore */\n this.fetch.call(undefined, url, fetchOptions).finally(() => {\n if (signal) signal.removeEventListener('abort', handler);\n clearTimeout(timeout);\n })\n );\n }\n}\n\nexport class ChatOpenAI extends OriginalChatOpenAI<t.ChatOpenAICallOptions> {\n protected _getClientOptions(\n options?: t.OpenAICoreRequestOptions\n ): t.OpenAICoreRequestOptions {\n if (!(this.client as OpenAIClient | undefined)) {\n const openAIEndpointConfig: t.OpenAIEndpointConfig = {\n baseURL: this.clientConfig.baseURL,\n };\n\n const endpoint = getEndpoint(openAIEndpointConfig);\n const params = {\n ...this.clientConfig,\n baseURL: endpoint,\n timeout: this.timeout,\n maxRetries: 0,\n };\n if (params.baseURL == null) {\n delete params.baseURL;\n }\n\n this.client = new CustomOpenAIClient(params);\n }\n const requestOptions = {\n ...this.clientConfig,\n ...options,\n } as t.OpenAICoreRequestOptions;\n return requestOptions;\n }\n}\n\nexport class AzureChatOpenAI extends OriginalAzureChatOpenAI {\n protected _getClientOptions(\n options?: t.OpenAICoreRequestOptions\n ): t.OpenAICoreRequestOptions {\n if (!(this.client as OpenAIClient | undefined)) {\n const openAIEndpointConfig: t.OpenAIEndpointConfig = {\n baseURL: this.clientConfig.baseURL,\n };\n\n const endpoint = getEndpoint(openAIEndpointConfig);\n const params = {\n ...this.clientConfig,\n baseURL: endpoint,\n timeout: this.timeout,\n maxRetries: 0,\n };\n if (params.baseURL == null) {\n delete params.baseURL;\n }\n\n this.client = new CustomOpenAIClient(params);\n }\n const requestOptions = {\n ...this.clientConfig,\n ...options,\n } as t.OpenAICoreRequestOptions;\n return requestOptions;\n }\n}\n\nexport class ChatDeepSeek extends OriginalChatDeepSeek {\n protected _getClientOptions(\n options?: t.OpenAICoreRequestOptions\n ): t.OpenAICoreRequestOptions {\n if (!(this.client as OpenAIClient | undefined)) {\n const openAIEndpointConfig: t.OpenAIEndpointConfig = {\n baseURL: this.clientConfig.baseURL,\n };\n\n const endpoint = getEndpoint(openAIEndpointConfig);\n const params = {\n ...this.clientConfig,\n baseURL: endpoint,\n timeout: this.timeout,\n maxRetries: 0,\n };\n if (params.baseURL == null) {\n delete params.baseURL;\n }\n\n this.client = new CustomOpenAIClient(params);\n }\n const requestOptions = {\n ...this.clientConfig,\n ...options,\n } as t.OpenAICoreRequestOptions;\n return requestOptions;\n }\n}\n\nexport class ChatXAI extends OriginalChatXAI {\n protected _getClientOptions(\n options?: t.OpenAICoreRequestOptions\n ): t.OpenAICoreRequestOptions {\n if (!(this.client as OpenAIClient | undefined)) {\n const openAIEndpointConfig: t.OpenAIEndpointConfig = {\n baseURL: this.clientConfig.baseURL,\n };\n\n const endpoint = getEndpoint(openAIEndpointConfig);\n const params = {\n ...this.clientConfig,\n baseURL: endpoint,\n timeout: this.timeout,\n maxRetries: 0,\n };\n if (params.baseURL == null) {\n delete params.baseURL;\n }\n\n this.client = new CustomOpenAIClient(params);\n }\n const requestOptions = {\n ...this.clientConfig,\n ...options,\n } as t.OpenAICoreRequestOptions;\n return requestOptions;\n }\n}\n"],"names":["OpenAIClient","OriginalChatOpenAI","getEndpoint","OriginalAzureChatOpenAI","OriginalChatDeepSeek","OriginalChatXAI"],"mappings":";;;;;;AAUM,MAAO,kBAAmB,SAAQA,mBAAY,CAAA;IAClD,MAAM,gBAAgB,CACpB,GAAgB,EAChB,IAA6B,EAC7B,EAAU,EACV,UAA2B,EAAA;QAE3B,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,EAAE,GAAG,IAAI,IAAI,EAAE;QACzC,MAAM,OAAO,GAAG,MAAY,UAAU,CAAC,KAAK,EAAE;AAC9C,QAAA,IAAI,MAAM;AAAE,YAAA,MAAM,CAAC,gBAAgB,CAAC,OAAO,EAAE,OAAO,CAAC;QAErD,MAAM,OAAO,GAAG,UAAU,CAAC,MAAM,OAAO,EAAE,EAAE,CAAC;AAE7C,QAAA,MAAM,YAAY,GAAG;YACnB,MAAM,EAAE,UAAU,CAAC,MAAqB;AACxC,YAAA,GAAG,OAAO;SACX;AACD,QAAA,IAAI,YAAY,CAAC,MAAM,IAAI,IAAI,EAAE;;;YAG/B,YAAY,CAAC,MAAM,GAAG,YAAY,CAAC,MAAM,CAAC,WAAW,EAAE;;QAGzD;;;;AAIE,QAAA,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,EAAE,GAAG,EAAE,YAAY,CAAC,CAAC,OAAO,CAAC,MAAK;AACzD,YAAA,IAAI,MAAM;AAAE,gBAAA,MAAM,CAAC,mBAAmB,CAAC,OAAO,EAAE,OAAO,CAAC;YACxD,YAAY,C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AQ,GAAGH,kBAAW,CAAC,oBAAoB,CAAC;AAClD,YAAA,MAAM,MAAM,GAAG;gBACb,GAAG,IAAI,CAAC,YAAY;AACpB,gBAAA,OAAO,EAAE,QAAQ;gBACjB,OAAO,EAAE,IAAI,CAAC,OAAO;AACrB,gBAAA,UAAU,EAAE,CAAC;aACd;AACD,YAAA,IAAI,MAAM,CAAC,OAAO,IAAI,IAAI,EAAE;gBAC1B,OAAO,MAAM,CAAC,OAAO;;YAGvB,IAAI,CAAC,MAAM,GAAG,IAAI,kBAAkB,CAAC,MAAM,CAAC;;AAE9C,QAAA,MAAM,cAAc,GAAG;YACrB,GAAG,IAAI,CAAC,YAAY;AACpB,YAAA,GAAG,OAAO;SACmB;AAC/B,QAAA,OAAO,cAAc;;AAExB;;;;;;;;"}
1
+ {"version":3,"file":"index.cjs","sources":["../../../../src/llm/openai/index.ts"],"sourcesContent":["import { AzureOpenAI as AzureOpenAIClient } from 'openai';\nimport { ChatXAI as OriginalChatXAI } from '@langchain/xai';\nimport { ChatDeepSeek as OriginalChatDeepSeek } from '@langchain/deepseek';\nimport {\n getEndpoint,\n OpenAIClient,\n ChatOpenAI as OriginalChatOpenAI,\n AzureChatOpenAI as OriginalAzureChatOpenAI,\n} from '@langchain/openai';\nimport type * as t from '@langchain/openai';\n\nexport class CustomOpenAIClient extends OpenAIClient {\n abortHandler?: () => void;\n async fetchWithTimeout(\n url: RequestInfo,\n init: RequestInit | undefined,\n ms: number,\n controller: AbortController\n ): Promise<Response> {\n const { signal, ...options } = init || {};\n const handler = (): void => controller.abort();\n if (signal) signal.addEventListener('abort', handler);\n\n const timeout = setTimeout(() => handler, ms);\n\n const fetchOptions = {\n signal: controller.signal as AbortSignal,\n ...options,\n };\n if (fetchOptions.method != null) {\n // Custom methods like 'patch' need to be uppercased\n // See https://github.com/nodejs/undici/issues/2294\n fetchOptions.method = fetchOptions.method.toUpperCase();\n }\n\n return (\n // use undefined this binding; fetch errors if bound to something else in browser/cloudflare\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n /** @ts-ignore */\n this.fetch.call(undefined, url, fetchOptions).finally(() => {\n clearTimeout(timeout);\n })\n );\n }\n}\nexport class CustomAzureOpenAIClient extends AzureOpenAIClient {\n abortHandler?: () => void;\n async fetchWithTimeout(\n url: RequestInfo,\n init: RequestInit | undefined,\n ms: number,\n controller: AbortController\n ): Promise<Response> {\n const { signal, ...options } = init || {};\n const handler = (): void => controller.abort();\n this.abortHandler = handler;\n if (signal) signal.addEventListener('abort', handler);\n\n const timeout = setTimeout(() => handler, ms);\n\n const fetchOptions = {\n signal: controller.signal as AbortSignal,\n ...options,\n };\n if (fetchOptions.method != null) {\n // Custom methods like 'patch' need to be uppercased\n // See https://github.com/nodejs/undici/issues/2294\n fetchOptions.method = fetchOptions.method.toUpperCase();\n }\n\n return (\n // use undefined this binding; fetch errors if bound to something else in browser/cloudflare\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n /** @ts-ignore */\n this.fetch.call(undefined, url, fetchOptions).finally(() => {\n clearTimeout(timeout);\n })\n );\n }\n}\n\nexport class ChatOpenAI extends OriginalChatOpenAI<t.ChatOpenAICallOptions> {\n protected _getClientOptions(\n options?: t.OpenAICoreRequestOptions\n ): t.OpenAICoreRequestOptions {\n if (!(this.client as OpenAIClient | undefined)) {\n const openAIEndpointConfig: t.OpenAIEndpointConfig = {\n baseURL: this.clientConfig.baseURL,\n };\n\n const endpoint = getEndpoint(openAIEndpointConfig);\n const params = {\n ...this.clientConfig,\n baseURL: endpoint,\n timeout: this.timeout,\n maxRetries: 0,\n };\n if (params.baseURL == null) {\n delete params.baseURL;\n }\n\n this.client = new CustomOpenAIClient(params);\n }\n const requestOptions = {\n ...this.clientConfig,\n ...options,\n } as t.OpenAICoreRequestOptions;\n return requestOptions;\n }\n}\n\nexport class AzureChatOpenAI extends OriginalAzureChatOpenAI {\n protected _getClientOptions(\n options: t.OpenAICoreRequestOptions | undefined\n ): t.OpenAICoreRequestOptions {\n if (!(this.client as AzureOpenAIClient | undefined)) {\n const openAIEndpointConfig: t.OpenAIEndpointConfig = {\n azureOpenAIApiDeploymentName: this.azureOpenAIApiDeploymentName,\n azureOpenAIApiInstanceName: this.azureOpenAIApiInstanceName,\n azureOpenAIApiKey: this.azureOpenAIApiKey,\n azureOpenAIBasePath: this.azureOpenAIBasePath,\n azureADTokenProvider: this.azureADTokenProvider,\n baseURL: this.clientConfig.baseURL,\n };\n\n const endpoint = getEndpoint(openAIEndpointConfig);\n\n const params = {\n ...this.clientConfig,\n baseURL: endpoint,\n timeout: this.timeout,\n maxRetries: 0,\n };\n\n if (!this.azureADTokenProvider) {\n params.apiKey = openAIEndpointConfig.azureOpenAIApiKey;\n }\n\n if (params.baseURL == null) {\n delete params.baseURL;\n }\n\n params.defaultHeaders = {\n ...params.defaultHeaders,\n 'User-Agent':\n params.defaultHeaders?.['User-Agent'] != null\n ? `${params.defaultHeaders['User-Agent']}: langchainjs-azure-openai-v2`\n : 'langchainjs-azure-openai-v2',\n };\n\n this.client = new CustomAzureOpenAIClient({\n apiVersion: this.azureOpenAIApiVersion,\n azureADTokenProvider: this.azureADTokenProvider,\n ...params,\n });\n }\n\n const requestOptions = {\n ...this.clientConfig,\n ...options,\n } as t.OpenAICoreRequestOptions;\n if (this.azureOpenAIApiKey != null) {\n requestOptions.headers = {\n 'api-key': this.azureOpenAIApiKey,\n ...requestOptions.headers,\n };\n requestOptions.query = {\n 'api-version': this.azureOpenAIApiVersion,\n ...requestOptions.query,\n };\n }\n return requestOptions;\n }\n}\n\nexport class ChatDeepSeek extends OriginalChatDeepSeek {\n protected _getClientOptions(\n options?: t.OpenAICoreRequestOptions\n ): t.OpenAICoreRequestOptions {\n if (!(this.client as OpenAIClient | undefined)) {\n const openAIEndpointConfig: t.OpenAIEndpointConfig = {\n baseURL: this.clientConfig.baseURL,\n };\n\n const endpoint = getEndpoint(openAIEndpointConfig);\n const params = {\n ...this.clientConfig,\n baseURL: endpoint,\n timeout: this.timeout,\n maxRetries: 0,\n };\n if (params.baseURL == null) {\n delete params.baseURL;\n }\n\n this.client = new CustomOpenAIClient(params);\n }\n const requestOptions = {\n ...this.clientConfig,\n ...options,\n } as t.OpenAICoreRequestOptions;\n return requestOptions;\n }\n}\n\nexport class ChatXAI extends OriginalChatXAI {\n protected _getClientOptions(\n options?: t.OpenAICoreRequestOptions\n ): t.OpenAICoreRequestOptions {\n if (!(this.client as OpenAIClient | undefined)) {\n const openAIEndpointConfig: t.OpenAIEndpointConfig = {\n baseURL: this.clientConfig.baseURL,\n };\n\n const endpoint = getEndpoint(openAIEndpointConfig);\n const params = {\n ...this.clientConfig,\n baseURL: endpoint,\n timeout: this.timeout,\n maxRetries: 0,\n };\n if (params.baseURL == null) {\n delete params.baseURL;\n }\n\n this.client = new CustomOpenAIClient(params);\n }\n const requestOptions = {\n ...this.clientConfig,\n ...options,\n } as t.OpenAICoreRequestOptions;\n return requestOptions;\n }\n}\n"],"names":["OpenAIClient","AzureOpenAIClient","OriginalChatOpenAI","getEndpoint","OriginalAzureChatOpenAI","OriginalChatDeepSeek","OriginalChatXAI"],"mappings":";;;;;;;AAWM,MAAO,kBAAmB,SAAQA,mBAAY,CAAA;AAClD,IAAA,YAAY;IACZ,MAAM,gBAAgB,CACpB,GAAgB,EAChB,IAA6B,EAC7B,EAAU,EACV,UAA2B,EAAA;QAE3B,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,EAAE,GAAG,IAAI,IAAI,EAAE;QACzC,MAAM,OAAO,GAAG,MAAY,UAAU,CAAC,KAAK,EAAE;AAC9C,QAAA,IAAI,MAAM;AAAE,YAAA,MAAM,CAAC,gBAAgB,CAAC,OAAO,EAAE,OAAO,CAAC;QAErD,MAAM,OAAO,GAAG,UAAU,CAAC,MAAM,OAAO,EAAE,EAAE,CAAC;AAE7C,QAAA,MAAM,YAAY,GAAG;YACnB,MAAM,EAAE,UAAU,CAAC,MAAqB;AACxC,YAAA,GAAG,OAAO;SACX;AACD,QAAA,IAAI,YAAY,CAA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AC/B,QAAA,OAAO,cAAc;;AAExB;;;;;;;;;"}
@@ -1,8 +1,10 @@
1
+ import { AzureOpenAI } from 'openai';
1
2
  import { ChatXAI as ChatXAI$1 } from '@langchain/xai';
2
3
  import { ChatDeepSeek as ChatDeepSeek$1 } from '@langchain/deepseek';
3
4
  import { getEndpoint, AzureChatOpenAI as AzureChatOpenAI$1, ChatOpenAI as ChatOpenAI$1, OpenAIClient } from '@langchain/openai';
4
5
 
5
6
  class CustomOpenAIClient extends OpenAIClient {
7
+ abortHandler;
6
8
  async fetchWithTimeout(url, init, ms, controller) {
7
9
  const { signal, ...options } = init || {};
8
10
  const handler = () => controller.abort();
@@ -23,8 +25,33 @@ class CustomOpenAIClient extends OpenAIClient {
23
25
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
24
26
  /** @ts-ignore */
25
27
  this.fetch.call(undefined, url, fetchOptions).finally(() => {
26
- if (signal)
27
- signal.removeEventListener('abort', handler);
28
+ clearTimeout(timeout);
29
+ }));
30
+ }
31
+ }
32
+ class CustomAzureOpenAIClient extends AzureOpenAI {
33
+ abortHandler;
34
+ async fetchWithTimeout(url, init, ms, controller) {
35
+ const { signal, ...options } = init || {};
36
+ const handler = () => controller.abort();
37
+ this.abortHandler = handler;
38
+ if (signal)
39
+ signal.addEventListener('abort', handler);
40
+ const timeout = setTimeout(() => handler, ms);
41
+ const fetchOptions = {
42
+ signal: controller.signal,
43
+ ...options,
44
+ };
45
+ if (fetchOptions.method != null) {
46
+ // Custom methods like 'patch' need to be uppercased
47
+ // See https://github.com/nodejs/undici/issues/2294
48
+ fetchOptions.method = fetchOptions.method.toUpperCase();
49
+ }
50
+ return (
51
+ // use undefined this binding; fetch errors if bound to something else in browser/cloudflare
52
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
53
+ /** @ts-ignore */
54
+ this.fetch.call(undefined, url, fetchOptions).finally(() => {
28
55
  clearTimeout(timeout);
29
56
  }));
30
57
  }
@@ -58,6 +85,11 @@ class AzureChatOpenAI extends AzureChatOpenAI$1 {
58
85
  _getClientOptions(options) {
59
86
  if (!this.client) {
60
87
  const openAIEndpointConfig = {
88
+ azureOpenAIApiDeploymentName: this.azureOpenAIApiDeploymentName,
89
+ azureOpenAIApiInstanceName: this.azureOpenAIApiInstanceName,
90
+ azureOpenAIApiKey: this.azureOpenAIApiKey,
91
+ azureOpenAIBasePath: this.azureOpenAIBasePath,
92
+ azureADTokenProvider: this.azureADTokenProvider,
61
93
  baseURL: this.clientConfig.baseURL,
62
94
  };
63
95
  const endpoint = getEndpoint(openAIEndpointConfig);
@@ -67,15 +99,38 @@ class AzureChatOpenAI extends AzureChatOpenAI$1 {
67
99
  timeout: this.timeout,
68
100
  maxRetries: 0,
69
101
  };
102
+ if (!this.azureADTokenProvider) {
103
+ params.apiKey = openAIEndpointConfig.azureOpenAIApiKey;
104
+ }
70
105
  if (params.baseURL == null) {
71
106
  delete params.baseURL;
72
107
  }
73
- this.client = new CustomOpenAIClient(params);
108
+ params.defaultHeaders = {
109
+ ...params.defaultHeaders,
110
+ 'User-Agent': params.defaultHeaders?.['User-Agent'] != null
111
+ ? `${params.defaultHeaders['User-Agent']}: langchainjs-azure-openai-v2`
112
+ : 'langchainjs-azure-openai-v2',
113
+ };
114
+ this.client = new CustomAzureOpenAIClient({
115
+ apiVersion: this.azureOpenAIApiVersion,
116
+ azureADTokenProvider: this.azureADTokenProvider,
117
+ ...params,
118
+ });
74
119
  }
75
120
  const requestOptions = {
76
121
  ...this.clientConfig,
77
122
  ...options,
78
123
  };
124
+ if (this.azureOpenAIApiKey != null) {
125
+ requestOptions.headers = {
126
+ 'api-key': this.azureOpenAIApiKey,
127
+ ...requestOptions.headers,
128
+ };
129
+ requestOptions.query = {
130
+ 'api-version': this.azureOpenAIApiVersion,
131
+ ...requestOptions.query,
132
+ };
133
+ }
79
134
  return requestOptions;
80
135
  }
81
136
  }
@@ -130,5 +185,5 @@ class ChatXAI extends ChatXAI$1 {
130
185
  }
131
186
  }
132
187
 
133
- export { AzureChatOpenAI, ChatDeepSeek, ChatOpenAI, ChatXAI, CustomOpenAIClient };
188
+ export { AzureChatOpenAI, ChatDeepSeek, ChatOpenAI, ChatXAI, CustomAzureOpenAIClient, CustomOpenAIClient };
134
189
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":["../../../../src/llm/openai/index.ts"],"sourcesContent":["import { ChatXAI as OriginalChatXAI } from '@langchain/xai';\nimport { ChatDeepSeek as OriginalChatDeepSeek } from '@langchain/deepseek';\nimport {\n getEndpoint,\n OpenAIClient,\n ChatOpenAI as OriginalChatOpenAI,\n AzureChatOpenAI as OriginalAzureChatOpenAI,\n} from '@langchain/openai';\nimport type * as t from '@langchain/openai';\n\nexport class CustomOpenAIClient extends OpenAIClient {\n async fetchWithTimeout(\n url: RequestInfo,\n init: RequestInit | undefined,\n ms: number,\n controller: AbortController\n ): Promise<Response> {\n const { signal, ...options } = init || {};\n const handler = (): void => controller.abort();\n if (signal) signal.addEventListener('abort', handler);\n\n const timeout = setTimeout(() => handler, ms);\n\n const fetchOptions = {\n signal: controller.signal as AbortSignal,\n ...options,\n };\n if (fetchOptions.method != null) {\n // Custom methods like 'patch' need to be uppercased\n // See https://github.com/nodejs/undici/issues/2294\n fetchOptions.method = fetchOptions.method.toUpperCase();\n }\n\n return (\n // use undefined this binding; fetch errors if bound to something else in browser/cloudflare\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n /** @ts-ignore */\n this.fetch.call(undefined, url, fetchOptions).finally(() => {\n if (signal) signal.removeEventListener('abort', handler);\n clearTimeout(timeout);\n })\n );\n }\n}\n\nexport class ChatOpenAI extends OriginalChatOpenAI<t.ChatOpenAICallOptions> {\n protected _getClientOptions(\n options?: t.OpenAICoreRequestOptions\n ): t.OpenAICoreRequestOptions {\n if (!(this.client as OpenAIClient | undefined)) {\n const openAIEndpointConfig: t.OpenAIEndpointConfig = {\n baseURL: this.clientConfig.baseURL,\n };\n\n const endpoint = getEndpoint(openAIEndpointConfig);\n const params = {\n ...this.clientConfig,\n baseURL: endpoint,\n timeout: this.timeout,\n maxRetries: 0,\n };\n if (params.baseURL == null) {\n delete params.baseURL;\n }\n\n this.client = new CustomOpenAIClient(params);\n }\n const requestOptions = {\n ...this.clientConfig,\n ...options,\n } as t.OpenAICoreRequestOptions;\n return requestOptions;\n }\n}\n\nexport class AzureChatOpenAI extends OriginalAzureChatOpenAI {\n protected _getClientOptions(\n options?: t.OpenAICoreRequestOptions\n ): t.OpenAICoreRequestOptions {\n if (!(this.client as OpenAIClient | undefined)) {\n const openAIEndpointConfig: t.OpenAIEndpointConfig = {\n baseURL: this.clientConfig.baseURL,\n };\n\n const endpoint = getEndpoint(openAIEndpointConfig);\n const params = {\n ...this.clientConfig,\n baseURL: endpoint,\n timeout: this.timeout,\n maxRetries: 0,\n };\n if (params.baseURL == null) {\n delete params.baseURL;\n }\n\n this.client = new CustomOpenAIClient(params);\n }\n const requestOptions = {\n ...this.clientConfig,\n ...options,\n } as t.OpenAICoreRequestOptions;\n return requestOptions;\n }\n}\n\nexport class ChatDeepSeek extends OriginalChatDeepSeek {\n protected _getClientOptions(\n options?: t.OpenAICoreRequestOptions\n ): t.OpenAICoreRequestOptions {\n if (!(this.client as OpenAIClient | undefined)) {\n const openAIEndpointConfig: t.OpenAIEndpointConfig = {\n baseURL: this.clientConfig.baseURL,\n };\n\n const endpoint = getEndpoint(openAIEndpointConfig);\n const params = {\n ...this.clientConfig,\n baseURL: endpoint,\n timeout: this.timeout,\n maxRetries: 0,\n };\n if (params.baseURL == null) {\n delete params.baseURL;\n }\n\n this.client = new CustomOpenAIClient(params);\n }\n const requestOptions = {\n ...this.clientConfig,\n ...options,\n } as t.OpenAICoreRequestOptions;\n return requestOptions;\n }\n}\n\nexport class ChatXAI extends OriginalChatXAI {\n protected _getClientOptions(\n options?: t.OpenAICoreRequestOptions\n ): t.OpenAICoreRequestOptions {\n if (!(this.client as OpenAIClient | undefined)) {\n const openAIEndpointConfig: t.OpenAIEndpointConfig = {\n baseURL: this.clientConfig.baseURL,\n };\n\n const endpoint = getEndpoint(openAIEndpointConfig);\n const params = {\n ...this.clientConfig,\n baseURL: endpoint,\n timeout: this.timeout,\n maxRetries: 0,\n };\n if (params.baseURL == null) {\n delete params.baseURL;\n }\n\n this.client = new CustomOpenAIClient(params);\n }\n const requestOptions = {\n ...this.clientConfig,\n ...options,\n } as t.OpenAICoreRequestOptions;\n return requestOptions;\n }\n}\n"],"names":["OriginalChatOpenAI","OriginalAzureChatOpenAI","OriginalChatDeepSeek","OriginalChatXAI"],"mappings":";;;;AAUM,MAAO,kBAAmB,SAAQ,YAAY,CAAA;IAClD,MAAM,gBAAgB,CACpB,GAAgB,EAChB,IAA6B,EAC7B,EAAU,EACV,UAA2B,EAAA;QAE3B,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,EAAE,GAAG,IAAI,IAAI,EAAE;QACzC,MAAM,OAAO,GAAG,MAAY,UAAU,CAAC,KAAK,EAAE;AAC9C,QAAA,IAAI,MAAM;AAAE,YAAA,MAAM,CAAC,gBAAgB,CAAC,OAAO,EAAE,OAAO,CAAC;QAErD,MAAM,OAAO,GAAG,UAAU,CAAC,MAAM,OAAO,EAAE,EAAE,CAAC;AAE7C,QAAA,MAAM,YAAY,GAAG;YACnB,MAAM,EAAE,UAAU,CAAC,MAAqB;AACxC,YAAA,GAAG,OAAO;SACX;AACD,QAAA,IAAI,YAAY,CAAC,MAAM,IAAI,IAAI,EAAE;;;YAG/B,YAAY,CAAC,MAAM,GAAG,YAAY,CAAC,MAAM,CAAC,WAAW,EAAE;;QAGzD;;;;AAIE,QAAA,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,EAAE,GAAG,EAAE,YAAY,CAAC,CAAC,OAAO,CAAC,MAAK;AACzD,YAAA,IAAI,MAAM;AAAE,gBAAA,MAAM,CAAC,mBAAmB,CAAC,OAAO,EAAE,OAAO,CAAC;YACxD,YAAY,CAAC,OAAO,CAAC;SAC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B,CAAC;AAClD,YAAA,MAAM,MAAM,GAAG;gBACb,GAAG,IAAI,CAAC,YAAY;AACpB,gBAAA,OAAO,EAAE,QAAQ;gBACjB,OAAO,EAAE,IAAI,CAAC,OAAO;AACrB,gBAAA,UAAU,EAAE,CAAC;aACd;AACD,YAAA,IAAI,MAAM,CAAC,OAAO,IAAI,IAAI,EAAE;gBAC1B,OAAO,MAAM,CAAC,OAAO;;YAGvB,IAAI,CAAC,MAAM,GAAG,IAAI,kBAAkB,CAAC,MAAM,CAAC;;AAE9C,QAAA,MAAM,cAAc,GAAG;YACrB,GAAG,IAAI,CAAC,YAAY;AACpB,YAAA,GAAG,OAAO;SACmB;AAC/B,QAAA,OAAO,cAAc;;AAExB;;;;"}
1
+ {"version":3,"file":"index.mjs","sources":["../../../../src/llm/openai/index.ts"],"sourcesContent":["import { AzureOpenAI as AzureOpenAIClient } from 'openai';\nimport { ChatXAI as OriginalChatXAI } from '@langchain/xai';\nimport { ChatDeepSeek as OriginalChatDeepSeek } from '@langchain/deepseek';\nimport {\n getEndpoint,\n OpenAIClient,\n ChatOpenAI as OriginalChatOpenAI,\n AzureChatOpenAI as OriginalAzureChatOpenAI,\n} from '@langchain/openai';\nimport type * as t from '@langchain/openai';\n\nexport class CustomOpenAIClient extends OpenAIClient {\n abortHandler?: () => void;\n async fetchWithTimeout(\n url: RequestInfo,\n init: RequestInit | undefined,\n ms: number,\n controller: AbortController\n ): Promise<Response> {\n const { signal, ...options } = init || {};\n const handler = (): void => controller.abort();\n if (signal) signal.addEventListener('abort', handler);\n\n const timeout = setTimeout(() => handler, ms);\n\n const fetchOptions = {\n signal: controller.signal as AbortSignal,\n ...options,\n };\n if (fetchOptions.method != null) {\n // Custom methods like 'patch' need to be uppercased\n // See https://github.com/nodejs/undici/issues/2294\n fetchOptions.method = fetchOptions.method.toUpperCase();\n }\n\n return (\n // use undefined this binding; fetch errors if bound to something else in browser/cloudflare\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n /** @ts-ignore */\n this.fetch.call(undefined, url, fetchOptions).finally(() => {\n clearTimeout(timeout);\n })\n );\n }\n}\nexport class CustomAzureOpenAIClient extends AzureOpenAIClient {\n abortHandler?: () => void;\n async fetchWithTimeout(\n url: RequestInfo,\n init: RequestInit | undefined,\n ms: number,\n controller: AbortController\n ): Promise<Response> {\n const { signal, ...options } = init || {};\n const handler = (): void => controller.abort();\n this.abortHandler = handler;\n if (signal) signal.addEventListener('abort', handler);\n\n const timeout = setTimeout(() => handler, ms);\n\n const fetchOptions = {\n signal: controller.signal as AbortSignal,\n ...options,\n };\n if (fetchOptions.method != null) {\n // Custom methods like 'patch' need to be uppercased\n // See https://github.com/nodejs/undici/issues/2294\n fetchOptions.method = fetchOptions.method.toUpperCase();\n }\n\n return (\n // use undefined this binding; fetch errors if bound to something else in browser/cloudflare\n // eslint-disable-next-line @typescript-eslint/ban-ts-comment\n /** @ts-ignore */\n this.fetch.call(undefined, url, fetchOptions).finally(() => {\n clearTimeout(timeout);\n })\n );\n }\n}\n\nexport class ChatOpenAI extends OriginalChatOpenAI<t.ChatOpenAICallOptions> {\n protected _getClientOptions(\n options?: t.OpenAICoreRequestOptions\n ): t.OpenAICoreRequestOptions {\n if (!(this.client as OpenAIClient | undefined)) {\n const openAIEndpointConfig: t.OpenAIEndpointConfig = {\n baseURL: this.clientConfig.baseURL,\n };\n\n const endpoint = getEndpoint(openAIEndpointConfig);\n const params = {\n ...this.clientConfig,\n baseURL: endpoint,\n timeout: this.timeout,\n maxRetries: 0,\n };\n if (params.baseURL == null) {\n delete params.baseURL;\n }\n\n this.client = new CustomOpenAIClient(params);\n }\n const requestOptions = {\n ...this.clientConfig,\n ...options,\n } as t.OpenAICoreRequestOptions;\n return requestOptions;\n }\n}\n\nexport class AzureChatOpenAI extends OriginalAzureChatOpenAI {\n protected _getClientOptions(\n options: t.OpenAICoreRequestOptions | undefined\n ): t.OpenAICoreRequestOptions {\n if (!(this.client as AzureOpenAIClient | undefined)) {\n const openAIEndpointConfig: t.OpenAIEndpointConfig = {\n azureOpenAIApiDeploymentName: this.azureOpenAIApiDeploymentName,\n azureOpenAIApiInstanceName: this.azureOpenAIApiInstanceName,\n azureOpenAIApiKey: this.azureOpenAIApiKey,\n azureOpenAIBasePath: this.azureOpenAIBasePath,\n azureADTokenProvider: this.azureADTokenProvider,\n baseURL: this.clientConfig.baseURL,\n };\n\n const endpoint = getEndpoint(openAIEndpointConfig);\n\n const params = {\n ...this.clientConfig,\n baseURL: endpoint,\n timeout: this.timeout,\n maxRetries: 0,\n };\n\n if (!this.azureADTokenProvider) {\n params.apiKey = openAIEndpointConfig.azureOpenAIApiKey;\n }\n\n if (params.baseURL == null) {\n delete params.baseURL;\n }\n\n params.defaultHeaders = {\n ...params.defaultHeaders,\n 'User-Agent':\n params.defaultHeaders?.['User-Agent'] != null\n ? `${params.defaultHeaders['User-Agent']}: langchainjs-azure-openai-v2`\n : 'langchainjs-azure-openai-v2',\n };\n\n this.client = new CustomAzureOpenAIClient({\n apiVersion: this.azureOpenAIApiVersion,\n azureADTokenProvider: this.azureADTokenProvider,\n ...params,\n });\n }\n\n const requestOptions = {\n ...this.clientConfig,\n ...options,\n } as t.OpenAICoreRequestOptions;\n if (this.azureOpenAIApiKey != null) {\n requestOptions.headers = {\n 'api-key': this.azureOpenAIApiKey,\n ...requestOptions.headers,\n };\n requestOptions.query = {\n 'api-version': this.azureOpenAIApiVersion,\n ...requestOptions.query,\n };\n }\n return requestOptions;\n }\n}\n\nexport class ChatDeepSeek extends OriginalChatDeepSeek {\n protected _getClientOptions(\n options?: t.OpenAICoreRequestOptions\n ): t.OpenAICoreRequestOptions {\n if (!(this.client as OpenAIClient | undefined)) {\n const openAIEndpointConfig: t.OpenAIEndpointConfig = {\n baseURL: this.clientConfig.baseURL,\n };\n\n const endpoint = getEndpoint(openAIEndpointConfig);\n const params = {\n ...this.clientConfig,\n baseURL: endpoint,\n timeout: this.timeout,\n maxRetries: 0,\n };\n if (params.baseURL == null) {\n delete params.baseURL;\n }\n\n this.client = new CustomOpenAIClient(params);\n }\n const requestOptions = {\n ...this.clientConfig,\n ...options,\n } as t.OpenAICoreRequestOptions;\n return requestOptions;\n }\n}\n\nexport class ChatXAI extends OriginalChatXAI {\n protected _getClientOptions(\n options?: t.OpenAICoreRequestOptions\n ): t.OpenAICoreRequestOptions {\n if (!(this.client as OpenAIClient | undefined)) {\n const openAIEndpointConfig: t.OpenAIEndpointConfig = {\n baseURL: this.clientConfig.baseURL,\n };\n\n const endpoint = getEndpoint(openAIEndpointConfig);\n const params = {\n ...this.clientConfig,\n baseURL: endpoint,\n timeout: this.timeout,\n maxRetries: 0,\n };\n if (params.baseURL == null) {\n delete params.baseURL;\n }\n\n this.client = new CustomOpenAIClient(params);\n }\n const requestOptions = {\n ...this.clientConfig,\n ...options,\n } as t.OpenAICoreRequestOptions;\n return requestOptions;\n }\n}\n"],"names":["AzureOpenAIClient","OriginalChatOpenAI","OriginalAzureChatOpenAI","OriginalChatDeepSeek","OriginalChatXAI"],"mappings":";;;;;AAWM,MAAO,kBAAmB,SAAQ,YAAY,CAAA;AAClD,IAAA,YAAY;IACZ,MAAM,gBAAgB,CACpB,GAAgB,EAChB,IAA6B,EAC7B,EAAU,EACV,UAA2B,EAAA;QAE3B,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,EAAE,GAAG,IAAI,IAAI,EAAE;QACzC,MAAM,OAAO,GAAG,MAAY,UAAU,CAAC,KAAK,EAAE;AAC9C,QAAA,IAAI,MAAM;AAAE,YAAA,MAAM,CAAC,gBAAgB,CAAC,OAAO,EAAE,OAAO,CAAC;QAErD,MAAM,OAAO,GAAG,UAAU,CAAC,MAAM,OAAO,EAAE,EAAE,CAAC;AAE7C,QAAA,MAAM,YAAY,GAAG;YACnB,MAAM,EAAE,UAAU,CAAC,MAAqB;AACxC,YAAA,GAAG,OAAO;SACX;AACD,QAAA,IAAI,YAAY,CAAC,MAAM,IAAI,IAAI,EAAE;;;YAG/B,YAAY,CAAC,MAAM,GAA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enAI as AzureOpenAIClient } from 'openai';
1
2
  import { ChatXAI as OriginalChatXAI } from '@langchain/xai';
2
3
  import { ChatDeepSeek as OriginalChatDeepSeek } from '@langchain/deepseek';
3
4
  import { OpenAIClient, ChatOpenAI as OriginalChatOpenAI, AzureChatOpenAI as OriginalAzureChatOpenAI } from '@langchain/openai';
4
5
  import type * as t from '@langchain/openai';
5
6
  export declare class CustomOpenAIClient extends OpenAIClient {
7
+ abortHandler?: () => void;
8
+ fetchWithTimeout(url: RequestInfo, init: RequestInit | undefined, ms: number, controller: AbortController): Promise<Response>;
9
+ }
10
+ export declare class CustomAzureOpenAIClient extends AzureOpenAIClient {
11
+ abortHandler?: () => void;
6
12
  fetchWithTimeout(url: RequestInfo, init: RequestInit | undefined, ms: number, controller: AbortController): Promise<Response>;
7
13
  }
8
14
  export declare class ChatOpenAI extends OriginalChatOpenAI<t.ChatOpenAICallOptions> {
9
15
  protected _getClientOptions(options?: t.OpenAICoreRequestOptions): t.OpenAICoreRequestOptions;
10
16
  }
11
17
  export declare class AzureChatOpenAI extends OriginalAzureChatOpenAI {
12
- protected _getClientOptions(options?: t.OpenAICoreRequestOptions): t.OpenAICoreRequestOptions;
18
+ protected _getClientOptions(options: t.OpenAICoreRequestOptions | undefined): t.OpenAICoreRequestOptions;
13
19
  }
14
20
  export declare class ChatDeepSeek extends OriginalChatDeepSeek {
15
21
  protected _getClientOptions(options?: t.OpenAICoreRequestOptions): t.OpenAICoreRequestOptions;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@librechat/agents",
3
- "version": "2.4.16",
3
+ "version": "2.4.18",
4
4
  "main": "./dist/cjs/main.cjs",
5
5
  "module": "./dist/esm/main.mjs",
6
6
  "types": "./dist/types/index.d.ts",
@@ -47,7 +47,7 @@
47
47
  "image": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/image.ts --provider 'google' --name 'Jo' --location 'New York, NY'",
48
48
  "code_exec_files": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/code_exec_files.ts --provider 'openAI' --name 'Jo' --location 'New York, NY'",
49
49
  "code_exec_simple": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/code_exec_simple.ts --provider 'google' --name 'Jo' --location 'New York, NY'",
50
- "simple": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/simple.ts --provider 'azureOpenAI' --name 'Jo' --location 'New York, NY'",
50
+ "simple": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/simple.ts --provider 'openAI' --name 'Jo' --location 'New York, NY'",
51
51
  "caching": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/caching.ts --name 'Jo' --location 'New York, NY'",
52
52
  "thinking": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/thinking.ts --name 'Jo' --location 'New York, NY'",
53
53
  "memory": "node -r dotenv/config --loader ./tsconfig-paths-bootstrap.mjs --experimental-specifier-resolution=node ./src/scripts/memory.ts --provider 'openAI' --name 'Jo' --location 'New York, NY'",
@@ -1,3 +1,4 @@
1
+ import { AzureOpenAI as AzureOpenAIClient } from 'openai';
1
2
  import { ChatXAI as OriginalChatXAI } from '@langchain/xai';
2
3
  import { ChatDeepSeek as OriginalChatDeepSeek } from '@langchain/deepseek';
3
4
  import {
@@ -9,6 +10,7 @@ import {
9
10
  import type * as t from '@langchain/openai';
10
11
 
11
12
  export class CustomOpenAIClient extends OpenAIClient {
13
+ abortHandler?: () => void;
12
14
  async fetchWithTimeout(
13
15
  url: RequestInfo,
14
16
  init: RequestInit | undefined,
@@ -36,7 +38,41 @@ export class CustomOpenAIClient extends OpenAIClient {
36
38
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
37
39
  /** @ts-ignore */
38
40
  this.fetch.call(undefined, url, fetchOptions).finally(() => {
39
- if (signal) signal.removeEventListener('abort', handler);
41
+ clearTimeout(timeout);
42
+ })
43
+ );
44
+ }
45
+ }
46
+ export class CustomAzureOpenAIClient extends AzureOpenAIClient {
47
+ abortHandler?: () => void;
48
+ async fetchWithTimeout(
49
+ url: RequestInfo,
50
+ init: RequestInit | undefined,
51
+ ms: number,
52
+ controller: AbortController
53
+ ): Promise<Response> {
54
+ const { signal, ...options } = init || {};
55
+ const handler = (): void => controller.abort();
56
+ this.abortHandler = handler;
57
+ if (signal) signal.addEventListener('abort', handler);
58
+
59
+ const timeout = setTimeout(() => handler, ms);
60
+
61
+ const fetchOptions = {
62
+ signal: controller.signal as AbortSignal,
63
+ ...options,
64
+ };
65
+ if (fetchOptions.method != null) {
66
+ // Custom methods like 'patch' need to be uppercased
67
+ // See https://github.com/nodejs/undici/issues/2294
68
+ fetchOptions.method = fetchOptions.method.toUpperCase();
69
+ }
70
+
71
+ return (
72
+ // use undefined this binding; fetch errors if bound to something else in browser/cloudflare
73
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
74
+ /** @ts-ignore */
75
+ this.fetch.call(undefined, url, fetchOptions).finally(() => {
40
76
  clearTimeout(timeout);
41
77
  })
42
78
  );
@@ -75,30 +111,64 @@ export class ChatOpenAI extends OriginalChatOpenAI<t.ChatOpenAICallOptions> {
75
111
 
76
112
  export class AzureChatOpenAI extends OriginalAzureChatOpenAI {
77
113
  protected _getClientOptions(
78
- options?: t.OpenAICoreRequestOptions
114
+ options: t.OpenAICoreRequestOptions | undefined
79
115
  ): t.OpenAICoreRequestOptions {
80
- if (!(this.client as OpenAIClient | undefined)) {
116
+ if (!(this.client as AzureOpenAIClient | undefined)) {
81
117
  const openAIEndpointConfig: t.OpenAIEndpointConfig = {
118
+ azureOpenAIApiDeploymentName: this.azureOpenAIApiDeploymentName,
119
+ azureOpenAIApiInstanceName: this.azureOpenAIApiInstanceName,
120
+ azureOpenAIApiKey: this.azureOpenAIApiKey,
121
+ azureOpenAIBasePath: this.azureOpenAIBasePath,
122
+ azureADTokenProvider: this.azureADTokenProvider,
82
123
  baseURL: this.clientConfig.baseURL,
83
124
  };
84
125
 
85
126
  const endpoint = getEndpoint(openAIEndpointConfig);
127
+
86
128
  const params = {
87
129
  ...this.clientConfig,
88
130
  baseURL: endpoint,
89
131
  timeout: this.timeout,
90
132
  maxRetries: 0,
91
133
  };
134
+
135
+ if (!this.azureADTokenProvider) {
136
+ params.apiKey = openAIEndpointConfig.azureOpenAIApiKey;
137
+ }
138
+
92
139
  if (params.baseURL == null) {
93
140
  delete params.baseURL;
94
141
  }
95
142
 
96
- this.client = new CustomOpenAIClient(params);
143
+ params.defaultHeaders = {
144
+ ...params.defaultHeaders,
145
+ 'User-Agent':
146
+ params.defaultHeaders?.['User-Agent'] != null
147
+ ? `${params.defaultHeaders['User-Agent']}: langchainjs-azure-openai-v2`
148
+ : 'langchainjs-azure-openai-v2',
149
+ };
150
+
151
+ this.client = new CustomAzureOpenAIClient({
152
+ apiVersion: this.azureOpenAIApiVersion,
153
+ azureADTokenProvider: this.azureADTokenProvider,
154
+ ...params,
155
+ });
97
156
  }
157
+
98
158
  const requestOptions = {
99
159
  ...this.clientConfig,
100
160
  ...options,
101
161
  } as t.OpenAICoreRequestOptions;
162
+ if (this.azureOpenAIApiKey != null) {
163
+ requestOptions.headers = {
164
+ 'api-key': this.azureOpenAIApiKey,
165
+ ...requestOptions.headers,
166
+ };
167
+ requestOptions.query = {
168
+ 'api-version': this.azureOpenAIApiVersion,
169
+ ...requestOptions.query,
170
+ };
171
+ }
102
172
  return requestOptions;
103
173
  }
104
174
  }