-
Notifications
You must be signed in to change notification settings - Fork 3
/
simulation_scoring.py
510 lines (455 loc) · 20.5 KB
/
simulation_scoring.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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
from typing import Optional
import base64
from io import BytesIO
import os
from openai import OpenAI
import numpy as np
from PIL import Image
import re
import time
from tqdm import tqdm
from concurrent.futures import ThreadPoolExecutor, as_completed
from world_model import WebWorldModel
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
def pil_to_b64(img: Image.Image) -> str:
with BytesIO() as image_buffer:
img.save(image_buffer, format="PNG")
byte_data = image_buffer.getvalue()
img_b64 = base64.b64encode(byte_data).decode("utf-8")
img_b64 = "data:image/png;base64," + img_b64
return img_b64
def evaluate_success_with_action(
screenshots: list[Image.Image],
actions: list[str],
current_url: str,
action_description: str,
intent: str,
models: list[str],
intent_images: Optional[Image.Image] = None,
n: int = 20,
top_p: float = 1.0
) -> float:
last_actions_str = '\n'.join(actions)
if intent_images is None:
content = []
for screenshot in screenshots:
content.append({
"type": "image_url",
"image_url": {
"url": pil_to_b64(screenshot),
"detail": "high"
},
})
content.append({
"type": "text",
"text": f"""User Intent: {intent}
Action History: {last_actions_str}
Current URL: {current_url}
The last {len(screenshots)} snapshots of the agent's trajectory are shown in the {len(screenshots)} images. The LAST IMAGE represents the current state of the webpage.
Propopsed Action: {action_description}
"""
})
else:
content = []
for img in intent_images:
content.extend([
{
"type": "image_url",
"image_url": {
"url": pil_to_b64(img)
},
}
])
content.append({
"type": "text",
"text": f"\nUser Intent: {intent}\n"
})
for screenshot in screenshots:
content.append({
"type": "image_url",
"image_url": {
"url": pil_to_b64(screenshot),
"detail": "high"
},
})
content.append({
"type": "text",
"text": f"""
Action History: {last_actions_str}
Current URL: {current_url}
The images corresponding to the user intent are shown in the FIRST {len(intent_images)} images (before the User Intent).
The last {len(screenshots)} snapshots of the agent's trajectory are shown in the LAST {len(screenshots)} images. The LAST IMAGE represents the current state of the webpage.
Proposed Action: {action_description}
"""
})
messages = [
{
"role": "system",
"content": f"""
You are an expert in evaluating the performance of a web navigation agent. The agent is designed to help a human user navigate a website to complete a task. Given the user's intent, the agent's action history, the final state of the webpage, your goal is to decide **whether the proposed action successfully accomplish the task**. If it does not but is on the right track towards success, you should also output as such. You don't have the ability to predict the future, so you should only consider the current state of the webpage and the proposed action.
*IMPORTANT*
Format your response into two lines as shown below:
Thoughts: <your thoughts and reasoning process>
Status: "success" or "failure"
On the right track to success: "yes" or "no"
"""
},
{
"role": "user",
"content": content
}
]
all_responses = []
for model in models:
response = client.chat.completions.create(
model=model,
messages=messages,
max_tokens=256,
top_p=top_p,
n=n // len(models)
)
all_responses.extend(response.choices)
all_scores = []
message_content = None
for r_idx, r in enumerate(all_responses):
# print(r.message.content)
if message_content is None:
message_content = r.message.content
try:
pred = re.search(r'Status: "?(.+)"?', r.message.content).group(1)
if 'success' in pred.lower():
score = 1.0
else:
# Check if it's on the path to success
on_path = re.search(r'On the right track to success: "?(.+)"?', r.message.content).group(1)
if 'yes' in on_path.lower():
score = 0.5
else:
score = 0.0
except Exception as e:
print(f"Error parsing response: {e}")
score = 0.0
all_scores.append(score)
score = np.mean(all_scores)
return score, message_content
def evaluate_simulation_inner(
screenshots: list[Image.Image],
actions: list[str],
current_url: str,
imagination: str,
intent: str,
models: list[str],
intent_images: Optional[Image.Image] = None,
n: int = 20,
top_p: float = 1.0
) -> float:
last_actions_str = '\n'.join(actions)
if intent_images is None:
content = []
for screenshot in screenshots:
content.append({
"type": "image_url",
"image_url": {
"url": pil_to_b64(screenshot),
"detail": "high"
},
})
content.append({
"type": "text",
"text": f"""User Intent: {intent}
Action History: {last_actions_str}
Current URL: {current_url}
The last {len(screenshots)} snapshots of the agent's trajectory are shown in the {len(screenshots)} images. The LAST IMAGE represents the current state of the webpage.
Simulated steps: {imagination}
"""
})
else:
content = []
for img in intent_images:
content.extend([
{
"type": "image_url",
"image_url": {
"url": pil_to_b64(img)
},
}
])
content.append({
"type": "text",
"text": f"\nUser Intent: {intent}\n"
})
for screenshot in screenshots:
content.append({
"type": "image_url",
"image_url": {
"url": pil_to_b64(screenshot),
"detail": "high"
},
})
content.append({
"type": "text",
"text": f"""
Action History: {last_actions_str}
Current URL: {current_url}
The images corresponding to the user intent are shown in the FIRST {len(intent_images)} images (before the User Intent).
The last {len(screenshots)} snapshots of the agent's trajectory are shown in the LAST {len(screenshots)} images. The LAST IMAGE represents the current state of the webpage.
Simulated steps: {imagination}
"""
})
messages = [
{
"role": "system",
"content": f"""
You are an expert in evaluating the performance of a web navigation agent. The agent is designed to help a human user navigate a website to complete a task. Given the user's intent, the agent's action history, the current state of the webpage, your goal is to decide **whether the simulated steps by the agent indicate a successful execution of the user intent**. In particular, if the predicted state (i.e., the current state represented by the last image plus all the predicted changes so far) corresponds to a successful final state. If it is a failure but it looks like the simulated steps are on the right track towards success, you should also output as such. Note that, in the simulated steps, all the state changes are predicted by the agent's world model, and they may not actually be faithful to the real website interactions (e.g., some proposed actions may not be avaiable in a realistic website). You should also account for this in your evaluation (e.g., if the predicted state changes are not reasonable then it's probably a failure).
*IMPORTANT*
Format your response into two lines as shown below:
Thoughts: <your thoughts and reasoning process>
Status: "success" or "failure"
On the right track to success: "yes" or "no"
"""
},
{
"role": "user",
"content": content
}
]
all_responses = []
for model in models:
response = client.chat.completions.create(
model=model,
messages=messages,
max_tokens=256,
top_p=top_p,
n=n // len(models)
)
all_responses.extend(response.choices)
all_scores = []
for r_idx, r in enumerate(all_responses):
# print(r.message.content)
try:
pred = re.search(r'Status: "?(.+)"?', r.message.content).group(1)
if 'success' in pred.lower():
score = 1.0
else:
# Check if it's on the path to success
on_path = re.search(r'On the right track to success: "?(.+)"?', r.message.content).group(1)
if 'yes' in on_path.lower():
score = 0.5
else:
score = 0.0
except Exception as e:
print(f"Error parsing response: {e}")
score = 0.0
all_scores.append(score)
score = np.mean(all_scores)
return score
def evaluate_success(screenshots: list[Image.Image], actions: list[str], current_url: str, last_reasoning: str,
intent: str, models: list[str], intent_images: Optional[Image.Image] = None,
n: int = 20, top_p: float = 1.0, should_log: bool = False) -> float:
"""Compute the value of a state using the value function.
Args:
state (str): The state to compute the value of.
action (list[str]): The action to take in the state.
intent (str): The intent to compute the value of.
intent_images (list[Image.Image], optional): The images corresponding to the intent. Defaults to None.
file_prefix (str, optional): The prefix to use for the file name. Defaults to ''.
Returns:
float: The value of the state.
"""
last_actions_str = '\n'.join(actions[:-1])
last_response = actions[-1]
if intent_images is None:
content = []
for screenshot in screenshots:
content.append({
"type": "image_url",
"image_url": {
"url": pil_to_b64(screenshot),
"detail": "high"
},
})
content.append({
"type": "text",
"text": f"""User Intent: {intent}
Action History: {last_actions_str}
Bot response to the user: {last_response}
Current URL: {current_url}
The last {len(screenshots)} snapshots of the agent's trajectory are shown in the {len(screenshots)} images. The LAST IMAGE represents the current state of the webpage.
"""
})
else:
content = []
for img in intent_images:
content.extend([
{
"type": "image_url",
"image_url": {
"url": pil_to_b64(img)
},
}
])
content.append({
"type": "text",
"text": f"\nUser Intent: {intent}\n"
})
for screenshot in screenshots:
content.append({
"type": "image_url",
"image_url": {
"url": pil_to_b64(screenshot),
"detail": "high"
},
})
content.append({
"type": "text",
"text": f"""
Action History: {last_actions_str}
Bot response to the user: {last_response}
Current URL: {current_url}
The images corresponding to the user intent are shown in the FIRST {len(intent_images)} images (before the User Intent).
The last {len(screenshots)} snapshots of the agent's trajectory are shown in the LAST {len(screenshots)} images. The LAST IMAGE represents the current state of the webpage.
"""
})
messages = [
{
"role": "system",
"content": f"""
You are an expert in evaluating the performance of a web navigation agent. The agent is designed to help a human user navigate a website to complete a task. Given the user's intent, the agent's action history, the final state of the webpage, and the agent's response to the user, your goal is to decide whether the agent's execution is successful or not. If the current state is a failure but it looks like the agent is on the right track towards success, you should also output as such.
There are three types of tasks:
1. Information seeking: The user wants to obtain certain information from the webpage, such as the information of a product, reviews, the text in a comment or post, the date of a submission, etc. This may be formulated in the intent as "tell me", "what is", or "list out". The agent's response must contain the information the user wants, or explicitly state that the information is not available. Otherwise, e.g. the agent encounters an exception and respond with the error content, the task is considered to be a failure. It is VERY IMPORTANT that the bot response is the stop action with the correct output. If the bot response is not stop (e.g., it is click, type, or goto), it is considered a failure for information seeking tasks.
2. Site navigation: The user wants to navigate to a specific page (which may also be specified in the intent as "find", "show me", "navigate to"). Carefully examine the agent's action history and the final state of the webpage (shown in the LAST IMAGE) to determine whether the agent successfully completes the task. It is VERY IMPORTANT that the agent actually navigates to the specified page (reflected by the final state of the webpage, in the LAST IMAGE) and NOT just output the name of the item or post. Make sure that the final url is compatible with the task. For example, if you are tasked to navigate to a comment or an item, the final page and url should be that of the specific comment/item and not the overall post or search page. If asked to navigate to a page with a similar image, make sure that an image on the page is semantically SIMILAR to the intent image. If asked to look for a particular post or item, make sure that the image on the page is EXACTLY the intent image. For this type of task to be considered successful, the LAST IMAGE and current URL should reflect the correct content. No need to consider the agent's response.
3. Content modification: The user wants to modify the content of a webpage or configuration. Ensure that the agent actually commits to the modification. For example, if the agent writes a review or a comment but does not click post, the task is considered to be a failure. Carefully examine the agent's action history and the final state of the webpage to determine whether the agent successfully completes the task. No need to consider the agent's response.
*IMPORTANT*
Format your response into two lines as shown below:
Thoughts: <your thoughts and reasoning process>
Status: "success" or "failure"
On the right track to success: "yes" or "no"
"""
},
{
"role": "user",
"content": content
}
]
all_responses = []
for model in models:
response = client.chat.completions.create(
model=model,
messages=messages,
max_tokens=256,
top_p=top_p,
n=n // len(models)
)
all_responses.extend(response.choices)
if should_log:
print('=' * 30)
print("Value function input:", content[-1])
all_scores = []
for r_idx, r in enumerate(all_responses):
if should_log:
print(f"Output {r_idx}: {r.message.content}")
try:
pred = re.search(r'Status: "?(.+)"?', r.message.content).group(1)
if 'success' in pred.lower():
score = 1.0
else:
# Check if it's on the path to success
on_path = re.search(r'On the right track to success: "?(.+)"?', r.message.content).group(1)
if 'yes' in on_path.lower():
score = 0.5
else:
score = 0.0
except Exception as e:
print(f"Error parsing response: {e}")
score = 0.0
all_scores.append(score)
score = np.mean(all_scores)
if should_log:
print(f"Final score: {score}")
print('=' * 30)
return score
def single_action_simulation(screenshots,
converted_screenshot,
converted_screenshot_path, # this is only for the sft model
actions, task, url, action_description, sim_idx,
models=["gpt-4o"], steps=1,
n=10):
simulation_results = {}
world_model = WebWorldModel(OpenAI(api_key=os.environ["OPENAI_API_KEY"]))
score, count = 0, 0
start_time = time.time()
try:
imagination = world_model.multiple_step_change_prediction(converted_screenshot, converted_screenshot_path,
task, action_description, k=steps)
score += evaluate_simulation_inner(screenshots, actions, url, imagination, task, models, n=n)
except:
return None
end_time = time.time()
duration = (end_time - start_time) % 60
return {
'action_description': action_description,
'imagination': imagination,
'score': score,
'duration': duration,
'sim_idx': sim_idx
}
def evaluate_simulation(screenshots, actions, task, url, action_description_list,
models=["gpt-4o"],
num_of_sim=3,
steps=1,
n=10, num_workers=15):
# this is for the sft model
screenshots[-1].save("last_screenshot.png", "PNG")
converted_screenshot = pil_to_b64(screenshots[-1])
preds = []
with ThreadPoolExecutor(max_workers=num_workers) as executor:
futures = []
for action_description in action_description_list:
for sim_idx in range(num_of_sim):
futures.append(executor.submit(single_action_simulation, screenshots,
converted_screenshot,
"last_screenshot.png", # this is for the sft model
actions, task, url, action_description, sim_idx, models, steps, n))
for future in tqdm(as_completed(futures), total=len(futures)):
result = future.result()
if result:
preds.append(result)
# Organize the output
all_scores = {}
all_simulations = {}
for item in preds:
action_description = item['action_description']
if action_description not in all_scores:
all_scores[action_description] = [item['score']]
else:
all_scores[action_description].append(item['score'])
if action_description not in all_simulations:
all_simulations[
action_description] = f"Simulation for action {action_description}:\n{item['imagination']}\nScore: {item['score']}\n\n"
else:
all_simulations[
action_description] += f"Simulation for action {action_description}:\n{item['imagination']}\nScore: {item['score']}\n\n"
avg_scores = {}
for action_description in all_scores:
action_scores = all_scores[action_description]
if len(action_scores) > 0:
avg_scores[action_description] = sum(action_scores) / len(action_scores)
else:
avg_scores[action_description] = 0
return avg_scores, all_simulations
if __name__ == "__main__":
# Test the value function
screenshot_path = "demo_data/shopping_0.png"
screenshots = [Image.open(screenshot_path)]
actions = ["None"]
action_description = "type 'red blanket' in the search bar"
task = "Buy the least expensive red blanket (in any size)"
action_description_list = [
"type 'red blanket' in the search bar",
"click the element Home & Kitchen",
"type 'kobe' in the search bar",
"type 'the ohio state university' in the search bar"
]
scores = evaluate_simulation(screenshots, actions, task, "https://www.amazon.com", action_description_list,
num_of_sim=3, num_workers=50, n=10, steps=2)
print(scores)