From 0f160d2c089e3b7c8c10a6408a86b9c3e18dc4ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Boisvert?= Date: Tue, 14 Jan 2025 15:22:28 -0500 Subject: [PATCH 1/2] Parse response of demo agent --- demo_agent/agent.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/demo_agent/agent.py b/demo_agent/agent.py index 632c0bbc..43e9e5aa 100644 --- a/demo_agent/agent.py +++ b/demo_agent/agent.py @@ -29,6 +29,11 @@ def image_to_jpg_base64_url(image: np.ndarray | Image.Image): return f"data:image/jpeg;base64,{image_base64}" +def extract_code_blocks(text) -> list[tuple[str, str]]: + pattern = re.compile(r"```(\w*\n)?(.*?)```", re.DOTALL) + + matches = pattern.findall(text) + return [(match[0].strip(), match[1].strip()) for match in matches] class DemoAgent(Agent): """A basic agent using OpenAI API, to demonstrate BrowserGym's functionalities.""" @@ -322,7 +327,7 @@ def get_action(self, obs: dict) -> tuple[str, dict]: {"role": "user", "content": user_msgs}, ], ) - action = response.choices[0].message.content + action = extract_code_blocks(response.choices[0].message.content) self.action_history.append(action) From 40d78c98c10186751b3c6597951e4d28255b736d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Boisvert?= Date: Tue, 14 Jan 2025 15:35:46 -0500 Subject: [PATCH 2/2] Update agent.py --- demo_agent/agent.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/demo_agent/agent.py b/demo_agent/agent.py index 43e9e5aa..ea002ede 100644 --- a/demo_agent/agent.py +++ b/demo_agent/agent.py @@ -327,7 +327,7 @@ def get_action(self, obs: dict) -> tuple[str, dict]: {"role": "user", "content": user_msgs}, ], ) - action = extract_code_blocks(response.choices[0].message.content) + action = extract_code_blocks(response.choices[0].message.content)[1] self.action_history.append(action)