From 0decab71a551b882ea57e7864012e88000c2d05a Mon Sep 17 00:00:00 2001 From: Tomas Dvorak Date: Mon, 23 Sep 2024 20:54:30 +0200 Subject: [PATCH] test(templates): custom function render --- src/template.test.ts | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/template.test.ts b/src/template.test.ts index fe9eace5..ea8a2c9c 100644 --- a/src/template.test.ts +++ b/src/template.test.ts @@ -137,6 +137,38 @@ describe("Prompt Template", () => { }); }); + describe("Functions", () => { + it("Trims", () => { + const template = new PromptTemplate({ + template: `{{#trim}}{{names}},{{/trim}}`, + schema: z.object({ + names: z.array(z.string()), + }), + }); + expect(template.render({ names: ["Tomas", "Alex"] })).toMatchInlineSnapshot(`"Tomas,Alex"`); + }); + + it("Custom function", () => { + const template = new PromptTemplate({ + template: [`Name: {{name}}`, `User Name: {{userName}}`].join("\n"), + schema: z.object({ name: z.string() }), + functions: { + userName: function () { + return this.name.replaceAll(" ", "-").toLowerCase().trim(); + }, + }, + }); + expect( + template.render({ + name: "John Doe", + }), + ).toMatchInlineSnapshot(` + "Name: John Doe + User Name: john-doe" + `); + }); + }); + describe("Customization", () => { const createTemplate = () => { return new PromptTemplate({