Skip to content

Commit

Permalink
agentfabric gradio setting with tool hub (#503)
Browse files Browse the repository at this point in the history
  • Loading branch information
zzhangpurdue authored Jun 24, 2024
1 parent 15521d1 commit ee77720
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
25 changes: 16 additions & 9 deletions apps/agentfabric/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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']
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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)
4 changes: 3 additions & 1 deletion apps/agentfabric/appBot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion apps/agentfabric/requirements.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit ee77720

Please sign in to comment.