@librechat/agents 3.0.32 → 3.0.34
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/common/enum.cjs +0 -1
- package/dist/cjs/common/enum.cjs.map +1 -1
- package/dist/cjs/llm/providers.cjs +0 -3
- package/dist/cjs/llm/providers.cjs.map +1 -1
- package/dist/cjs/run.cjs +10 -1
- package/dist/cjs/run.cjs.map +1 -1
- package/dist/cjs/utils/llm.cjs +0 -1
- package/dist/cjs/utils/llm.cjs.map +1 -1
- package/dist/esm/common/enum.mjs +0 -1
- package/dist/esm/common/enum.mjs.map +1 -1
- package/dist/esm/llm/providers.mjs +0 -3
- package/dist/esm/llm/providers.mjs.map +1 -1
- package/dist/esm/run.mjs +10 -1
- package/dist/esm/run.mjs.map +1 -1
- package/dist/esm/utils/llm.mjs +0 -1
- package/dist/esm/utils/llm.mjs.map +1 -1
- package/dist/types/common/enum.d.ts +0 -1
- package/dist/types/types/llm.d.ts +1 -6
- package/package.json +1 -2
- package/src/common/enum.ts +0 -1
- package/src/llm/providers.ts +0 -3
- package/src/run.ts +13 -4
- package/src/types/llm.ts +0 -6
- package/src/utils/llm.ts +0 -1
- package/src/utils/llmConfig.ts +5 -3
- package/dist/cjs/llm/ollama/index.cjs +0 -70
- package/dist/cjs/llm/ollama/index.cjs.map +0 -1
- package/dist/cjs/llm/ollama/utils.cjs +0 -158
- package/dist/cjs/llm/ollama/utils.cjs.map +0 -1
- package/dist/esm/llm/ollama/index.mjs +0 -68
- package/dist/esm/llm/ollama/index.mjs.map +0 -1
- package/dist/esm/llm/ollama/utils.mjs +0 -155
- package/dist/esm/llm/ollama/utils.mjs.map +0 -1
- package/dist/types/llm/ollama/index.d.ts +0 -8
- package/dist/types/llm/ollama/utils.d.ts +0 -7
- package/src/llm/ollama/index.ts +0 -92
- package/src/llm/ollama/utils.ts +0 -193
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { ChatGenerationChunk } from '@langchain/core/outputs';
|
|
2
|
-
import { ChatOllama as BaseChatOllama } from '@langchain/ollama';
|
|
3
|
-
import { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';
|
|
4
|
-
import type { BaseMessage } from '@langchain/core/messages';
|
|
5
|
-
export declare class ChatOllama extends BaseChatOllama {
|
|
6
|
-
static lc_name(): 'LibreChatOllama';
|
|
7
|
-
_streamResponseChunks(messages: BaseMessage[], options: this['ParsedCallOptions'], runManager?: CallbackManagerForLLMRun): AsyncGenerator<ChatGenerationChunk>;
|
|
8
|
-
}
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { AIMessageChunk, BaseMessage, UsageMetadata } from '@langchain/core/messages';
|
|
2
|
-
import type { Message as OllamaMessage } from 'ollama';
|
|
3
|
-
export declare function convertOllamaMessagesToLangChain(messages: OllamaMessage, extra?: {
|
|
4
|
-
responseMetadata?: Record<string, any>;
|
|
5
|
-
usageMetadata?: UsageMetadata;
|
|
6
|
-
}): AIMessageChunk;
|
|
7
|
-
export declare function convertToOllamaMessages(messages: BaseMessage[]): OllamaMessage[];
|
package/src/llm/ollama/index.ts
DELETED
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
import { AIMessageChunk } from '@langchain/core/messages';
|
|
2
|
-
import { ChatGenerationChunk } from '@langchain/core/outputs';
|
|
3
|
-
import { ChatOllama as BaseChatOllama } from '@langchain/ollama';
|
|
4
|
-
import { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';
|
|
5
|
-
import type {
|
|
6
|
-
ChatResponse as OllamaChatResponse,
|
|
7
|
-
Message as OllamaMessage,
|
|
8
|
-
} from 'ollama';
|
|
9
|
-
import type { UsageMetadata, BaseMessage } from '@langchain/core/messages';
|
|
10
|
-
import {
|
|
11
|
-
convertOllamaMessagesToLangChain,
|
|
12
|
-
convertToOllamaMessages,
|
|
13
|
-
} from './utils';
|
|
14
|
-
|
|
15
|
-
export class ChatOllama extends BaseChatOllama {
|
|
16
|
-
static lc_name(): 'LibreChatOllama' {
|
|
17
|
-
return 'LibreChatOllama';
|
|
18
|
-
}
|
|
19
|
-
async *_streamResponseChunks(
|
|
20
|
-
messages: BaseMessage[],
|
|
21
|
-
options: this['ParsedCallOptions'],
|
|
22
|
-
runManager?: CallbackManagerForLLMRun
|
|
23
|
-
): AsyncGenerator<ChatGenerationChunk> {
|
|
24
|
-
if (this.checkOrPullModel) {
|
|
25
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
26
|
-
// @ts-ignore
|
|
27
|
-
if (!((await this.checkModelExistsOnMachine(this.model)) as boolean)) {
|
|
28
|
-
await this.pull(this.model, {
|
|
29
|
-
logProgress: true,
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const params = this.invocationParams(options);
|
|
35
|
-
// TODO: remove cast after SDK adds support for tool calls
|
|
36
|
-
const ollamaMessages = convertToOllamaMessages(messages) as OllamaMessage[];
|
|
37
|
-
|
|
38
|
-
const usageMetadata: UsageMetadata = {
|
|
39
|
-
input_tokens: 0,
|
|
40
|
-
output_tokens: 0,
|
|
41
|
-
total_tokens: 0,
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
const stream = await this.client.chat({
|
|
45
|
-
...params,
|
|
46
|
-
messages: ollamaMessages,
|
|
47
|
-
stream: true,
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
let lastMetadata: Omit<OllamaChatResponse, 'message'> | undefined;
|
|
51
|
-
|
|
52
|
-
for await (const chunk of stream) {
|
|
53
|
-
if (options.signal?.aborted === true) {
|
|
54
|
-
this.client.abort();
|
|
55
|
-
}
|
|
56
|
-
const { message: responseMessage, ...rest } =
|
|
57
|
-
chunk as Partial<OllamaChatResponse>;
|
|
58
|
-
usageMetadata.input_tokens += rest.prompt_eval_count ?? 0;
|
|
59
|
-
usageMetadata.output_tokens += rest.eval_count ?? 0;
|
|
60
|
-
usageMetadata.total_tokens =
|
|
61
|
-
usageMetadata.input_tokens + usageMetadata.output_tokens;
|
|
62
|
-
lastMetadata = rest as Omit<OllamaChatResponse, 'message'>;
|
|
63
|
-
if (!responseMessage) {
|
|
64
|
-
continue;
|
|
65
|
-
}
|
|
66
|
-
const message = convertOllamaMessagesToLangChain(responseMessage);
|
|
67
|
-
const generationChunk = new ChatGenerationChunk({
|
|
68
|
-
text: responseMessage.content || '',
|
|
69
|
-
message,
|
|
70
|
-
});
|
|
71
|
-
yield generationChunk;
|
|
72
|
-
await runManager?.handleLLMNewToken(
|
|
73
|
-
responseMessage.content || '',
|
|
74
|
-
undefined,
|
|
75
|
-
undefined,
|
|
76
|
-
undefined,
|
|
77
|
-
undefined,
|
|
78
|
-
{ chunk: generationChunk }
|
|
79
|
-
);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// Yield the `response_metadata` as the final chunk.
|
|
83
|
-
yield new ChatGenerationChunk({
|
|
84
|
-
text: '',
|
|
85
|
-
message: new AIMessageChunk({
|
|
86
|
-
content: '',
|
|
87
|
-
response_metadata: lastMetadata,
|
|
88
|
-
usage_metadata: usageMetadata,
|
|
89
|
-
}),
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
}
|
package/src/llm/ollama/utils.ts
DELETED
|
@@ -1,193 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
AIMessage,
|
|
3
|
-
AIMessageChunk,
|
|
4
|
-
BaseMessage,
|
|
5
|
-
HumanMessage,
|
|
6
|
-
MessageContentText,
|
|
7
|
-
SystemMessage,
|
|
8
|
-
ToolMessage,
|
|
9
|
-
UsageMetadata,
|
|
10
|
-
} from '@langchain/core/messages';
|
|
11
|
-
import type {
|
|
12
|
-
Message as OllamaMessage,
|
|
13
|
-
ToolCall as OllamaToolCall,
|
|
14
|
-
} from 'ollama';
|
|
15
|
-
import { v4 as uuidv4 } from 'uuid';
|
|
16
|
-
|
|
17
|
-
export function convertOllamaMessagesToLangChain(
|
|
18
|
-
messages: OllamaMessage,
|
|
19
|
-
extra?: {
|
|
20
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
21
|
-
responseMetadata?: Record<string, any>;
|
|
22
|
-
usageMetadata?: UsageMetadata;
|
|
23
|
-
}
|
|
24
|
-
): AIMessageChunk {
|
|
25
|
-
const additional_kwargs: BaseMessage['additional_kwargs'] = {};
|
|
26
|
-
if ('thinking' in messages) {
|
|
27
|
-
additional_kwargs.reasoning_content = messages.thinking as string;
|
|
28
|
-
}
|
|
29
|
-
return new AIMessageChunk({
|
|
30
|
-
content: messages.content || '',
|
|
31
|
-
tool_call_chunks: messages.tool_calls?.map((tc) => ({
|
|
32
|
-
name: tc.function.name,
|
|
33
|
-
args: JSON.stringify(tc.function.arguments),
|
|
34
|
-
type: 'tool_call_chunk',
|
|
35
|
-
index: 0,
|
|
36
|
-
id: uuidv4(),
|
|
37
|
-
})),
|
|
38
|
-
response_metadata: extra?.responseMetadata,
|
|
39
|
-
usage_metadata: extra?.usageMetadata,
|
|
40
|
-
additional_kwargs,
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function extractBase64FromDataUrl(dataUrl: string): string {
|
|
45
|
-
const match = dataUrl.match(/^data:.*?;base64,(.*)$/);
|
|
46
|
-
return match ? match[1] : '';
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function convertAMessagesToOllama(messages: AIMessage): OllamaMessage[] {
|
|
50
|
-
if (typeof messages.content === 'string') {
|
|
51
|
-
return [
|
|
52
|
-
{
|
|
53
|
-
role: 'assistant',
|
|
54
|
-
content: messages.content,
|
|
55
|
-
},
|
|
56
|
-
];
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
const textFields = messages.content.filter(
|
|
60
|
-
(c) => c.type === 'text' && typeof c.text === 'string'
|
|
61
|
-
);
|
|
62
|
-
const textMessages = (textFields as MessageContentText[]).map((c) => ({
|
|
63
|
-
role: 'assistant',
|
|
64
|
-
content: c.text,
|
|
65
|
-
}));
|
|
66
|
-
let toolCallMsgs: OllamaMessage | undefined;
|
|
67
|
-
|
|
68
|
-
if (
|
|
69
|
-
messages.content.find((c) => c.type === 'tool_use') &&
|
|
70
|
-
messages.tool_calls?.length
|
|
71
|
-
) {
|
|
72
|
-
// `tool_use` content types are accepted if the message has tool calls
|
|
73
|
-
const toolCalls: OllamaToolCall[] | undefined = messages.tool_calls.map(
|
|
74
|
-
(tc) => ({
|
|
75
|
-
id: tc.id,
|
|
76
|
-
type: 'function',
|
|
77
|
-
function: {
|
|
78
|
-
name: tc.name,
|
|
79
|
-
arguments: tc.args,
|
|
80
|
-
},
|
|
81
|
-
})
|
|
82
|
-
);
|
|
83
|
-
|
|
84
|
-
if (toolCalls) {
|
|
85
|
-
toolCallMsgs = {
|
|
86
|
-
role: 'assistant',
|
|
87
|
-
tool_calls: toolCalls,
|
|
88
|
-
content: '',
|
|
89
|
-
};
|
|
90
|
-
}
|
|
91
|
-
} else if (
|
|
92
|
-
messages.content.find((c) => c.type === 'tool_use') &&
|
|
93
|
-
!messages.tool_calls?.length
|
|
94
|
-
) {
|
|
95
|
-
throw new Error(
|
|
96
|
-
'\'tool_use\' content type is not supported without tool calls.'
|
|
97
|
-
);
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
return [...textMessages, ...(toolCallMsgs ? [toolCallMsgs] : [])];
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
function convertHumanGenericMessagesToOllama(
|
|
104
|
-
message: HumanMessage
|
|
105
|
-
): OllamaMessage[] {
|
|
106
|
-
if (typeof message.content === 'string') {
|
|
107
|
-
return [
|
|
108
|
-
{
|
|
109
|
-
role: 'user',
|
|
110
|
-
content: message.content,
|
|
111
|
-
},
|
|
112
|
-
];
|
|
113
|
-
}
|
|
114
|
-
return message.content.map((c) => {
|
|
115
|
-
if (c.type === 'text') {
|
|
116
|
-
return {
|
|
117
|
-
role: 'user',
|
|
118
|
-
content: c.text,
|
|
119
|
-
};
|
|
120
|
-
} else if (c.type === 'image_url') {
|
|
121
|
-
if (typeof c.image_url === 'string') {
|
|
122
|
-
return {
|
|
123
|
-
role: 'user',
|
|
124
|
-
content: '',
|
|
125
|
-
images: [extractBase64FromDataUrl(c.image_url)],
|
|
126
|
-
};
|
|
127
|
-
} else if (c.image_url.url && typeof c.image_url.url === 'string') {
|
|
128
|
-
return {
|
|
129
|
-
role: 'user',
|
|
130
|
-
content: '',
|
|
131
|
-
images: [extractBase64FromDataUrl(c.image_url.url)],
|
|
132
|
-
};
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
throw new Error(`Unsupported content type: ${c.type}`);
|
|
136
|
-
});
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
function convertSystemMessageToOllama(message: SystemMessage): OllamaMessage[] {
|
|
140
|
-
if (typeof message.content === 'string') {
|
|
141
|
-
return [
|
|
142
|
-
{
|
|
143
|
-
role: 'system',
|
|
144
|
-
content: message.content,
|
|
145
|
-
},
|
|
146
|
-
];
|
|
147
|
-
} else if (
|
|
148
|
-
message.content.every(
|
|
149
|
-
(c) => c.type === 'text' && typeof c.text === 'string'
|
|
150
|
-
)
|
|
151
|
-
) {
|
|
152
|
-
return (message.content as MessageContentText[]).map((c) => ({
|
|
153
|
-
role: 'system',
|
|
154
|
-
content: c.text,
|
|
155
|
-
}));
|
|
156
|
-
} else {
|
|
157
|
-
throw new Error(
|
|
158
|
-
`Unsupported content type(s): ${message.content
|
|
159
|
-
.map((c) => c.type)
|
|
160
|
-
.join(', ')}`
|
|
161
|
-
);
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
function convertToolMessageToOllama(message: ToolMessage): OllamaMessage[] {
|
|
166
|
-
if (typeof message.content !== 'string') {
|
|
167
|
-
throw new Error('Non string tool message content is not supported');
|
|
168
|
-
}
|
|
169
|
-
return [
|
|
170
|
-
{
|
|
171
|
-
role: 'tool',
|
|
172
|
-
content: message.content,
|
|
173
|
-
},
|
|
174
|
-
];
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
export function convertToOllamaMessages(
|
|
178
|
-
messages: BaseMessage[]
|
|
179
|
-
): OllamaMessage[] {
|
|
180
|
-
return messages.flatMap((msg) => {
|
|
181
|
-
if (['human', 'generic'].includes(msg._getType())) {
|
|
182
|
-
return convertHumanGenericMessagesToOllama(msg);
|
|
183
|
-
} else if (msg._getType() === 'ai') {
|
|
184
|
-
return convertAMessagesToOllama(msg);
|
|
185
|
-
} else if (msg._getType() === 'system') {
|
|
186
|
-
return convertSystemMessageToOllama(msg);
|
|
187
|
-
} else if (msg._getType() === 'tool') {
|
|
188
|
-
return convertToolMessageToOllama(msg as ToolMessage);
|
|
189
|
-
} else {
|
|
190
|
-
throw new Error(`Unsupported message type: ${msg._getType()}`);
|
|
191
|
-
}
|
|
192
|
-
});
|
|
193
|
-
}
|