UNPKG

@arizeai/phoenix-client

Version:

A client for the Phoenix API

61 lines 2.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.formatPromptMessages = formatPromptMessages; const messagePartSchemas_1 = require("../schemas/llm/phoenixPrompt/messagePartSchemas"); const assertUnreachable_1 = require("./assertUnreachable"); /** * Format a list of prompt messages * * @param format - The format of the prompt message variables, e.g. MUSTACHE, F_STRING, NONE * @param promptMessages - The prompt messages to format * @param variables - The variables to use in the formatting * @returns The formatted prompt messages */ function formatPromptMessages(format, promptMessages, variables = {}) { const replacements = []; switch (format) { case "MUSTACHE": { const asMustache = Object.entries(variables).map(([key, value]) => [ new RegExp(`\\{\\{\\s*${key}\\s*\\}\\}(?!\\})`, "g"), value.toString(), ]); replacements.push(...asMustache); break; } case "F_STRING": { const asF_STRING = Object.entries(variables).map(([key, value]) => [ new RegExp(`(?<!\\{)\\{\\s*${key}\\s*\\}(?!\\})`, "g"), value.toString(), ]); replacements.push(...asF_STRING); break; } case "NONE": break; default: (0, assertUnreachable_1.assertUnreachable)(format); } return promptMessages.map((message) => (Object.assign(Object.assign({}, message), { content: typeof message.content == "string" ? applyReplacements(message.content, replacements) : message.content.map((content) => { const textPart = (0, messagePartSchemas_1.asTextPart)(content); if (textPart) { return Object.assign(Object.assign({}, textPart), { text: applyReplacements(textPart.text, replacements) }); } return content; }) }))); } /** * Apply a list of replacements to a string * @param text - The text to apply the replacements to * @param replacements - The replacements to apply * @returns The text with the replacements applied */ function applyReplacements(text, replacements) { let newText = text; for (const [key, value] of replacements) { newText = newText.replaceAll(key, value); } return newText; } //# sourceMappingURL=formatPromptMessages.js.map