-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimulator.py
More file actions
1540 lines (1298 loc) · 63.8 KB
/
simulator.py
File metadata and controls
1540 lines (1298 loc) · 63.8 KB
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Code for simulator implementation, including web simulator and android simulator.
"""
import json
from actions.action import *
from actions.webarena_action import *
import os
from converter import *
from openai import OpenAI
import openai
import copy
import random
import logging
from utils import *
import re
from typing import Optional
STATIC_ELEMENT = {
"text",
"div",
"main",
"RootWebArea"
}
####################################
########## Web Simulator ##########
####################################
class Simulator:
def __init__(self,
init_state=None,
width=1920,
height=1080,
webarena_mode=False,
debug=False,
):
self.client = OpenAI(
organization=os.environ['OPENAI_ORG_ID'],
api_key=os.environ['OPENAI_API_KEY']
)
self.init_state = init_state
self.cur_state = None
self.home_state = None
# use array of arrays to store previous states
self.prev_states = []
self.tab_index = -1
self.window_index = []
# offset for each tab
self.coord_offset = []
self.steps = 0
# simulation history
self.intent_history = []
self.key_infos = []
# screen parameters
self.width = width
self.height = height
self.style_hint = ''
self.webarena_mode = webarena_mode
self.webarena_converter = WebArenaConverter()
# logging module
self.debug = debug
if debug:
logging.basicConfig(level=logging.INFO,
filemode='w',
filename='tmp/simulator.log',
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
self.logger = logging.getLogger(__name__)
def call_openai(self,
prompt,
sys_prompt,
model="gpt-4o-mini",
stop=None,
return_json=False,
max_tokens=None,
temperature=0.5):
completion = self.client.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": sys_prompt},
{"role": "user", "content": prompt}
],
response_format={ "type": "json_object" } if return_json else openai.NOT_GIVEN,
max_tokens=max_tokens,
stop=stop,
temperature=temperature
)
caller = inspect.stack()[1].function
usage = completion.usage
# print(f"Function ##{caller}## called with usage: Input token:{usage.prompt_tokens}, Output token:{usage.completion_tokens}")
return completion.choices[0].message.content
# Function to locate an element by coordinates
@staticmethod
def find_element_by_coordinates(elements, x, y, filter_static=False, filter_image=False):
if isinstance(elements, dict):
elements = [elements]
for element in elements:
coord = element['coord']
if coord['left'] <= x <= coord['right'] and coord['up'] <= y <= coord['down']:
if not (filter_static and element['tag'] in STATIC_ELEMENT) and not (filter_image and element['tag'] == 'img'):
return element
else:
return None
elif 'elements' in element:
e = Simulator.find_element_by_coordinates(element['elements'], x, y, filter_static, filter_image)
if e:
return e
return None
@staticmethod
def find_element_by_id(elements, id, filter_static=False, filter_image=False):
if isinstance(elements, dict):
elements = [elements]
for element in elements:
if element['coord']['id'] == id:
if not ((filter_static and element['tag'] in STATIC_ELEMENT) or (filter_image and element['tag'] == 'img')):
return element
else:
return None
elif 'elements' in element:
e = Simulator.find_element_by_id(element['elements'], id, filter_static, filter_image)
if e:
return e
return None
def _get_leaf_node(self, elements):
# get one leaf node from the tree
if isinstance(elements, dict):
elements = [elements]
for element in elements:
if 'elements' in element:
return self._get_leaf_node(element['elements'])
else:
return element
def _get_depths(self, state, depth=0) -> dict:
# first-order traverse the tree to get the depth of each node
if isinstance(state, dict):
state = [state]
depths = {}
for element in state:
if 'elements' in element:
child_depths = self._get_depths(element['elements'], depth + 1)
for key, value in child_depths.items():
depths[key] = value
depths[element['coord']['id']] = depth
return depths
def shuffle_element_id(self, elements):
if isinstance(elements, dict):
elements = [elements]
for element in elements:
if 'coord' in element:
element['coord']['id'] = random.randint(0, 20000)
if 'elements' in element:
self.shuffle_element_id(element['elements'])
return elements[0]
def check_element_visibility(self, element=None, coord=None):
if not coord:
coord = copy.deepcopy(element['coord'])
offset = self.coord_offset[self.tab_index]
coord['left'] += offset[0]
coord['right'] += offset[0]
coord['up'] += offset[1]
coord['down'] += offset[1]
# check if the element is visible
return coord['left'] < self.width and coord['right'] > 0 and coord['up'] < self.height and coord['down'] > 0
def get_element_coordinates(self, element) -> dict:
if 'coord' in element and 'left' in element['coord']:
return {
'left': element['coord']['left'],
'right': element['coord']['right'],
'up': element['coord']['up'],
'down': element['coord']['down']
}
elif 'elements' in element:
# aggregate the coordinates of the child nodes
coord = {
'left': 19200,
'right': -10,
'up': 10800,
'down': -10
}
for e in element['elements']:
child_coord = self.get_element_coordinates(e)
coord['left'] = min(coord['left'], child_coord['left'])
coord['right'] = max(coord['right'], child_coord['right'])
coord['up'] = min(coord['up'], child_coord['up'])
coord['down'] = max(coord['down'], child_coord['down'])
return coord
raise ValueError("Element does not have coordinates.")
def get_visible_elements(self, elements) -> list:
# Return: a list of visible elements, the tree structure is flattened.
visible_elements = []
if isinstance(elements, dict):
elements = [elements]
for element in elements:
if 'elements' in element:
coord = self.get_element_coordinates(element)
if self.check_element_visibility(coord=coord):
cur_element = copy.deepcopy(element)
del cur_element['elements']
visible_elements.append(cur_element)
visible_elements.extend(self.get_visible_elements(element['elements']))
else:
continue
elif self.check_element_visibility(element=element):
visible_element = copy.deepcopy(element)
# if up > down or left > right, reverse them
if visible_element['coord']['up'] > visible_element['coord']['down']:
visible_element['coord']['up'], visible_element['coord']['down'] = visible_element['coord']['down'], visible_element['coord']['up']
if visible_element['coord']['left'] > visible_element['coord']['right']:
visible_element['coord']['left'], visible_element['coord']['right'] = visible_element['coord']['right'], visible_element['coord']['left']
coord = copy.deepcopy(element['coord'])
offset = self.coord_offset[self.tab_index]
coord['left'] += offset[0]
coord['right'] += offset[0]
coord['up'] += offset[1]
coord['down'] += offset[1]
visible_element['coord']['left'] = max(0, coord['left'])
visible_element['coord']['right'] = min(1920, coord['right'])
visible_element['coord']['up'] = max(0, coord['up'])
visible_element['coord']['down'] = min(1080, coord['down'])
visible_elements.append(visible_element)
return visible_elements
def scale_element_height(self, elements: list, page_scale=1.2, element_scale=1.5) -> list:
# scale the height of the elements to be within 2 * height
scale = page_scale * self.height / elements[-1]['coord']['down']
# get random index from elements
ind = random.randint(0, len(elements) - 1)
for i, element in enumerate(elements):
element['coord']['up'] = int(element['coord']['up'] * scale)
element['coord']['down'] = int(element['coord']['down'] * scale)
# expand the height of the elements a little bit
displacement = int(elements[ind]['coord']['down'] * (element_scale - 1))
for i, element in enumerate(elements):
if i <= ind:
element['coord']['up'] = int(element['coord']['up'] * element_scale)
element['coord']['down'] = int(element['coord']['down'] * element_scale)
else:
element['coord']['up'] += displacement
element['coord']['down'] += displacement
return elements
def remove_coord_info(self, elements: list) -> list:
# elements: list of elements
# remove the coord info from the elements in the tree
for element in elements:
if 'id' not in element['coord']:
element['coord'] = {'id': random.randint(0, 20000)}
else:
element['coord'] = {'id': element['coord']['id']}
return elements
def erase_coord_info_in_tree(self, state: dict, duplicate = False) -> dict:
if duplicate:
tmp_state = copy.deepcopy(state)
# clear the coordinate info in the tree
def erase_coord(elements: list):
for element in elements:
if 'elements' in element:
erase_coord(element['elements'])
if 'coord' in element:
element['coord'] = {'id': element['coord']['id']}
if duplicate:
erase_coord([tmp_state])
return tmp_state
else:
erase_coord([state])
return state
def flatten_tree_state(self, state: dict, leaf_only = False) -> list[dict]:
# flatten the tree structure of the state
if isinstance(state, list):
state = state[0]
def flatten_tree(elements):
flat_state = []
for element in elements:
if leaf_only:
if 'elements' in element:
flat_state.extend(flatten_tree(element['elements']))
else:
new_element = copy.deepcopy(element)
flat_state.append(new_element)
else:
new_element = copy.deepcopy(element)
if 'elements' in element:
del new_element['elements']
flat_state.append(new_element)
if 'elements' in element:
flat_state.extend(flatten_tree(element['elements']))
return flat_state
if 'elements' in state:
if leaf_only:
return flatten_tree(state['elements'])
else:
new_element = copy.deepcopy(state)
del new_element['elements']
return [new_element] + flatten_tree(state['elements'])
def transform_response_into_json(self, response: str):
# simple cleaning
response = response.strip()
if response.startswith('```'):
response = '\n'.join(response.split('\n')[1:-1])
try:
state = json.loads(response)
except Exception as e:
print(e)
raise ValueError("Response is not a valid JSON format.")
# state = self.collate_coord(state)
state = self.shuffle_element_id(state)
if self.webarena_mode:
# 1. put the leaves nodes into one list
elements = self.flatten_tree_state(state, leaf_only=True)
# 2. render the leaves nodes
elements = self._render_webarena_state(elements)
# 3. scale the height of the new state to be within 1.5 * height
elements = self.scale_element_height(elements, page_scale=1.0, element_scale=1.5)
# 4. replace the coordinates with the rendered coordinates
for element in elements:
id = element['coord']['id']
target_element = self.find_element_by_id(state, id)
if target_element:
target_element['coord'] = element['coord']
return state
@timer
def extract_intent(self, action):
with open('system_prompts/web_simulation/intention_prompt.txt', 'r') as f:
sys_prompt = f.read()
related_elements = []
if isinstance(action, Click) or isinstance(action, Type):
related_element = self.find_element_by_coordinates(self.cur_state, action.x, action.y, filter_static=True, filter_image=True)
if not related_element:
return "No related element found."
elif isinstance(action, Type) and ('input' not in related_element['tag'] and 'textarea' not in related_element['tag']):
return "No related element found."
related_elements.append(related_element)
elif isinstance(action, WA_Click) or isinstance(action, WA_Type) or isinstance(action, WA_Hover):
related_element = self.find_element_by_id(self.cur_state, action.id, filter_static=True)
if not related_element:
return "No related element found."
related_elements.append(related_element)
prompt = '''GUI:
{}
Browsing history:
{}
Action definition: {}
Current action: {}
Related elements:
{}\n**Note: try to use realistic URL, if needed. Don't use URL like "example.com".**'''.format(str(self.cur_state), '\n'.join(self.intent_history), action.INTRO, str(action), "\n".join([str(related_element) for related_element in related_elements]))
response = self.call_openai(prompt, sys_prompt)
if self.debug:
self.logger.info(f"Extracted intent: {response}")
return response
@timer
def direct_update(self, intent):
prompt = """
Current state:
{}
Update Message: {}""".format(self._get_backup_current_state(), intent)
path = 'system_prompts/web_simulation/direct_step.txt' if not self.webarena_mode else 'system_prompts/web_simulation/webarena_direct_step.txt'
with open(path, 'r') as f:
sys_prompt = f.read()
response = self.call_openai(prompt, sys_prompt)
response = self.transform_response_into_json(response)
return response
@timer
def compose(self, intent, prev_state, polish = True):
with open('system_prompts/web_simulation/compose_prompt.txt', 'r') as f:
sys_prompt = f.read()
prompt = '''Previous Info: {}\n{}\nNote: Don't generate duplicate things! just put the elements and sections in the order that they shall appear in the webpage. E.g. you don't want to put footer before main content.\nThought: Let's think step by step. The description is'''.format('\n'.join(self.key_infos), intent.replace('New window', 'Descrition'))
response = self.call_openai(prompt, sys_prompt, model='gpt-4o-mini')
# further diversify the response
if polish:
with open('system_prompts/web_simulation/polish_compose.txt', 'r') as f:
sys_prompt = f.read()
prompt = '''Previous page:{}\nOld content:{}\n Thought: Let's think step by step. '''.format(prev_state, response)
response = self.call_openai(prompt, sys_prompt, model='gpt-4o-mini', temperature=0.5)
try:
response = response.split("New content:")[1].strip()
except:
response = response
if self.debug:
self.logger.info(f"Composed state: {response}")
return response
@timer
def format(self, composition, prev_state):
path = 'system_prompts/web_simulation/format_prompt.txt' if not self.webarena_mode else 'system_prompts/web_simulation/webarena_format_prompt.txt'
with open(path, 'r') as f:
sys_prompt = f.read()
prompt = f"""Previous state:\n{prev_state}\nDescription of new state: {composition}"""
response = self.call_openai(prompt, sys_prompt, return_json=True)
return response
def complete_state(self, incomplete_state):
old_elements = self.flatten_tree_state(incomplete_state, leaf_only=True)
length = len(old_elements)
with open('system_prompts/web_simulation/complete_prompt.txt', 'r') as f:
sys_prompt = f.read()
prompt = f"""Incomplete state: {incomplete_state}"""
response = self.call_openai(prompt, sys_prompt, return_json=True)
# Tree-structured state
state = json.loads(response)
# start rendering the state
new_elements = self.flatten_tree_state(state, leaf_only=True)
new_elements = self._render_webarena_state(new_elements)
# the scale factor: we need to ensure that old elements are within a screen
scale = self.height / new_elements[length - 1]['coord']['down']
for i, element in enumerate(new_elements):
element['coord']['up'] = int(element['coord']['up'] * scale)
element['coord']['down'] = int(element['coord']['down'] * scale)
# replace the coordinates with the rendered coordinates
for element in new_elements:
id = element['coord']['id']
target_element = self.find_element_by_id(state, id)
if target_element:
target_element['coord'] = element['coord']
return state
def create_new_page(self, cur_state, intent, polish=True):
if polish:
cur_state = self.erase_coord_info_in_tree(cur_state, duplicate=True)
WA_cur_state = self.webarena_converter.convert_tree_venv_to_real_env(cur_state)
else:
WA_cur_state = None
composition = self.compose(intent, WA_cur_state, polish=polish)
formatted = self.format(composition, cur_state)
# continue composition
new_state = self.transform_response_into_json(formatted)
return new_state
def perturb_state(self, cur_state):
with open('system_prompts/web_simulation/perturb_state.txt', 'r') as f:
sys_prompt = f.read()
prompt = f"""Current webpage: \n{cur_state}\n New webpage:"""
response = self.call_openai(prompt, sys_prompt)
return response
def _get_backup_current_state(self):
return self.prev_states[self.tab_index][self.window_index[self.tab_index]]
def _render_webarena_state(self, state):
path = 'system_prompts/web_simulation/webarena_render_prompt.txt'
with open(path, 'r') as f:
sys_prompt = f.read()
prompt = str(state)
response = self.call_openai(prompt, sys_prompt, return_json=True)
rendered_elements = json.loads(response)
return rendered_elements['elements']
# Execute the action on current state
# Return: whether need LLM to execute the action
def execute_action(self, action) -> bool:
if isinstance(action, Click) or isinstance(action, WA_Click):
return True
elif isinstance(action, Type) or isinstance(action, WA_Type):
# find the element to type
if isinstance(action, Type):
typed_element = self.find_element_by_coordinates(self._get_backup_current_state(), action.x, action.y, filter_static=True, filter_image=True)
else:
typed_element = self.find_element_by_id(self._get_backup_current_state(), action.id, filter_static=True, filter_image=True)
if not typed_element:
return False
if action.press_enter_after == 1:
return True
if typed_element:
if ('input' in typed_element['tag'] or 'textarea' in typed_element['tag'] or 'combobox' in typed_element['tag']):
typed_element['content_description'] = action.text
return False
elif 'select' in typed_element['tag']:
return False
else:
return True
elif isinstance(action, Scroll) or isinstance(action, WA_Scroll):
direction2offset = {'up': (0, 1080), 'down': (0, -1080), 'left': (200, 0), 'right': (-200, 0)}
offset = direction2offset[action.direction]
if action.direction == 'up' and self.coord_offset[self.tab_index][1] >= 0:
return False
cur_state = self._get_backup_current_state()
leaf_node = self._get_leaf_node(cur_state)
if 'left' not in leaf_node['coord']:
return True
self.coord_offset[self.tab_index] = (self.coord_offset[self.tab_index][0] + offset[0], self.coord_offset[self.tab_index][1] + offset[1])
return False
elif isinstance(action, WA_Navigate_Forward):
if self.window_index[self.tab_index] < len(self.prev_states[self.tab_index]) - 1:
self.window_index[self.tab_index] += 1
self.coord_offset[self.tab_index] = (0, 0)
return False
elif isinstance(action, Navigate_Back) or isinstance(action, WA_Navigate_Back):
if self.window_index[self.tab_index] > 0:
self.window_index[self.tab_index] -= 1
self.coord_offset[self.tab_index] = (0, 0)
return False
elif isinstance(action, Navigate_Home):
self.prev_states[self.tab_index].append(self.home_state)
self.window_index[self.tab_index] += 1
self.coord_offset[self.tab_index] = (0, 0)
return False
elif isinstance(action, Wait):
return False
elif isinstance(action, WA_New_Tab):
self.prev_states.append([self.home_state])
self.window_index.append(0)
self.tab_index = len(self.prev_states) - 1
self.coord_offset.append((0, 0))
return False
elif isinstance(action, WA_Switch_Tab):
self.tab_index = action.index
return False
elif isinstance(action, WA_Close_Tab):
self.prev_states.pop(self.tab_index)
self.window_index.pop(self.tab_index)
self.coord_offset.pop(self.tab_index)
self.tab_index -= 1
return False
elif isinstance(action, WA_Go_To):
return True
elif isinstance(action, WA_Press) or isinstance(action, WA_Hover):
return True
else:
raise ValueError("Action not defined")
def step(self, action):
# execute the action
if self.execute_action(action):
intent = self.extract_intent(action)
print("--------------")
print(intent)
print("--------------")
if "No related element found" in intent:
self.intent_history.append(f"Step {self.steps + 1}: Nothing happened.")
self.steps += 1
return self.cur_state
# clean all empty str in the list
thought, intent, key_info, answer = [item for item in intent.strip().split("\n") if item != '']
thought = thought.split(': ')[1].strip()
if 'None' not in key_info:
key_info = key_info.split('Key Info: ')[1].strip()
self.key_infos.append(key_info)
# judge whether creating a new page is required
if "Yes" in answer:
new_state = self.create_new_page(self._get_backup_current_state(), intent)
if self.webarena_mode:
new_cur_state = self.remove_coord_info(self.get_visible_elements(new_state))
else:
new_cur_state = self.get_visible_elements(new_state)
# update the simulator
prev_coord_offset = self.coord_offset[self.tab_index]
try:
# not using coord_offset in prev step
self.coord_offset[self.tab_index] = (0, 0)
if self.webarena_mode:
new_cur_state = self.remove_coord_info(self.get_visible_elements(new_state))
else:
new_cur_state = self.get_visible_elements(new_state)
if isinstance(action, Type):
self.prev_states[self.tab_index][self.window_index[self.tab_index]] = new_state
else:
self.window_index[self.tab_index] += 1
self.prev_states[self.tab_index] = self.prev_states[self.tab_index][:self.window_index[self.tab_index]]
self.prev_states[self.tab_index].append(new_state)
except:
self.coord_offset[self.tab_index] = prev_coord_offset
new_cur_state = self.remove_coord_info(self.get_visible_elements(self._get_backup_current_state()))
else:
# directly update the current state
new_state = self.direct_update(intent)
if self.webarena_mode:
new_cur_state = self.remove_coord_info(self.get_visible_elements(new_state))
else:
new_cur_state = self.get_visible_elements(new_state)
# update the simulator
self.prev_states[self.tab_index][self.window_index[self.tab_index]] = new_state
self.intent_history.append(f"Step {self.steps + 1}: " + intent)
else:
new_cur_state = self.remove_coord_info(self.get_visible_elements(self._get_backup_current_state()))
intent = f"Step {self.steps + 1}: " + str(action)
self.intent_history.append(intent)
# update the current state
self.cur_state = new_cur_state
self.steps += 1
return self.cur_state
def reset(self,
init_state: dict,
):
self.tab_index = 0
self.window_index = [0]
self.coord_offset = [(0, 0)]
self.steps = 0
# init_state: tree structure of the initial state
leaf_node = self._get_leaf_node(init_state)
if 'left' not in leaf_node['coord']:
self.cur_state = self.flatten_tree_state(init_state)
else:
self.cur_state = self.remove_coord_info(self.get_visible_elements(init_state))
self.prev_states = [[init_state]]
def _get_state(self) -> dict:
# function for getting intermediate state
return {
'cur_state': self.cur_state,
'prev_states': self.prev_states,
'tab_index': self.tab_index,
'window_index': self.window_index,
'coord_offset': self.coord_offset,
'steps': self.steps,
'intent_history': self.intent_history,
'key_infos': self.key_infos
}
def _set_state(self, simulator_state: dict) -> None:
# function for setting intermediate state
self.cur_state = simulator_state['cur_state']
self.prev_states = simulator_state['prev_states']
self.tab_index = simulator_state['tab_index']
self.window_index = simulator_state['window_index']
self.coord_offset = simulator_state['coord_offset']
self.steps = simulator_state['steps']
self.intent_history = simulator_state['intent_history']
self.key_infos = simulator_state['key_infos']
# Retrieval augmented simulator
class RAG_Simulator(Simulator):
def __init__(self,
webarena_domain,
init_state=None,
width=1920,
height=1080,
webarena_mode=False,
debug=False,
):
super().__init__(init_state, width, height, webarena_mode, debug)
self.webarena_domain = webarena_domain
self.retrieve_key = None
def reset(self, init_state: dict):
super().reset(init_state)
WA_init_state = self.webarena_converter.convert_tree_venv_to_real_env(init_state)
self.retrieve_key = clean_state(WA_init_state)
def model_retrieve(self, action_history, reference_seqs: list[str]):
with open('system_prompts/web_simulation/model_retrieve.txt', 'r') as f:
sys_prompt = f.read()
reference_seqs = '\n'.join(reference_seqs)
prompt = f"""Reference action sequences:\n{reference_seqs}\n\nCurrent action sequence:\n{action_history}\n\Thought:Let's think step by step."""
response = self.call_openai(prompt, sys_prompt, model='gpt-4o', temperature=0.1)
try:
thought, output = response.split('Output:')
except:
output = response
return output.strip().split('\n')
def find_best_match(self, action_history: list[str]):
# key: cleaned webarena state
key = self.retrieve_key + ' '.join(action_history)
root = f'intermediate_states/webarena/{self.webarena_domain}'
files = [f for f in os.listdir(root) if f.endswith('.json')]
def get_indexed_action_history(act_his: list[str]) -> str:
return ' '.join([f"{i}. {act}" for i, act in enumerate(act_his)])
keys = []
actions = []
action_lists = []
for file in files:
d = json.load(open(f'{root}/{file}', 'r'))
keys.append(d['clean_prev_state'] + d['action_history'])
actions.append(d['action_history'])
if self.webarena_domain != 'map':
action_lists.append(d['action_history_list'])
# First BM25 over action_history to select top 10
top_n_initial = max(len(keys) // 20, 1) # Ensure at least one selection
fuzzy, top_indices_initial_1st = BM25_ranking(' '.join(action_history), actions, top_n=top_n_initial)
# filter out seqs that not ending with the same action as action_history
if self.webarena_domain != 'map':
tmp = [i for i in top_indices_initial_1st if action_lists[i][-1] == action_history[-1]]
if tmp:
top_indices_initial_1st = tmp
filtered_actions = [get_indexed_action_history(action_lists[i]) for i in top_indices_initial_1st]
else:
filtered_actions = [actions[i] for i in top_indices_initial_1st]
top_indices_initial_2nd = []
if fuzzy:
# do model-based retrieval
# breakpoint()
filtered_actions = self.model_retrieve(get_indexed_action_history(action_history), filtered_actions)
if self.webarena_domain != 'map':
top_indices_initial_2nd = [i for i in top_indices_initial_1st for a in filtered_actions if get_indexed_action_history(action_lists[i]) in a]
else:
top_indices_initial_2nd = [i for i in top_indices_initial_1st for a in filtered_actions if actions[i] in a]
if not top_indices_initial_2nd:
top_indices_initial = top_indices_initial_1st
else:
top_indices_initial = top_indices_initial_2nd
filtered_files = [files[i] for i in top_indices_initial]
filtered_keys = [keys[i] for i in top_indices_initial]
# Then BM25 over retrieve_key on the filtered candidates
_, top_indices_final = BM25_ranking(key, filtered_keys, top_n=1)
best_match = json.load(open(f'{root}/{filtered_files[top_indices_final[0]]}', 'r'))
return best_match
@timer
def rag_compose(self, intent, reference_state):
path = 'system_prompts/web_simulation/rag_compose_prompt.txt'
with open(path, 'r') as f:
sys_prompt = f.read()
prompt = f"""Reference state:\n{reference_state}\nDescription of new state: {intent}"""
response = self.call_openai(prompt, sys_prompt)
return response
@timer
def format(self, composition, reference_state):
path = 'system_prompts/web_simulation/rag_format_prompt.txt'
with open(path, 'r') as f:
sys_prompt = f.read()
prompt = f"""Reference state:\n{reference_state}\nDescription of new state: {composition}"""
response = self.call_openai(prompt, sys_prompt, return_json=True)
return response
def create_new_page(self, WA_reference_state, intent):
composition = self.rag_compose(intent, WA_reference_state)
# breakpoint()
reference_state = self.webarena_converter.convert_to_tree_venv(WA_reference_state)
formatted = self.format(composition, reference_state)
# breakpoint()
new_state = self.transform_response_into_json(formatted)
return new_state
def step(self, action, action_history: list[str]):
# execute the action
if self.execute_action(action):
intent = self.extract_intent(action)
print("--------------")
print(intent)
print("--------------")
if "No related element found" in intent:
self.intent_history.append(f"Step {self.steps + 1}: Nothing happened.")
self.steps += 1
return self.cur_state
# clean all empty str in the list
thought, intent, key_info, answer = [item for item in intent.strip().split("\n") if item != '']
thought = thought.split(': ')[1].strip()
if 'None' not in key_info:
key_info = key_info.split('Key Info: ')[1].strip()
self.key_infos.append(key_info)
# judge whether creating a new page is required
if "Yes" in answer:
# here we need to retrieve the best matching state
# used as reference state for generation
best_match = self.find_best_match(action_history)
new_state = self.create_new_page(best_match['next_state'], intent)
if self.webarena_mode:
new_cur_state = self.remove_coord_info(self.get_visible_elements(new_state))
else:
new_cur_state = self.get_visible_elements(new_state)
# update the simulator
if isinstance(action, Type):
self.prev_states[self.tab_index][self.window_index[self.tab_index]] = new_state
else:
self.window_index[self.tab_index] += 1
self.prev_states[self.tab_index] = self.prev_states[self.tab_index][:self.window_index[self.tab_index]]
self.prev_states[self.tab_index].append(new_state)
self.coord_offset[self.tab_index] = (0, 0)
self.retrieve_key = best_match['clean_next_state']
else: # directly update the current state
if isinstance(action, Type) or isinstance(action, WA_Type):
tmp_action = Type(text=action.text, x=action.x, y=action.y, press_enter_after=0) if isinstance(action, Type) else WA_Type(id=action.id, text=action.text, press_enter_after=0)
self.execute_action(tmp_action)
if self.webarena_mode:
new_cur_state = self.remove_coord_info(self.get_visible_elements(self._get_backup_current_state()))
else:
new_cur_state = self.get_visible_elements(self._get_backup_current_state())
else:
new_state = self.direct_update(intent)
if self.webarena_mode:
new_cur_state = self.remove_coord_info(self.get_visible_elements(new_state))
else:
new_cur_state = self.get_visible_elements(new_state)
# update the simulator
self.prev_states[self.tab_index][self.window_index[self.tab_index]] = new_state
self.intent_history.append(f"Step {self.steps + 1}: " + str(action))
else:
new_cur_state = self.remove_coord_info(self.get_visible_elements(self._get_backup_current_state()))
intent = f"Step {self.steps + 1}: " + str(action)
self.intent_history.append(intent)
# update the current state
self.cur_state = new_cur_state
self.steps += 1
return self.cur_state
class AblationSimulator(Simulator):
def step(self, action):
# execute the action
if self.execute_action(action):
intent = self.extract_intent(action)
print("--------------")
print(intent)
print("--------------")
if "No related element found" in intent:
self.intent_history.append(f"Step {self.steps + 1}: Nothing happened.")
self.steps += 1
return self.cur_state
# clean all empty str in the list
thought, intent, key_info, answer = [item for item in intent.strip().split("\n") if item != '']
thought = thought.split(': ')[1].strip()
if 'None' not in key_info:
key_info = key_info.split('Key Info: ')[1].strip()
self.key_infos.append(key_info)
new_state = self.direct_update(intent)
if self.webarena_mode:
new_cur_state = self.remove_coord_info(self.get_visible_elements(new_state))
else:
new_cur_state = self.get_visible_elements(new_state)
# update the simulator
if isinstance(action, Type):
self.prev_states[self.tab_index][self.window_index[self.tab_index]] = new_state
else:
self.window_index[self.tab_index] += 1
self.prev_states[self.tab_index] = self.prev_states[self.tab_index][:self.window_index[self.tab_index]]
self.prev_states[self.tab_index].append(new_state)
self.intent_history.append(f"Step {self.steps + 1}: " + str(action))
else:
new_cur_state = self.remove_coord_info(self.get_visible_elements(self._get_backup_current_state()))
intent = f"Step {self.steps + 1}: " + str(action)
self.intent_history.append(intent)
# update the current state
self.cur_state = new_cur_state
self.steps += 1
return self.cur_state
####################################
########## Android Simulator ##########
####################################
class UIElement:
def __init__(self,
text,
content_description,
class_name,
bbox,
bbox_pixels: tuple[int, int, int, int],
hint_text = None,
is_checked=False,
is_checkable=False,
is_clickable=False,
is_editable=False,
is_enabled=False,
is_focused=False,
is_focusable=False,
is_long_clickable=False,
is_scrollable=False,
is_selected=False,
is_visible=False,
package_name='',
resource_name='',
tooltip=None,
resource_id=None,
metadata=None,
):
self.text = text
self.content_description = content_description
self.class_name = class_name
self.bbox = bbox
self.bbox_pixels = bbox_pixels
self.hint_text = hint_text
self.is_checked = is_checked
self.is_checkable = is_checkable
self.is_clickable = is_clickable
self.is_editable = is_editable
self.is_enabled = is_enabled
self.is_focused = is_focused
self.is_focusable = is_focusable
self.is_long_clickable = is_long_clickable
self.is_scrollable = is_scrollable
self.is_selected = is_selected