Skip to content

Commit

Permalink
Fix the issue with the tool being uncallable. (#606)
Browse files Browse the repository at this point in the history
  • Loading branch information
yrk111222 authored Nov 27, 2024
1 parent 72a8b12 commit 00ace00
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
23 changes: 20 additions & 3 deletions modelscope_agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 9 additions & 0 deletions modelscope_agent/tools/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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',
Expand Down
9 changes: 5 additions & 4 deletions modelscope_agent/tools/utils/openapi_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 00ace00

Please sign in to comment.