Skip to content

Commit 82ac684

Browse files
fix: ollama tool use
1 parent 01d88a9 commit 82ac684

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

core/llm/llms/Ollama.ts

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Mutex } from "async-mutex";
22
import { JSONSchema7, JSONSchema7Object } from "json-schema";
3+
import { v4 as uuidv4 } from "uuid";
34

45
import {
56
ChatMessage,
@@ -90,10 +91,10 @@ type OllamaBaseResponse = {
9091
model: string;
9192
created_at: string;
9293
} & (
93-
| {
94+
| {
9495
done: false;
9596
}
96-
| {
97+
| {
9798
done: true;
9899
done_reason: string;
99100
total_duration: number; // Time spent generating the response in nanoseconds
@@ -104,7 +105,7 @@ type OllamaBaseResponse = {
104105
eval_duration: number; // Time spent generating the response in nanoseconds
105106
context: number[]; // An encoding of the conversation used in this response; can be sent in the next request to keep conversational memory
106107
}
107-
);
108+
);
108109

109110
type OllamaErrorResponse = {
110111
error: string;
@@ -113,14 +114,14 @@ type OllamaErrorResponse = {
113114
type OllamaRawResponse =
114115
| OllamaErrorResponse
115116
| (OllamaBaseResponse & {
116-
response: string; // the generated response
117-
});
117+
response: string; // the generated response
118+
});
118119

119120
type OllamaChatResponse =
120121
| OllamaErrorResponse
121122
| (OllamaBaseResponse & {
122-
message: OllamaChatMessage;
123-
});
123+
message: OllamaChatMessage;
124+
});
124125

125126
interface OllamaTool {
126127
type: "function";
@@ -370,7 +371,7 @@ class Ollama extends BaseLLM implements ModelInstaller {
370371
if ("error" in j) {
371372
throw new Error(j.error);
372373
}
373-
j.response ??= ''
374+
j.response ??= "";
374375
yield j.response;
375376
} catch (e) {
376377
throw new Error(`Error parsing Ollama response: ${e} ${chunk}`);
@@ -439,6 +440,7 @@ class Ollama extends BaseLLM implements ModelInstaller {
439440
// But ollama returns the full object in one response with no streaming
440441
chatMessage.toolCalls = res.message.tool_calls.map((tc) => ({
441442
type: "function",
443+
id: `tc_${uuidv4()}`, // Generate a proper UUID with a prefix
442444
function: {
443445
name: tc.function.name,
444446
arguments: JSON.stringify(tc.function.arguments),

0 commit comments

Comments
 (0)