OpenAI Responses接口兼容

更新时间:
复制为 MD 格式

阿里云百炼的通义千问模型支持 OpenAI 兼容 Responses 接口。作为Chat Completions API的演进版本,Responses API能够以更简洁的方式提供智能体原生功能。

相较于OpenAI Chat Completions API 的优势:

  • 内置工具:内置联网搜索、网页抓取和代码解释器工具。建议同时开启这些工具,以在处理复杂任务时获得最佳效果,详情参考调用内置工具

  • 更灵活的输入:支持直接传入字符串作为模型输入,也兼容 Chat 格式的消息数组。

  • 简化上下文管理:通过传递上一轮响应的 previous_response_id,无需手动构建完整的消息历史数组。

输入输出参数说明请参考OpenAI Responses API参考

前提条件

您需要先获取API Key配置API Key到环境变量。若通过 OpenAI SDK 进行调用,需要安装SDK

支持的模型

当前仅支持qwen3-max-2026-01-23

服务地址

当前仅支持华北2(北京)地域。

华北2(北京)

SDK 调用配置的base_urlhttps://dashscope.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1

HTTP 请求地址:POST https://dashscope.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1/responses

新加坡

SDK 调用配置的base_urlhttps://dashscope-intl.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1

HTTP 请求地址:POST https://dashscope-intl.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1/responses

代码示例

基础调用

最简单的调用方式,发送一条消息并获取模型回复。

Python

import os
from openai import OpenAI

client = OpenAI(
    # 若没有配置环境变量,请用百炼 API Key 将下行替换为:api_key="sk-xxx"
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    base_url="https://dashscope.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1",
)

response = client.responses.create(
    model="qwen3-max-2026-01-23",
    input="你好,请用一句话介绍你自己。"
)

# 获取模型回复
# print(response.model_dump_json())
print(response.output_text)

Node.js

import OpenAI from "openai";

const openai = new OpenAI({
    // 若没有配置环境变量,请用百炼 API Key 将下行替换为:apiKey: "sk-xxx"
    apiKey: process.env.DASHSCOPE_API_KEY,
    baseURL: "https://dashscope.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1"
});

async function main() {
    const response = await openai.responses.create({
        model: "qwen3-max-2026-01-23",
        input: "你好,请用一句话介绍你自己。"
    });

    // 获取模型回复
    console.log(response.output_text);
}

main();

curl

curl -X POST https://dashscope.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1/responses \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "qwen3-max-2026-01-23",
    "input": "你好,请用一句话介绍你自己。"
}'

响应示例

以下为API返回的完整响应。
{
  "id": "b8104cd2-ee57-903d-aae0-93d99254axxx",
  "created_at": 1769084048.0,
  "model": "qwen3-max-2026-01-23",
  "object": "response",
  "status": "completed",
  "output": [
    {
      "id": "msg_1eb85c78-a627-4c7e-aac6-22235c173xxx",
      "type": "message",
      "role": "assistant",
      "status": "completed",
      "content": [
        {
          "type": "output_text",
          "text": "你好!我是通义千问(Qwen),由通义实验室研发的超大规模语言模型,能够回答问题、创作文字、编程、表达观点等,致力于为你提供准确、有用和友好的帮助。",
          "annotations": []
        }
      ]
    }
  ],
  "usage": {
    "input_tokens": 39,
    "output_tokens": 46,
    "total_tokens": 85,
    "input_tokens_details": {
      "cached_tokens": 0
    },
    "output_tokens_details": {
      "reasoning_tokens": 0
    }
  }
}

多轮对话

通过 previous_response_id 参数自动关联上下文,无需手动构建消息历史,当前响应id有效期为7天。

previous_response_id 应传入上一轮响应中的顶层 idf0dbb153-117f-9bbf-8176-5284b47f3xxx,UUID格式),而不是 output 数组内消息的 idmsg_56c860c4-3ad8-4a96-8553-d2f94c259xxx)。

Python

import os
from openai import OpenAI

client = OpenAI(
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    base_url="https://dashscope.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1",
)

# 第一轮对话
response1 = client.responses.create(
    model="qwen3-max-2026-01-23",
    input="我的名字是张三,请记住。"
)
print(f"第一轮回复: {response1.output_text}")

# 第二轮对话 - 使用 previous_response_id 关联上下文,响应id有效期为7天
response2 = client.responses.create(
    model="qwen3-max-2026-01-23",
    input="你还记得我的名字吗?",
    previous_response_id=response1.id
)
print(f"第二轮回复: {response2.output_text}")

Node.js

import OpenAI from "openai";

const openai = new OpenAI({
    apiKey: process.env.DASHSCOPE_API_KEY,
    baseURL: "https://dashscope.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1"
});

async function main() {
    // 第一轮对话
    const response1 = await openai.responses.create({
        model: "qwen3-max-2026-01-23",
        input: "我的名字是张三,请记住。"
    });
    console.log(`第一轮回复: ${response1.output_text}`);

    // 第二轮对话 - 使用 previous_response_id 关联上下文,响应id有效期为7天
    const response2 = await openai.responses.create({
        model: "qwen3-max-2026-01-23",
        input: "你还记得我的名字吗?",
        previous_response_id: response1.id
    });
    console.log(`第二轮回复: ${response2.output_text}`);
}

main();

curl

# 第一轮对话
curl -X POST https://dashscope.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1/responses \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "qwen3-max-2026-01-23",
    "input": "我的名字是张三,请记住。"
}'

# 第二轮对话 - 使用上一轮返回的 id 作为 previous_response_id
curl -X POST https://dashscope.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1/responses \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "qwen3-max-2026-01-23",
    "input": "你还记得我的名字吗?",
    "previous_response_id": "第一轮返回的响应id"
}'

第二轮对话响应示例

{
  "id": "f0dbb153-117f-9bbf-8176-5284b47f3xxx",
  "created_at": 1769169951.0,
  "model": "qwen3-max-2026-01-23",
  "object": "response",
  "status": "completed",
  "output": [
    {
      "id": "msg_56c860c4-3ad8-4a96-8553-d2f94c259xxx",
      "type": "message",
      "role": "assistant",
      "status": "completed",
      "content": [
        {
          "type": "output_text",
          "text": "当然记得,你的名字是张三!",
          "annotations": []
        }
      ]
    }
  ],
  "usage": {
    "input_tokens": 73,
    "output_tokens": 10,
    "total_tokens": 83,
    "input_tokens_details": {
      "cached_tokens": 0
    },
    "output_tokens_details": {
      "reasoning_tokens": 0
    }
  }
}

说明:第二轮对话的 input_tokens 为 73,包含了第一轮的上下文,模型成功记住了名字"张三"。

流式输出

通过流式输出实时接收模型生成的内容,适合长文本生成场景。

Python

import os
from openai import OpenAI

client = OpenAI(
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    base_url="https://dashscope.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1",
)

stream = client.responses.create(
    model="qwen3-max-2026-01-23",
    input="请简单介绍一下人工智能。",
    stream=True
)

print("开始接收流式输出:")
for event in stream:
    # print(event.model_dump_json())  # 取消注释以查看原始事件响应
    if event.type == 'response.output_text.delta':
        # 实时打印增量文本
        print(event.delta, end='', flush=True)
    elif event.type == 'response.completed':
        print("\n流式输出完成")
        print(f"总 Token 数: {event.response.usage.total_tokens}")

Node.js

import OpenAI from "openai";

const openai = new OpenAI({
    apiKey: process.env.DASHSCOPE_API_KEY,
    baseURL: "https://dashscope.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1"
});

async function main() {
    const stream = await openai.responses.create({
        model: "qwen3-max-2026-01-23",
        input: "请简单介绍一下人工智能。",
        stream: true
    });

    console.log("开始接收流式输出:");
    for await (const event of stream) {
        // console.log(JSON.stringify(event));  // 取消注释以查看原始事件响应
        if (event.type === 'response.output_text.delta') {
            process.stdout.write(event.delta);
        } else if (event.type === 'response.completed') {
            console.log("\n流式输出完成");
            console.log(`总 Token 数: ${event.response.usage.total_tokens}`);
        }
    }
}

main();

curl

curl -X POST https://dashscope.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1/responses \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "qwen3-max-2026-01-23",
    "input": "请简单介绍一下人工智能。",
    "stream": true
}'

响应示例

{"response":{"id":"47a71e7d-868c-4204-9693-ef8ff9058xxx","created_at":1769417481.0,"error":null,"incomplete_details":null,"instructions":null,"metadata":null,"model":"","object":"response","output":[],"parallel_tool_calls":false,"temperature":null,"tool_choice":"auto","tools":[],"top_p":null,"background":null,"completed_at":null,"conversation":null,"max_output_tokens":null,"max_tool_calls":null,"previous_response_id":null,"prompt":null,"prompt_cache_key":null,"prompt_cache_retention":null,"reasoning":null,"safety_identifier":null,"service_tier":null,"status":"queued","text":null,"top_logprobs":null,"truncation":null,"usage":null,"user":null},"sequence_number":0,"type":"response.created"}
{"response":{"id":"47a71e7d-868c-4204-9693-ef8ff9058xxx","created_at":1769417481.0,"error":null,"incomplete_details":null,"instructions":null,"metadata":null,"model":"","object":"response","output":[],"parallel_tool_calls":false,"temperature":null,"tool_choice":"auto","tools":[],"top_p":null,"background":null,"completed_at":null,"conversation":null,"max_output_tokens":null,"max_tool_calls":null,"previous_response_id":null,"prompt":null,"prompt_cache_key":null,"prompt_cache_retention":null,"reasoning":null,"safety_identifier":null,"service_tier":null,"status":"in_progress","text":null,"top_logprobs":null,"truncation":null,"usage":null,"user":null},"sequence_number":1,"type":"response.in_progress"}
{"item":{"id":"msg_16db29d6-c1d3-47d7-9177-0fba81964xxx","content":[],"role":"assistant","status":"in_progress","type":"message"},"output_index":0,"sequence_number":2,"type":"response.output_item.added"}
{"content_index":0,"item_id":"msg_16db29d6-c1d3-47d7-9177-0fba81964xxx","output_index":0,"part":{"annotations":[],"text":"","type":"output_text","logprobs":null},"sequence_number":3,"type":"response.content_part.added"}
{"content_index":0,"delta":"人工智能","item_id":"msg_16db29d6-c1d3-47d7-9177-0fba81964xxx","logprobs":[],"output_index":0,"sequence_number":4,"type":"response.output_text.delta"}
{"content_index":0,"delta":"(Art","item_id":"msg_16db29d6-c1d3-47d7-9177-0fba81964xxx","logprobs":[],"output_index":0,"sequence_number":5,"type":"response.output_text.delta"}
{"content_index":0,"delta":"ificial Intelligence,","item_id":"msg_16db29d6-c1d3-47d7-9177-0fba81964xxx","logprobs":[],"output_index":0,"sequence_number":6,"type":"response.output_text.delta"}
{"content_index":0,"delta":"简称 AI)","item_id":"msg_16db29d6-c1d3-47d7-9177-0fba81964xxx","logprobs":[],"output_index":0,"sequence_number":7,"type":"response.output_text.delta"}
... (省略中间事件) ...
{"content_index":0,"delta":"领域,正在深刻改变我们的","item_id":"msg_16db29d6-c1d3-47d7-9177-0fba81964xxx","logprobs":[],"output_index":0,"sequence_number":38,"type":"response.output_text.delta"}
{"content_index":0,"delta":"生活和工作方式","item_id":"msg_16db29d6-c1d3-47d7-9177-0fba81964xxx","logprobs":[],"output_index":0,"sequence_number":39,"type":"response.output_text.delta"}
{"content_index":0,"delta":"。","item_id":"msg_16db29d6-c1d3-47d7-9177-0fba81964xxx","logprobs":[],"output_index":0,"sequence_number":40,"type":"response.output_text.delta"}
{"content_index":0,"item_id":"msg_16db29d6-c1d3-47d7-9177-0fba81964xxx","logprobs":[],"output_index":0,"sequence_number":41,"text":"人工智能(Artificial Intelligence,简称 AI)是指由计算机系统模拟人类智能行为的技术和科学。xxxx","type":"response.output_text.done"}
{"content_index":0,"item_id":"msg_16db29d6-c1d3-47d7-9177-0fba81964xxx","output_index":0,"part":{"annotations":[],"text":"人工智能(Artificial Intelligence,简称 AI)是指由计算机系统模拟人类智能行为的技术和科学。xxx","type":"output_text","logprobs":null},"sequence_number":42,"type":"response.content_part.done"}
{"item":{"id":"msg_16db29d6-c1d3-47d7-9177-0fba81964xxx","content":[{"annotations":[],"text":"人工智能(Artificial Intelligence,简称 AI)是指由计算机系统模拟人类智能行为的技术和科学。它旨在让机器能够执行通常需要人类智能才能完成的任务,例如:\n\n- **学习**(如通过数据训练模型)  \n- **推理**(如逻辑判断和问题求解)  \n- **感知**(如识别图像、语音或文字)  \n- **理解语言**(如自然语言处理)  \n- **决策**(如在复杂环境中做出最优选择)\n\n人工智能可分为**弱人工智能**(专注于特定任务,如语音助手、推荐系统)和**强人工智能**(具备类似人类的通用智能,目前尚未实现)。\n\n当前,AI 已广泛应用于医疗、金融、交通、教育、娱乐等多个领域,正在深刻改变我们的生活和工作方式。","type":"output_text","logprobs":null}],"role":"assistant","status":"completed","type":"message"},"output_index":0,"sequence_number":43,"type":"response.output_item.done"}
{"response":{"id":"47a71e7d-868c-4204-9693-ef8ff9058xxx","created_at":1769417481.0,"error":null,"incomplete_details":null,"instructions":null,"metadata":null,"model":"qwen3-max-2026-01-23","object":"response","output":[{"id":"msg_16db29d6-c1d3-47d7-9177-0fba81964xxx","content":[{"annotations":[],"text":"人工智能(Artificial Intelligence,简称 AI)是xxxxxx","type":"output_text","logprobs":null}],"role":"assistant","status":"completed","type":"message"}],"parallel_tool_calls":false,"temperature":null,"tool_choice":"auto","tools":[],"top_p":null,"background":null,"completed_at":null,"conversation":null,"max_output_tokens":null,"max_tool_calls":null,"previous_response_id":null,"prompt":null,"prompt_cache_key":null,"prompt_cache_retention":null,"reasoning":null,"safety_identifier":null,"service_tier":null,"status":"completed","text":null,"top_logprobs":null,"truncation":null,"usage":{"input_tokens":37,"input_tokens_details":{"cached_tokens":0},"output_tokens":166,"output_tokens_details":{"reasoning_tokens":0},"total_tokens":203},"user":null},"sequence_number":44,"type":"response.completed"}

调用内置工具

建议同时开启内置工具,以在处理复杂任务时获得最佳效果,当前网页抓取与代码解释器工具限时免费,详情请参见联网搜索网页抓取代码解释器

Python

import os
from openai import OpenAI

client = OpenAI(
    api_key=os.getenv("DASHSCOPE_API_KEY"),
    base_url="https://dashscope.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1",
)

response = client.responses.create(
    model="qwen3-max-2026-01-23",
    input="帮我找一下阿里云官网,并提取首页的关键信息",
    # 建议同时开启内置工具以取得最佳效果
    tools=[
        {"type": "web_search"},
        {"type": "code_interpreter"},
        {"type": "web_extractor"}
    ],
    extra_body={"enable_thinking": True}
)

# 取消以下注释查看中间过程输出
# print(response.output)
print(response.output_text)

Node.js

import OpenAI from "openai";

const openai = new OpenAI({
    apiKey: process.env.DASHSCOPE_API_KEY,
    baseURL: "https://dashscope.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1"
});

async function main() {
    const response = await openai.responses.create({
        model: "qwen3-max-2026-01-23",
        input: "帮我找一下阿里云官网,并提取首页的关键信息",
        tools: [
            { type: "web_search" },
            { type: "code_interpreter" },
            { type: "web_extractor" }
        ],
        enable_thinking: true
    });

    for (const item of response.output) {
        if (item.type === "reasoning") {
            console.log("模型正在思考...");
        } else if (item.type === "web_search_call") {
            console.log(`搜索查询: ${item.action.query}`);
        } else if (item.type === "web_extractor_call") {
            console.log("正在抽取网页内容...");
        } else if (item.type === "message") {
            console.log(`回复内容: ${item.content[0].text}`);
        }
    }
}

main();

curl

curl -X POST https://dashscope.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1/responses \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "qwen3-max-2026-01-23",
    "input": "帮我找一下阿里云官网,并提取首页的关键信息",
    "tools": [
        {
            "type": "web_search"
        },
        {
            "type": "code_interpreter"
        },
        {
            "type": "web_extractor"
        }
    ],
    "enable_thinking": true
}'

响应示例

{
    "id": "69258b21-5099-9d09-92e8-8492b1955xxx",
    "object": "response",
    "status": "completed",
    "output": [
        {
            "type": "reasoning",
            "summary": [
                {
                    "type": "summary_text",
                    "text": "用户要求找阿里云官网并提取信息..."
                }
            ]
        },
        {
            "type": "web_search_call",
            "status": "completed",
            "action": {
                "query": "阿里云官网",
                "type": "search",
                "sources": [
                    {
                        "type": "url",
                        "url": "https://cn.aliyun.com/"
                    },
                    {
                        "type": "url",
                        "url": "https://www.alibabacloud.com/zh"
                    }
                ]
            }
        },
        {
            "type": "reasoning",
            "summary": [
                {
                    "type": "summary_text",
                    "text": "搜索结果显示阿里云官网URL..."
                }
            ]
        },
        {
            "type": "web_extractor_call",
            "status": "completed",
            "goal": "提取阿里云官网首页的关键信息",
            "output": "通义大模型、完整产品体系、AI解决方案...",
            "urls": [
                "https://cn.aliyun.com/"
            ]
        },
        {
            "type": "message",
            "role": "assistant",
            "status": "completed",
            "content": [
                {
                    "type": "output_text",
                    "text": "阿里云官网关键信息:通义大模型,云计算服务..."
                }
            ]
        }
    ],
    "usage": {
        "input_tokens": 40836,
        "output_tokens": 2106,
        "total_tokens": 42942,
        "output_tokens_details": {
            "reasoning_tokens": 677
        },
        "x_tools": {
            "web_extractor": {
                "count": 1
            },
            "web_search": {
                "count": 1
            }
        }
    }
}

从 Chat Completions 迁移到 Responses API

如果您当前使用的是 OpenAI Chat Completions API,可以通过以下步骤迁移到 Responses API。Responses API 提供了更简洁的接口和更强大的功能,同时保持了与 Chat Completions 的兼容性。

1. 更新端点地址和 base_url

需要同时更新两处:

  • 端点路径:从 /v1/chat/completions 更新为 /v1/responses

  • base_url

    • 华北2(北京):从 https://dashscope.aliyuncs.com/compatible-mode/v1更新为 https://dashscope.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1

Python

# Chat Completions API
completion = client.chat.completions.create(
    model="qwen3-max-2026-01-23",
    messages=[
        {"role": "system", "content": "你是一个有帮助的助手。"},
        {"role": "user", "content": "你好!"}
    ]
)
print(completion.choices[0].message.content)

# Responses API - 可以使用相同的消息格式
response = client.responses.create(
    model="qwen3-max-2026-01-23",
    input=[
        {"role": "system", "content": "你是一个有帮助的助手。"},
        {"role": "user", "content": "你好!"}
    ]
)
print(response.output_text)

# Responses API - 或使用更简洁的格式
response = client.responses.create(
    model="qwen3-max-2026-01-23",
    input="你好!"
)
print(response.output_text)

Node.js

// Chat Completions API
const completion = await client.chat.completions.create({
    model: "qwen3-max-2026-01-23",
    messages: [
        { role: "system", content: "你是一个有帮助的助手。" },
        { role: "user", content: "你好!" }
    ]
});
console.log(completion.choices[0].message.content);

// Responses API - 可以使用相同的消息格式
const response = await client.responses.create({
    model: "qwen3-max-2026-01-23",
    input: [
        { role: "system", content: "你是一个有帮助的助手。" },
        { role: "user", content: "你好!" }
    ]
});
console.log(response.output_text);

// Responses API - 或使用更简洁的格式
const response2 = await client.responses.create({
    model: "qwen3-max-2026-01-23",
    input: "你好!"
});
console.log(response2.output_text);

curl

# Chat Completions API
curl -X POST https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "qwen3-max-2026-01-23",
    "messages": [
        {"role": "system", "content": "你是一个有帮助的助手。"},
        {"role": "user", "content": "你好!"}
    ]
}'

# Responses API - 使用更简洁的格式
curl -X POST https://dashscope.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1/responses \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "qwen3-max-2026-01-23",
    "input": "你好!"
}'

2. 更新响应处理

Responses API 的响应结构有所不同。使用 output_text 快捷方法获取文本输出,或通过 output 数组访问详细信息。

响应对比

# Chat Completions 响应
{
  "id": "chatcmpl-416b0ea5-e362-9fec-97c5-0a60b5d7xxx",
  "choices": [
    {
      "finish_reason": "stop",
      "index": 0,
      "logprobs": null,
      "message": {
        "content": "你好!很高兴见到你~ 有什么我可以帮你的吗?",
        "refusal": null,
        "role": "assistant",
        "function_call": null,
        "tool_calls": null
      }
    }
  ],
  "created": 1769416269,
  "model": "qwen3-max-2026-01-23",
  "object": "chat.completion",
  "service_tier": null,
  "system_fingerprint": null,
  "usage": {
    "completion_tokens": 14,
    "prompt_tokens": 22,
    "total_tokens": 36,
    "prompt_tokens_details": {
      "cached_tokens": 0
    }
  }
}
# Responses API 响应
{
  "id": "d69c735d-0f5e-4b6c-9c2a-8cab5eb14xxx",
  "created_at": 1769416269.0,
  "model": "qwen3-max-2026-01-23",
  "object": "response",
  "status": "completed",
  "output": [
    {
      "id": "msg_3426d3e5-8da7-4dd8-a6a5-7c2cd866xxx",
      "type": "message",
      "role": "assistant",
      "status": "completed",
      "content": [
        {
          "type": "output_text",
          "text": "你好!今天是2026126日,星期一。有什么我可以帮你的吗?",
          "annotations": []
        }
      ]
    }
  ],
  "usage": {
    "input_tokens": 34,
    "output_tokens": 25,
    "total_tokens": 59,
    "input_tokens_details": {
      "cached_tokens": 0
    },
    "output_tokens_details": {
      "reasoning_tokens": 0
    }
  }
}

3. 简化多轮对话管理

在 Chat Completions 中需要手动管理消息历史数组,而 Responses API 提供了 previous_response_id 参数自动关联上下文,当前响应id有效期为7天。

Python

# Chat Completions - 需要手动管理消息历史
messages = [
    {"role": "system", "content": "你是一个有帮助的助手。"},
    {"role": "user", "content": "法国的首都是哪里?"}
]
res1 = client.chat.completions.create(
    model="qwen3-max-2026-01-23",
    messages=messages
)

# 手动添加响应到历史
messages.append(res1.choices[0].message)
messages.append({"role": "user", "content": "它的人口是多少?"})

res2 = client.chat.completions.create(
    model="qwen3-max-2026-01-23",
    messages=messages
)
# Responses API - 使用 previous_response_id 自动关联
res1 = client.responses.create(
    model="qwen3-max-2026-01-23",
    input="法国的首都是哪里?"
)

# 只需传递上一轮的 ID
res2 = client.responses.create(
    model="qwen3-max-2026-01-23",
    input="它的人口是多少?",
    previous_response_id=res1.id
)

Node.js

// Chat Completions - 需要手动管理消息历史
let messages = [
    { role: "system", content: "你是一个有帮助的助手。" },
    { role: "user", content: "法国的首都是哪里?" }
];
const res1 = await client.chat.completions.create({
    model: "qwen3-max-2026-01-23",
    messages
});

// 手动添加响应到历史
messages = messages.concat([res1.choices[0].message]);
messages.push({ role: "user", content: "它的人口是多少?" });

const res2 = await client.chat.completions.create({
    model: "qwen3-max-2026-01-23",
    messages
});
// Responses API - 使用 previous_response_id 自动关联
const res1 = await client.responses.create({
    model: "qwen3-max-2026-01-23",
    input: "法国的首都是哪里?"
});

// 只需传递上一轮的 ID
const res2 = await client.responses.create({
    model: "qwen3-max-2026-01-23",
    input: "它的人口是多少?",
    previous_response_id: res1.id
});

4. 使用内置工具

Responses API 内置了多种工具,无需自行实现。只需在 tools 参数中指定即可,当前代码解释器与网页抓取工具限时免费,详情请参见联网搜索代码解释器网页抓取

Python

# Chat Completions - 需要自己实现工具函数
def web_search(query):
    # 需要自己实现网络搜索逻辑
    import requests
    r = requests.get(f"https://api.example.com/search?q={query}")
    return r.json().get("results", [])

completion = client.chat.completions.create(
    model="qwen3-max-2026-01-23",
    messages=[{"role": "user", "content": "法国现任总统是谁?"}],
    functions=[{
        "name": "web_search",
        "description": "搜索网络信息",
        "parameters": {
            "type": "object",
            "properties": {"query": {"type": "string"}},
            "required": ["query"]
        }
    }]
)
# Responses API - 直接使用内置工具
response = client.responses.create(
    model="qwen3-max-2026-01-23",
    input="法国现任总统是谁?",
    tools=[{"type": "web_search"}]  # 直接启用网络搜索
)
print(response.output_text)

Node.js

// Chat Completions - 需要自己实现工具函数
async function web_search(query) {
    const fetch = (await import('node-fetch')).default;
    const res = await fetch(`https://api.example.com/search?q=${query}`);
    const data = await res.json();
    return data.results;
}

const completion = await client.chat.completions.create({
    model: "qwen3-max-2026-01-23",
    messages: [{ role: "user", content: "法国现任总统是谁?" }],
    functions: [{
        name: "web_search",
        description: "搜索网络信息",
        parameters: {
            type: "object",
            properties: { query: { type: "string" } },
            required: ["query"]
        }
    }]
});
// Responses API - 直接使用内置工具
const response = await client.responses.create({
    model: "qwen3-max-2026-01-23",
    input: "法国现任总统是谁?",
    tools: [{ type: "web_search" }]  // 直接启用网络搜索
});
console.log(response.output_text);

curl

# Chat Completions - 需要自己实现工具
# 这里展示调用外部搜索 API 的示例
curl https://api.example.com/search \
  -G \
  --data-urlencode "q=法国现任总统" \
  --data-urlencode "key=$SEARCH_API_KEY"
# Responses API - 直接使用内置工具
curl -X POST https://dashscope.aliyuncs.com/api/v2/apps/protocols/compatible-mode/v1/responses \
-H "Authorization: Bearer $DASHSCOPE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
    "model": "qwen3-max-2026-01-23",
    "input": "法国现任总统是谁?",
    "tools": [{"type": "web_search"}]
}'

常见问题

Q:如何传递多轮对话的上下文?

A:在发起新一轮对话请求时,请将上一轮模型响应成功返回的id作为 previous_response_id 参数传入。