Skip to content

Commit

Permalink
Parse response of demo agent
Browse files Browse the repository at this point in the history
  • Loading branch information
jardinetsouffleton authored Jan 14, 2025
1 parent ec6b802 commit 0f160d2
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion demo_agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 0f160d2

Please sign in to comment.