forked from i-am-bee/bee-agent-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.ts
76 lines (68 loc) · 1.75 KB
/
template.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import "./helpers/setup.js";
import { PromptTemplate } from "bee-agent-framework/template";
import { Logger } from "bee-agent-framework/logger/logger";
import { z } from "zod";
const logger = new Logger({ name: "template" });
// Primitives
{
const greetTemplate = new PromptTemplate({
template: `Hello {{name}}`,
schema: z.object({
name: z.string(),
}),
});
const output = greetTemplate.render({
name: "Alex",
});
logger.info(output); // "Hello Alex!"
}
// Arrays
{
const template = new PromptTemplate({
schema: z.object({
colors: z.any(z.string()),
}),
template: `My Favorite Colors: {{#colors}}{{.}} {{/colors}}`,
});
const output = template.render({
colors: ["Green", "Yellow"],
});
logger.info(output);
}
// Objects
{
const template = new PromptTemplate({
template: `Expected Duration: {{expected}}ms; Retrieved: {{#responses}}{{duration}}ms {{/responses}}`,
schema: z.object({
expected: z.number().default(5),
responses: z.array(z.object({ duration: z.number() })),
}),
});
const output = template.render({
expected: undefined,
responses: [{ duration: 3 }, { duration: 5 }, { duration: 6 }],
});
logger.info(output);
}
// Forking
{
const original = new PromptTemplate({
template: `You are a helpful assistant called {{name}}. You objective is to {{objective}}.`,
schema: z.object({
name: z.string(),
objective: z.string(),
}),
});
const modified = original.fork((oldConfig) => ({
...oldConfig,
template: `${oldConfig.template} Your answers must be concise.`,
defaults: {
name: "Alex",
},
}));
const output = modified.render({
name: undefined,
objective: "fulfill the user needs",
});
logger.info(output);
}