Skip to content

fix: ollama tool use #5596

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions core/llm/llms/Ollama.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Mutex } from "async-mutex";
import { JSONSchema7, JSONSchema7Object } from "json-schema";
import { v4 as uuidv4 } from "uuid";

import {
ChatMessage,
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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";
Expand Down Expand Up @@ -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}`);
Expand Down Expand Up @@ -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),
Expand Down
Loading