-
Notifications
You must be signed in to change notification settings - Fork 2
/
construct_preference_monte_carlo_webshop.py
418 lines (385 loc) · 22.8 KB
/
construct_preference_monte_carlo_webshop.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import os
import json
import argparse
import glob
import numpy as np
instruction_part = {
"webshop": [
{
"from": "human",
"value": "You are web shopping.\nI will give you instructions about what to do.\nYou have to follow the instructions.\nEvery round I will give you an observation and a list of available actions, you have to respond an action based on the state and instruction.\nYou can use search action if search is available.\nYou can click one of the buttons in clickables.\nAn action should be of the following structure:\nsearch[keywords]\nclick[value]\nIf the action is not valid, perform nothing.\nKeywords in search are up to you, but the value in click must be a value in the list of available actions.\nRemember that your keywords in search should be carefully designed.\nYour response should use the following format:\n\nThought: I think ...\nAction: click[something]"
},
{
"from": "gpt",
"value": "OK"
},
],
"sciworld": [
{
"from": "human",
"value": "You are a helpful assistant to do some scientific experiment in an environment.\nIn the environment, there are several rooms: kitchen, foundry, workshop, bathroom, outside, living room, bedroom, greenhouse, art studio, hallway\nYou should explore the environment and find the items you need to complete the experiment.\nYou can teleport to any room in one step.\nAll containers in the environment have already been opened, you can directly get items from the containers.\n\nThe available actions are:\n open OBJ: open a container\n close OBJ: close a container\n activate OBJ: activate a device\n deactivate OBJ: deactivate a device\n connect OBJ to OBJ: connect electrical components\n disconnect OBJ: disconnect electrical components\n use OBJ [on OBJ]: use a device/item\n look around: describe the current room\n examine OBJ: describe an object in detail\n look at OBJ: describe a container's contents\n read OBJ: read a note or book\n move OBJ to OBJ: move an object to a container\n pick up OBJ: move an object to the inventory\n pour OBJ into OBJ: pour a liquid into a container\n mix OBJ: chemically mix a container\n teleport to LOC: teleport to a specific room\n focus on OBJ: signal intent on a task object\n wait: task no action for 10 steps\n wait1: task no action for a step"
},
{
"from": "gpt",
"value": "OK"
},
],
"alfworld": [
{
"from": "human",
"value": "Interact with a household to solve a task. Imagine you are an intelligent agent in a household environment and your target is to perform actions to complete the task goal. At the beginning of your interactions, you will be given the detailed description of the current environment and your goal to accomplish. \nFor each of your turn, you will be given the observation of the last turn. You should first think about the current condition and plan for your future actions, and then output your action in this turn. Your output must strictly follow this format:\"Thought: your thoughts.\\nAction: your next action\".\n\nThe available actions are:\n1. go to {recep}\n2. task {obj} from {recep}\n3. put {obj} in/on {recep}\n4. open {recep}\n5. close {recep}\n6. toggle {obj} {recep}\n7. clean {obj} with {recep}\n8. heat {obj} with {recep}\n9. cool {obj} with {recep}\nwhere {obj} and {recep} correspond to objects and receptacles.\nAfter your each turn, the environment will give you immediate feedback based on which you plan your next few steps. if the envrionment output \"Nothing happened\", that means the previous action is invalid and you should try more options.\n\nYour response should use the following format:\n\nThought: <your thoughts>\nAction: <your next action>"
},
{
"from": "gpt",
"value": "OK"
},
]
}
icl_prompt_part = {
"webshop":[
{
"from": "human",
"value": "You are web shopping.\nI will give you instructions about what to do.\nYou have to follow the instructions.\nEvery round I will give you an observation and a list of available actions, you have to respond an action based on the state and instruction.\nYou can use search action if search is available.\nYou can click one of the buttons in clickables.\nAn action should be of the following structure:\nsearch[keywords]\nclick[value]\nIf the action is not valid, perform nothing.\nKeywords in search are up to you, but the value in click must be a value in the list of available actions.\nRemember that your keywords in search should be carefully designed.\nYour response should use the following format:\n\nThought: I think ...\nAction: click[something]"
},
{
"from": "gpt",
"value": "OK"
},
{
"from": "human",
"value": "Observation:\nWebShop [SEP] Instruction: [SEP] i need a long lasting 6.76 fl oz bottle of l'eau d'issey, and price lower than 100.00 dollars [SEP] Search"
},
{
"from": "gpt",
"value": "Thought: I think I should use the search bar to look for the product I need.\nAction: search[l'eau d'issey 6.76 fl oz bottle price < 100.00]"
},
{
"from": "human",
"value": "Observation:\nInstruction: [SEP] i need a long lasting 6.76 fl oz bottle of l'eau d'issey, and price lower than 100.00 dollars [SEP] Back to Search [SEP] Page 1 (Total results: 50) [SEP] Next > [SEP] B000VOHH8I [SEP] L'eau D'issey By Issey Miyake for MenEau De Toilette Spray, 6.7 Fl Oz Bottle [SEP] $64.98 [SEP] B000MJZOPK [SEP] L'eau d'Issey by Issey Miyake for Women 3.3 oz Eau de Toilette Spray [SEP] $49.98 [SEP] B0012S249E [SEP] L'eau D'issey By Issey Miyake For Women. Shower Cream 6.7-Ounces [SEP] $31.36 [SEP] B01H8PGKZS [SEP] L'eau D'Issey FOR MEN by Issey Miyake - 6.7 oz EDT Spray [SEP] $67.97 [SEP] B00G3C8FHE [SEP] L'Eau d'Issey pour Homme - Eau de Toilette 4.2 fl oz [SEP] $51.25 [SEP] B000R94HRG [SEP] Issey Miyake L'Eau D'Issey Pour Homme Eau De Toilette Natural Spray [SEP] $44.99 [SEP] B000C214CO [SEP] Issey Miyake L'eau D'issey Eau de Toilette Spray for Men, 4.2 Fl Oz [SEP] $53.99 [SEP] B0018SBRDC [SEP] Issey Miyake L'eau d'Issey for Women EDT, White, 0.84 Fl Oz [SEP] $27.04 [SEP] B000XEAZ9Y [SEP] L'eau De Issey By Issey Miyake For Men. Eau De Toilette Spray 6.7 Fl Oz [SEP] $67.08 [SEP] B079HZR2RX [SEP] L'eau d'Issey Pure by Issey Miyake for Women 3.0 oz Nectar de Parfum Spray [SEP] $71.49"
},
{
"from": "gpt",
"value": "Thought: I think I should click on the product I need, which is B000VOHH8I.\nAction: click[B000VOHH8I]"
},
{
"from": "human",
"value": "Observation:\nInstruction: [SEP] i need a long lasting 6.76 fl oz bottle of l'eau d'issey, and price lower than 100.00 dollars [SEP] Back to Search [SEP] < Prev [SEP] size [SEP] 2.5 fl oz [SEP] 6.76 fl oz (pack of 1) [SEP] L'eau D'issey By Issey Miyake for MenEau De Toilette Spray, 6.7 Fl Oz Bottle [SEP] Price: $64.98 [SEP] Rating: N.A. [SEP] Description [SEP] Features [SEP] Reviews [SEP] Buy Now"
},
{
"from": "gpt",
"value": "Thought: I think I should click on the '6.76 fl oz (pack of 1)' option to select the size I need.\nAction: click[6.76 fl oz (pack of 1)]"
},
{
"from": "human",
"value": "Observation:\nInstruction: [SEP] i need a long lasting 6.76 fl oz bottle of l'eau d'issey, and price lower than 100.00 dollars [SEP] Back to Search [SEP] < Prev [SEP] size [SEP] 2.5 fl oz [SEP] 6.76 fl oz (pack of 1) [SEP] L'eau D'issey By Issey Miyake for MenEau De Toilette Spray, 6.7 Fl Oz Bottle [SEP] Price: $64.98 [SEP] Rating: N.A. [SEP] Description [SEP] Features [SEP] Reviews [SEP] Buy Now"
},
{
"from": "gpt",
"value": "Thought: I think I should click on the 'Buy Now' button to purchase the product.\nAction: click[Buy Now]"
}
],
"alfworld": [{
"from": "human",
"value": "Interact with a household to solve a task. Imagine you are an intelligent agent in a household environment and your target is to perform actions to complete the task goal. At the beginning of your interactions, you will be given the detailed description of the current environment and your goal to accomplish. \nFor each of your turn, you will be given the observation of the last turn. You should first think about the current condition and plan for your future actions, and then output your action in this turn. Your output must strictly follow this format:\"Thought: your thoughts.\\nAction: your next action\".\n\nThe available actions are:\n1. go to {recep}\n2. task {obj} from {recep}\n3. put {obj} in/on {recep}\n4. open {recep}\n5. close {recep}\n6. toggle {obj} {recep}\n7. clean {obj} with {recep}\n8. heat {obj} with {recep}\n9. cool {obj} with {recep}\nwhere {obj} and {recep} correspond to objects and receptacles.\nAfter your each turn, the environment will give you immediate feedback based on which you plan your next few steps. if the envrionment output \"Nothing happened\", that means the previous action is invalid and you should try more options.\n\nYour response should use the following format:\n\nThought: <your thoughts>\nAction: <your next action>"
},
{
"from": "gpt",
"value": "OK"
},
{
"from": "human",
"value": "You are in the middle of a room. Looking quickly around you, you see a bathtubbasin 1, a cabinet 2, a cabinet 1, a countertop 1, a garbagecan 1, a handtowelholder 1, a sinkbasin 1, a toilet 1, a toiletpaperhanger 1, and a towelholder 1.\n\nYour task is to: put a toiletpaper in toiletpaperhanger."
},
{
"from": "gpt",
"value": "Thought: I need to place a toilet paper roll on the toilet paper hanger. First, I should check if there's already a roll on the hanger or if it's empty.\nAction: go to toiletpaperhanger 1"
},
{
"from": "human",
"value": "Observation: On the toiletpaperhanger 1, you see nothing."
},
{
"from": "gpt",
"value": "Thought: The toilet paper hanger is empty, so I need to find a new roll of toilet paper to put on it.\nAction: go to toilet 1"
},
{
"from": "human",
"value": "Observation: On the toilet 1, you see a soapbottle 1, and a toiletpaper 1."
},
{
"from": "gpt",
"value": "Thought: I've found a toilet paper roll on the toilet. I should pick it up so I can place it on the hanger.\nAction: take toiletpaper 1 from toilet 1"
},
{
"from": "human",
"value": "Observation: You pick up the toiletpaper 1 from the toilet 1."
},
{
"from": "gpt",
"value": "Thought: Now that I have the toilet paper roll, I need to return to the toilet paper hanger to install it.\nAction: go to toiletpaperhanger 1"
},
{
"from": "human",
"value": "Observation: On the toiletpaperhanger 1, you see nothing."
},
{
"from": "gpt",
"value": "Thought: I'm back at the toilet paper hanger with the roll in hand. It's time to put the toilet paper on the hanger.\nAction: put toiletpaper 1 in/on toiletpaperhanger 1"
}
]
}
pred_traj_offset = {
"webshop": 10,
"sciworld": 16,
}
def template_change(conversation):
messages = []
for item in conversation:
message = {}
if item['role'] == "assistant":
message['from'] = "gpt"
message['value'] = item['content'].strip()
else:
message['from'] = "human"
message['value'] = item['content'].strip()
messages.append(message)
return messages
def is_empty_conversations(conversation):
for item in conversation:
if item['value'].strip() == "":
return True
return False
def cal_monte_carlo_reward(args):
traj_path = args.sample_path
results = {}
results_original_reward = {}
sample_num = len(glob.glob(traj_path + "/*"))
for i in range(sample_num):
paths = glob.glob(f"{traj_path}/sampled_traj_{i}/*.json")
cur_results = []
for path in paths:
cur_results.extend(json.load(open(path)))
for item in cur_results:
id = item['id']
iteration = item['iteration']
id_iteration = f"{id}_{iteration}"
if id_iteration not in results:
results[id_iteration] = [item['agent_final_reward']]
else:
results[id_iteration].append(item['agent_final_reward'])
if id_iteration not in results_original_reward:
results_original_reward[id_iteration] = item['agent_step_reward']
else:
assert results_original_reward[id_iteration] == item['agent_step_reward']
final_results = {}
for key, value in results.items():
final_results[key] = {
"monte_carlo_reward": np.mean(value),
"env_reward": results_original_reward[key]
}
output_data = []
for file in glob.glob(f"{args.traj_path}/*.json"):
data = json.load(open(file))
for item in data:
id = item['id']
iteration = item['iteration']
id_iteration = f"{id}_{iteration}"
if id_iteration in final_results:
item['monte_carlo_step_reward'] = final_results[id_iteration]['monte_carlo_reward']
else:
item['monte_carlo_step_reward'] = item['agent_step_reward']
output_data.append(item)
return output_data
def build_preference(args):
win = 0
tie = 0
lose = 0
global_traj = 0
local_step_traj = 0
local_entire_traj = 0
golden_raw = json.load(open(f"data/sft_data_{args.task}.json"))
game_file_to_id = json.load(open(f"data/game_file_to_id.json"))
pm_data = []
explored_traj = cal_monte_carlo_reward(args)
# json.dump(explored_traj, open("explored_traj.json", "w"), indent=4)
step_monte_carlo_threshold = args.step_threshold
traj_threshold = args.traj_threshold
if args.global_traj:
for item in explored_traj:
if args.task == "webshop":
id = item['id']
elif args.task == "alfworld":
game_file = item['game_file']
id = game_file_to_id[game_file]
iteration = item['iteration']
if iteration != 0:
continue
agent_final_reward = item['agent_final_reward']
gpt_reward = golden_raw[f"{id}_0"]['gpt_reward']
gpt_conversations = golden_raw[f"{id}_0"]['gpt_conversations']
agent_conversations = template_change(item['agent_conversations'])
if is_empty_conversations(agent_conversations) or is_empty_conversations(gpt_conversations):
continue
if agent_final_reward > gpt_reward + traj_threshold:
win += 1
global_traj += 1
pm_data.append({
"id": int(f"{id}{iteration}"),
"prompt": gpt_conversations[:len(instruction_part[args.task])+1],
"chosen": agent_conversations[len(icl_prompt_part[args.task])+1: -1],
"rejected": gpt_conversations[len(instruction_part[args.task])+1:],
})
elif gpt_reward>agent_final_reward + traj_threshold:
lose += 1
global_traj += 1
pm_data.append({
"id": int(f"{id}{iteration}"),
"prompt": gpt_conversations[:len(instruction_part[args.task])+1],
"chosen": gpt_conversations[len(instruction_part[args.task])+1:],
"rejected": agent_conversations[len(icl_prompt_part[args.task])+1: -1],
})
else:
tie += 1
if args.local_traj:
for item in explored_traj:
if args.task == "webshop":
id = item['id']
elif args.task == "alfworld":
game_file = item['game_file']
id = game_file_to_id[game_file]
iteration = item['iteration']
if iteration == 0:
continue
if args.step_action:
agent_step_conversations = template_change(item['agent_step_conversations'])
agent_step_reward = item['monte_carlo_step_reward']
agent_final_reward = item['agent_final_reward']
gpt_step_conversations = golden_raw[f"{id}_{iteration}"]['gpt_step_conversations']
gpt_step_reward = golden_raw[f"{id}_{iteration}"]['monte_carlo_step_reward']
gpt_final_reward = golden_raw[f"{id}_{iteration}"]['gpt_reward']
gpt_length = len(gpt_step_conversations)
agent_length = len(gpt_step_conversations) - len(instruction_part[args.task])+len(icl_prompt_part[args.task])
if is_empty_conversations(agent_step_conversations) or is_empty_conversations(gpt_step_conversations):
continue
if agent_final_reward > gpt_final_reward + traj_threshold and agent_step_reward > gpt_step_reward + step_monte_carlo_threshold:
win += 1
local_step_traj += 1
pm_data.append({
"id": int(f"{id}{iteration}"),
"prompt": gpt_step_conversations[:gpt_length-1],
"chosen": [agent_step_conversations[agent_length-1]],
"rejected": [gpt_step_conversations[gpt_length-1]],
})
elif gpt_final_reward > agent_final_reward + traj_threshold and gpt_step_reward > agent_step_reward + step_monte_carlo_threshold:
lose += 1
local_step_traj += 1
pm_data.append({
"id": int(f"{id}{iteration}"),
"prompt": gpt_step_conversations[:gpt_length-1],
"chosen": [gpt_step_conversations[gpt_length-1]],
"rejected": [agent_step_conversations[agent_length-1]],
})
else:
tie += 1
else:
agent_conversations = template_change(item['agent_conversations'])
agent_step_reward = item['monte_carlo_step_reward']
agent_final_reward = item['agent_final_reward']
agent_step_conversations = item['agent_step_conversations']
gpt_conversations = golden_raw[f"{id}_{iteration}"]['gpt_conversations']
gpt_step_reward = golden_raw[f"{id}_{iteration}"]['monte_carlo_step_reward']
gpt_final_reward = golden_raw[f"{id}_{iteration}"]['gpt_reward']
gpt_step_conversations = golden_raw[f"{id}_{iteration}"]['gpt_step_conversations']
gpt_length = len(gpt_step_conversations)
agent_length = len(gpt_step_conversations) - len(instruction_part[args.task])+len(icl_prompt_part[args.task])
flag = False
if args.task == "webshop":
flag = False
elif args.task == "alfworld":
if len(agent_conversations[agent_length-1: -1]) < len(gpt_conversations[gpt_length-1:]):
flag = True
if is_empty_conversations(agent_conversations) or is_empty_conversations(gpt_conversations):
continue
if (agent_final_reward > gpt_final_reward + traj_threshold or flag) and agent_step_reward > gpt_step_reward + step_monte_carlo_threshold:
win += 1
local_entire_traj += 1
pm_data.append({
"id": int(f"{id}{iteration}"),
"prompt": gpt_conversations[:gpt_length-1],
"chosen": agent_conversations[agent_length-1: -1],
"rejected": gpt_conversations[gpt_length-1:],
})
elif gpt_final_reward > agent_final_reward + traj_threshold and gpt_step_reward > agent_step_reward + step_monte_carlo_threshold:
lose += 1
local_entire_traj += 1
pm_data.append({
"id": int(f"{id}{iteration}"),
"prompt": gpt_conversations[:gpt_length-1],
"chosen": gpt_conversations[gpt_length-1:],
"rejected": agent_conversations[agent_length-1: -1],
})
else:
tie += 1
json.dump(pm_data, open(args.output_path, "w"), indent=4)
print(f"win: {win}, tie: {tie}, lose: {lose}")
print(f"global_traj: {global_traj}, local_step_traj: {local_step_traj}, local_entire_traj: {local_entire_traj}")
def main():
parser = argparse.ArgumentParser("Construct trajectory preference dataset")
parser.add_argument(
"--task",
type=str,
default="webshop",
help="task name",
)
parser.add_argument(
"--golden_traj_path",
type=str,
default="data/webshop_sft.json",
help="path of the golden trajectory",
)
parser.add_argument(
"--output_path",
type=str,
default='test.json',
help="output path of the trajectory preference dataset",
)
parser.add_argument(
"--traj_path",
type=str,
default="/home/azureuser/weimin/agentpipeline/experiments/Llama-2-7b-hf-webshop-sft-step-entire-monte-carlo-beta-0.1-lr3e-6/Llama-2-7b-hf-webshop-sft-step-entire-monte-carlo-beta-0.1-lr3e-6-explore",
help="task name",
)
parser.add_argument(
"--step_action",
action="store_true",
help="use step action or entire trajectory",
)
parser.add_argument(
"--global_traj",
action="store_true",
help="use global trajectory"
)
parser.add_argument(
"--local_traj",
action="store_true",
help="use local trajectory"
)
parser.add_argument(
"--sample_path",
type=str,
default="/home/azureuser/weimin/agentpipeline/experiments/Llama-2-7b-hf-webshop-sft-step-entire-monte-carlo-beta-0.1-lr3e-6/monte_carlo_sample_iteration_1"
)
parser.add_argument(
"--step_threshold",
type=float,
default=0.35
)
parser.add_argument(
"--traj_threshold",
type=float,
default=0.2
)
args = parser.parse_args()
build_preference(args)
if __name__ == "__main__":
main()