From ee77720837527430f8e12078c0c80dcec564afc9 Mon Sep 17 00:00:00 2001 From: Zhicheng Zhang Date: Mon, 24 Jun 2024 15:40:55 +0800 Subject: [PATCH] agentfabric gradio setting with tool hub (#503) --- apps/agentfabric/app.py | 25 ++++++++++++++++--------- apps/agentfabric/appBot.py | 4 +++- apps/agentfabric/requirements.txt | 2 +- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/apps/agentfabric/app.py b/apps/agentfabric/app.py index c3c4044b1..ccebcf2db 100644 --- a/apps/agentfabric/app.py +++ b/apps/agentfabric/app.py @@ -28,10 +28,14 @@ from user_core import init_user_chatbot_agent -def init_user(uuid_str, state): +def init_user(uuid_str, state, _user_token=None): try: + allow_tool_hub = False # modelscope-agent < 0.6.4 will be false to disable tool hub + in_ms_studio = os.getenv('MODELSCOPE_ENVIRONMENT', + 'None') == 'studio' and allow_tool_hub seed = state.get('session_seed', random.randint(0, 1000000000)) - user_agent, user_memory = init_user_chatbot_agent(uuid_str) + user_agent, user_memory = init_user_chatbot_agent( + uuid_str, use_tool_api=in_ms_studio, user_token=_user_token) user_agent.seed = seed state['user_agent'] = user_agent state['user_memory'] = user_memory @@ -94,7 +98,7 @@ def delete(state): # 创建 Gradio 界面 demo = gr.Blocks(css='assets/app.css') with demo: - + user_token = gr.Textbox(label='modelscope_agent_tool_token', visible=False) uuid_str = gr.Textbox(label='modelscope_uuid', visible=False) draw_seed = random.randint(0, 1000000000) state = gr.State({'session_seed': draw_seed}, delete_callback=delete) @@ -591,7 +595,7 @@ def process_configuration(uuid_str, bot_avatar, name, description, ]) # 配置 "Preview" 的消息发送功能 - def preview_send_message(chatbot, input, _state, uuid_str): + def preview_send_message(chatbot, input, _state, uuid_str, _user_token): # 将发送的消息添加到聊天历史 # _uuid_str = check_uuid(uuid_str) user_agent = _state['user_agent'] @@ -634,7 +638,8 @@ def preview_send_message(chatbot, input, _state, uuid_str): history=history, ref_doc=ref_doc, append_files=append_files, - uuid_str=uuid_str): + uuid_str=uuid_str, + user_token=_user_token): # append_files=new_file_paths): # important! do not change this response += frame @@ -661,7 +666,7 @@ def preview_send_message(chatbot, input, _state, uuid_str): preview_chat_input.submit( preview_send_message, - inputs=[user_chatbot, preview_chat_input, state, uuid_str], + inputs=[user_chatbot, preview_chat_input, state, uuid_str, user_token], outputs=[user_chatbot, user_chat_bot_cover, preview_chat_input]) # configuration for publish @@ -775,14 +780,14 @@ def change_lang(language): knowledge_upload_button ]) - def init_all(uuid_str, _state): + def init_all(uuid_str, _state, _user_token): uuid_str = check_uuid(uuid_str) builder_cfg, model_cfg, tool_cfg, available_tool_list, _, _ = parse_configuration( uuid_str) ret = init_ui_config(uuid_str, _state, builder_cfg, model_cfg, tool_cfg) yield ret - init_user(uuid_str, _state) + init_user(uuid_str, _state, _user_token) init_builder(uuid_str, _state) yield { state: @@ -798,7 +803,9 @@ def init_all(uuid_str, _state): } demo.load( - init_all, inputs=[uuid_str, state], outputs=configure_updated_outputs) + init_all, + inputs=[uuid_str, state, user_token], + outputs=configure_updated_outputs) demo.queue() demo.launch(show_error=True, max_threads=10) diff --git a/apps/agentfabric/appBot.py b/apps/agentfabric/appBot.py index 1336fffbd..1c970dd7a 100644 --- a/apps/agentfabric/appBot.py +++ b/apps/agentfabric/appBot.py @@ -45,7 +45,9 @@ def check_uuid(uuid_str): def init_user(state, _user_token=None): try: - in_ms_studio = os.getenv('MODELSCOPE_ENVIRONMENT', 'None') == 'studio' + allow_tool_hub = False # modelscope-agent < 0.6.4 will be false to disable tool hub + in_ms_studio = os.getenv('MODELSCOPE_ENVIRONMENT', + 'None') == 'studio' and allow_tool_hub seed = state.get('session_seed', random.randint(0, 1000000000)) # use tool api in ms studio user_agent, user_memory = init_user_chatbot_agent( diff --git a/apps/agentfabric/requirements.txt b/apps/agentfabric/requirements.txt index 974962029..f22e5ec72 100644 --- a/apps/agentfabric/requirements.txt +++ b/apps/agentfabric/requirements.txt @@ -1,10 +1,10 @@ dashscope faiss-cpu gradio==4.36.1 -https://modelscope-agent.oss-cn-hangzhou.aliyuncs.com/releases/v0.6.2/modelscope_agent-0.6.2-py3-none-any.whl langchain markdown-cjk-spacing mdx_truly_sane_lists +modelscope-agent==0.6.3 modelscope_studio pymdown-extensions python-slugify