From 00ace00a152fa98a11946756e5de5e2b9bd807e6 Mon Sep 17 00:00:00 2001 From: yrk111222 <2493404415@qq.com> Date: Wed, 27 Nov 2024 15:51:27 +0800 Subject: [PATCH] Fix the issue with the tool being uncallable. (#606) --- modelscope_agent/agent.py | 23 ++++++++++++++++--- modelscope_agent/tools/base.py | 9 ++++++++ modelscope_agent/tools/utils/openapi_utils.py | 9 ++++---- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/modelscope_agent/agent.py b/modelscope_agent/agent.py index 0815fa2c..67f22780 100644 --- a/modelscope_agent/agent.py +++ b/modelscope_agent/agent.py @@ -193,11 +193,28 @@ def _register_tool(self, Returns: """ - tool_name = tool + tool_name = None tool_cfg = {} + + # # Check if the tool is a dictionary and extract the tool name and configuration from it. if isinstance(tool, dict): - tool_name = next(iter(tool)) - tool_cfg = tool[tool_name] + try: + tool_name = next(iter(tool)) + tool_cfg = tool[tool_name] + except StopIteration: + # If the tool is an empty dictionary, proceed to register the next tool. + print("Empty tool dictionary provided, skipping the registration of the current tool") + return + + # If the tool is a string, assign it directly to tool_name. + elif isinstance(tool, str): + tool_name = tool + + # If the tool_name is empty, skip the registration of the current tool. + if not tool_name: + print("No tool name provided, skipping the registration of the current tool") + return + if tool_name not in TOOL_REGISTRY and not self.use_tool_api: raise NotImplementedError if tool_name not in self.function_list: diff --git a/modelscope_agent/tools/base.py b/modelscope_agent/tools/base.py index 8ad6e67a..6ea85d3d 100644 --- a/modelscope_agent/tools/base.py +++ b/modelscope_agent/tools/base.py @@ -335,6 +335,10 @@ def __init__(self, self.tenant_id = tenant_id self._register_tool() + if not self.tool_name: + print("Skipping tool registration and status check because 'tool_name' is not defined or empty.") + return # When tool_name is an empty string, skip registration and status check. + max_retry = 10 while max_retry > 0: status = self._check_tool_status() @@ -368,6 +372,11 @@ def parse_service_response(response): def _register_tool(self): try: + # Check if `tool_name` is defined and not empty. + if not self.tool_name: + print("Skipping tool registration because 'tool_name' is not defined or empty.") + return # Return directly, skipping registration. + service_token = os.getenv('TOOL_MANAGER_AUTH', '') headers = { 'Content-Type': 'application/json', diff --git a/modelscope_agent/tools/utils/openapi_utils.py b/modelscope_agent/tools/utils/openapi_utils.py index 29b956bd..e3e2bc3e 100644 --- a/modelscope_agent/tools/utils/openapi_utils.py +++ b/modelscope_agent/tools/utils/openapi_utils.py @@ -189,13 +189,14 @@ def swagger_to_openapi(swagger_data): def openapi_schema_convert(schema: dict, auth: dict = {}): config_data = {} - host = schema.get('host', '') + host = schema.get('host', '') if schema else '' # Check if schema is None + if host: - schema = swagger_to_openapi(schema) + schema = swagger_to_openapi(schema) if schema else {} # Call only if schema is not None - schema = jsonref.replace_refs(schema) + schema = jsonref.replace_refs(schema) if schema else {} # Call only if schema is not None - servers = schema.get('servers', []) + servers = schema.get('servers', []) if schema else [] # Check if schema is None if servers: servers_url = servers[0].get('url')