From 82ac6848cabca21122995460328b8f4420ae3222 Mon Sep 17 00:00:00 2001 From: Tomasz Stefaniak Date: Fri, 9 May 2025 13:36:04 -0700 Subject: [PATCH] fix: ollama tool use --- core/llm/llms/Ollama.ts | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/core/llm/llms/Ollama.ts b/core/llm/llms/Ollama.ts index 427d2492e..ca029488a 100644 --- a/core/llm/llms/Ollama.ts +++ b/core/llm/llms/Ollama.ts @@ -1,5 +1,6 @@ import { Mutex } from "async-mutex"; import { JSONSchema7, JSONSchema7Object } from "json-schema"; +import { v4 as uuidv4 } from "uuid"; import { ChatMessage, @@ -90,10 +91,10 @@ type OllamaBaseResponse = { model: string; created_at: string; } & ( - | { + | { done: false; } - | { + | { done: true; done_reason: string; total_duration: number; // Time spent generating the response in nanoseconds @@ -104,7 +105,7 @@ type OllamaBaseResponse = { eval_duration: number; // Time spent generating the response in nanoseconds context: number[]; // An encoding of the conversation used in this response; can be sent in the next request to keep conversational memory } - ); +); type OllamaErrorResponse = { error: string; @@ -113,14 +114,14 @@ type OllamaErrorResponse = { type OllamaRawResponse = | OllamaErrorResponse | (OllamaBaseResponse & { - response: string; // the generated response - }); + response: string; // the generated response + }); type OllamaChatResponse = | OllamaErrorResponse | (OllamaBaseResponse & { - message: OllamaChatMessage; - }); + message: OllamaChatMessage; + }); interface OllamaTool { type: "function"; @@ -370,7 +371,7 @@ class Ollama extends BaseLLM implements ModelInstaller { if ("error" in j) { throw new Error(j.error); } - j.response ??= '' + j.response ??= ""; yield j.response; } catch (e) { throw new Error(`Error parsing Ollama response: ${e} ${chunk}`); @@ -439,6 +440,7 @@ class Ollama extends BaseLLM implements ModelInstaller { // But ollama returns the full object in one response with no streaming chatMessage.toolCalls = res.message.tool_calls.map((tc) => ({ type: "function", + id: `tc_${uuidv4()}`, // Generate a proper UUID with a prefix function: { name: tc.function.name, arguments: JSON.stringify(tc.function.arguments),