|
| 1 | +import logging |
| 2 | + |
| 3 | +from debug_gym.agents.base_agent import register_agent |
| 4 | +from debug_gym.agents.rewrite_agent import RewriteAgent |
| 5 | +from debug_gym.llms.base import LLM |
| 6 | +from debug_gym.logger import DebugGymLogger |
| 7 | + |
| 8 | + |
| 9 | +@register_agent |
| 10 | +class GuidedRewriteAgent(RewriteAgent): |
| 11 | + name: str = "guided_agent" |
| 12 | + |
| 13 | + def try_rewrite(self, task_name): |
| 14 | + # make a copy of the env for the llm |
| 15 | + cloned_env = self.env.clone() |
| 16 | + |
| 17 | + # Only keep the rewrite tool in the cloned env |
| 18 | + for tool in cloned_env.tools: |
| 19 | + if tool.name != "rewrite": |
| 20 | + cloned_env.remove_tool(tool.name) |
| 21 | + |
| 22 | + # Reset the cloned environment and replay the history. |
| 23 | + info = cloned_env.reset(options={"task_name": task_name}) |
| 24 | + # replay the history up to the current step |
| 25 | + for step in self.history.get_all(): |
| 26 | + assert not step.done |
| 27 | + info = cloned_env.step(step.action) |
| 28 | + |
| 29 | + prompt = self.build_prompt(info) |
| 30 | + response = self.llm(prompt, info.tools) |
| 31 | + info = cloned_env.step(response.response) |
| 32 | + |
| 33 | + return info.done |
| 34 | + |
| 35 | + def run(self, task_name=None, debug=False): |
| 36 | + self.llm.logger = DebugGymLogger(name="LLM", level=logging.ERROR) |
| 37 | + self.human = LLM.instantiate(llm_name="human", logger=self.logger) |
| 38 | + |
| 39 | + self.history.reset() |
| 40 | + info = self.env.reset(options={"task_name": task_name}) |
| 41 | + # initial state does not have prompt and response |
| 42 | + self.history.step(info, None) |
| 43 | + |
| 44 | + if info.done is True: |
| 45 | + # msg = "Environment started with entrypoint passing without errors." |
| 46 | + return True |
| 47 | + |
| 48 | + highscore = info.score |
| 49 | + |
| 50 | + for step in self.logger.tqdm(range(self.config["max_steps"])): |
| 51 | + highscore = max(highscore, info.score) |
| 52 | + self.logger.info( |
| 53 | + f"Score: {info.score}/{info.max_score} ({info.score/info.max_score:.1%}) [Best: {highscore}]" |
| 54 | + ) |
| 55 | + |
| 56 | + llm_done = self.try_rewrite(task_name) |
| 57 | + if llm_done: |
| 58 | + self.logger.info( |
| 59 | + f"*** The rewrite-only agent with {self.llm.model_name} managed to solve the task with the current context. ***" |
| 60 | + ) |
| 61 | + break |
| 62 | + |
| 63 | + # If the LLM did not manage to solve the task, we continue with the guided approach. |
| 64 | + prompt = self.build_prompt(info) |
| 65 | + human_response = self.human(prompt, info.tools) |
| 66 | + |
| 67 | + if debug: |
| 68 | + breakpoint() |
| 69 | + |
| 70 | + # step the environment with the human response |
| 71 | + info = self.env.step(human_response.response) |
| 72 | + # log the human response |
| 73 | + self.history.step(info, human_response) |
| 74 | + |
| 75 | + if info.done: |
| 76 | + self.logger.info( |
| 77 | + "You managed to provide the patch that solves the task before the LLM. Congrats!" |
| 78 | + ) |
| 79 | + break |
| 80 | + |
| 81 | + return info.done |
0 commit comments