-
Notifications
You must be signed in to change notification settings - Fork 137
/
functions.ts
42 lines (37 loc) · 1.02 KB
/
functions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { PromptTemplate } from "bee-agent-framework/template";
import { z } from "zod";
const messageTemplate = new PromptTemplate({
schema: z
.object({
text: z.string(),
author: z.string().optional(),
createdAt: z.string().datetime().optional(),
})
.passthrough(),
functions: {
formatMeta: function () {
if (!this.author && !this.createdAt) {
return "";
}
const author = this.author || "anonymous";
const createdAt = this.createdAt || new Date().toISOString();
return `\nThis message was created at ${createdAt} by ${author}.`;
},
},
template: `Message: {{text}}{{formatMeta}}`,
});
// Message: Hello from 2024!
// This message was created at 2024-01-01T00:00:00.000Z by John.
console.log(
messageTemplate.render({
text: "Hello from 2024!",
author: "John",
createdAt: new Date("2024-01-01").toISOString(),
}),
);
// Message: Hello from the present!
console.log(
messageTemplate.render({
text: "Hello from the present!",
}),
);