Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix: the tool to fail execution on ollama when a message contains b… (#10259)

Hypo1ad80809cf09
diff --git a/packages/model-runtime/src/core/streams/ollama.test.ts b/packages/model-runtime/src/core/streams/ollama.test.ts
index bd710f0681b..ea5a418068c 100644
--- a/packages/model-runtime/src/core/streams/ollama.test.ts
+++ b/packages/model-runtime/src/core/streams/ollama.test.ts
@@ -235,6 +235,73 @@ describe('OllamaStream', () => {
expect(onToolCall).toHaveBeenCalledTimes(1);
expect(onCompletionMock).toHaveBeenCalledTimes(1);
});
+
+ it('tools use with a done', async () => {
+ vi.spyOn(uuidModule, 'nanoid').mockReturnValueOnce('1').mockReturnValueOnce('abcd1234');
+
+ const mockOllamaStream = new ReadableStream<ChatResponse>({
+ start(controller) {
+ controller.enqueue({
+ model: 'qwen2.5',
+ created_at: new Date('2024-12-01T03:34:55.166692Z'),
+ message: {
+ role: 'assistant',
+ content: '',
+ tool_calls: [
+ {
+ function: {
+ name: 'realtime-weather____fetchCurrentWeather',
+ arguments: { city: '杭州' },
+ },
+ },
+ ],
+ },
+ done_reason: 'stop',
+ done: true,
+ total_duration: 1122415333,
+ load_duration: 26178333,
+ prompt_eval_count: 221,
+ prompt_eval_duration: 507000000,
+ eval_count: 26,
+ eval_duration: 583000000,
+ } as unknown as ChatResponse);
+
+ controller.close();
+ },
+ });
+ const onStartMock = vi.fn();
+ const onTextMock = vi.fn();
+ const onToolCall = vi.fn();
+ const onCompletionMock = vi.fn();
+
+ const protocolStream = OllamaStream(mockOllamaStream, {
+ onStart: onStartMock,
+ onText: onTextMock,
+ onCompletion: onCompletionMock,
+ onToolsCalling: onToolCall,
+ });
+
+ const decoder = new TextDecoder();
+ const chunks = [];
+
+ // @ts-ignore
+ for await (const chunk of protocolStream) {
+ chunks.push(decoder.decode(chunk, { stream: true }));
+ }
+
+ expect(chunks).toEqual(
+ [
+ 'id: chat_1',
+ 'event: tool_calls',
+ `data: [{"function":{"arguments":"{\\"city\\":\\"杭州\\"}","name":"realtime-weather____fetchCurrentWeather"},"id":"realtime-weather____fetchCurrentWeather_0_abcd1234","index":0,"type":"function"}]\n`,
+ ].map((i) => `${i}\n`),
+ );
+
+ expect(onTextMock).toHaveBeenCalledTimes(0);
+ expect(onStartMock).toHaveBeenCalledTimes(1);
+ expect(onToolCall).toHaveBeenCalledTimes(1);
+ expect(onCompletionMock).toHaveBeenCalledTimes(1);
+ });
});
it('should handle empty stream', async () => {
diff --git a/packages/model-runtime/src/core/streams/ollama.ts b/packages/model-runtime/src/core/streams/ollama.ts
index 273c97d43e7..0e73f4da4db 100644
--- a/packages/model-runtime/src/core/streams/ollama.ts
+++ b/packages/model-runtime/src/core/streams/ollama.ts
@@ -11,11 +11,6 @@ import {
} from './protocol';
const transformOllamaStream = (chunk: ChatResponse, stack: StreamContext): StreamProtocolChunk => {
- // maybe need another structure to add support for multiple choices
- if (chunk.done && !chunk.message.content) {
- return { data: 'finished', id: stack.id, type: 'stop' };
- }
-
if (chunk.message.thinking) {
return { data: chunk.message.thinking, id: stack.id, type: 'reasoning' };
}
@@ -36,6 +31,11 @@ const transformOllamaStream = (chunk: ChatResponse, stack: StreamContext): Strea
};
}
+ // maybe need another structure to add support for multiple choices
+ if (chunk.done && !chunk.message.content) {
+ return { data: 'finished', id: stack.id, type: 'stop' };
+ }
+
// 判断是否有 <think> 或 </think> 标签,更新 thinkingInContent 状态
if (chunk.message.content.includes('<think>')) {
stack.thinkingInContent = true;