diff --git a/modelscope_agent/agent.py b/modelscope_agent/agent.py index 213bdab40..5ec4917ce 100644 --- a/modelscope_agent/agent.py +++ b/modelscope_agent/agent.py @@ -115,7 +115,14 @@ def _register_tool(self, tool: Union[str, Dict]): raise NotImplementedError if tool not in self.function_list: self.function_list.append(tool) - self.function_map[tool_name] = TOOL_REGISTRY[tool_name](tool_cfg) + tool_class = TOOL_REGISTRY[tool_name] + try: + self.function_map[tool_name] = tool_class(tool_cfg) + except TypeError: + # When using OpenAPI, tool_class is already an instantiated object, not a corresponding class + self.function_map[tool_name] = tool_class + except Exception as e: + raise RuntimeError(e) def _detect_tool(self, message: Union[str, dict]): # use built-in default judgment functions diff --git a/modelscope_agent/tools/base.py b/modelscope_agent/tools/base.py index c8cd3e4b7..5fb5633a0 100644 --- a/modelscope_agent/tools/base.py +++ b/modelscope_agent/tools/base.py @@ -76,10 +76,16 @@ def _build_function(self): }, } for para in self.parameters: - function['parameters']['properties'][para['name']] = { - 'type': para['type'], - 'description': para['description'] + function_details = { + 'type': + para['type'] if 'type' in para else para['schema']['type'], + 'description': + para['description'] } + if 'enum' in para and para['enum'] not in ['', []]: + function_details['enum'] = para['enum'] + function['parameters']['properties'][ + para['name']] = function_details if 'required' in para and para['required']: function['parameters']['required'].append(para['name']) else: diff --git a/modelscope_agent/tools/dashscope_tools/wordart_tool.py b/modelscope_agent/tools/dashscope_tools/wordart_tool.py index fab28dae7..5f4d2b91f 100644 --- a/modelscope_agent/tools/dashscope_tools/wordart_tool.py +++ b/modelscope_agent/tools/dashscope_tools/wordart_tool.py @@ -45,7 +45,6 @@ def call(self, params: str, **kwargs) -> str: if isinstance(params, str): return 'Parameter Error' remote_parsed_input = json.dumps(self._remote_parse_input(**params)) - origin_result = None self.token = kwargs.get('token', os.environ.get('DASHSCOPE_API_KEY', '')) assert self.token != '', 'dashscope api token must be acquired with wordart' diff --git a/modelscope_agent/tools/openapi_plugin.py b/modelscope_agent/tools/openapi_plugin.py index 6f3fa764e..6aa2a0236 100644 --- a/modelscope_agent/tools/openapi_plugin.py +++ b/modelscope_agent/tools/openapi_plugin.py @@ -47,26 +47,17 @@ def __init__(self, cfg, name): self.description = self.cfg.get('description', 'This is a api tool that ...') self.responses_param = self.cfg.get('responses_param', []) - try: - all_para = { - 'name': self.name, - 'description': self.description, - 'parameters': self.parameters - } - self.tool_schema = ToolSchema(**all_para) - except ValidationError: - raise ValueError(f'Error when parsing parameters of {self.name}') - self._str = self.tool_schema.model_dump_json() - self._function = self.parse_pydantic_model_to_openai_function(all_para) + super().__init__(cfg) def call(self, params: str, **kwargs): if self.url == '': raise ValueError( f"Could not use remote call for {self.name} since this tool doesn't have a remote endpoint" ) - # - # remote_parsed_input = json.dumps( - # self._remote_parse_input(*args, **kwargs)) + + json_params = json.loads(params) + remote_parsed_input = json.dumps( + self._remote_parse_input(**json_params)) params = self._verify_args(params) if isinstance(params, str): return 'Parameter Error' @@ -80,7 +71,10 @@ def call(self, params: str, **kwargs): print(f'data: {kwargs}') print(f'header: {self.header}') response = requests.request( - 'POST', url=self.url, headers=self.header, data=params) + 'POST', + url=self.url, + headers=self.header, + data=remote_parsed_input) if response.status_code != requests.codes.ok: response.raise_for_status() @@ -96,6 +90,8 @@ def call(self, params: str, **kwargs): raise ValueError( f'Remote call failed with error code: {e.response.status_code},\ error message: {e.response.content.decode("utf-8")}') + except Exception as e: + raise ValueError(e) raise ValueError( 'Remote call max retry times exceeded! Please try to use local call.' @@ -106,8 +102,8 @@ def call(self, params: str, **kwargs): new_url = self.url matches = re.findall(r'\{(.*?)\}', self.url) for match in matches: - if match in kwargs: - new_url = new_url.replace('{' + match + '}', kwargs[match]) + if match in params: + new_url = new_url.replace('{' + match + '}', params[match]) else: print( f'The parameter {match} was not generated by the model.' @@ -209,7 +205,7 @@ def parse_nested_parameters(param_name, param_info, parameters_list, content): inner_param_required, 'type': inner_param_type, - 'value': + 'enum': inner_param_info.get('enum', '') }) else: @@ -219,7 +215,7 @@ def parse_nested_parameters(param_name, param_info, parameters_list, content): 'description': param_description, 'required': param_required, 'type': param_type, - 'value': param_info.get('enum', '') + 'enum': param_info.get('enum', '') }) except Exception as e: raise ValueError(f'{e}:schema结构出错') @@ -348,7 +344,6 @@ def openapi_schema_convert(schema, auth): } } elif method.upper() == 'GET': - parameters_list = [] parameters_list = details.get('parameters', []) config_entry = { 'name': name,