Skip to content

Commit

Permalink
test(templates): custom function render
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomas2D committed Sep 23, 2024
1 parent 312626d commit 0decab7
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions src/template.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit 0decab7

Please sign in to comment.