# Copyright 2023 The Qwen team, Alibaba Group. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. """A multi-agent cooperation example implemented by router and assistant""" import os from typing import Optional from qwen_agent.agents import Assistant, ReActChat, Router from qwen_agent.gui import WebUI ROOT_RESOURCE = os.path.join(os.path.dirname(__file__), 'resource') def init_agent_service(): # settings llm_cfg = {'model': 'qwen-max'} llm_cfg_vl = {'model': 'qwen-vl-max'} tools = ['image_gen', 'code_interpreter'] # Define a vl agent bot_vl = Assistant(llm=llm_cfg_vl, name='多模态助手', description='可以理解图像内容。') # Define a tool agent bot_tool = ReActChat( llm=llm_cfg, name='工具助手', description='可以使用画图工具和运行代码来解决问题', function_list=tools, ) # Define a router (simultaneously serving as a text agent) bot = Router( llm=llm_cfg, agents=[bot_vl, bot_tool], ) return bot def test( query: str = 'hello', image: str = 'https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg', file: Optional[str] = os.path.join(ROOT_RESOURCE, 'poem.pdf'), ): # Define the agent bot = init_agent_service() # Chat messages = [] if not image and not file: messages.append({'role': 'user', 'content': query}) else: messages.append({'role': 'user', 'content': [{'text': query}]}) if image: messages[-1]['content'].append({'image': image}) if file: messages[-1]['content'].append({'file': file}) for response in bot.run(messages): print('bot response:', response) def app_tui(): # Define the agent bot = init_agent_service() # Chat messages = [] while True: query = input('user question: ') # Image example: https://dashscope.oss-cn-beijing.aliyuncs.com/images/dog_and_girl.jpeg image = input('image url (press enter if no image): ') # File example: resource/poem.pdf file = input('file url (press enter if no file): ').strip() if not query: print('user question cannot be empty!') continue if not image and not file: messages.append({'role': 'user', 'content': query}) else: messages.append({'role': 'user', 'content': [{'text': query}]}) if image: messages[-1]['content'].append({'image': image}) if file: messages[-1]['content'].append({'file': file}) response = [] for response in bot.run(messages): print('bot response:', response) messages.extend(response) def app_gui(): bot = init_agent_service() chatbot_config = { 'verbose': True, } WebUI(bot, chatbot_config=chatbot_config).run() if __name__ == '__main__': # test() # app_tui() app_gui()