Merge pull request #5596 from continuedev/tomasz/fix-ollama-tool-use

fix: ollama tool use
This commit is contained in:
Tomasz Stefaniak 2025-05-09 13:59:07 -07:00 • committed by GitHub
commit 4b33db701a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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),