Download src/lib/utils/template.ts from aminbashi/sidekick: direct link, hf CLI and curl.
- Browser
- Download file 1.09 kB
-
https://huggingface.co/spaces/aminbashi/sidekick/resolve/main/src/lib/utils/template.ts
- Command line
-
hf download hf://spaces/aminbashi/sidekick/src/lib/utils/template.ts
-
curl -L -o template.ts https://huggingface.co/spaces/aminbashi/sidekick/resolve/main/src/lib/utils/template.ts
1.09 kB
| import type { Message } from "$lib/types/Message"; | |
| import type { ChatTemplateInput, LegacyParamatersTemplateInput } from "$lib/types/Template"; | |
| 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: LegacyParamatersTemplateInput) { | |
| const template = Handlebars.compile<T & LegacyParamatersTemplateInput>(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); | |
| }; | |
| } | |
| export function compileTemplate2(input: string, model: LegacyParamatersTemplateInput) { | |
| return compileTemplate<ChatTemplateInput>(input, model) | |
| } | |