Spaces:
Running
Running
Download src/lib/utils/template.ts from huggingchat/chat-ui: direct link, hf CLI and curl.
- Browser
- Download file 808 Bytes
-
https://huggingface.co/spaces/huggingchat/chat-ui/resolve/refs%2Fpr%2F511/src/lib/utils/template.ts
- Command line
-
hf download hf://spaces/huggingchat/chat-ui@refs/pr/511/src/lib/utils/template.ts
-
curl -L -o template.ts https://huggingface.co/spaces/huggingchat/chat-ui/resolve/refs%2Fpr%2F511/src/lib/utils/template.ts
808 Bytes
| import type { Message } from "$lib/types/Message"; | |
| import Handlebars from "handlebars"; | |
| 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 }) { | |
| const template = Handlebars.compile<T>(input, { | |
| knownHelpers: { ifUser: true, ifAssistant: true }, | |
| knownHelpersOnly: true, | |
| noEscape: true, | |
| strict: true, | |
| preventIndent: true, | |
| }); | |
| return function render(inputs: T, options?: RuntimeOptions) { | |
| return template({ ...model, ...inputs }, options); | |
| }; | |
| } | |