| import type { Message } from "$lib/types/Message"; |
| import Handlebars from "handlebars"; |
| import { Template } from "@huggingface/jinja"; |
| import { logger } from "$lib/server/logger"; |
|
|
| |
| Handlebars.registerHelper("ifUser", function (this: Pick<Message, "from" | "content">, options) { |
| if (this.from == "user") return options.fn(this); |
| }); |
|
|
| Handlebars.registerHelper( |
| "ifAssistant", |
| function (this: Pick<Message, "from" | "content">, options) { |
| if (this.from == "assistant") return options.fn(this); |
| } |
| ); |
|
|
| |
| export function compileTemplate<T>( |
| input: string, |
| model: { preprompt: string; templateEngine?: string } |
| ) { |
| let jinjaTemplate: Template | undefined; |
| try { |
| |
| jinjaTemplate = new Template(input); |
| } catch (e) { |
| |
| |
| jinjaTemplate = undefined; |
| } |
|
|
| const hbTemplate = Handlebars.compile<T>(input, { |
| knownHelpers: { ifUser: true, ifAssistant: true }, |
| knownHelpersOnly: true, |
| noEscape: true, |
| strict: true, |
| preventIndent: true, |
| }); |
|
|
| return function render(inputs: T) { |
| if (jinjaTemplate) { |
| try { |
| return jinjaTemplate.render({ ...model, ...inputs }); |
| } catch (e) { |
| logger.error(e, "Could not render with Jinja"); |
| |
| return hbTemplate({ ...model, ...inputs }); |
| } |
| } |
| return hbTemplate({ ...model, ...inputs }); |
| }; |
| } |
|
|