forked from posgnu/rci-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathllm_agent.py
394 lines (330 loc) · 12.7 KB
/
llm_agent.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import json
from prompt import Prompt
import time
import openai
from pathlib import Path
from selenium.webdriver.common.keys import Keys
import os
import logging
from computergym.miniwob.miniwob_interface.action import (
MiniWoBType,
MiniWoBElementClickId,
MiniWoBElementClickXpath,
MiniWoBElementClickOption,
MiniWoBMoveXpath,
)
import re
class LLMAgent:
def __init__(
self,
env: str,
rci_plan_loop: int = 1,
rci_limit: int = 1,
llm="chatgpt",
with_task=True,
state_grounding=True,
) -> None:
self.rci_limit = rci_limit
self.rci_plan_loop = rci_plan_loop
self.llm = llm
self.prompt = Prompt(env=env)
self.state_grounding = state_grounding
self.load_model()
self.html_state = ""
self.task = ""
self.with_task = with_task
self.current_plan = ""
self.past_plan = []
self.past_instruction = []
self.custom_gaol = False
self.history_name = time.strftime("%Y%m%d-%H%M%S")
config_string = (
f"erci{rci_plan_loop}_state{self.state_grounding}_irci{rci_limit}"
)
if self.prompt.example_prompt:
self.file_path = Path(
f"history/{self.llm}/{env}/{config_string}/few-shot/{self.history_name}.txt"
)
else:
self.file_path = Path(
f"history/{self.llm}/{env}/{config_string}/zero-shot/{self.history_name}.txt"
)
self.file_path.parent.mkdir(parents=True, exist_ok=True)
def load_model(self):
with open("config.json") as config_file:
api_key = json.load(config_file)["api_key"]
openai.api_key = api_key
if self.llm == "chatgpt":
self.model = "gpt-3.5-turbo"
elif self.llm == "gpt4":
self.model = "gpt-4"
elif self.llm == "davinci":
self.model = "text-davinci-003"
elif self.llm == "ada":
self.model = "ada"
elif self.llm == "babbage":
self.model = "babbage"
elif self.llm == "curie":
self.model = "curie"
elif self.llm == "davinci1":
self.model = "davinci"
elif self.llm == "davinci2":
self.model = "text-davinci-002"
else:
raise NotImplemented
def save_result(self, result):
with open(self.file_path, "a") as f:
if result:
f.write("\n\nSUCCESS\n\n")
new_file_path = self.file_path.with_name(
f"{self.history_name}_success.txt"
)
else:
f.write("\n\nFAIL\n\n")
new_file_path = self.file_path.with_name(
f"{self.history_name}_fail.txt"
)
os.rename(self.file_path, new_file_path)
return
def save(self, pt):
with open(self.file_path, "a") as f:
f.write("\n")
ho_line = "-" * 30
f.write(ho_line)
f.write("\n\n")
f.write(pt)
return
def set_goal(self, goal: str):
self.custom_gaol = True
self.task = goal
return
def instruction_history_prompt(self):
pt = "\n\n"
pt += "We have a history of instructions that have been already executed by the autonomous agent so far.\n"
if not self.past_instruction:
pt += "No instruction has been executed yet."
else:
for idx, inst in enumerate(self.past_instruction):
pt += f"{idx+1}: "
pt += inst
pt += "\n"
pt += "\n\n"
return pt
def webpage_state_prompt(self, init_plan: bool = False, with_task=False):
pt = "\n\n"
pt += "Below is the HTML code of the webpage where the agent should solve a task.\n"
pt += self.html_state
pt += "\n\n"
if self.prompt.example_prompt and (init_plan or self.rci_plan_loop == -1):
pt += self.prompt.example_prompt
pt += "\n\n"
if with_task:
pt += "Current task: "
pt += self.task
pt += "\n"
return pt
def update_html_state(self, state: str):
self.html_state = state
return
def rci_plan(self, pt=None):
pt += "\n\nFind problems with this plan for the given task compared to the example plans.\n\n"
criticizm = self.get_response(pt)
pt += criticizm
pt += "\n\nBased on this, what is the plan for the agent to complete the task?\n\n"
# pt += self.webpage_state_prompt()
plan = self.get_response(pt)
return pt, plan
def rci_action(self, instruciton: str, pt=None):
instruciton = self.process_instruction(instruciton)
loop_num = 0
while self.check_regex(instruciton):
if loop_num >= self.rci_limit:
print(instruciton)
self.save(pt)
raise ValueError("Action RCI failed")
pt += self.prompt.rci_action_prompt
instruciton = self.get_response(pt)
pt += instruciton
instruciton = self.process_instruction(instruciton)
loop_num += 1
return pt, instruciton
def check_regex(self, instruciton):
return (
(not re.search(self.prompt.clickxpath_regex, instruciton, flags=re.I))
and (not re.search(self.prompt.chatgpt_type_regex, instruciton, flags=re.I))
and (not re.search(self.prompt.davinci_type_regex, instruciton, flags=re.I))
and (not re.search(self.prompt.press_regex, instruciton, flags=re.I))
and (not re.search(self.prompt.clickoption_regex, instruciton, flags=re.I))
and (not re.search(self.prompt.movemouse_regex, instruciton, flags=re.I))
)
def process_instruction(self, instruciton: str):
end_idx = instruciton.find("`")
if end_idx != -1:
instruciton = instruciton[:end_idx]
instruciton = instruciton.replace("`", "")
instruciton = instruciton.replace("\n", "")
instruciton = instruciton.replace("\\n", "\n")
instruciton = instruciton.strip()
instruciton = instruciton.strip("'")
return instruciton
def get_plan_step(self):
idx = 1
while True:
if (str(idx) + ".") not in self.current_plan:
return (idx - 1) + 1
idx += 1
def initialize_plan(self):
if not self.custom_gaol:
if self.with_task:
self.initialize_task()
if not self.prompt.init_plan_prompt or self.rci_plan_loop == -1:
return
pt = self.prompt.base_prompt
pt += self.webpage_state_prompt(True, with_task=self.with_task)
pt += self.prompt.init_plan_prompt
message = "\n" + self.get_response(pt)
pt += message
for _ in range(self.rci_plan_loop):
pt, message = self.rci_plan(pt)
pt += message
self.current_plan = message
self.save(pt)
return
def get_response(self, pt):
import inspect
logging.info(
f"Send a request to the language model from {inspect.stack()[1].function}"
)
while True:
try:
if self.llm == "chatgpt" or self.llm == "gpt4":
time.sleep(1)
response = openai.ChatCompletion.create(
model=self.model,
temperature=0,
top_p=1,
frequency_penalty=0.0,
presence_penalty=0.0,
max_tokens=256,
messages=[
{
"role": "system",
"content": "You are an autoregressive language model that completes user's sentences. You should not conversate with user.",
},
{"role": "user", "content": pt},
],
)
message = response["choices"][0]["message"]["content"]
else:
time.sleep(1)
response = openai.Completion.create(
model=self.model,
prompt=pt,
temperature=0,
max_tokens=256,
top_p=1,
frequency_penalty=0.0,
presence_penalty=0.0,
)
message = response["choices"][0]["text"]
except Exception as e:
print(e)
if "maximum context" in str(e):
raise ValueError
time.sleep(10)
else:
if message:
break
return message
def generate_action(self) -> str:
pt = self.prompt.base_prompt
pt += self.webpage_state_prompt(with_task=self.with_task)
if self.prompt.init_plan_prompt and self.rci_plan_loop != -1:
pt += self.current_plan_prompt()
pt += self.instruction_history_prompt()
if self.past_instruction:
update_action_prompt = self.prompt.action_prompt.replace(
"{prev_inst}", self.past_instruction[-1]
)
if len(self.past_instruction) == 1:
update_action_prompt = self.prompt.action_prompt.replace(
"{order}", "2nd"
)
elif len(self.past_instruction) == 2:
update_action_prompt = self.prompt.action_prompt.replace(
"{order}", "3rd"
)
else:
update_action_prompt = self.prompt.action_prompt.replace(
"{order}", f"{len(self.past_instruction)+1}th"
)
action_prompt = update_action_prompt
else:
action_prompt = self.prompt.first_action_prompt
if self.rci_plan_loop == -1:
action_prompt = "Based on the task, " + action_prompt
else:
action_prompt = (
"Based on the plan and the history of instructions executed so far, "
+ action_prompt
)
pt += action_prompt
message = self.get_response(pt)
pt += self.process_instruction(message) + "`."
pt, message = self.update_action(pt, message)
pt, instruction = self.rci_action(pt=pt, instruciton=message)
self.past_instruction.append(instruction)
self.save(pt)
return instruction
def update_action(self, pt=None, message=None):
if self.prompt.update_action and self.state_grounding:
pt += self.prompt.update_action
message = self.get_response(pt)
pt += message
return pt, message
def current_plan_prompt(self):
pt = "\n\n"
pt += "Here is a plan you are following now.\n"
pt += f"{self.current_plan}"
pt += "\n\n"
return pt
def convert_to_miniwob_action(self, instruction: str):
instruction = instruction.split(" ")
inst_type = instruction[0]
inst_type = inst_type.lower()
if inst_type == "type":
characters = " ".join(instruction[1:])
characters = characters.replace('"', "")
return MiniWoBType(characters)
elif inst_type == "clickid":
element_id = " ".join(instruction[1:])
return MiniWoBElementClickId(element_id)
elif inst_type == "press":
key_type = instruction[1].lower()
if key_type == "enter":
return MiniWoBType("\n")
elif key_type == "space":
return MiniWoBType(" ")
elif key_type == "arrowleft":
return MiniWoBType(Keys.LEFT)
elif key_type == "arrowright":
return MiniWoBType(Keys.RIGHT)
elif key_type == "backspace":
return MiniWoBType(Keys.BACKSPACE)
elif key_type == "arrowup":
return MiniWoBType(Keys.UP)
elif key_type == "arrowdown":
return MiniWoBType(Keys.DOWN)
else:
raise NotImplemented
elif inst_type == "movemouse":
xpath = " ".join(instruction[1:])
return MiniWoBMoveXpath(xpath)
elif inst_type == "clickxpath":
xpath = " ".join(instruction[1:])
return MiniWoBElementClickXpath(xpath)
elif inst_type == "clickoption":
xpath = " ".join(instruction[1:])
return MiniWoBElementClickOption(xpath)
else:
raise ValueError("Invalid instruction")