@arizeai/phoenix-client
Version:
A client for the Phoenix API
64 lines • 2.3 kB
JavaScript
import { asTextPart, } from "../schemas/llm/phoenixPrompt/messagePartSchemas.js";
import { assertUnreachable } from "./assertUnreachable.js";
/**
* 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
*/
export 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:
assertUnreachable(format);
}
return promptMessages.map((message) => ({
...message,
content: typeof message.content == "string"
? applyReplacements(message.content, replacements)
: message.content.map((content) => {
const textPart = asTextPart(content);
if (textPart) {
return {
...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