Skip to content
Open
Show file tree
Hide file tree
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
62 changes: 62 additions & 0 deletions core/llm/llms/Ollama.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,68 @@ describe("Ollama", () => {
});
});

describe("_streamChat", () => {
const tool = {
type: "function",
function: {
name: "read_file",
description: "Read a file",
parameters: {
type: "object",
required: ["filepath"],
properties: { filepath: { type: "string" } },
},
},
};

async function requestBody(messages: ChatMessage[]) {
const ollama = createOllama();
(ollama as any).ensureModelInfo = jest.fn();
(ollama as any).getEndpoint = jest.fn(() => "http://localhost/api/chat");
(ollama as any)._getModel = jest.fn(() => "test-model");
(ollama.fetch as jest.Mock).mockResolvedValue({
json: async () => ({
message: { role: "assistant", content: "done" },
}),
});

const stream = (ollama as any)._streamChat(
messages,
new AbortController().signal,
{ stream: false, tools: [tool] },
);
for await (const _ of stream) {
// Drain the response so the request completes.
}

const [, init] = (ollama.fetch as jest.Mock).mock.calls[0];
return JSON.parse(init.body);
}

it("should attach tools after a tool result for multi-step agent loops", async () => {
const body = await requestBody([
{ role: "user", content: "Read file A, then file B" },
{
role: "assistant",
content: "",
toolCalls: [
{
id: "tc_1",
type: "function",
function: {
name: "read_file",
arguments: '{"filepath":"fileA"}',
},
},
],
},
{ role: "tool", content: "file A", toolCallId: "tc_1" },
]);

expect(body.tools).toEqual([tool]);
});
});

describe("_reorderMessagesForToolCompat", () => {
let ollama: Ollama;

Expand Down
2 changes: 1 addition & 1 deletion core/llm/llms/Ollama.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ class Ollama extends BaseLLM implements ModelInstaller {
stream: options.stream,
// format: options.format, // Not currently in base completion options
};
if (options.tools?.length && ollamaMessages.at(-1)?.role === "user") {
if (options.tools?.length) {
chatOptions.tools = options.tools.map((tool) => ({
type: "function",
function: {
Expand Down
Loading