1+ from bgym import AbstractActionSet
2+ from tapeagents .tool_calling import FunctionCall , ToolCallAction , ToolSpec
3+
4+ from agentlab .llm .llm_utils import parse_html_tags_raise
5+
6+
7+ class ToolsActionSet (AbstractActionSet ):
8+ def __init__ (self , actions :list [ToolSpec ]):
9+ self .actions = actions
10+
11+ def describe (self , with_long_description : bool = True , with_examples : bool = True ) -> str :
12+ tools_description = "\n " .join ([action .description () for action in self .actions ])
13+ return tools_description
14+
15+ def example_action (self , abstract : bool ) -> str :
16+ if abstract :
17+ return """<action>
18+ {
19+ "name": "<action_name>",
20+ "arguments": {
21+ "<argument_name_1>": "<argument_value_1>",
22+ "<argument_name_2>": "<argument_value_2>",
23+ ...
24+ }
25+ }
26+ </action>
27+ """
28+ else :
29+ return """<action>
30+ {
31+ "name": "browser_navigate",
32+ "arguments": {
33+ "url": "https://www.google.com"
34+ }
35+ }
36+ </action>
37+ """
38+ @classmethod
39+ def parse_action (cls , llm_output : str ) -> ToolCallAction :
40+ content_dict , valid , retry_message = parse_html_tags_raise (llm_output , keys = ["action" ])
41+ if not valid or "action" not in content_dict :
42+ raise ValueError (f"Invalid action: llm_output: { llm_output } , retry_message: { retry_message } " )
43+ action_str = content_dict ["action" ]
44+ return ToolCallAction (function = FunctionCall (name = action_str ["name" ], arguments = action_str ["arguments" ]))
45+
46+ def to_python_code (self , action ) -> str :
47+ return action .model_dump_json (indent = 2 )
0 commit comments