UNPKG

jorel

Version:

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

134 lines (133 loc) 4.85 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.convertLlmMessagesToGoogleGenerativeAiMessages = convertLlmMessagesToGoogleGenerativeAiMessages; exports.extractTextFromContent = extractTextFromContent; function convertContentToGenerativeAiPart(content) { switch (content.type) { case "text": return { text: content.text }; case "imageUrl": return { fileData: { mimeType: content.mimeType || "image/jpeg", fileUri: content.url, }, }; case "imageData": return { inlineData: { mimeType: content.mimeType || "image/jpeg", data: content.data, }, }; } } /** * Converts a tool call to a Google Generative AI function call part */ function convertToolCallToFunctionCallPart(toolCall) { return { functionCall: { name: toolCall.request.function.name, args: toolCall.request.function.arguments, }, }; } /** * Converts a completed tool call to a Google Generative AI function response part */ function convertCompletedToolCallToFunctionResponsePart(toolCall) { if (toolCall.executionState === "completed") { return { functionResponse: { name: toolCall.request.function.name, response: toolCall.result, }, }; } else if (toolCall.executionState === "error") { return { functionResponse: { name: toolCall.request.function.name, response: { error: toolCall.error.message }, }, }; } throw new Error(`Cannot convert tool call with execution state ${toolCall.executionState} to function response`); } function convertLlmMessagesToGoogleGenerativeAiMessages(messages) { const contents = []; let systemInstruction; for (const message of messages) { switch (message.role) { case "system": systemInstruction = message.content; break; case "user": { const parts = []; if (typeof message.content === "string") { // Handle string content (backward compatibility) parts.push({ text: message.content }); } else if (Array.isArray(message.content)) { // Handle array of content parts for (const content of message.content) { parts.push(convertContentToGenerativeAiPart(content)); } } contents.push({ role: "user", parts, }); break; } case "assistant": contents.push({ role: "model", parts: [{ text: message.content }], }); break; case "assistant_with_tools": { // First add the assistant's text response if it exists const parts = []; if (message.content) { parts.push({ text: message.content }); } // Then add any function calls the assistant made for (const toolCall of message.toolCalls) { parts.push(convertToolCallToFunctionCallPart(toolCall)); } // Add the assistant message with function calls contents.push({ role: "model", parts, }); // Add function responses as a user message const functionResponseParts = []; for (const toolCall of message.toolCalls) { if (toolCall.executionState === "completed" || toolCall.executionState === "error") { functionResponseParts.push(convertCompletedToolCallToFunctionResponsePart(toolCall)); } } // Only add the function response message if there are any completed tool calls if (functionResponseParts.length > 0) { contents.push({ role: "user", parts: functionResponseParts, }); } break; } } } return { contents, systemInstruction }; } /** * Helper function to extract text content from a Content object */ function extractTextFromContent(content) { return content.parts .map((part) => ("text" in part ? part.text : "")) .filter((text) => text !== undefined) .join(""); }