UNPKG

@llumiverse/drivers

Version:

LLM driver implementations. Currently supported are: openai, huggingface, bedrock, replicate.

131 lines 5.21 kB
"use strict"; // This file is used by multiple drivers // to format prompts in a way that is compatible with OpenAI's API. Object.defineProperty(exports, "__esModule", { value: true }); exports.formatOpenAILikeTextPrompt = formatOpenAILikeTextPrompt; exports.formatOpenAILikeMultimodalPrompt = formatOpenAILikeMultimodalPrompt; const common_1 = require("@llumiverse/common"); const core_1 = require("@llumiverse/core"); /** * OpenAI text only prompts * @param segments * @returns */ function formatOpenAILikeTextPrompt(segments) { const system = []; const safety = []; const user = []; for (const msg of segments) { if (msg.role === common_1.PromptRole.system) { system.push({ content: msg.content, role: "system" }); } else if (msg.role === common_1.PromptRole.safety) { safety.push({ content: "IMPORTANT: " + msg.content, role: "system" }); } else if (msg.role !== common_1.PromptRole.negative && msg.role !== common_1.PromptRole.mask && msg.role !== common_1.PromptRole.tool) { user.push({ content: msg.content, role: msg.role || 'user', }); } } // put system messages first and safety last return system.concat(user).concat(safety); } async function formatOpenAILikeMultimodalPrompt(segments, opts) { const system = []; const safety = []; const others = []; for (const msg of segments) { const parts = []; //generate the parts based on PromptSegment if (msg.files) { for (const file of msg.files) { const stream = await file.getStream(); const data = await (0, core_1.readStreamAsBase64)(stream); parts.push({ type: "input_image", image_url: `data:${file.mime_type || "image/jpeg"};base64,${data}`, detail: "auto", }); } } if (msg.content) { parts.push({ type: "input_text", text: msg.content, }); } if (msg.role === common_1.PromptRole.system) { // For system messages, filter to only text parts const textParts = parts.filter((part) => part.type === 'input_text'); const textContent = textParts.length === 1 && !msg.files ? textParts[0].text : textParts; const systemMsg = { role: "system", content: textContent, }; system.push(systemMsg); if (opts.useToolForFormatting && opts.schema) { system.forEach(s => { if (s.role === 'system') { const sysMsg = s; if (typeof sysMsg.content === 'string') { sysMsg.content = "TOOL: " + sysMsg.content; } else if (Array.isArray(sysMsg.content)) { sysMsg.content.forEach((c) => { if (c.type === "input_text") c.text = "TOOL: " + c.text; }); } } }); } } else if (msg.role === common_1.PromptRole.safety) { const textParts = parts.filter((part) => part.type === 'input_text'); const safetyMsg = { role: "system", content: textParts, }; if (Array.isArray(safetyMsg.content)) { safetyMsg.content.forEach((c) => { if (c.type === "input_text") c.text = "DO NOT IGNORE - IMPORTANT: " + c.text; }); } system.push(safetyMsg); } else if (msg.role === common_1.PromptRole.tool) { if (!msg.tool_use_id) { throw new Error("Tool use id is required for tool messages"); } const toolOutputMsg = { type: "function_call_output", call_id: msg.tool_use_id, output: msg.content || "" }; others.push(toolOutputMsg); } else if (msg.role !== common_1.PromptRole.negative && msg.role !== common_1.PromptRole.mask) { const inputMsg = { role: msg.role === 'assistant' ? 'assistant' : 'user', content: parts, }; others.push(inputMsg); } } if (opts.result_schema && !opts.useToolForFormatting) { const schemaMsg = { role: "system", content: [{ type: "input_text", text: "IMPORTANT: only answer using JSON, and respecting the schema included below, between the <response_schema> tags. " + `<response_schema>${JSON.stringify(opts.result_schema)}</response_schema>` }] }; system.push(schemaMsg); } // put system messages first and safety last return [].concat(system).concat(others).concat(safety); } //# sourceMappingURL=openai_format.js.map