pip install -U "qwen-agent[gui,rag,code_interpreter,mcp]"# Or use `pip install -U qwen-agent` for the minimal requirements.# The optional requirements, specified in double brackets, are:# [gui] for Gradio-based GUI support;# [rag] for RAG support;# [code_interpreter] for Code Interpreter support;# [mcp] for MCP support.
または、ソースから最新の開発版をインストールできます:
git clone https://github.com/QwenLM/Qwen-Agent.git
cd Qwen-Agent
pip install -e ./"[gui,rag,code_interpreter,mcp]"# Or `pip install -e ./` for minimal requirements.
importpprintimporturllib.parseimportjson5fromqwen_agent.agentsimportAssistantfromqwen_agent.tools.baseimportBaseTool, register_toolfromqwen_agent.utils.output_beautifyimporttypewriter_print# Step 1 (Optional): Add a custom tool named `my_image_gen`.@register_tool('my_image_gen')classMyImageGen(BaseTool):
# The `description` tells the agent the functionality of this tool.description='AI painting (image generation) service, input text description, and return the image URL drawn based on text information.'# The `parameters` tell the agent what input parameters the tool has.parameters= [{
'name': 'prompt',
'type': 'string',
'description': 'Detailed description of the desired image content, in English',
'required': True
}]
defcall(self, params: str, **kwargs) ->str:
# `params` are the arguments generated by the LLM agent.prompt=json5.loads(params)['prompt']
prompt=urllib.parse.quote(prompt)
returnjson5.dumps(
{'image_url': f'https://image.pollinations.ai/prompt/{prompt}'},
ensure_ascii=False)
# Step 2: Configure the LLM you are using.llm_cfg= {
# Use the model service provided by DashScope:'model': 'qwen-max-latest',
'model_type': 'qwen_dashscope',
# 'api_key': 'YOUR_DASHSCOPE_API_KEY',# It will use the `DASHSCOPE_API_KEY' environment variable if 'api_key' is not set here.# Use a model service compatible with the OpenAI API, such as vLLM or Ollama:# 'model': 'Qwen2.5-7B-Instruct',# 'model_server': 'http://localhost:8000/v1', # base_url, also known as api_base# 'api_key': 'EMPTY',# (Optional) LLM hyperparameters for generation:'generate_cfg': {
'top_p': 0.8
}
}
# Step 3: Create an agent. Here we use the `Assistant` agent as an example, which is capable of using tools and reading files.system_instruction='''After receiving the user's request, you should:- first draw an image and obtain the image url,- then run code `request.get(image_url)` to download the image,- and finally select an image operation from the given document to process the image.Please show the image using `plt.show()`.'''tools= ['my_image_gen', 'code_interpreter'] # `code_interpreter` is a built-in tool for executing code. For configuration details, please refer to the FAQ.files= ['./examples/resource/doc.pdf'] # Give the bot a PDF file to read.bot=Assistant(llm=llm_cfg,
system_message=system_instruction,
function_list=tools,
files=files)
# Step 4: Run the agent as a chatbot.messages= [] # This stores the chat history.whileTrue:
# For example, enter the query "draw a dog and rotate it 90 degrees".query=input('\nuser query: ')
# Append the user query to the chat history.messages.append({'role': 'user', 'content': query})
response= []
response_plain_text=''print('bot response:')
forresponseinbot.run(messages=messages):
# Streaming output.response_plain_text=typewriter_print(response, response_plain_text)
# Append the bot responses to the chat history.messages.extend(response)
class Assistant などの組み込みエージェント実装を使用するほかに、class Agent から継承することで独自のエージェント実装を開発することもできます。
# Node.js (Download and install the latest version from the Node.js official website)
# uv 0.4.18 or higher (Check with uv --version)
# Git (Check with git --version)
# SQLite (Check with sqlite3 --version)
# For macOS users, you can install these components using Homebrew:
brew install uv git sqlite3
# For Windows users, you can install these components using winget:
winget install --id=astral-sh.uv -e
winget install git.git sqlite.sqlite
llm_cfg= {
# The model name being used:'model': 'qwen3-32b',
# The model service being used:'model_type': 'qwen_dashscope',
# If 'api_key' is not set here, it will default to reading the `DASHSCOPE_API_KEY` environment variable:'api_key': 'YOUR_DASHSCOPE_API_KEY',
# Using an OpenAI API compatible model service, such as vLLM or Ollama:# 'model': 'qwen3-32b',# 'model_server': 'http://localhost:8000/v1', # base_url, also known as api_base# 'api_key': 'EMPTY',# (Optional) LLM hyperparameters:'generate_cfg': {
# This parameter will affect the tool-call parsing logic. Default is False:# Set to True: when content is `<think>this is the thought</think>this is the answer`# Set to False: when response consists of reasoning_content and content# 'thought_in_content': True,# tool-call template: default is nous (recommended for qwen3):# 'fncall_prompt_type': 'nous'# Maximum input length, messages will be truncated if they exceed this length, please adjust according to model API:# 'max_input_tokens': 58000# Parameters that will be passed directly to the model API, such as top_p, enable_thinking, etc., according to the API specifications:# 'top_p': 0.8# Using the API's native tool call interface# 'use_raw_api': True,
}
}