Agent that follows a pre-defined action sequence
| 51 | |
| 52 | |
| 53 | class TeacherForcingAgent(Agent): |
| 54 | """Agent that follows a pre-defined action sequence""" |
| 55 | |
| 56 | def __init__(self) -> None: |
| 57 | super().__init__() |
| 58 | |
| 59 | def set_action_set_tag(self, tag: str) -> None: |
| 60 | self.action_set_tag = tag |
| 61 | |
| 62 | def set_actions(self, action_seq: str | list[str]) -> None: |
| 63 | if isinstance(action_seq, str): |
| 64 | action_strs = action_seq.strip().split("\n") |
| 65 | else: |
| 66 | action_strs = action_seq |
| 67 | action_strs = [a.strip() for a in action_strs] |
| 68 | |
| 69 | actions = [] |
| 70 | for a_str in action_strs: |
| 71 | try: |
| 72 | if self.action_set_tag == "playwright": |
| 73 | cur_action = create_playwright_action(a_str) |
| 74 | elif self.action_set_tag == "id_accessibility_tree": |
| 75 | cur_action = create_id_based_action(a_str) |
| 76 | else: |
| 77 | raise ValueError( |
| 78 | f"Unknown action type {self.action_set_tag}" |
| 79 | ) |
| 80 | except ActionParsingError as e: |
| 81 | cur_action = create_none_action() |
| 82 | |
| 83 | cur_action["raw_prediction"] = a_str |
| 84 | actions.append(cur_action) |
| 85 | |
| 86 | self.actions: list[Action] = actions |
| 87 | |
| 88 | def next_action( |
| 89 | self, trajectory: Trajectory, intent: str, meta_data: Any |
| 90 | ) -> Action: |
| 91 | """Predict the next action given the observation""" |
| 92 | return self.actions.pop(0) |
| 93 | |
| 94 | def reset( |
| 95 | self, |
| 96 | test_config_file: str, |
| 97 | ) -> None: |
| 98 | with open(test_config_file) as f: |
| 99 | ref_actions = json.load(f)["reference_action_sequence"] |
| 100 | tag = ref_actions["action_set_tag"] |
| 101 | action_seq = ref_actions["action_sequence"] |
| 102 | self.set_action_set_tag(tag) |
| 103 | self.set_actions(action_seq) |
| 104 | |
| 105 | |
| 106 | class PromptAgent(Agent): |
no outgoing calls