Skip to content
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
50 changes: 49 additions & 1 deletion core/llm/llms/Ollama.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,18 @@ type OllamaErrorResponse = {
error: string;
};

type N8nChatReponse = {
type: string;
content?: string;
metadata: {
nodeId: string;
nodeName: string;
itemIndex: number;
runIndex: number;
timestamps: number;
};
};

type OllamaRawResponse =
| OllamaErrorResponse
| (OllamaBaseResponse & {
Expand All @@ -124,7 +136,8 @@ type OllamaChatResponse =
| OllamaErrorResponse
| (OllamaBaseResponse & {
message: OllamaChatMessage;
});
})
| N8nChatReponse;

interface OllamaTool {
type: "function";
Expand Down Expand Up @@ -427,12 +440,47 @@ class Ollama extends BaseLLM implements ModelInstaller {
body: JSON.stringify(chatOptions),
signal,
});
let isThinking: boolean = false;

function convertChatMessage(res: OllamaChatResponse): ChatMessage[] {
if ("error" in res) {
throw new Error(res.error);
}

if ("type" in res) {
const { content } = res;

if (content === "<think>") {
isThinking = true;
}

if (isThinking && content) {
// TODO better support for streaming thinking chunks, or remove this and depend on redux <think/> parsing logic
const thinkingMessage: ThinkingChatMessage = {
role: "thinking",
content: content,
};

if (thinkingMessage) {
// could cause issues with termination if chunk doesn't match this exactly
if (content === "</think>") {
isThinking = false;
}
// When Streaming you can't have both thinking and content
return [thinkingMessage];
}
}

if (content) {
const chatMessage: ChatMessage = {
role: "assistant",
content: content,
};
return [chatMessage];
}
return [];
}

const { role, content, thinking, tool_calls: toolCalls } = res.message;

if (role === "tool") {
Expand Down
Loading