prompt-based agent that emits action given the history
| 104 | |
| 105 | |
| 106 | class PromptAgent(Agent): |
| 107 | """prompt-based agent that emits action given the history""" |
| 108 | |
| 109 | @beartype |
| 110 | def __init__( |
| 111 | self, |
| 112 | action_set_tag: str, |
| 113 | lm_config: lm_config.LMConfig, |
| 114 | prompt_constructor: PromptConstructor, |
| 115 | ) -> None: |
| 116 | super().__init__() |
| 117 | self.lm_config = lm_config |
| 118 | self.prompt_constructor = prompt_constructor |
| 119 | self.action_set_tag = action_set_tag |
| 120 | |
| 121 | def set_action_set_tag(self, tag: str) -> None: |
| 122 | self.action_set_tag = tag |
| 123 | |
| 124 | @beartype |
| 125 | def next_action( |
| 126 | self, trajectory: Trajectory, intent: str, meta_data: dict[str, Any] |
| 127 | ) -> Action: |
| 128 | prompt = self.prompt_constructor.construct( |
| 129 | trajectory, intent, meta_data |
| 130 | ) |
| 131 | lm_config = self.lm_config |
| 132 | n = 0 |
| 133 | while True: |
| 134 | response = call_llm(lm_config, prompt) |
| 135 | force_prefix = self.prompt_constructor.instruction[ |
| 136 | "meta_data" |
| 137 | ].get("force_prefix", "") |
| 138 | response = f"{force_prefix}{response}" |
| 139 | n += 1 |
| 140 | try: |
| 141 | parsed_response = self.prompt_constructor.extract_action( |
| 142 | response |
| 143 | ) |
| 144 | if self.action_set_tag in ["id_html_tree", "id_html_nasc_tree", "id_accessibility_tree"]: |
| 145 | action = create_id_based_action(parsed_response) |
| 146 | elif self.action_set_tag == "playwright": |
| 147 | action = create_playwright_action(parsed_response) |
| 148 | else: |
| 149 | raise ValueError( |
| 150 | f"Unknown action type {self.action_set_tag}" |
| 151 | ) |
| 152 | action["raw_prediction"] = response |
| 153 | break |
| 154 | except ActionParsingError as e: |
| 155 | if n >= lm_config.gen_config["max_retry"]: |
| 156 | action = create_none_action() |
| 157 | action["raw_prediction"] = response |
| 158 | break |
| 159 | |
| 160 | return action |
| 161 | |
| 162 | def check_action( |
| 163 | self, trajectory: Trajectory, intent: str, meta_data: dict[str, Any], target_action: str |