UNPKG

ai-utils.js

Version:

Build AI applications, chatbots, and agents with JavaScript and TypeScript.

46 lines (45 loc) 1.51 kB
import { validateChatPrompt } from "./chat/validateChatPrompt.js"; export const InstructionToTextPromptMapping = () => ({ stopTokens: [], map: (instruction) => instruction.system != null ? `${instruction.system}\n\n${instruction.instruction}` : instruction.instruction, }); /** * A mapping from a chat prompt to a text prompt. * * @param user The label of the user in the chat. * @param ai The name of the AI in the chat. */ export const ChatToTextPromptMapping = ({ user, ai }) => ({ map: (chatPrompt) => { validateChatPrompt(chatPrompt); let text = ""; for (let i = 0; i < chatPrompt.length; i++) { const message = chatPrompt[i]; // system message: if (i === 0 && "system" in message && typeof message.system === "string") { text += `${message.system}\n\n`; continue; } // user message if ("user" in message) { text += `${user}:\n${message.user}\n\n`; continue; } // ai message: if ("ai" in message) { text += `${ai}:\n${message.ai}\n\n`; continue; } // unsupported message: throw new Error(`Unsupported message: ${JSON.stringify(message)}`); } // AI message prefix: text += `${ai}:\n`; return text; }, stopTokens: [`\n${user}:`], });