@arizeai/phoenix-evals
Version:
A library for running evaluations for AI use cases
27 lines • 1.1 kB
JavaScript
import Mustache from "mustache";
import { createTemplateVariablesProxy } from "./createTemplateVariablesProxy.js";
/**
* A function that applies a set of variables to a template (e.g. a prompt)
* Uses the Mustache library to apply the variables to the template
*/
export function formatTemplate(args) {
const { template, variables } = args;
const variablesProxy = createTemplateVariablesProxy(variables);
if (typeof template === "string") {
return renderTemplateString(template, variablesProxy);
}
return template.map((message) => {
if (message.content !== undefined && typeof message.content === "string") {
return {
...message,
content: renderTemplateString(message.content, variablesProxy),
};
}
return message;
});
}
function renderTemplateString(template, variables) {
// Disable HTML escaping by providing a custom escape function that returns text as-is
return Mustache.render(template, variables, {}, { escape: (text) => text });
}
//# sourceMappingURL=applyTemplate.js.map