UNPKG

jorel

Version:

The easiest way to use LLMs, including streams, images, documents, tools and various agent scenarios.

77 lines (76 loc) 3.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.convertLlmMessagesToOllamaMessages = void 0; const media_1 = require("../../media"); const tools_1 = require("../../tools"); /** Convert unified LLM messages to Ollama messages (Message) */ const convertLlmMessagesToOllamaMessages = async (messages) => { const convertedMessages = []; for (const message of messages) { if (message.role === "system") { convertedMessages.push({ role: "system", content: message.content, }); } else if (message.role === "assistant") { convertedMessages.push({ role: "assistant", content: message.content, }); } else if (message.role === "assistant_with_tools") { convertedMessages.push({ role: "assistant", content: message.content || "", tool_calls: message.toolCalls.map((toolCall) => ({ function: { name: toolCall.request.function.name, arguments: toolCall.request.function.arguments, }, })), }); for (const toolCall of message.toolCalls) { if (toolCall.executionState === "completed") { convertedMessages.push({ role: "tool", content: tools_1.LlmToolKit.serialize(toolCall.result), }); } else if (toolCall.executionState === "error") { convertedMessages.push({ role: "tool", content: toolCall.error.message, }); } } } else if (message.role === "user") { const content = []; const images = []; for (const entry of message.content) { if (entry.type === "text") { content.push(entry.text); } else if (entry.type === "imageUrl") { const image = await media_1.ImageContent.fromUrl(entry.url); images.push(await image.toUint8Array()); } else if (entry.type === "imageData") { const image = media_1.ImageContent.fromDataUrl(entry.data); images.push(await image.toUint8Array()); } else { throw new Error(`Unsupported content type`); } convertedMessages.push({ role: message.role, content: content.join(""), images: images.length > 0 ? images : undefined, }); } } } return convertedMessages; }; exports.convertLlmMessagesToOllamaMessages = convertLlmMessagesToOllamaMessages;