@librechat/agents 2.4.16 → 2.4.17
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,5 +1,6 @@
|
|
|
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');
|
|
@@ -31,6 +32,33 @@ class CustomOpenAIClient extends openai.OpenAIClient {
|
|
|
31
32
|
}));
|
|
32
33
|
}
|
|
33
34
|
}
|
|
35
|
+
class CustomAzureOpenAIClient extends openai$1.AzureOpenAI {
|
|
36
|
+
async fetchWithTimeout(url, init, ms, controller) {
|
|
37
|
+
const { signal, ...options } = init || {};
|
|
38
|
+
const handler = () => controller.abort();
|
|
39
|
+
if (signal)
|
|
40
|
+
signal.addEventListener('abort', handler);
|
|
41
|
+
const timeout = setTimeout(() => handler, ms);
|
|
42
|
+
const fetchOptions = {
|
|
43
|
+
signal: controller.signal,
|
|
44
|
+
...options,
|
|
45
|
+
};
|
|
46
|
+
if (fetchOptions.method != null) {
|
|
47
|
+
// Custom methods like 'patch' need to be uppercased
|
|
48
|
+
// See https://github.com/nodejs/undici/issues/2294
|
|
49
|
+
fetchOptions.method = fetchOptions.method.toUpperCase();
|
|
50
|
+
}
|
|
51
|
+
return (
|
|
52
|
+
// use undefined this binding; fetch errors if bound to something else in browser/cloudflare
|
|
53
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
54
|
+
/** @ts-ignore */
|
|
55
|
+
this.fetch.call(undefined, url, fetchOptions).finally(() => {
|
|
56
|
+
if (signal)
|
|
57
|
+
signal.removeEventListener('abort', handler);
|
|
58
|
+
clearTimeout(timeout);
|
|
59
|
+
}));
|
|
60
|
+
}
|
|
61
|
+
}
|
|
34
62
|
class ChatOpenAI extends openai.ChatOpenAI {
|
|
35
63
|
_getClientOptions(options) {
|
|
36
64
|
if (!this.client) {
|
|
@@ -60,6 +88,11 @@ class AzureChatOpenAI extends openai.AzureChatOpenAI {
|
|
|
60
88
|
_getClientOptions(options) {
|
|
61
89
|
if (!this.client) {
|
|
62
90
|
const openAIEndpointConfig = {
|
|
91
|
+
azureOpenAIApiDeploymentName: this.azureOpenAIApiDeploymentName,
|
|
92
|
+
azureOpenAIApiInstanceName: this.azureOpenAIApiInstanceName,
|
|
93
|
+
azureOpenAIApiKey: this.azureOpenAIApiKey,
|
|
94
|
+
azureOpenAIBasePath: this.azureOpenAIBasePath,
|
|
95
|
+
azureADTokenProvider: this.azureADTokenProvider,
|
|
63
96
|
baseURL: this.clientConfig.baseURL,
|
|
64
97
|
};
|
|
65
98
|
const endpoint = openai.getEndpoint(openAIEndpointConfig);
|
|
@@ -69,15 +102,38 @@ class AzureChatOpenAI extends openai.AzureChatOpenAI {
|
|
|
69
102
|
timeout: this.timeout,
|
|
70
103
|
maxRetries: 0,
|
|
71
104
|
};
|
|
105
|
+
if (!this.azureADTokenProvider) {
|
|
106
|
+
params.apiKey = openAIEndpointConfig.azureOpenAIApiKey;
|
|
107
|
+
}
|
|
72
108
|
if (params.baseURL == null) {
|
|
73
109
|
delete params.baseURL;
|
|
74
110
|
}
|
|
75
|
-
|
|
111
|
+
params.defaultHeaders = {
|
|
112
|
+
...params.defaultHeaders,
|
|
113
|
+
'User-Agent': params.defaultHeaders?.['User-Agent'] != null
|
|
114
|
+
? `${params.defaultHeaders['User-Agent']}: langchainjs-azure-openai-v2`
|
|
115
|
+
: 'langchainjs-azure-openai-v2',
|
|
116
|
+
};
|
|
117
|
+
this.client = new CustomAzureOpenAIClient({
|
|
118
|
+
apiVersion: this.azureOpenAIApiVersion,
|
|
119
|
+
azureADTokenProvider: this.azureADTokenProvider,
|
|
120
|
+
...params,
|
|
121
|
+
});
|
|
76
122
|
}
|
|
77
123
|
const requestOptions = {
|
|
78
124
|
...this.clientConfig,
|
|
79
125
|
...options,
|
|
80
126
|
};
|
|
127
|
+
if (this.azureOpenAIApiKey != null) {
|
|
128
|
+
requestOptions.headers = {
|
|
129
|
+
'api-key': this.azureOpenAIApiKey,
|
|
130
|
+
...requestOptions.headers,
|
|
131
|
+
};
|
|
132
|
+
requestOptions.query = {
|
|
133
|
+
'api-version': this.azureOpenAIApiVersion,
|
|
134
|
+
...requestOptions.query,
|
|
135
|
+
};
|
|
136
|
+
}
|
|
81
137
|
return requestOptions;
|
|
82
138
|
}
|
|
83
139
|
}
|
|
@@ -136,5 +192,6 @@ exports.AzureChatOpenAI = AzureChatOpenAI;
|
|
|
136
192
|
exports.ChatDeepSeek = ChatDeepSeek;
|
|
137
193
|
exports.ChatOpenAI = ChatOpenAI;
|
|
138
194
|
exports.ChatXAI = ChatXAI;
|
|
195
|
+
exports.CustomAzureOpenAIClient = CustomAzureOpenAIClient;
|
|
139
196
|
exports.CustomOpenAIClient = CustomOpenAIClient;
|
|
140
197
|
//# 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,EAA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AM,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;AAEK,MAAO,OAAQ,SAAQG,WAAe,CAAA;AAChC,IAAA,iBAAiB,CACzB,OAAoC,EAAA;AAEpC,QAAA,IAAI,CAAE,IAAI,CAAC,MAAmC,EAAE;AAC9C,YAAA,MAAM,oBAAoB,GAA2B;AACnD,gBAAA,OAAO,EAAE,IAAI,CAAC,YAAY,CAAC,OAAO;aACnC;AAED,YAAA,MAAM,QAAQ,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 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}\nexport class CustomAzureOpenAIClient extends AzureOpenAIClient {\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 | 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;IAClD,MAAM,gBAAgB,CACpB,GAAgB,EA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9C,YAAA,MAAM,oBAAoB,GAA2B;AACnD,gBAAA,OAAO,EAAE,IAAI,CAAC,YAAY,CAAC,OAAO;aACnC;AAED,YAAA,MAAM,QAAQ,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,3 +1,4 @@
|
|
|
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';
|
|
@@ -29,6 +30,33 @@ class CustomOpenAIClient extends OpenAIClient {
|
|
|
29
30
|
}));
|
|
30
31
|
}
|
|
31
32
|
}
|
|
33
|
+
class CustomAzureOpenAIClient extends AzureOpenAI {
|
|
34
|
+
async fetchWithTimeout(url, init, ms, controller) {
|
|
35
|
+
const { signal, ...options } = init || {};
|
|
36
|
+
const handler = () => controller.abort();
|
|
37
|
+
if (signal)
|
|
38
|
+
signal.addEventListener('abort', handler);
|
|
39
|
+
const timeout = setTimeout(() => handler, ms);
|
|
40
|
+
const fetchOptions = {
|
|
41
|
+
signal: controller.signal,
|
|
42
|
+
...options,
|
|
43
|
+
};
|
|
44
|
+
if (fetchOptions.method != null) {
|
|
45
|
+
// Custom methods like 'patch' need to be uppercased
|
|
46
|
+
// See https://github.com/nodejs/undici/issues/2294
|
|
47
|
+
fetchOptions.method = fetchOptions.method.toUpperCase();
|
|
48
|
+
}
|
|
49
|
+
return (
|
|
50
|
+
// use undefined this binding; fetch errors if bound to something else in browser/cloudflare
|
|
51
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
52
|
+
/** @ts-ignore */
|
|
53
|
+
this.fetch.call(undefined, url, fetchOptions).finally(() => {
|
|
54
|
+
if (signal)
|
|
55
|
+
signal.removeEventListener('abort', handler);
|
|
56
|
+
clearTimeout(timeout);
|
|
57
|
+
}));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
32
60
|
class ChatOpenAI extends ChatOpenAI$1 {
|
|
33
61
|
_getClientOptions(options) {
|
|
34
62
|
if (!this.client) {
|
|
@@ -58,6 +86,11 @@ class AzureChatOpenAI extends AzureChatOpenAI$1 {
|
|
|
58
86
|
_getClientOptions(options) {
|
|
59
87
|
if (!this.client) {
|
|
60
88
|
const openAIEndpointConfig = {
|
|
89
|
+
azureOpenAIApiDeploymentName: this.azureOpenAIApiDeploymentName,
|
|
90
|
+
azureOpenAIApiInstanceName: this.azureOpenAIApiInstanceName,
|
|
91
|
+
azureOpenAIApiKey: this.azureOpenAIApiKey,
|
|
92
|
+
azureOpenAIBasePath: this.azureOpenAIBasePath,
|
|
93
|
+
azureADTokenProvider: this.azureADTokenProvider,
|
|
61
94
|
baseURL: this.clientConfig.baseURL,
|
|
62
95
|
};
|
|
63
96
|
const endpoint = getEndpoint(openAIEndpointConfig);
|
|
@@ -67,15 +100,38 @@ class AzureChatOpenAI extends AzureChatOpenAI$1 {
|
|
|
67
100
|
timeout: this.timeout,
|
|
68
101
|
maxRetries: 0,
|
|
69
102
|
};
|
|
103
|
+
if (!this.azureADTokenProvider) {
|
|
104
|
+
params.apiKey = openAIEndpointConfig.azureOpenAIApiKey;
|
|
105
|
+
}
|
|
70
106
|
if (params.baseURL == null) {
|
|
71
107
|
delete params.baseURL;
|
|
72
108
|
}
|
|
73
|
-
|
|
109
|
+
params.defaultHeaders = {
|
|
110
|
+
...params.defaultHeaders,
|
|
111
|
+
'User-Agent': params.defaultHeaders?.['User-Agent'] != null
|
|
112
|
+
? `${params.defaultHeaders['User-Agent']}: langchainjs-azure-openai-v2`
|
|
113
|
+
: 'langchainjs-azure-openai-v2',
|
|
114
|
+
};
|
|
115
|
+
this.client = new CustomAzureOpenAIClient({
|
|
116
|
+
apiVersion: this.azureOpenAIApiVersion,
|
|
117
|
+
azureADTokenProvider: this.azureADTokenProvider,
|
|
118
|
+
...params,
|
|
119
|
+
});
|
|
74
120
|
}
|
|
75
121
|
const requestOptions = {
|
|
76
122
|
...this.clientConfig,
|
|
77
123
|
...options,
|
|
78
124
|
};
|
|
125
|
+
if (this.azureOpenAIApiKey != null) {
|
|
126
|
+
requestOptions.headers = {
|
|
127
|
+
'api-key': this.azureOpenAIApiKey,
|
|
128
|
+
...requestOptions.headers,
|
|
129
|
+
};
|
|
130
|
+
requestOptions.query = {
|
|
131
|
+
'api-version': this.azureOpenAIApiVersion,
|
|
132
|
+
...requestOptions.query,
|
|
133
|
+
};
|
|
134
|
+
}
|
|
79
135
|
return requestOptions;
|
|
80
136
|
}
|
|
81
137
|
}
|
|
@@ -130,5 +186,5 @@ class ChatXAI extends ChatXAI$1 {
|
|
|
130
186
|
}
|
|
131
187
|
}
|
|
132
188
|
|
|
133
|
-
export { AzureChatOpenAI, ChatDeepSeek, ChatOpenAI, ChatXAI, CustomOpenAIClient };
|
|
189
|
+
export { AzureChatOpenAI, ChatDeepSeek, ChatOpenAI, ChatXAI, CustomAzureOpenAIClient, CustomOpenAIClient };
|
|
134
190
|
//# 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;SACtB,CAAC;;AAGP;AAEK,MAAO,UAAW,SAAQA,YAA2C,CAAA;AAC/D,IAAA,iBAAiB,CACzB,OAAoC,EAAA;AAEpC,QAAA,IAAI,CAAE,IAAI,CAAC,MAAmC,EAAE;AAC9C,YAAA,MAAM,oBAAoB,GAA2B;AACnD,gBAAA,OAAO,EAAE,IAAI,CAAC,YAAY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AE;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 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}\nexport class CustomAzureOpenAIClient extends AzureOpenAIClient {\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 | 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;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,OAA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eek as OriginalChatDeepSeek } from '@langchain/deepseek';
|
|
3
4
|
import { OpenAIClient, ChatOpenAI as OriginalChatOpenAI, AzureChatOpenAI as OriginalAzureChatOpenAI } from '@langchain/openai';
|
|
@@ -5,11 +6,14 @@ import type * as t from '@langchain/openai';
|
|
|
5
6
|
export declare class CustomOpenAIClient extends OpenAIClient {
|
|
6
7
|
fetchWithTimeout(url: RequestInfo, init: RequestInit | undefined, ms: number, controller: AbortController): Promise<Response>;
|
|
7
8
|
}
|
|
9
|
+
export declare class CustomAzureOpenAIClient extends AzureOpenAIClient {
|
|
10
|
+
fetchWithTimeout(url: RequestInfo, init: RequestInit | undefined, ms: number, controller: AbortController): Promise<Response>;
|
|
11
|
+
}
|
|
8
12
|
export declare class ChatOpenAI extends OriginalChatOpenAI<t.ChatOpenAICallOptions> {
|
|
9
13
|
protected _getClientOptions(options?: t.OpenAICoreRequestOptions): t.OpenAICoreRequestOptions;
|
|
10
14
|
}
|
|
11
15
|
export declare class AzureChatOpenAI extends OriginalAzureChatOpenAI {
|
|
12
|
-
protected _getClientOptions(options
|
|
16
|
+
protected _getClientOptions(options: t.OpenAICoreRequestOptions | undefined): t.OpenAICoreRequestOptions;
|
|
13
17
|
}
|
|
14
18
|
export declare class ChatDeepSeek extends OriginalChatDeepSeek {
|
|
15
19
|
protected _getClientOptions(options?: t.OpenAICoreRequestOptions): t.OpenAICoreRequestOptions;
|
package/package.json
CHANGED
package/src/llm/openai/index.ts
CHANGED
|
@@ -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 {
|
|
@@ -42,6 +43,40 @@ export class CustomOpenAIClient extends OpenAIClient {
|
|
|
42
43
|
);
|
|
43
44
|
}
|
|
44
45
|
}
|
|
46
|
+
export class CustomAzureOpenAIClient extends AzureOpenAIClient {
|
|
47
|
+
async fetchWithTimeout(
|
|
48
|
+
url: RequestInfo,
|
|
49
|
+
init: RequestInit | undefined,
|
|
50
|
+
ms: number,
|
|
51
|
+
controller: AbortController
|
|
52
|
+
): Promise<Response> {
|
|
53
|
+
const { signal, ...options } = init || {};
|
|
54
|
+
const handler = (): void => controller.abort();
|
|
55
|
+
if (signal) signal.addEventListener('abort', handler);
|
|
56
|
+
|
|
57
|
+
const timeout = setTimeout(() => handler, ms);
|
|
58
|
+
|
|
59
|
+
const fetchOptions = {
|
|
60
|
+
signal: controller.signal as AbortSignal,
|
|
61
|
+
...options,
|
|
62
|
+
};
|
|
63
|
+
if (fetchOptions.method != null) {
|
|
64
|
+
// Custom methods like 'patch' need to be uppercased
|
|
65
|
+
// See https://github.com/nodejs/undici/issues/2294
|
|
66
|
+
fetchOptions.method = fetchOptions.method.toUpperCase();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return (
|
|
70
|
+
// use undefined this binding; fetch errors if bound to something else in browser/cloudflare
|
|
71
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
72
|
+
/** @ts-ignore */
|
|
73
|
+
this.fetch.call(undefined, url, fetchOptions).finally(() => {
|
|
74
|
+
if (signal) signal.removeEventListener('abort', handler);
|
|
75
|
+
clearTimeout(timeout);
|
|
76
|
+
})
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
45
80
|
|
|
46
81
|
export class ChatOpenAI extends OriginalChatOpenAI<t.ChatOpenAICallOptions> {
|
|
47
82
|
protected _getClientOptions(
|
|
@@ -75,30 +110,64 @@ export class ChatOpenAI extends OriginalChatOpenAI<t.ChatOpenAICallOptions> {
|
|
|
75
110
|
|
|
76
111
|
export class AzureChatOpenAI extends OriginalAzureChatOpenAI {
|
|
77
112
|
protected _getClientOptions(
|
|
78
|
-
options
|
|
113
|
+
options: t.OpenAICoreRequestOptions | undefined
|
|
79
114
|
): t.OpenAICoreRequestOptions {
|
|
80
|
-
if (!(this.client as
|
|
115
|
+
if (!(this.client as AzureOpenAIClient | undefined)) {
|
|
81
116
|
const openAIEndpointConfig: t.OpenAIEndpointConfig = {
|
|
117
|
+
azureOpenAIApiDeploymentName: this.azureOpenAIApiDeploymentName,
|
|
118
|
+
azureOpenAIApiInstanceName: this.azureOpenAIApiInstanceName,
|
|
119
|
+
azureOpenAIApiKey: this.azureOpenAIApiKey,
|
|
120
|
+
azureOpenAIBasePath: this.azureOpenAIBasePath,
|
|
121
|
+
azureADTokenProvider: this.azureADTokenProvider,
|
|
82
122
|
baseURL: this.clientConfig.baseURL,
|
|
83
123
|
};
|
|
84
124
|
|
|
85
125
|
const endpoint = getEndpoint(openAIEndpointConfig);
|
|
126
|
+
|
|
86
127
|
const params = {
|
|
87
128
|
...this.clientConfig,
|
|
88
129
|
baseURL: endpoint,
|
|
89
130
|
timeout: this.timeout,
|
|
90
131
|
maxRetries: 0,
|
|
91
132
|
};
|
|
133
|
+
|
|
134
|
+
if (!this.azureADTokenProvider) {
|
|
135
|
+
params.apiKey = openAIEndpointConfig.azureOpenAIApiKey;
|
|
136
|
+
}
|
|
137
|
+
|
|
92
138
|
if (params.baseURL == null) {
|
|
93
139
|
delete params.baseURL;
|
|
94
140
|
}
|
|
95
141
|
|
|
96
|
-
|
|
142
|
+
params.defaultHeaders = {
|
|
143
|
+
...params.defaultHeaders,
|
|
144
|
+
'User-Agent':
|
|
145
|
+
params.defaultHeaders?.['User-Agent'] != null
|
|
146
|
+
? `${params.defaultHeaders['User-Agent']}: langchainjs-azure-openai-v2`
|
|
147
|
+
: 'langchainjs-azure-openai-v2',
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
this.client = new CustomAzureOpenAIClient({
|
|
151
|
+
apiVersion: this.azureOpenAIApiVersion,
|
|
152
|
+
azureADTokenProvider: this.azureADTokenProvider,
|
|
153
|
+
...params,
|
|
154
|
+
});
|
|
97
155
|
}
|
|
156
|
+
|
|
98
157
|
const requestOptions = {
|
|
99
158
|
...this.clientConfig,
|
|
100
159
|
...options,
|
|
101
160
|
} as t.OpenAICoreRequestOptions;
|
|
161
|
+
if (this.azureOpenAIApiKey != null) {
|
|
162
|
+
requestOptions.headers = {
|
|
163
|
+
'api-key': this.azureOpenAIApiKey,
|
|
164
|
+
...requestOptions.headers,
|
|
165
|
+
};
|
|
166
|
+
requestOptions.query = {
|
|
167
|
+
'api-version': this.azureOpenAIApiVersion,
|
|
168
|
+
...requestOptions.query,
|
|
169
|
+
};
|
|
170
|
+
}
|
|
102
171
|
return requestOptions;
|
|
103
172
|
}
|
|
104
173
|
}
|