Skip to content

Commit

Permalink
openapi update
Browse files Browse the repository at this point in the history
  • Loading branch information
zzhangpurdue committed Sep 26, 2024
1 parent 1b37fca commit 8921c9b
Show file tree
Hide file tree
Showing 10 changed files with 901 additions and 230 deletions.
61 changes: 33 additions & 28 deletions apps/agentfabric/config_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import traceback

import json
from modelscope_agent.tools.openapi_plugin import openapi_schema_convert
from modelscope_agent.tools.utils.openapi_utils import openapi_schema_convert
from modelscope_agent.utils.logger import agent_logger as logger

from modelscope.utils.config import Config
Expand Down Expand Up @@ -127,7 +127,7 @@ def save_avatar_image(image_path, uuid_str=''):
return bot_avatar, bot_avatar_path


def parse_configuration(uuid_str=''):
def parse_configuration(uuid_str='', use_tool_api=False):
"""parse configuration
Args:
Expand Down Expand Up @@ -167,33 +167,38 @@ def parse_configuration(uuid_str=''):
if value['use']:
available_tool_list.append(key)

openapi_plugin_file = get_user_openapi_plugin_cfg_file(uuid_str)
plugin_cfg = {}
available_plugin_list = []
openapi_plugin_cfg_file_temp = './config/openapi_plugin_config.json'
if os.path.exists(openapi_plugin_file):
openapi_plugin_cfg = Config.from_file(openapi_plugin_file)
try:
config_dict = openapi_schema_convert(
schema=openapi_plugin_cfg.schema,
auth=openapi_plugin_cfg.auth.to_dict())
plugin_cfg = Config(config_dict)
for name, config in config_dict.items():
available_plugin_list.append(name)
except Exception as e:
logger.query_error(
uuid=uuid_str,
error=str(e),
content={
'error_traceback':
traceback.format_exc(),
'error_details':
'The format of the plugin config file is incorrect.'
})
elif not os.path.exists(openapi_plugin_file):
if os.path.exists(openapi_plugin_cfg_file_temp):
os.makedirs(os.path.dirname(openapi_plugin_file), exist_ok=True)
if openapi_plugin_cfg_file_temp != openapi_plugin_file:
shutil.copy(openapi_plugin_cfg_file_temp, openapi_plugin_file)
if use_tool_api:
available_plugin_list = builder_cfg.openapi_list
else:
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):
openapi_plugin_cfg = Config.from_file(openapi_plugin_file)
try:
config_dict = openapi_schema_convert(
schema=openapi_plugin_cfg.schema,
auth=openapi_plugin_cfg.auth.to_dict())
plugin_cfg = Config(config_dict)
for name, config in config_dict.items():
available_plugin_list.append(name)
except Exception as e:
logger.query_error(
uuid=uuid_str,
error=str(e),
details={
'error_traceback':
traceback.format_exc(),
'error_details':
'The format of the plugin config file is incorrect.'
})
elif not os.path.exists(openapi_plugin_file):
if os.path.exists(openapi_plugin_cfg_file_temp):
os.makedirs(
os.path.dirname(openapi_plugin_file), exist_ok=True)
if openapi_plugin_cfg_file_temp != openapi_plugin_file:
shutil.copy(openapi_plugin_cfg_file_temp,
openapi_plugin_file)

return builder_cfg, model_cfg, tool_cfg, available_tool_list, plugin_cfg, available_plugin_list
3 changes: 0 additions & 3 deletions apps/agentfabric/server_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,6 @@ def get_user_bot(
user_agent = self.user_bots[unique_id]
if renew or user_agent is None:
logger.info(f'init_user_chatbot_agent: {builder_id} {session}')

builder_cfg, _, tool_cfg, _, _, _ = parse_configuration(builder_id)

user_agent = init_user_chatbot_agent(
builder_id, session, use_tool_api=True, user_token=user_token)
self.user_bots[unique_id] = user_agent
Expand Down
12 changes: 7 additions & 5 deletions apps/agentfabric/user_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ def init_user_chatbot_agent(uuid_str='',
session='default',
use_tool_api=False,
user_token=None):
builder_cfg, model_cfg, tool_cfg, _, plugin_cfg, _ = parse_configuration(
uuid_str)
builder_cfg, model_cfg, tool_cfg, _, openapi_plugin_cfg, openapi_plugin_list = parse_configuration(
uuid_str, use_tool_api)
# set top_p and stop_words for role play
if 'generate_cfg' not in model_cfg[builder_cfg.model]:
model_cfg[builder_cfg.model]['generate_cfg'] = dict()
Expand All @@ -26,8 +26,10 @@ def init_user_chatbot_agent(uuid_str='',

# update function_list
function_list = parse_tool_cfg(tool_cfg)
function_list = add_openapi_plugin_to_additional_tool(
plugin_cfg, function_list)

if not use_tool_api:
function_list = add_openapi_plugin_to_additional_tool(
openapi_plugin_cfg, function_list)

# build model
logger.query_info(
Expand All @@ -50,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)

# build memory
preview_history_dir = get_user_preview_history_dir(uuid_str, session)
Expand Down
43 changes: 39 additions & 4 deletions modelscope_agent/agent.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import copy
import os
from abc import ABC, abstractmethod
from functools import wraps
Expand All @@ -7,7 +8,7 @@
from modelscope_agent.llm import get_chat_model
from modelscope_agent.llm.base import BaseChatModel
from modelscope_agent.tools.base import (TOOL_REGISTRY, BaseTool,
ToolServiceProxy)
OpenapiServiceProxy, ToolServiceProxy)
from modelscope_agent.utils.utils import has_chinese_chars


Expand Down Expand Up @@ -51,7 +52,9 @@ def __init__(self,
description: Optional[str] = None,
instruction: Union[str, dict] = None,
use_tool_api: bool = False,
callbacks=[],
callbacks: list = None,
openapi_list_for_remote: Optional[List[Union[str,
Dict]]] = None,
**kwargs):
"""
init tools/llm/instruction for one agent
Expand All @@ -68,6 +71,8 @@ def __init__(self,
description: the description of agent, which is used for multi_agent
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
kwargs: other potential parameters
"""
if isinstance(llm, Dict):
Expand All @@ -84,6 +89,12 @@ def __init__(self,
for function in function_list:
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:
self._register_openapi_for_remote_calling(
openapi_name, **kwargs)

self.storage_path = storage_path
self.mem = None
self.name = name
Expand Down Expand Up @@ -129,6 +140,8 @@ def _call_tool(self, tool_list: list, **kwargs):
# version < 0.6.6 only one tool is in the tool_list
tool_name = tool_list[0]['name']
tool_args = tool_list[0]['arguments']
# for openapi tool only
kwargs['tool_name'] = tool_name
self.callback_manager.on_tool_start(tool_name, tool_args)
try:
result = self.function_map[tool_name].call(tool_args, **kwargs)
Expand All @@ -142,6 +155,28 @@ def _call_tool(self, tool_list: list, **kwargs):
self.callback_manager.on_tool_end(tool_name, result)
return result

def _register_openapi_for_remote_calling(self, openapi: Union[str, Dict],
**kwargs):
"""
Instantiate the openapi the will running remote on
Args:
openapi: the remote openapi schema name or the json schema itself
**kwargs:
Returns:
"""
openapi_instance = OpenapiServiceProxy(openapi, **kwargs)
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
self.function_map[tool_name] = openapi_instance

def _register_tool(self,
tool: Union[str, Dict],
tenant_id: str = 'default',
Expand All @@ -165,8 +200,8 @@ def _register_tool(self,
tool_cfg = tool[tool_name]
if tool_name not in TOOL_REGISTRY and not self.use_tool_api:
raise NotImplementedError
if tool not in self.function_list:
self.function_list.append(tool)
if tool_name not in self.function_list:
self.function_list.append(tool_name)

try:
tool_class_with_tenant = TOOL_REGISTRY[tool_name]
Expand Down
13 changes: 11 additions & 2 deletions modelscope_agent/agents/role_play.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,18 @@ def __init__(self,
name: Optional[str] = None,
description: Optional[str] = None,
instruction: Union[str, dict] = None,
openapi_list_for_remote: Optional[List] = None,
**kwargs):
Agent.__init__(self, function_list, llm, storage_path, name,
description, instruction, **kwargs)
Agent.__init__(
self,
function_list,
llm,
storage_path,
name,
description,
instruction,
openapi_list_for_remote=openapi_list_for_remote,
**kwargs)
AgentEnvMixin.__init__(self, **kwargs)

def _prepare_tool_system(self,
Expand Down
Loading

0 comments on commit 8921c9b

Please sign in to comment.