@librechat/agents 2.4.22 → 2.4.30
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.
- package/dist/cjs/llm/anthropic/index.cjs +1 -1
- package/dist/cjs/llm/anthropic/index.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/types.cjs +50 -0
- package/dist/cjs/llm/anthropic/types.cjs.map +1 -0
- package/dist/cjs/llm/anthropic/utils/message_inputs.cjs +227 -21
- package/dist/cjs/llm/anthropic/utils/message_inputs.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/utils/message_outputs.cjs +1 -0
- package/dist/cjs/llm/anthropic/utils/message_outputs.cjs.map +1 -1
- package/dist/cjs/llm/openai/index.cjs.map +1 -1
- package/dist/cjs/run.cjs.map +1 -1
- package/dist/esm/llm/anthropic/index.mjs +1 -1
- package/dist/esm/llm/anthropic/index.mjs.map +1 -1
- package/dist/esm/llm/anthropic/types.mjs +48 -0
- package/dist/esm/llm/anthropic/types.mjs.map +1 -0
- package/dist/esm/llm/anthropic/utils/message_inputs.mjs +228 -22
- package/dist/esm/llm/anthropic/utils/message_inputs.mjs.map +1 -1
- package/dist/esm/llm/anthropic/utils/message_outputs.mjs +1 -0
- package/dist/esm/llm/anthropic/utils/message_outputs.mjs.map +1 -1
- package/dist/esm/llm/openai/index.mjs.map +1 -1
- package/dist/esm/run.mjs.map +1 -1
- package/dist/types/llm/anthropic/index.d.ts +3 -4
- package/dist/types/llm/anthropic/types.d.ts +4 -35
- package/dist/types/llm/anthropic/utils/message_inputs.d.ts +2 -2
- package/dist/types/llm/anthropic/utils/message_outputs.d.ts +1 -3
- package/dist/types/llm/anthropic/utils/output_parsers.d.ts +22 -0
- package/dist/types/llm/openai/index.d.ts +3 -2
- package/dist/types/tools/example.d.ts +21 -3
- package/package.json +9 -9
- package/src/llm/anthropic/index.ts +6 -5
- package/src/llm/anthropic/llm.spec.ts +176 -179
- package/src/llm/anthropic/types.ts +64 -39
- package/src/llm/anthropic/utils/message_inputs.ts +275 -37
- package/src/llm/anthropic/utils/message_outputs.ts +4 -21
- package/src/llm/anthropic/utils/output_parsers.ts +114 -0
- package/src/llm/openai/index.ts +7 -6
- package/src/run.ts +1 -1
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/explicit-function-return-type */
|
|
2
|
+
/* eslint-disable @typescript-eslint/no-empty-object-type */
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
import {
|
|
5
|
+
BaseLLMOutputParser,
|
|
6
|
+
OutputParserException,
|
|
7
|
+
} from '@langchain/core/output_parsers';
|
|
8
|
+
import { JsonOutputKeyToolsParserParams } from '@langchain/core/output_parsers/openai_tools';
|
|
9
|
+
import { ChatGeneration } from '@langchain/core/outputs';
|
|
10
|
+
import { ToolCall } from '@langchain/core/messages/tool';
|
|
11
|
+
|
|
12
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
13
|
+
interface AnthropicToolsOutputParserParams<T extends Record<string, any>>
|
|
14
|
+
extends JsonOutputKeyToolsParserParams<T> {}
|
|
15
|
+
|
|
16
|
+
export class AnthropicToolsOutputParser<
|
|
17
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
18
|
+
T extends Record<string, any> = Record<string, any>,
|
|
19
|
+
> extends BaseLLMOutputParser<T> {
|
|
20
|
+
static lc_name() {
|
|
21
|
+
return 'AnthropicToolsOutputParser';
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
lc_namespace = ['langchain', 'anthropic', 'output_parsers'];
|
|
25
|
+
|
|
26
|
+
returnId = false;
|
|
27
|
+
|
|
28
|
+
/** The type of tool calls to return. */
|
|
29
|
+
keyName: string;
|
|
30
|
+
|
|
31
|
+
/** Whether to return only the first tool call. */
|
|
32
|
+
returnSingle = false;
|
|
33
|
+
|
|
34
|
+
zodSchema?: z.ZodType<T>;
|
|
35
|
+
|
|
36
|
+
constructor(params: AnthropicToolsOutputParserParams<T>) {
|
|
37
|
+
super(params);
|
|
38
|
+
this.keyName = params.keyName;
|
|
39
|
+
this.returnSingle = params.returnSingle ?? this.returnSingle;
|
|
40
|
+
this.zodSchema = params.zodSchema;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
protected async _validateResult(result: unknown): Promise<T> {
|
|
44
|
+
let parsedResult = result;
|
|
45
|
+
if (typeof result === 'string') {
|
|
46
|
+
try {
|
|
47
|
+
parsedResult = JSON.parse(result);
|
|
48
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
49
|
+
} catch (e: any) {
|
|
50
|
+
throw new OutputParserException(
|
|
51
|
+
`Failed to parse. Text: "${JSON.stringify(
|
|
52
|
+
result,
|
|
53
|
+
null,
|
|
54
|
+
2
|
|
55
|
+
)}". Error: ${JSON.stringify(e.message)}`,
|
|
56
|
+
result
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
} else {
|
|
60
|
+
parsedResult = result;
|
|
61
|
+
}
|
|
62
|
+
if (this.zodSchema === undefined) {
|
|
63
|
+
return parsedResult as T;
|
|
64
|
+
}
|
|
65
|
+
const zodParsedResult = await this.zodSchema.safeParseAsync(parsedResult);
|
|
66
|
+
if (zodParsedResult.success) {
|
|
67
|
+
return zodParsedResult.data;
|
|
68
|
+
} else {
|
|
69
|
+
throw new OutputParserException(
|
|
70
|
+
`Failed to parse. Text: "${JSON.stringify(
|
|
71
|
+
result,
|
|
72
|
+
null,
|
|
73
|
+
2
|
|
74
|
+
)}". Error: ${JSON.stringify(zodParsedResult.error.errors)}`,
|
|
75
|
+
JSON.stringify(parsedResult, null, 2)
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async parseResult(generations: ChatGeneration[]): Promise<T> {
|
|
81
|
+
const tools = generations.flatMap((generation) => {
|
|
82
|
+
const { message } = generation;
|
|
83
|
+
if (!Array.isArray(message.content)) {
|
|
84
|
+
return [];
|
|
85
|
+
}
|
|
86
|
+
const tool = extractToolCalls(message.content)[0];
|
|
87
|
+
return tool;
|
|
88
|
+
});
|
|
89
|
+
if (tools[0] === undefined) {
|
|
90
|
+
throw new Error(
|
|
91
|
+
'No parseable tool calls provided to AnthropicToolsOutputParser.'
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
const [tool] = tools;
|
|
95
|
+
const validatedResult = await this._validateResult(tool.args);
|
|
96
|
+
return validatedResult;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
101
|
+
export function extractToolCalls(content: Record<string, any>[]) {
|
|
102
|
+
const toolCalls: ToolCall[] = [];
|
|
103
|
+
for (const block of content) {
|
|
104
|
+
if (block.type === 'tool_use') {
|
|
105
|
+
toolCalls.push({
|
|
106
|
+
name: block.name,
|
|
107
|
+
args: block.input,
|
|
108
|
+
id: block.id,
|
|
109
|
+
type: 'tool_call',
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return toolCalls;
|
|
114
|
+
}
|
package/src/llm/openai/index.ts
CHANGED
|
@@ -7,6 +7,7 @@ import {
|
|
|
7
7
|
ChatOpenAI as OriginalChatOpenAI,
|
|
8
8
|
AzureChatOpenAI as OriginalAzureChatOpenAI,
|
|
9
9
|
} from '@langchain/openai';
|
|
10
|
+
import type { OpenAICoreRequestOptions } from 'node_modules/@langchain/deepseek/node_modules/@langchain/openai';
|
|
10
11
|
import type * as t from '@langchain/openai';
|
|
11
12
|
|
|
12
13
|
function createAbortHandler(controller: AbortController): () => void {
|
|
@@ -191,8 +192,8 @@ export class ChatDeepSeek extends OriginalChatDeepSeek {
|
|
|
191
192
|
return this.client;
|
|
192
193
|
}
|
|
193
194
|
protected _getClientOptions(
|
|
194
|
-
options?:
|
|
195
|
-
):
|
|
195
|
+
options?: OpenAICoreRequestOptions
|
|
196
|
+
): OpenAICoreRequestOptions {
|
|
196
197
|
if (!(this.client as OpenAIClient | undefined)) {
|
|
197
198
|
const openAIEndpointConfig: t.OpenAIEndpointConfig = {
|
|
198
199
|
baseURL: this.clientConfig.baseURL,
|
|
@@ -214,7 +215,7 @@ export class ChatDeepSeek extends OriginalChatDeepSeek {
|
|
|
214
215
|
const requestOptions = {
|
|
215
216
|
...this.clientConfig,
|
|
216
217
|
...options,
|
|
217
|
-
} as
|
|
218
|
+
} as OpenAICoreRequestOptions;
|
|
218
219
|
return requestOptions;
|
|
219
220
|
}
|
|
220
221
|
}
|
|
@@ -224,8 +225,8 @@ export class ChatXAI extends OriginalChatXAI {
|
|
|
224
225
|
return this.client;
|
|
225
226
|
}
|
|
226
227
|
protected _getClientOptions(
|
|
227
|
-
options?:
|
|
228
|
-
):
|
|
228
|
+
options?: OpenAICoreRequestOptions
|
|
229
|
+
): OpenAICoreRequestOptions {
|
|
229
230
|
if (!(this.client as OpenAIClient | undefined)) {
|
|
230
231
|
const openAIEndpointConfig: t.OpenAIEndpointConfig = {
|
|
231
232
|
baseURL: this.clientConfig.baseURL,
|
|
@@ -247,7 +248,7 @@ export class ChatXAI extends OriginalChatXAI {
|
|
|
247
248
|
const requestOptions = {
|
|
248
249
|
...this.clientConfig,
|
|
249
250
|
...options,
|
|
250
|
-
} as
|
|
251
|
+
} as OpenAICoreRequestOptions;
|
|
251
252
|
return requestOptions;
|
|
252
253
|
}
|
|
253
254
|
}
|
package/src/run.ts
CHANGED