qwen3 中文文档 下载 App

Qwen3 Agent 工具调用能力 — 连接外部 API 执行多步任务

Qwen3 的 Agent 能力

Qwen3 相比前代强化了 Agent(智能体)能力——模型可以:

  1. 理解何时需要调用外部工具
  2. 生成正确的工具调用参数
  3. 解读工具返回结果,决定下一步行动
  4. 在思考模式下更精准地规划多步任务

官方声称 Qwen3 在开源模型的复杂 Agent 任务上表现领先。

Function Calling 基础

Function Calling 是主流 LLM 连接外部工具的标准方式:

  1. 在 API 请求中描述可用工具(名称、参数、描述)
  2. 模型决定是否调用某个工具
  3. 如果调用,模型返回工具名和参数(JSON 格式)
  4. 你的代码执行工具调用
  5. 把工具返回结果发回模型继续对话

DashScope SDK 示例

import os
import dashscope
from dashscope import Generation

dashscope.api_key = os.getenv("DASHSCOPE_API_KEY")

# 定义可用工具
tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "获取指定城市的天气",
            "parameters": {
                "type": "object",
                "properties": {
                    "city": {
                        "type": "string",
                        "description": "城市名称"
                    }
                },
                "required": ["city"]
            }
        }
    }
]

# 带工具调用的对话
messages = [
    {"role": "user", "content": "北京今天天气怎么样?"}
]

response = Generation.call(
    model="qwen3-plus",
    messages=messages,
    tools=tools,
    result_format="message"
)

# 模型可能返回工具调用意图
msg = response.output.choices[0].message
if msg.tool_calls:
    # 模型决定调用 get_weather
    tool_call = msg.tool_calls[0]
    print(f"调用工具: {tool_call.function.name}")
    print(f"参数: {tool_call.function.arguments}")

OpenAI 兼容接口

from openai import OpenAI

client = OpenAI(
    api_key="YOUR_DASHSCOPE_API_KEY",
    base_url="https://dashscope.aliyuncs.com/compatible-mode/v1"
)

tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "获取指定城市的天气",
            "parameters": {
                "type": "object",
                "properties": {
                    "city": {"type": "string", "description": "城市名称"}
                },
                "required": ["city"]
            }
        }
    }
]

response = client.chat.completions.create(
    model="qwen3-plus",
    messages=[{"role": "user", "content": "北京今天天气怎么样?"}],
    tools=tools
)

# 处理 tool_calls
message = response.choices[0].message
if message.tool_calls:
    for tc in message.tool_calls:
        print(f"工具: {tc.function.name}, 参数: {tc.function.arguments}")

多步 Agent 循环

一个完整的 Agent 应用需要一个循环:

def run_agent(user_prompt, tools):
    messages = [{"role": "user", "content": user_prompt}]
    
    for step in range(10):  # 最多 10 步
        response = client.chat.completions.create(
            model="qwen3-plus",
            messages=messages,
            tools=tools,
            extra_body={"enable_thinking": True}  # Agent 任务建议开思考
        )
        
        message = response.choices[0].message
        
        # 如果模型不再调用工具,说明任务完成
        if not message.tool_calls:
            return message.content
        
        # 执行所有工具调用
        for tc in message.tool_calls:
            tool_result = execute_tool(tc.function.name, tc.function.arguments)
            messages.append({
                "role": "tool",
                "tool_call_id": tc.id,
                "content": str(tool_result)
            })
        
        # 把 assistant 的回复也加进去
        messages.append({
            "role": "assistant",
            "content": message.content,
            "tool_calls": message.tool_calls
        })
    
    return "超过最大步数"

思考模式下的 Agent

开启思考模式后,模型会:

  1. 在思考链中先规划需要调用哪些工具
  2. 思考每个工具的参数应该是什么
  3. 思考执行顺序
  4. 根据中间结果调整后续计划

这比直接让模型盲调工具效果好很多,尤其是复杂的多步任务。

常见 Agent 框架

如果你不想自己写 Agent 循环,可以用现成框架:

框架说明
LangChain最流行,生态丰富
LlamaIndex侧重 RAG + Agent
AutoGen微软,多 Agent 协作
CrewAI角色型多 Agent
通义灵码阿里巴巴企业级 Agent 平台

相关阅读