-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
Copy pathreply.ts
138 lines (127 loc) · 3.6 KB
/
reply.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { composePrompt, parseJSONObjectFromText } from "../prompts";
import {
type Action,
type ActionExample,
type Content,
type HandlerCallback,
type IAgentRuntime,
type Memory,
ModelTypes,
type State,
} from "../types";
const replyTemplate = `# Task: Generate dialog and actions for the character {{agentName}}.
{{providers}}
# Instructions: Write the next message for {{agentName}}.
First, think about what you want to do next and plan your actions. Then, write the next message and include the actions you plan to take.
"thought" should be a short description of what the agent is thinking about and planning.
"message" should be the next message for {{agentName}} which they will send to the conversation.
These are the available valid actions: {{actionNames}}
Response format should be formatted in a valid JSON block like this:
\`\`\`json
{
"thought": "<string>",
"message": "<string>"
}
\`\`\`
Your response should include the valid JSON block and nothing else.`;
export const replyAction = {
name: "REPLY",
similes: ["REPLY_TO_MESSAGE", "SEND_REPLY", "RESPOND"],
description:
"Replies to the current conversation with the text from the generated message. Default if the agent is responding with a message and no other action. Use REPLY at the beginning of a chain of actions as an acknowledgement, and at the end of a chain of actions as a final response.",
validate: async (_runtime: IAgentRuntime) => {
return true;
},
handler: async (
runtime: IAgentRuntime,
message: Memory,
state: State,
_options: any,
callback: HandlerCallback
) => {
state = await runtime.composeState(message, [
...(message.content.providers ?? []),
"RECENT_MESSAGES",
]);
const prompt = composePrompt({
state,
template: replyTemplate,
});
console.log("*** replyAction prompt ****");
console.log(prompt);
const response = await runtime.useModel(ModelTypes.TEXT_LARGE, {
prompt,
});
console.log("*** replyAction response ****");
console.log(response);
const responseContentObj = parseJSONObjectFromText(response) as Content;
const responseContent = {
thought: responseContentObj.thought,
text: (responseContentObj.message as string) || "",
actions: ["REPLY_SENT"],
};
await callback(responseContent);
},
examples: [
[
{
name: "{{name1}}",
content: {
text: "Hello there!",
},
},
{
name: "{{name2}}",
content: {
text: "Hi! How can I help you today?",
actions: ["REPLY"],
},
},
],
[
{
name: "{{name1}}",
content: {
text: "What's your favorite color?",
},
},
{
name: "{{name2}}",
content: {
text: "I really like deep shades of blue. They remind me of the ocean and the night sky.",
actions: ["REPLY"],
},
},
],
[
{
name: "{{name1}}",
content: {
text: "Can you explain how neural networks work?",
},
},
{
name: "{{name2}}",
content: {
text: "Let me break that down for you in simple terms...",
actions: ["REPLY"],
},
},
],
[
{
name: "{{name1}}",
content: {
text: "Could you help me solve this math problem?",
},
},
{
name: "{{name2}}",
content: {
text: "Of course! Let's work through it step by step.",
actions: ["REPLY"],
},
},
],
] as ActionExample[][],
} as Action;