From 955f8605d4cd3b3a62c628be22acdf4c7eb448e6 Mon Sep 17 00:00:00 2001 From: Zhicheng Zhang Date: Mon, 30 Sep 2024 15:46:54 +0800 Subject: [PATCH] make sure openapi proxy is working --- apps/agentfabric/config_utils.py | 3 ++- apps/agentfabric/user_core.py | 2 +- modelscope_agent/agent.py | 14 ++++++------ modelscope_agent/agents/role_play.py | 4 ++-- modelscope_agent/callbacks/base.py | 6 +++-- modelscope_agent/memory/base.py | 2 +- modelscope_agent/rag/emb.py | 9 ++++++++ modelscope_agent/tools/base.py | 33 ++++++++++++++++++---------- 8 files changed, 48 insertions(+), 25 deletions(-) diff --git a/apps/agentfabric/config_utils.py b/apps/agentfabric/config_utils.py index b9e61b287..e3264cc48 100644 --- a/apps/agentfabric/config_utils.py +++ b/apps/agentfabric/config_utils.py @@ -169,9 +169,10 @@ def parse_configuration(uuid_str='', use_tool_api=False): plugin_cfg = {} available_plugin_list = [] - if use_tool_api: + if use_tool_api and getattr(builder_cfg, 'openapi_list', None): available_plugin_list = builder_cfg.openapi_list else: + available_plugin_list = [] openapi_plugin_file = get_user_openapi_plugin_cfg_file(uuid_str) openapi_plugin_cfg_file_temp = './config/openapi_plugin_config.json' if os.path.exists(openapi_plugin_file): diff --git a/apps/agentfabric/user_core.py b/apps/agentfabric/user_core.py index e49eb1242..32e245a2e 100644 --- a/apps/agentfabric/user_core.py +++ b/apps/agentfabric/user_core.py @@ -52,7 +52,7 @@ def init_user_chatbot_agent(uuid_str='', uuid_str=uuid_str, use_tool_api=use_tool_api, user_token=user_token, - openapi_list_for_remote=openapi_plugin_list) + openapi_list=openapi_plugin_list) # build memory preview_history_dir = get_user_preview_history_dir(uuid_str, session) diff --git a/modelscope_agent/agent.py b/modelscope_agent/agent.py index 9ace07e7f..0815fa2ca 100644 --- a/modelscope_agent/agent.py +++ b/modelscope_agent/agent.py @@ -17,7 +17,8 @@ def enable_run_callback(func): @wraps(func) def wrapper(self, *args, **kwargs): callbacks = self.callback_manager - callbacks.on_run_start(*args, **kwargs) + if callbacks.callbacks: + callbacks.on_run_start(*args, **kwargs) response = func(self, *args, **kwargs) name = self.name or self.__class__.__name__ if not isinstance(response, str): @@ -53,8 +54,7 @@ def __init__(self, instruction: Union[str, dict] = None, use_tool_api: bool = False, callbacks: list = None, - openapi_list_for_remote: Optional[List[Union[str, - Dict]]] = None, + openapi_list: Optional[List[Union[str, Dict]]] = None, **kwargs): """ init tools/llm/instruction for one agent @@ -72,7 +72,7 @@ def __init__(self, instruction: the system instruction of this agent use_tool_api: whether to use the tool service api, else to use the tool cls instance callbacks: the callbacks that could be used during different phase of agent loop - openapi_list_for_remote: the openapi list for remote calling only + openapi_list: the openapi list for remote calling only kwargs: other potential parameters """ if isinstance(llm, Dict): @@ -90,8 +90,8 @@ def __init__(self, self._register_tool(function, **kwargs) # this logic is for remote openapi calling only, by using this method apikey only be accessed by service. - if openapi_list_for_remote: - for openapi_name in openapi_list_for_remote: + if openapi_list: + for openapi_name in openapi_list: self._register_openapi_for_remote_calling( openapi_name, **kwargs) @@ -175,7 +175,7 @@ def _register_openapi_for_remote_calling(self, openapi: Union[str, Dict], function_plain_text = openapi_instance_for_specific_tool.parser_function_by_tool_name( tool_name) openapi_instance_for_specific_tool.function_plain_text = function_plain_text - self.function_map[tool_name] = openapi_instance + self.function_map[tool_name] = openapi_instance_for_specific_tool def _register_tool(self, tool: Union[str, Dict], diff --git a/modelscope_agent/agents/role_play.py b/modelscope_agent/agents/role_play.py index 8fdf53e1e..4fce0c5c2 100644 --- a/modelscope_agent/agents/role_play.py +++ b/modelscope_agent/agents/role_play.py @@ -89,7 +89,7 @@ def __init__(self, name: Optional[str] = None, description: Optional[str] = None, instruction: Union[str, dict] = None, - openapi_list_for_remote: Optional[List] = None, + openapi_list: Optional[List] = None, **kwargs): Agent.__init__( self, @@ -99,7 +99,7 @@ def __init__(self, name, description, instruction, - openapi_list_for_remote=openapi_list_for_remote, + openapi_list=openapi_list, **kwargs) AgentEnvMixin.__init__(self, **kwargs) diff --git a/modelscope_agent/callbacks/base.py b/modelscope_agent/callbacks/base.py index ba309ff55..00170436d 100644 --- a/modelscope_agent/callbacks/base.py +++ b/modelscope_agent/callbacks/base.py @@ -1,4 +1,4 @@ -from typing import List +from typing import List, Optional class BaseCallback: @@ -42,10 +42,12 @@ def on_step_end(self, *args, **kwargs): class CallbackManager(BaseCallback): - def __init__(self, callbacks: List[BaseCallback]): + def __init__(self, callbacks: Optional[List[BaseCallback]] = None): self.callbacks = callbacks def call_event(self, event, *args, **kwargs): + if not self.callbacks: + return for callback in self.callbacks: func = getattr(callback, event) func(*args, **kwargs) diff --git a/modelscope_agent/memory/base.py b/modelscope_agent/memory/base.py index d3dc10384..da71530b3 100644 --- a/modelscope_agent/memory/base.py +++ b/modelscope_agent/memory/base.py @@ -14,7 +14,7 @@ def enable_rag_callback(func): @wraps(func) def wrapper(self, *args, **kwargs): callbacks = self.callback_manager - if callbacks: + if callbacks.callbacks: callbacks.on_rag_start(*args, **kwargs) response = func(self, *args, **kwargs) if callbacks: diff --git a/modelscope_agent/rag/emb.py b/modelscope_agent/rag/emb.py index 737c78e45..cb6ce646a 100644 --- a/modelscope_agent/rag/emb.py +++ b/modelscope_agent/rag/emb.py @@ -88,3 +88,12 @@ def _embed(self, raise ValueError(f'call dashscope api failed: {resp}') return [list(map(float, e['embedding'])) for e in res] + + +if __name__ == '__main__': + # Example usage + embedding = DashscopeEmbedding(model_name='text-embedding-v2') + query = 'This is a query' + text = 'This is a document' + query_embedding = embedding._embed(query) + print(query_embedding) diff --git a/modelscope_agent/tools/base.py b/modelscope_agent/tools/base.py index e6185ce6c..3911b7c2f 100644 --- a/modelscope_agent/tools/base.py +++ b/modelscope_agent/tools/base.py @@ -482,14 +482,14 @@ def call(self, params: str, **kwargs): class OpenapiServiceProxy: - def __init__( - self, - openapi: Union[str, Dict], - openapi_service_manager_url: str = os.getenv( - 'TOOL_MANAGER_SERVICE_URL', DEFAULT_TOOL_MANAGER_SERVICE_URL), - user_token: str = None, - is_remote: bool = True, - ): + def __init__(self, + openapi: Union[str, Dict], + openapi_service_manager_url: str = os.getenv( + 'TOOL_MANAGER_SERVICE_URL', + DEFAULT_TOOL_MANAGER_SERVICE_URL), + user_token: str = None, + is_remote: bool = True, + **kwargs): """ Openapi service proxy class Args: @@ -511,7 +511,7 @@ def __init__( for item in openapi_formatted_schema: self.api_info_dict[openapi_formatted_schema[item] ['name']] = openapi_formatted_schema[item] - self.tool_names = list(self.api_info_dict.values()) + self.tool_names = list(self.api_info_dict.keys()) def parser_function_by_tool_name(self, tool_name: str): tool_desc_template = { @@ -660,7 +660,18 @@ def call(self, params: str, **kwargs): if __name__ == '__main__': - tool = OpenapiServiceProxy('openapi_plugin') + import copy + openapi_instance = OpenapiServiceProxy('openapi_plugin') + function_map = {} + tool_names = openapi_instance.tool_names + for tool_name in tool_names: + openapi_instance_for_specific_tool = copy.deepcopy(openapi_instance) + openapi_instance_for_specific_tool.name = tool_name + function_plain_text = openapi_instance_for_specific_tool.parser_function_by_tool_name( + tool_name) + openapi_instance_for_specific_tool.function_plain_text = function_plain_text + function_map[tool_name] = openapi_instance_for_specific_tool + print( - tool.call( + openapi_instance.call( '{"username":"test"}', tool_name='getTodos', user_token='test'))