ai-utils.js
Version:
Build AI applications, chatbots, and agents with JavaScript and TypeScript.
51 lines (50 loc) • 1.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChatToTextPromptMapping = exports.InstructionToTextPromptMapping = void 0;
const validateChatPrompt_js_1 = require("./chat/validateChatPrompt.cjs");
const InstructionToTextPromptMapping = () => ({
stopTokens: [],
map: (instruction) => instruction.system != null
? `${instruction.system}\n\n${instruction.instruction}`
: instruction.instruction,
});
exports.InstructionToTextPromptMapping = InstructionToTextPromptMapping;
/**
* 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.
*/
const ChatToTextPromptMapping = ({ user, ai }) => ({
map: (chatPrompt) => {
(0, validateChatPrompt_js_1.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}:`],
});
exports.ChatToTextPromptMapping = ChatToTextPromptMapping;