Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix agentfabric bugs #604

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion apps/agentfabric/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -621,11 +621,17 @@ def preview_send_message(chatbot, input, _state, uuid_str, _user_token):

# get chat history from memory
history = user_memory.get_history()
# skip
filtered_files = [
item for item in append_files
if not item.lower().endswith(('.jpeg', '.png', '.jpg', '.wav',
'.gif', '.mp3'))
]

use_llm = True if len(user_agent.function_list) else False
ref_doc = user_memory.run(
query=input.text,
url=append_files,
url=filtered_files,
max_token=4000,
top_k=2,
checked=True,
Expand Down
12 changes: 11 additions & 1 deletion apps/agentfabric/appBot.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,19 @@ def send_message(chatbot, input, _state, _user_token):
# get short term memory history
history = user_memory.get_history()

# skip
filtered_files = [
item for item in append_files
if not item.lower().endswith(('.jpeg', '.png', '.jpg', '.wav',
'.gif', '.mp3'))
]

use_llm = True if len(user_agent.function_list) else False
ref_doc = user_memory.run(
query=input.text, url=append_files, checked=True, use_llm=use_llm)
query=input.text,
url=filtered_files,
checked=True,
use_llm=use_llm)

response = ''
try:
Expand Down
2 changes: 1 addition & 1 deletion apps/agentfabric/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
gradio==4.36.1
markdown-cjk-spacing
mdx_truly_sane_lists
modelscope-agent>=0.6.6
modelscope-agent>=0.7.2
modelscope_studio>=0.4.0
moviepy
pymdown-extensions
Expand Down
15 changes: 12 additions & 3 deletions apps/agentfabric/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,10 +424,11 @@ def generate():
f'load history method: time consumed {time.time() - start_time}'
)

# skip image upsert
# skip
filtered_files = [
item for item in file_paths
if not item.lower().endswith(('.jpeg', '.png', '.jpg'))
if not item.lower().endswith(('.jpeg', '.png', '.jpg', '.wav',
'.gif', '.mp3'))
]

use_llm = True if len(user_agent.function_list) else False
Expand Down Expand Up @@ -495,7 +496,15 @@ def generate():
stack_trace = stack_trace.replace('\n', '\\n')
logger.error(
f'preview_chat_generate_error: {str(e)}, {stack_trace}')
raise e
error_info = f'data: Please check your configuration and try again. Error: {str(e)}, {stack_trace}\n\n'
res = json.dumps(
{
'data': error_info,
'is_final': True,
'request_id': request_id_var.get('')
},
ensure_ascii=False)
yield f'data: {res}\n\n'

return Response(generate(), mimetype='text/event-stream')

Expand Down
27 changes: 24 additions & 3 deletions modelscope_agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,32 @@ 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
7 changes: 3 additions & 4 deletions modelscope_agent/agents/multi_role_play.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
Action: 工具的名称,必须是[{tool_names}]之一
Action Input: 工具的输入
Observation: <result>工具返回的结果</result>
Answer: 根据Observation总结本次工具调用返回的结果如果结果中出现url,请使用如下格式展示出来:![图片](url)
Answer: 根据Observation总结本次工具调用返回的结果如果结果中出现url,请严格遵循Observation中url的格式进行展示。

"""

Expand Down Expand Up @@ -59,10 +59,9 @@
Action: The name of the tool, must be one of [{tool_names}]
Action Input: Tool input
Observation: <result>Tool returns result</result>
Answer: Summarize the results of this tool call based on Observation. If the result contains url, %s
Answer: Summarize the results of this tool call based on Observation.

""" % ('You can call zero or more times according to your needs:',
'please display it in the following format:![Image](URL)')
""" % ('You can call zero or more times according to your needs:')

PROMPT_TEMPLATE_EN = """{role_prompt}"""

Expand Down
14 changes: 6 additions & 8 deletions modelscope_agent/llm/utils/function_call_with_raw_prompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
Action: 工具的名称,必须是[{tool_names}]之一
Action Input: 工具的输入
Observation: <result>工具返回的结果</result>
Answer: 根据Observation总结本次工具调用返回的结果如果结果中出现url,请使用如下格式展示出来:![图片](url)
Answer: 根据Observation总结本次工具调用返回的结果如果结果中出现url,请严格遵循Observation中url的格式进行展示。

"""

Expand All @@ -47,7 +47,7 @@
...
Observation: <result>工具N返回的结果</result>

Answer: 根据Observation总结本次工具调用返回的结果,如果结果中出现url,请使用如下格式展示出来:![图片](url)
Answer: 根据Observation总结本次工具调用返回的结果

"""

Expand All @@ -64,10 +64,9 @@
Action: The name of the tool, must be one of [{tool_names}]
Action Input: Tool input
Observation: <result>Tool returns result</result>
Answer: Summarize the results of this tool call based on Observation. If the result contains url, %s
Answer: Summarize the results of this tool call based on Observation.

""" % ('You can call zero or more times according to your needs:',
'please display it in the following format:![Image](URL)')
""" % ('You can call zero or more times according to your needs:')

TOOL_TEMPLATE_EN_PARALLEL = """
# Tools
Expand All @@ -90,10 +89,9 @@
Observation: <result>Tool 1 returns result</result>
...
Observation: <result>Tool N returns result</result>
Answer: Summarize the results of this tool call based on Observation. If the result contains url, %s
Answer: Summarize the results of this tool call based on Observation.

""" % ('You can call zero or more times according to your needs:',
'please display it in the following format:![Image](URL)')
""" % ('You can call zero or more times according to your needs:')

TOOL_TEMPLATE = {
'zh': TOOL_TEMPLATE_ZH,
Expand Down
28 changes: 24 additions & 4 deletions modelscope_agent/tools/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import time
import traceback
from abc import ABC, abstractmethod
from copy import deepcopy
from typing import Dict, List, Optional, Union
Expand Down Expand Up @@ -335,6 +336,12 @@ 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 +375,13 @@ 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 Expand Up @@ -422,8 +436,8 @@ def _check_tool_status(self):
return result['status']
except Exception as e:
raise RuntimeError(
f'Get error during checking status from tool manager service with detail {e}'
)
f'Get error during checking status from tool manager service: {self.tool_name} '
f'{self.tenant_id}. detail {e}, {traceback.format_exc()}')

def _get_tool_info(self):
try:
Expand All @@ -442,10 +456,16 @@ def _get_tool_info(self):
},
headers=headers)
response.raise_for_status()
return ToolServiceProxy.parse_service_response(response)
tool_info = ToolServiceProxy.parse_service_response(response)
# check required params
_ = tool_info['name']
_ = tool_info['description']
_ = tool_info['parameters']
return tool_info
except Exception as e:
raise RuntimeError(
f'Get error during getting tool info from tool manager service with detail {e}'
f'Get error during getting tool info from tool manager, '
f'{self.tool_name} {self.tenant_id}. detail {e}, {traceback.format_exc()}'
)

def call(self, params: str, **kwargs):
Expand Down
12 changes: 8 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,17 @@ 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
2 changes: 1 addition & 1 deletion modelscope_agent/tools/utils/output_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def __init__(self, video, **kwargs) -> None:

self._raw_data = video

if self._path.endswith('.gif'):
if self._path.split('?')[0].endswith('.gif'):
self._repr = f'![IMAGEGEN]({self._path})'
else:
self._repr = f'<video src="{self._path}"/>'
Expand Down
2 changes: 1 addition & 1 deletion modelscope_agent/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.7.1'
__version__ = '0.7.2'
Loading