# -*- coding: utf-8 -*-"""The Chinese tools for ACEBench evaluation."""fromfunctoolsimportwrapsfromtypingimportCallable,Anyfrom._ace_tools_apiimport(ReminderApi,FoodPlatformApi,TravelApi,MessageApi,)from...messageimportTextBlockfrom...toolimportToolResponsedef_tool_function_wrapper(get_tool_function:Callable)->Callable:"""Wrap the tool function result to be ToolResponse."""@wraps(get_tool_function)defwrapper(self:"ACEPhone",name:str)->Callable:"""Wrap the tool function to return ToolResponse."""tool_function=get_tool_function(self,name)@wraps(tool_function)defwrapper_tool_function(*args:Any,**kwargs:Any)->ToolResponse:"""The wrapped tool function"""res=tool_function(*args,**kwargs)returnToolResponse(content=[TextBlock(type="text",text=str(res),),],)returnwrapper_tool_functionreturnwrapper
[文档]classACEPhone:"""Simulate a user phone with various apps and functionalities in ACEBench. The code is implemented with reference to the `ACEBench <https://github.com/ACEBench/ACEBench>`_. """
[文档]def__init__(self)->None:"""Initialize the shared state and apps for the ACEPhone."""self._state={"wifi":False,"logged_in":False,}self._message_app=MessageApi(self._state)self._reminder_app=ReminderApi(self._state)self._food_platform_app=FoodPlatformApi(self._state)self._travel=TravelApi()
[文档]defload_initial_config(self,initial_config:dict)->None:"""Load the initial config from the application configuration."""# Empty initial configiflen(initial_config)==0:return# Fix the typo in ACEBench by renaming "Baspi" to "BaseApi"if"Baspi"ininitial_config:initial_config["BaseApi"]=initial_config.pop("Baspi")# Verify stateassert("BaseApi"ininitial_configand"wifi"ininitial_config["BaseApi"]and"logged_in"ininitial_config["BaseApi"]),f"Invalid initial config: {initial_config}"self._state["wifi"]=initial_config["BaseApi"]["wifi"]self._state["logged_in"]=initial_config["BaseApi"]["logged_in"]
[文档]defget_current_state(self)->list[dict]:"""Follow ACEBench to get the current state of the ACEPhone."""return[{"BaseApi":self._state},self._message_app.get_state_dict(),self._reminder_app.get_state_dict(),self._food_platform_app.get_state_dict(),self._travel.get_state_dict(),]
[文档]@_tool_function_wrapperdefget_tool_function(self,name:str)->Callable:"""Get a tool function by name."""ifnamein["turn_on_wifi","login_device",]:returngetattr(self,name)ifnameinself._message_app.tool_functions:returngetattr(self._message_app,name)ifnameinself._food_platform_app.tool_functions:returngetattr(self._food_platform_app,name)ifnameinself._reminder_app.tool_functions:returngetattr(self._reminder_app,name)ifnameinself._travel.tool_functions:returngetattr(self._travel,name)raiseValueError(f"Tool function '{name}' not found in ACEPhone.",)