-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathagent_arch.py
245 lines (212 loc) · 10.3 KB
/
agent_arch.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
"""
Copyright (c) 2023, salesforce.com, inc.
All rights reserved.
SPDX-License-Identifier: Apache License 2.0
For full license text, see the LICENSE file in the repo root or https://www.apache.org/licenses/LICENSE-2.0
"""
import random
import re
import json
import time
import os
import tiktoken
import web_run.pre_prompt as pre_prompt
from web_run.utils import get_query
from web_run.llms import token_enc
class BaseAgent(object):
def __init__(self, llm, context_len=2000):
self.type = "Base_Webrun_Agent"
self.life_label = time.time()
self.name = f"{self.type}_{self.life_label}"
self.task = {}
self.actions = {}
self.observations = {}
self.item_recall = {}
self.llm = llm
self.context_len = context_len
self.cur_session = 0
def new_session(self, idx, task):
self.cur_session = idx
self.task[idx] = task
self.actions[idx] = ['reset']
self.observations[idx] = []
self.item_recall[idx] = []
def llm_layer(self, prompt):
return self.llm(prompt)
def get_history(self, session=None):
if not session:
session = self.cur_session
history = ""
for idx in range(1, len(self.actions[session])):
history += f"Action: {self.actions[session][idx]}\n"
history += f"Observation: {self.observations[session][idx]}\n\n"
return history
def save(self, path):
file_name = os.path.join(path, self.name+'_'+str(self.life_label))
with open(file_name, 'a') as f:
f.write(f"session_idx:{self.cur_session}\n")
history = self.get_history(self.cur_session)
f.write(history)
f.write('\n')
def action_parser(self, text, available_actions):
nor_text = text.strip().lower()
if nor_text.startswith('search'):
query = get_query(nor_text)
return f"search[{query}]"
if nor_text.startswith('click'):
if 'click' in available_actions:
for a in available_actions['click']:
if a.lower() in nor_text:
return f"click[{a}]"
return text
def prompt_layer(self):
raise NotImplementedError
def forward(self, observation, available_actions=None):
raise NotImplementedError
class ReactAgent(BaseAgent):
def __init__(self, llm, context_len=2000):
super().__init__(llm, context_len)
self.type = "React_Webrun_Agent"
self.name = f"{self.type}_{self.life_label}"
def prompt_layer(self):
one_shot = pre_prompt.oneshot
prompt = f"{one_shot}{self.observations[self.cur_session][0]}\n\nAction:"
if len(self.actions[self.cur_session]) > 1:
initial_prompt = f"{one_shot}{self.observations[self.cur_session][0]}\n\n"
history = self.get_history()
remain_context_space = (self.context_len - len(token_enc.encode(initial_prompt)))*3
history = history[-int(remain_context_space):]
prompt = f"{initial_prompt}{history}\n\nAction:"
return prompt
def forward(self, observation, available_actions=None):
self.observations[self.cur_session].append(observation)
prompt = self.prompt_layer()
action = self.llm_layer(prompt)
self.actions[self.cur_session].append(action)
return action
class ZeroshotAgent(BaseAgent):
def __init__(self, llm, context_len=2000, temperature=0.9, stop=['\n']):
super().__init__(llm, context_len)
self.type = "Zeroshot_Webrun_Agent"
self.life_label = time.time()
self.name = f"{self.type}_{self.life_label}"
def llm_layer(self, prompt):
return self.llm(prompt)
def avai_action_prompt(self, available_actions):
if 'search' in available_actions:
return "{search}"
elif 'click' in available_actions:
actions = available_actions['click']
ac_string = "\t".join(actions)
return f"click: [{ac_string}]"
def prompt_layer(self, available_actions):
zeroshot = pre_prompt.zeroshot
actions_prompt = f"current available action is {self.avai_action_prompt(available_actions)[-3000:]}"
prompt = f"{zeroshot}{self.observations[self.cur_session][0]}{actions_prompt}\n\nAction:"
if len(self.actions[self.cur_session]) > 1:
initial_prompt = f"{zeroshot}{self.observations[self.cur_session][0]}\n\n"
history = self.get_history()
remain_context_space = (self.context_len - len(token_enc.encode(f"{initial_prompt}{actions_prompt}")))*3
history = history[-int(remain_context_space):]
prompt = f"{initial_prompt}{history}{actions_prompt}\n\nAction:"
return prompt
def forward(self, observation, available_actions=None):
self.observations[self.cur_session].append(observation)
prompt = self.prompt_layer(available_actions)
action = self.llm_layer(prompt).lstrip()
action = self.action_parser(action, available_actions)
self.actions[self.cur_session].append(action)
return action
class ZeroshotThinkAgent(BaseAgent):
def __init__(self, llm, context_len=2000, temperature=0.9, stop=['\n']):
super().__init__(llm, context_len)
self.type = "ZeroshotThink_Webrun_Agent"
self.life_label = time.time()
self.name = f"{self.type}_{self.life_label}"
self.temperature = temperature
self.stop = stop
def llm_layer(self, prompt):
return self.llm(prompt)
def avai_action_prompt(self, available_actions):
if 'search' in available_actions:
return "{search}"
elif 'click' in available_actions:
actions = available_actions['click']
ac_string = "\t".join(actions)
return f"click: [{ac_string}]"
def prompt_layer(self, available_actions):
zeroshot = pre_prompt.zeroshot_think
actions_prompt = f"current available action is {self.avai_action_prompt(available_actions)[-3000:]}"
prompt = f"{zeroshot}{self.observations[self.cur_session][0]}{actions_prompt}\n\nAction:"
if len(self.actions[self.cur_session]) > 1:
initial_prompt = f"{zeroshot}{self.observations[self.cur_session][0]}\n\n"
history = self.get_history()
remain_context_space = (self.context_len - len(token_enc.encode(initial_prompt+actions_prompt)))*3
history = history[-int(remain_context_space):]
prompt = f"{initial_prompt}{history}{actions_prompt}\n\nAction:"
return prompt
def forward(self, observation, available_actions=None):
self.observations[self.cur_session].append(observation)
prompt = self.prompt_layer(available_actions)
action = self.llm_layer(prompt, temperature=self.temperature, stop=self.stop).lstrip()
action = self.action_parser(action, available_actions)
self.actions[self.cur_session].append(action)
return action
class PlannerAgent(BaseAgent):
def __init__(self, llm, context_len=2000, temperature=0.9, stop=['\n']):
super().__init__(llm, context_len)
self.type = "Planner_Webrun_Agent"
self.name = f"{self.type}_{self.life_label}"
self.temperature = temperature
self.stop = stop
self.max_plan_token_len = 256
self.plan = None
def planning(self):
plan_prompt = f"{pre_prompt.plan}\n\nInstruction: {self.task[self.cur_session]}\nPlan:"
self.plan = self.llm(plan_prompt, temperature=self.temperature, stop=["\n"],
max_tokens=self.max_plan_token_len)
def llm_layer(self, prompt):
return self.llm(prompt)
def prompt_layer(self, available_actions):
oneshot = pre_prompt.oneshot_plan
prompt = f"{oneshot}\n\nInstruction: {self.task[self.cur_session]}\nPlan: {self.plan}\n\nAction:"
if len(self.actions[self.cur_session]) > 1:
initial_prompt = f"{oneshot}\n\nInstruction: {self.task[self.cur_session]}\nPlan: {self.plan}\n\n"
history = self.get_history()
remain_context_space = (self.context_len - len(token_enc.encode(initial_prompt)))*3
history = history[-remain_context_space:]
prompt = f"{initial_prompt}{history}Action:"
return prompt
def forward(self, observation, available_actions=None):
self.observations[self.cur_session].append(observation)
prompt = self.prompt_layer(available_actions)
action = self.llm_layer(prompt).lstrip(' ')
action = self.action_parser(action, available_actions)
self.actions[self.cur_session].append(action)
return action
class PlannerReactAgent(PlannerAgent):
def __init__(self, llm, context_len=2000, temperature=0.9, stop=['\n']):
super().__init__(llm, context_len, temperature, stop)
self.type = "PlannerReact_Webrun_Agent"
self.name = f"{self.type}_{self.life_label}"
def planning(self):
plan_prompt = f"{pre_prompt.plan_react}\n\nInstruction: {self.task[self.cur_session]}\nPlan:"
self.plan = self.llm(plan_prompt, temperature=self.temperature, stop=["\n"],
max_tokens=self.max_plan_token_len)
def prompt_layer(self, available_actions=None):
oneshot = pre_prompt.oneshot_plan_react
prompt = f"{oneshot}\n\nInstruction: {self.task[self.cur_session]}\nPlan: {self.plan}\n\nAction:"
if len(self.actions[self.cur_session]) > 1:
initial_prompt = f"{oneshot}\n\nInstruction: {self.task[self.cur_session]}\nPlan: {self.plan}\n\n"
history = self.get_history()
remain_context_space = (self.context_len - len(token_enc.encode(initial_prompt)))*3
history = history[-remain_context_space:]
prompt = f"{initial_prompt}{history}Action:"
return prompt
def forward(self, observation, available_actions=None):
self.observations[self.cur_session].append(observation)
prompt = self.prompt_layer(available_actions)
action = self.llm_layer(prompt).lstrip(' ')
action = self.action_parser(action, available_actions)
self.actions[self.cur_session].append(action)
return action