Skip to content

Commit

Permalink
make sure openapi proxy is working
Browse files Browse the repository at this point in the history
  • Loading branch information
zzhangpurdue committed Sep 30, 2024
1 parent 8921c9b commit 955f860
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 25 deletions.
3 changes: 2 additions & 1 deletion apps/agentfabric/config_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion apps/agentfabric/user_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 7 additions & 7 deletions modelscope_agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand All @@ -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)

Expand Down Expand Up @@ -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],
Expand Down
4 changes: 2 additions & 2 deletions modelscope_agent/agents/role_play.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)

Expand Down
6 changes: 4 additions & 2 deletions modelscope_agent/callbacks/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List
from typing import List, Optional


class BaseCallback:
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion modelscope_agent/memory/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 9 additions & 0 deletions modelscope_agent/rag/emb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
33 changes: 22 additions & 11 deletions modelscope_agent/tools/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 = {
Expand Down Expand Up @@ -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'))

0 comments on commit 955f860

Please sign in to comment.