Qwen-Agent¶
Qwen-Agent 是一个基于 Qwen 的指令跟随、工具使用、计划和记忆能力来开发 LLM 应用程序的框架。
本教程展示基于 Qwen-Agent 快速体验 Qwen3 智能体能力的流程。更多信息请参考 Qwen-Agent 仓库。
安装¶
从 PyPI 安装 Qwen-Agent 的稳定版本:
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.
开发您自己的智能体¶
Qwen3 在工具调用能力方面表现出色。Qwen-Agent 内部封装了工具调用模板和工具调用解析器,大大降低了编码复杂性。
要定义可用的工具,您可以使用 MCP 配置文件,使用 Qwen-Agent 的集成工具,或者自行集成其他工具。
import os
from qwen_agent.agents import Assistant
# Define LLM
llm_cfg = {
# Use a custom endpoint compatible with OpenAI API by vLLM/SGLang:
'model': 'Qwen/Qwen3-32B',
'model_server': 'http://localhost:8000/v1', # api_base
'api_key': 'EMPTY',
# 'generate_cfg': {
# # When using vLLM/SGLang OAI API, pass the parameter of whether to enable thinking mode in this way
# 'extra_body': {
# 'chat_template_kwargs': {'enable_thinking': False}
# },
#
# # Add: When the content is `<think>this is the thought</think>this is the answer`
# # Do not add: When the response has been separated by reasoning_content and content
# # This parameter will affect the parsing strategy of tool call
# # 'thought_in_content': True,
# },
}
# llm_cfg = {
# # Use the model service provided by DashScope:
# 'model': 'qwen3-235b-a22b',
# 'model_type': 'qwen_dashscope',
#
# # 'generate_cfg': {
# # # When using the Dash Scope API, pass the parameter of whether to enable thinking mode in this way
# # 'enable_thinking': False,
# # },
# }
# llm_cfg = {
# # Use the OpenAI-compatible model service provided by DashScope:
# 'model': 'qwen3-235b-a22b',
# 'model_server': 'https://dashscope.aliyuncs.com/compatible-mode/v1',
# 'api_key': os.getenv('DASHSCOPE_API_KEY'),
#
# # 'generate_cfg': {
# # # When using Dash Scope OAI API, pass the parameter of whether to enable thinking mode in this way
# # 'extra_body': {
# # 'enable_thinking': False
# # },
# # },
# }
# Define Tools
tools = [
{'mcpServers': { # You can specify the MCP configuration file
'time': {
'command': 'uvx',
'args': ['mcp-server-time', '--local-timezone=Asia/Shanghai']
},
"fetch": {
"command": "uvx",
"args": ["mcp-server-fetch"]
}
}
},
'code_interpreter', # Built-in tools
]
# Define Agent
bot = Assistant(llm=llm_cfg, function_list=tools)
# Streaming generation
messages = [{'role': 'user', 'content': 'https://qwenlm.github.io/blog/ Introduce the latest developments of Qwen'}]
for responses in bot.run(messages=messages):
pass
print(responses)
有关更详细的示例和 MCP 使用指南,请参阅 Qwen-Agent 仓库。