@librechat/agents 2.4.75 → 2.4.77
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/ollama/index.cjs +67 -0
- package/dist/cjs/llm/ollama/index.cjs.map +1 -0
- package/dist/cjs/llm/ollama/utils.cjs +158 -0
- package/dist/cjs/llm/ollama/utils.cjs.map +1 -0
- package/dist/cjs/llm/openai/index.cjs +3 -0
- package/dist/cjs/llm/openai/index.cjs.map +1 -1
- package/dist/cjs/llm/openai/utils/index.cjs +1 -3
- package/dist/cjs/llm/openai/utils/index.cjs.map +1 -1
- package/dist/cjs/llm/providers.cjs +2 -2
- package/dist/cjs/llm/providers.cjs.map +1 -1
- package/dist/cjs/tools/search/rerankers.cjs +8 -6
- package/dist/cjs/tools/search/rerankers.cjs.map +1 -1
- package/dist/cjs/tools/search/tool.cjs +2 -1
- package/dist/cjs/tools/search/tool.cjs.map +1 -1
- package/dist/esm/llm/ollama/index.mjs +65 -0
- package/dist/esm/llm/ollama/index.mjs.map +1 -0
- package/dist/esm/llm/ollama/utils.mjs +155 -0
- package/dist/esm/llm/ollama/utils.mjs.map +1 -0
- package/dist/esm/llm/openai/index.mjs +3 -0
- package/dist/esm/llm/openai/index.mjs.map +1 -1
- package/dist/esm/llm/openai/utils/index.mjs +1 -3
- package/dist/esm/llm/openai/utils/index.mjs.map +1 -1
- package/dist/esm/llm/providers.mjs +1 -1
- package/dist/esm/llm/providers.mjs.map +1 -1
- package/dist/esm/tools/search/rerankers.mjs +8 -6
- package/dist/esm/tools/search/rerankers.mjs.map +1 -1
- package/dist/esm/tools/search/tool.mjs +2 -1
- package/dist/esm/tools/search/tool.mjs.map +1 -1
- package/dist/types/llm/ollama/index.d.ts +7 -0
- package/dist/types/llm/ollama/utils.d.ts +7 -0
- package/dist/types/tools/search/rerankers.d.ts +4 -1
- package/dist/types/tools/search/types.d.ts +1 -0
- package/package.json +2 -2
- package/src/llm/ollama/index.ts +89 -0
- package/src/llm/ollama/utils.ts +193 -0
- package/src/llm/openai/index.ts +2 -0
- package/src/llm/openai/utils/index.ts +1 -5
- package/src/llm/openai/utils/isReasoningModel.test.ts +90 -0
- package/src/llm/providers.ts +1 -1
- package/src/scripts/simple.ts +10 -4
- package/src/tools/search/jina-reranker.test.ts +126 -0
- package/src/tools/search/rerankers.ts +11 -5
- package/src/tools/search/tool.ts +2 -0
- package/src/tools/search/types.ts +1 -0
- package/src/utils/llmConfig.ts +11 -2
|
@@ -0,0 +1,89 @@
|
|
|
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
|
+
async *_streamResponseChunks(
|
|
17
|
+
messages: BaseMessage[],
|
|
18
|
+
options: this['ParsedCallOptions'],
|
|
19
|
+
runManager?: CallbackManagerForLLMRun
|
|
20
|
+
): AsyncGenerator<ChatGenerationChunk> {
|
|
21
|
+
if (this.checkOrPullModel) {
|
|
22
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
23
|
+
// @ts-ignore
|
|
24
|
+
if (!((await this.checkModelExistsOnMachine(this.model)) as boolean)) {
|
|
25
|
+
await this.pull(this.model, {
|
|
26
|
+
logProgress: true,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const params = this.invocationParams(options);
|
|
32
|
+
// TODO: remove cast after SDK adds support for tool calls
|
|
33
|
+
const ollamaMessages = convertToOllamaMessages(messages) as OllamaMessage[];
|
|
34
|
+
|
|
35
|
+
const usageMetadata: UsageMetadata = {
|
|
36
|
+
input_tokens: 0,
|
|
37
|
+
output_tokens: 0,
|
|
38
|
+
total_tokens: 0,
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const stream = await this.client.chat({
|
|
42
|
+
...params,
|
|
43
|
+
messages: ollamaMessages,
|
|
44
|
+
stream: true,
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
let lastMetadata: Omit<OllamaChatResponse, 'message'> | undefined;
|
|
48
|
+
|
|
49
|
+
for await (const chunk of stream) {
|
|
50
|
+
if (options.signal?.aborted === true) {
|
|
51
|
+
this.client.abort();
|
|
52
|
+
}
|
|
53
|
+
const { message: responseMessage, ...rest } =
|
|
54
|
+
chunk as Partial<OllamaChatResponse>;
|
|
55
|
+
usageMetadata.input_tokens += rest.prompt_eval_count ?? 0;
|
|
56
|
+
usageMetadata.output_tokens += rest.eval_count ?? 0;
|
|
57
|
+
usageMetadata.total_tokens =
|
|
58
|
+
usageMetadata.input_tokens + usageMetadata.output_tokens;
|
|
59
|
+
lastMetadata = rest as Omit<OllamaChatResponse, 'message'>;
|
|
60
|
+
if (!responseMessage) {
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
const message = convertOllamaMessagesToLangChain(responseMessage);
|
|
64
|
+
const generationChunk = new ChatGenerationChunk({
|
|
65
|
+
text: responseMessage.content || '',
|
|
66
|
+
message,
|
|
67
|
+
});
|
|
68
|
+
yield generationChunk;
|
|
69
|
+
await runManager?.handleLLMNewToken(
|
|
70
|
+
responseMessage.content || '',
|
|
71
|
+
undefined,
|
|
72
|
+
undefined,
|
|
73
|
+
undefined,
|
|
74
|
+
undefined,
|
|
75
|
+
{ chunk: generationChunk }
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Yield the `response_metadata` as the final chunk.
|
|
80
|
+
yield new ChatGenerationChunk({
|
|
81
|
+
text: '',
|
|
82
|
+
message: new AIMessageChunk({
|
|
83
|
+
content: '',
|
|
84
|
+
response_metadata: lastMetadata,
|
|
85
|
+
usage_metadata: usageMetadata,
|
|
86
|
+
}),
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
}
|
|
@@ -0,0 +1,193 @@
|
|
|
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
|
+
}
|
package/src/llm/openai/index.ts
CHANGED
|
@@ -342,6 +342,8 @@ export class ChatOpenAI extends OriginalChatOpenAI<t.ChatOpenAICallOptions> {
|
|
|
342
342
|
);
|
|
343
343
|
if ('reasoning_content' in delta) {
|
|
344
344
|
chunk.additional_kwargs.reasoning_content = delta.reasoning_content;
|
|
345
|
+
} else if ('reasoning' in delta) {
|
|
346
|
+
chunk.additional_kwargs.reasoning_content = delta.reasoning;
|
|
345
347
|
}
|
|
346
348
|
defaultRole = delta.role ?? defaultRole;
|
|
347
349
|
const newTokenIndices = {
|
|
@@ -648,11 +648,7 @@ export function _convertMessagesToOpenAIResponsesParams(
|
|
|
648
648
|
}
|
|
649
649
|
|
|
650
650
|
export function isReasoningModel(model?: string) {
|
|
651
|
-
return (
|
|
652
|
-
model != null &&
|
|
653
|
-
model !== '' &&
|
|
654
|
-
(/^o\d/.test(model) || /^gpt-[5-9]/.test(model))
|
|
655
|
-
);
|
|
651
|
+
return model != null && model !== '' && /\b(o\d|gpt-[5-9])\b/i.test(model);
|
|
656
652
|
}
|
|
657
653
|
|
|
658
654
|
function _convertOpenAIResponsesMessageToBaseMessage(
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { isReasoningModel } from './index';
|
|
2
|
+
|
|
3
|
+
describe('isReasoningModel', () => {
|
|
4
|
+
describe('should return true for reasoning models', () => {
|
|
5
|
+
test('basic o-series models', () => {
|
|
6
|
+
expect(isReasoningModel('o1')).toBe(true);
|
|
7
|
+
expect(isReasoningModel('o2')).toBe(true);
|
|
8
|
+
expect(isReasoningModel('o9')).toBe(true);
|
|
9
|
+
expect(isReasoningModel('o1-preview')).toBe(true);
|
|
10
|
+
expect(isReasoningModel('o1-mini')).toBe(true);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
test('gpt-5+ models', () => {
|
|
14
|
+
expect(isReasoningModel('gpt-5')).toBe(true);
|
|
15
|
+
expect(isReasoningModel('gpt-6')).toBe(true);
|
|
16
|
+
expect(isReasoningModel('gpt-7')).toBe(true);
|
|
17
|
+
expect(isReasoningModel('gpt-8')).toBe(true);
|
|
18
|
+
expect(isReasoningModel('gpt-9')).toBe(true);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test('with provider prefixes', () => {
|
|
22
|
+
expect(isReasoningModel('azure/o1')).toBe(true);
|
|
23
|
+
expect(isReasoningModel('azure/gpt-5')).toBe(true);
|
|
24
|
+
expect(isReasoningModel('openai/o1')).toBe(true);
|
|
25
|
+
expect(isReasoningModel('openai/gpt-5')).toBe(true);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
test('with custom prefixes', () => {
|
|
29
|
+
expect(isReasoningModel('custom-provider/o1')).toBe(true);
|
|
30
|
+
expect(isReasoningModel('my-deployment/gpt-5')).toBe(true);
|
|
31
|
+
expect(isReasoningModel('company/azure/gpt-5')).toBe(true);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test('case insensitive', () => {
|
|
35
|
+
expect(isReasoningModel('O1')).toBe(true);
|
|
36
|
+
expect(isReasoningModel('GPT-5')).toBe(true);
|
|
37
|
+
expect(isReasoningModel('gPt-6')).toBe(true);
|
|
38
|
+
expect(isReasoningModel('Azure/O1')).toBe(true);
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
describe('should return false for non-reasoning models', () => {
|
|
43
|
+
test('older GPT models', () => {
|
|
44
|
+
expect(isReasoningModel('gpt-3.5-turbo')).toBe(false);
|
|
45
|
+
expect(isReasoningModel('gpt-4')).toBe(false);
|
|
46
|
+
expect(isReasoningModel('gpt-4-turbo')).toBe(false);
|
|
47
|
+
expect(isReasoningModel('gpt-4o')).toBe(false);
|
|
48
|
+
expect(isReasoningModel('gpt-4o-mini')).toBe(false);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test('other model families', () => {
|
|
52
|
+
expect(isReasoningModel('claude-3')).toBe(false);
|
|
53
|
+
expect(isReasoningModel('claude-3-opus')).toBe(false);
|
|
54
|
+
expect(isReasoningModel('llama-2')).toBe(false);
|
|
55
|
+
expect(isReasoningModel('gemini-pro')).toBe(false);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test('partial matches that should not match', () => {
|
|
59
|
+
expect(isReasoningModel('proto1')).toBe(false);
|
|
60
|
+
expect(isReasoningModel('version-o1')).toBe(true);
|
|
61
|
+
expect(isReasoningModel('gpt-40')).toBe(false);
|
|
62
|
+
expect(isReasoningModel('gpt-3.5')).toBe(false);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test('empty, null, and undefined', () => {
|
|
66
|
+
expect(isReasoningModel('')).toBe(false);
|
|
67
|
+
expect(isReasoningModel()).toBe(false);
|
|
68
|
+
expect(isReasoningModel(undefined)).toBe(false);
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
describe('edge cases', () => {
|
|
73
|
+
test('with special characters', () => {
|
|
74
|
+
expect(isReasoningModel('deployment_o1_model')).toBe(false);
|
|
75
|
+
expect(isReasoningModel('gpt-5-deployment')).toBe(true);
|
|
76
|
+
expect(isReasoningModel('o1@latest')).toBe(true);
|
|
77
|
+
expect(isReasoningModel('gpt-5.0')).toBe(true);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test('word boundary behavior', () => {
|
|
81
|
+
// These should match because o1 and gpt-5 are whole words
|
|
82
|
+
expect(isReasoningModel('use-o1-model')).toBe(true);
|
|
83
|
+
expect(isReasoningModel('model-gpt-5-latest')).toBe(true);
|
|
84
|
+
|
|
85
|
+
// These should not match because o1/gpt-5 are not whole words
|
|
86
|
+
expect(isReasoningModel('proto1model')).toBe(false);
|
|
87
|
+
expect(isReasoningModel('supergpt-50')).toBe(false);
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
});
|
package/src/llm/providers.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
// src/llm/providers.ts
|
|
2
|
-
import { ChatOllama } from '@langchain/ollama';
|
|
3
2
|
import { ChatMistralAI } from '@langchain/mistralai';
|
|
4
3
|
import { ChatBedrockConverse } from '@langchain/aws';
|
|
5
4
|
// import { ChatAnthropic } from '@langchain/anthropic';
|
|
@@ -20,6 +19,7 @@ import { CustomChatGoogleGenerativeAI } from '@/llm/google';
|
|
|
20
19
|
import { CustomAnthropic } from '@/llm/anthropic';
|
|
21
20
|
import { ChatOpenRouter } from '@/llm/openrouter';
|
|
22
21
|
import { ChatVertexAI } from '@/llm/vertexai';
|
|
22
|
+
import { ChatOllama } from '@/llm/ollama';
|
|
23
23
|
import { Providers } from '@/common';
|
|
24
24
|
|
|
25
25
|
export const llmProviders: Partial<ChatModelConstructorMap> = {
|
package/src/scripts/simple.ts
CHANGED
|
@@ -24,7 +24,12 @@ let _contentParts: t.MessageContentComplex[] = [];
|
|
|
24
24
|
let collectedUsage: UsageMetadata[] = [];
|
|
25
25
|
|
|
26
26
|
async function testStandardStreaming(): Promise<void> {
|
|
27
|
-
const {
|
|
27
|
+
const {
|
|
28
|
+
userName,
|
|
29
|
+
location,
|
|
30
|
+
provider: _provider,
|
|
31
|
+
currentDate,
|
|
32
|
+
} = await getArgs();
|
|
28
33
|
const { contentParts, aggregateContent } = createContentAggregator();
|
|
29
34
|
_contentParts = contentParts as t.MessageContentComplex[];
|
|
30
35
|
const customHandlers = {
|
|
@@ -96,7 +101,7 @@ async function testStandardStreaming(): Promise<void> {
|
|
|
96
101
|
},
|
|
97
102
|
};
|
|
98
103
|
|
|
99
|
-
const llmConfig = getLLMConfig(
|
|
104
|
+
const llmConfig = getLLMConfig(_provider);
|
|
100
105
|
if (
|
|
101
106
|
'configuration' in llmConfig &&
|
|
102
107
|
(llmConfig as t.OpenAIClientOptions).configuration != null
|
|
@@ -112,6 +117,7 @@ async function testStandardStreaming(): Promise<void> {
|
|
|
112
117
|
};
|
|
113
118
|
}
|
|
114
119
|
}
|
|
120
|
+
const provider = llmConfig.provider;
|
|
115
121
|
|
|
116
122
|
if (provider === Providers.ANTHROPIC) {
|
|
117
123
|
(llmConfig as t.AnthropicClientOptions).clientOptions = {
|
|
@@ -128,7 +134,7 @@ async function testStandardStreaming(): Promise<void> {
|
|
|
128
134
|
type: 'standard',
|
|
129
135
|
llmConfig,
|
|
130
136
|
// tools: [new TavilySearchResults()],
|
|
131
|
-
reasoningKey: 'reasoning',
|
|
137
|
+
// reasoningKey: 'reasoning',
|
|
132
138
|
instructions:
|
|
133
139
|
'You are a friendly AI assistant. Always address the user by their name.',
|
|
134
140
|
additional_instructions: `The user's name is ${userName} and they are located in ${location}.`,
|
|
@@ -168,7 +174,7 @@ async function testStandardStreaming(): Promise<void> {
|
|
|
168
174
|
provider,
|
|
169
175
|
inputText: userMessage,
|
|
170
176
|
contentParts,
|
|
171
|
-
titleMethod: TitleMethod.STRUCTURED,
|
|
177
|
+
// titleMethod: TitleMethod.STRUCTURED,
|
|
172
178
|
chainOptions: {
|
|
173
179
|
callbacks: [
|
|
174
180
|
{
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { JinaReranker } from './rerankers';
|
|
2
|
+
import { createDefaultLogger } from './utils';
|
|
3
|
+
|
|
4
|
+
describe('JinaReranker', () => {
|
|
5
|
+
const mockLogger = createDefaultLogger();
|
|
6
|
+
|
|
7
|
+
describe('constructor', () => {
|
|
8
|
+
it('should use default API URL when no apiUrl is provided', () => {
|
|
9
|
+
const reranker = new JinaReranker({
|
|
10
|
+
apiKey: 'test-key',
|
|
11
|
+
logger: mockLogger,
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
// Access private property for testing
|
|
15
|
+
const apiUrl = (reranker as any).apiUrl;
|
|
16
|
+
expect(apiUrl).toBe('https://api.jina.ai/v1/rerank');
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('should use custom API URL when provided', () => {
|
|
20
|
+
const customUrl = 'https://custom-jina-endpoint.com/v1/rerank';
|
|
21
|
+
const reranker = new JinaReranker({
|
|
22
|
+
apiKey: 'test-key',
|
|
23
|
+
apiUrl: customUrl,
|
|
24
|
+
logger: mockLogger,
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const apiUrl = (reranker as any).apiUrl;
|
|
28
|
+
expect(apiUrl).toBe(customUrl);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it('should use environment variable JINA_API_URL when available', () => {
|
|
32
|
+
const originalEnv = process.env.JINA_API_URL;
|
|
33
|
+
process.env.JINA_API_URL = 'https://env-jina-endpoint.com/v1/rerank';
|
|
34
|
+
|
|
35
|
+
const reranker = new JinaReranker({
|
|
36
|
+
apiKey: 'test-key',
|
|
37
|
+
logger: mockLogger,
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const apiUrl = (reranker as any).apiUrl;
|
|
41
|
+
expect(apiUrl).toBe('https://env-jina-endpoint.com/v1/rerank');
|
|
42
|
+
|
|
43
|
+
// Restore original environment
|
|
44
|
+
if (originalEnv) {
|
|
45
|
+
process.env.JINA_API_URL = originalEnv;
|
|
46
|
+
} else {
|
|
47
|
+
delete process.env.JINA_API_URL;
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('should prioritize explicit apiUrl over environment variable', () => {
|
|
52
|
+
const originalEnv = process.env.JINA_API_URL;
|
|
53
|
+
process.env.JINA_API_URL = 'https://env-jina-endpoint.com/v1/rerank';
|
|
54
|
+
|
|
55
|
+
const customUrl = 'https://explicit-jina-endpoint.com/v1/rerank';
|
|
56
|
+
const reranker = new JinaReranker({
|
|
57
|
+
apiKey: 'test-key',
|
|
58
|
+
apiUrl: customUrl,
|
|
59
|
+
logger: mockLogger,
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
const apiUrl = (reranker as any).apiUrl;
|
|
63
|
+
expect(apiUrl).toBe(customUrl);
|
|
64
|
+
|
|
65
|
+
// Restore original environment
|
|
66
|
+
if (originalEnv) {
|
|
67
|
+
process.env.JINA_API_URL = originalEnv;
|
|
68
|
+
} else {
|
|
69
|
+
delete process.env.JINA_API_URL;
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
describe('rerank method', () => {
|
|
75
|
+
it('should log the API URL being used', async () => {
|
|
76
|
+
const customUrl = 'https://test-jina-endpoint.com/v1/rerank';
|
|
77
|
+
const reranker = new JinaReranker({
|
|
78
|
+
apiKey: 'test-key',
|
|
79
|
+
apiUrl: customUrl,
|
|
80
|
+
logger: mockLogger,
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
const logSpy = jest.spyOn(mockLogger, 'debug');
|
|
84
|
+
|
|
85
|
+
try {
|
|
86
|
+
await reranker.rerank('test query', ['document1', 'document2'], 2);
|
|
87
|
+
} catch (error) {
|
|
88
|
+
// Expected to fail due to missing API key, but we can check the log
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
expect(logSpy).toHaveBeenCalledWith(
|
|
92
|
+
expect.stringContaining(`Reranking 2 chunks with Jina using API URL: ${customUrl}`)
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
logSpy.mockRestore();
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
describe('createReranker', () => {
|
|
101
|
+
const { createReranker } = require('./rerankers');
|
|
102
|
+
|
|
103
|
+
it('should create JinaReranker with jinaApiUrl when provided', () => {
|
|
104
|
+
const customUrl = 'https://custom-jina-endpoint.com/v1/rerank';
|
|
105
|
+
const reranker = createReranker({
|
|
106
|
+
rerankerType: 'jina',
|
|
107
|
+
jinaApiKey: 'test-key',
|
|
108
|
+
jinaApiUrl: customUrl,
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
expect(reranker).toBeInstanceOf(JinaReranker);
|
|
112
|
+
const apiUrl = (reranker as any).apiUrl;
|
|
113
|
+
expect(apiUrl).toBe(customUrl);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('should create JinaReranker with default URL when jinaApiUrl is not provided', () => {
|
|
117
|
+
const reranker = createReranker({
|
|
118
|
+
rerankerType: 'jina',
|
|
119
|
+
jinaApiKey: 'test-key',
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
expect(reranker).toBeInstanceOf(JinaReranker);
|
|
123
|
+
const apiUrl = (reranker as any).apiUrl;
|
|
124
|
+
expect(apiUrl).toBe('https://api.jina.ai/v1/rerank');
|
|
125
|
+
});
|
|
126
|
+
});
|
|
@@ -28,15 +28,20 @@ export abstract class BaseReranker {
|
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
export class JinaReranker extends BaseReranker {
|
|
31
|
+
private apiUrl: string;
|
|
32
|
+
|
|
31
33
|
constructor({
|
|
32
34
|
apiKey = process.env.JINA_API_KEY,
|
|
35
|
+
apiUrl = process.env.JINA_API_URL || 'https://api.jina.ai/v1/rerank',
|
|
33
36
|
logger,
|
|
34
37
|
}: {
|
|
35
38
|
apiKey?: string;
|
|
39
|
+
apiUrl?: string;
|
|
36
40
|
logger?: t.Logger;
|
|
37
41
|
}) {
|
|
38
42
|
super(logger);
|
|
39
43
|
this.apiKey = apiKey;
|
|
44
|
+
this.apiUrl = apiUrl;
|
|
40
45
|
}
|
|
41
46
|
|
|
42
47
|
async rerank(
|
|
@@ -44,7 +49,7 @@ export class JinaReranker extends BaseReranker {
|
|
|
44
49
|
documents: string[],
|
|
45
50
|
topK: number = 5
|
|
46
51
|
): Promise<t.Highlight[]> {
|
|
47
|
-
this.logger.debug(`Reranking ${documents.length} chunks with Jina`);
|
|
52
|
+
this.logger.debug(`Reranking ${documents.length} chunks with Jina using API URL: ${this.apiUrl}`);
|
|
48
53
|
|
|
49
54
|
try {
|
|
50
55
|
if (this.apiKey == null || this.apiKey === '') {
|
|
@@ -61,7 +66,7 @@ export class JinaReranker extends BaseReranker {
|
|
|
61
66
|
};
|
|
62
67
|
|
|
63
68
|
const response = await axios.post<t.JinaRerankerResponse | undefined>(
|
|
64
|
-
|
|
69
|
+
this.apiUrl,
|
|
65
70
|
requestData,
|
|
66
71
|
{
|
|
67
72
|
headers: {
|
|
@@ -201,17 +206,18 @@ export class InfinityReranker extends BaseReranker {
|
|
|
201
206
|
export const createReranker = (config: {
|
|
202
207
|
rerankerType: t.RerankerType;
|
|
203
208
|
jinaApiKey?: string;
|
|
209
|
+
jinaApiUrl?: string;
|
|
204
210
|
cohereApiKey?: string;
|
|
205
211
|
logger?: t.Logger;
|
|
206
212
|
}): BaseReranker | undefined => {
|
|
207
|
-
const { rerankerType, jinaApiKey, cohereApiKey, logger } = config;
|
|
213
|
+
const { rerankerType, jinaApiKey, jinaApiUrl, cohereApiKey, logger } = config;
|
|
208
214
|
|
|
209
215
|
// Create a default logger if none is provided
|
|
210
216
|
const defaultLogger = logger || createDefaultLogger();
|
|
211
217
|
|
|
212
218
|
switch (rerankerType.toLowerCase()) {
|
|
213
219
|
case 'jina':
|
|
214
|
-
return new JinaReranker({ apiKey: jinaApiKey, logger: defaultLogger });
|
|
220
|
+
return new JinaReranker({ apiKey: jinaApiKey, apiUrl: jinaApiUrl, logger: defaultLogger });
|
|
215
221
|
case 'cohere':
|
|
216
222
|
return new CohereReranker({
|
|
217
223
|
apiKey: cohereApiKey,
|
|
@@ -226,7 +232,7 @@ export const createReranker = (config: {
|
|
|
226
232
|
defaultLogger.warn(
|
|
227
233
|
`Unknown reranker type: ${rerankerType}. Defaulting to InfinityReranker.`
|
|
228
234
|
);
|
|
229
|
-
return new JinaReranker({ apiKey: jinaApiKey, logger: defaultLogger });
|
|
235
|
+
return new JinaReranker({ apiKey: jinaApiKey, apiUrl: jinaApiUrl, logger: defaultLogger });
|
|
230
236
|
}
|
|
231
237
|
};
|
|
232
238
|
|
package/src/tools/search/tool.ts
CHANGED
|
@@ -349,6 +349,7 @@ export const createSearchTool = (
|
|
|
349
349
|
firecrawlOptions,
|
|
350
350
|
scraperTimeout,
|
|
351
351
|
jinaApiKey,
|
|
352
|
+
jinaApiUrl,
|
|
352
353
|
cohereApiKey,
|
|
353
354
|
onSearchResults: _onSearchResults,
|
|
354
355
|
onGetHighlights,
|
|
@@ -395,6 +396,7 @@ export const createSearchTool = (
|
|
|
395
396
|
const selectedReranker = createReranker({
|
|
396
397
|
rerankerType,
|
|
397
398
|
jinaApiKey,
|
|
399
|
+
jinaApiUrl,
|
|
398
400
|
cohereApiKey,
|
|
399
401
|
logger,
|
|
400
402
|
});
|