-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.py
293 lines (262 loc) · 11.9 KB
/
action.py
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import re
import time
import voyager.utils as U
from javascript import require
from langchain.prompts import SystemMessagePromptTemplate
from langchain.schema import AIMessage, HumanMessage, SystemMessage
from voyager.prompts import load_prompt
from voyager.control_primitives_context import load_control_primitives_context
class ActionAgent:
def __init__(
self,
model_name="gpt-3.5-turbo",
temperature=0,
request_timout=120,
ckpt_dir="ckpt",
resume=False,
chat_log=True,
execution_error=True,
):
self.ckpt_dir = ckpt_dir
self.chat_log = chat_log
self.execution_error = execution_error
U.f_mkdir(f"{ckpt_dir}/action")
if resume:
print(f"\033[32mLoading Action Agent from {ckpt_dir}/action\033[0m")
self.chest_memory = U.load_json(f"{ckpt_dir}/action/chest_memory.json")
else:
self.chest_memory = {}
self.llm = model_name
def update_chest_memory(self, chests):
for position, chest in chests.items():
if position in self.chest_memory:
if isinstance(chest, dict):
self.chest_memory[position] = chest
if chest == "Invalid":
print(
f"\033[32mAction Agent removing chest {position}: {chest}\033[0m"
)
self.chest_memory.pop(position)
else:
if chest != "Invalid":
print(f"\033[32mAction Agent saving chest {position}: {chest}\033[0m")
self.chest_memory[position] = chest
U.dump_json(self.chest_memory, f"{self.ckpt_dir}/action/chest_memory.json")
def render_chest_observation(self):
chests = []
for chest_position, chest in self.chest_memory.items():
if isinstance(chest, dict) and len(chest) > 0:
chests.append(f"{chest_position}: {chest}")
for chest_position, chest in self.chest_memory.items():
if isinstance(chest, dict) and len(chest) == 0:
chests.append(f"{chest_position}: Empty")
for chest_position, chest in self.chest_memory.items():
if isinstance(chest, str):
assert chest == "Unknown"
chests.append(f"{chest_position}: Unknown items inside")
assert len(chests) == len(self.chest_memory)
if chests:
chests = "\n".join(chests)
return f"Chests:\n{chests}\n\n"
else:
return f"Chests: None\n\n"
def render_system_message(self, skills=[]):
system_template = load_prompt("action_template")
# FIXME: Hardcoded control_primitives
base_skills = [
"exploreUntil",
"mineBlock",
"craftItem",
"placeItem",
"smeltItem",
"killMob",
]
base_skills += [
"useChest",
"mineflayer",
]
programs = "\n\n".join(load_control_primitives_context(base_skills) + skills)
response_format = load_prompt("action_response_format")
system_message_prompt = SystemMessagePromptTemplate.from_template(
system_template
)
system_message = system_message_prompt.format(
programs=programs, response_format=response_format
)
assert isinstance(system_message, SystemMessage)
return system_message
def render_human_message(
self, *, events, code="", task="", context="", critique="", messages=[]
):
# observationMessages = []
# # we want to have a moving window of previous messages
# if len(messages) > 0:
# for i, message in enumerate(messages):
# if len(observationMessages) == 5:
# observationMessages.pop(0)
# observationMessages.append(message.content)
chat_messages = []
error_messages = []
# FIXME: damage_messages is not used
damage_messages = []
assert events[-1][0] == "observe", "Last event must be observe"
for i, (event_type, event) in enumerate(events):
if event_type == "onChat":
chat_messages.append(event["onChat"])
elif event_type == "onError":
error_messages.append(event["onError"])
elif event_type == "onDamage":
damage_messages.append(event["onDamage"])
elif event_type == "observe":
biome = event["status"]["biome"]
time_of_day = event["status"]["timeOfDay"]
voxels = event["voxels"]
entities = event["status"]["entities"]
health = event["status"]["health"]
hunger = event["status"]["food"]
position = event["status"]["position"]
equipment = event["status"]["equipment"]
inventory_used = event["status"]["inventoryUsed"]
inventory = event["inventory"]
assert i == len(events) - 1, "observe must be the last event"
observation = ""
# observation += "Previous responses:\n"
# for i, message in enumerate(observationMessages):
# observation += f"Previous response #{i+1}:\n{message}\n\n"
# if code:
# observation += f"Code from the last round:\n{code}\n\n"
# else:
# observation += f"Code from the last round: No code in the first round\n\n"
if self.execution_error:
if error_messages:
error = "\n".join(error_messages)
observation += f"Execution error:\n{error}\n\n"
else:
observation += f"Execution error: No error\n\n"
if self.chat_log:
if chat_messages:
chat_log = "\n".join(chat_messages)
observation += f"Chat log: {chat_log}\n\n"
else:
observation += f"Chat log: None\n\n"
observation += f"Biome: {biome}\n\n"
observation += f"Time: {time_of_day}\n\n"
if voxels:
observation += f"Nearby blocks: {', '.join(voxels)}\n\n"
else:
observation += f"Nearby blocks: None\n\n"
if entities:
nearby_entities = [
k for k, v in sorted(entities.items(), key=lambda x: x[1])
]
observation += f"Nearby entities (nearest to farthest): {', '.join(nearby_entities)}\n\n"
else:
observation += f"Nearby entities (nearest to farthest): None\n\n"
observation += f"Health: {health:.1f}/20\n\n"
observation += f"Hunger: {hunger:.1f}/20\n\n"
observation += f"Position: x={position['x']:.1f}, y={position['y']:.1f}, z={position['z']:.1f}\n\n"
observation += f"Equipment: {equipment}\n\n"
if inventory:
observation += f"Inventory ({inventory_used}/36): {inventory}\n\n"
else:
observation += f"Inventory ({inventory_used}/36): Empty\n\n"
if not (
task == "Place and deposit useless items into a chest"
or task.startswith("Deposit useless items into the chest at")
):
observation += self.render_chest_observation()
observation += f"Task: {task}\n\n"
if context:
observation += f"Context: {context}\n\n"
else:
observation += f"Context: None\n\n"
if critique:
observation += f"Critique: {critique}\n\n"
else:
observation += f"Critique: None\n\n"
return HumanMessage(content=observation)
def process_ai_message(self, message):
assert isinstance(message, AIMessage)
retry = 3
error = None
while retry > 0:
try:
babel = require("@babel/core")
babel_generator = require("@babel/generator").default
code_pattern = re.compile(r"```(?:javascript|js)(.*?)```", re.DOTALL)
# code = "\n".join(code_pattern.findall(message.content))
functions = []
functionDic = {}
code_snippets = code_pattern.findall(message.content)
for snippet in reversed(code_snippets):
parsed_snippet = babel.parse(snippet)
assert len(list(parsed_snippet.program.body)) > 0, "No functions found"
for i, node in enumerate(parsed_snippet.program.body):
if node.type != "FunctionDeclaration":
continue
node_type = (
"AsyncFunctionDeclaration"
if node["async"]
else "FunctionDeclaration"
)
if node.id.name not in functionDic:
functionDic[node.id.name] = 1
else:
functionDic[node.id.name] += 1
node.id.name = f"{node.id.name}V{functionDic[node.id.name]}"
functions.append(
{
"name": node.id.name,
"type": node_type,
"body": babel_generator(node).code,
"params": list(node["params"]),
}
)
break
# find the last async function
main_function = None
for function in reversed(functions):
if function["type"] == "AsyncFunctionDeclaration":
main_function = function
break
assert (
main_function is not None
), "No async function found. Your main function must be async."
assert (
len(main_function["params"]) == 1
and main_function["params"][0].name == "bot"
), f"Main function {main_function['name']} must take a single argument named 'bot'"
program_code = "\n\n".join(function["body"] for function in functions)
exec_code = f"await {main_function['name']}(bot);"
return {
"program_code": program_code,
"program_name": main_function["name"],
"exec_code": exec_code,
}
except Exception as e:
retry -= 1
error = e
time.sleep(1)
return f"Error parsing action response (before program execution): {error}"
def summarize_chatlog(self, events):
def filter_item(message: str):
craft_pattern = r"I cannot make \w+ because I need: (.*)"
craft_pattern2 = (
r"I cannot make \w+ because there is no crafting table nearby"
)
mine_pattern = r"I need at least a (.*) to mine \w+!"
if re.match(craft_pattern, message):
return re.match(craft_pattern, message).groups()[0]
elif re.match(craft_pattern2, message):
return "a nearby crafting table"
elif re.match(mine_pattern, message):
return re.match(mine_pattern, message).groups()[0]
else:
return ""
chatlog = set()
for event_type, event in events:
if event_type == "onChat":
item = filter_item(event["onChat"])
if item:
chatlog.add(item)
return "I also need " + ", ".join(chatlog) + "." if chatlog else ""