跳转到主要内容
模型调用

思维链摘要与加密

Qwen4-Max 返回可展示的思维链摘要和完整思维链的密文,用于多轮对话中恢复历史思考。

适用模型:qwen4-max。

工作原理

默认返回完整思维链。开启本功能后,原始思维链不再以明文返回,改为返回摘要与完整密文。摘要用于展示,密文用于恢复历史思考。 摘要与加密同时开启。未产生思考或加密失败时,密文可能缺失;摘要可能缺失或不完整,接口仍不返回原始思维链。 每轮请求都需开启本功能,并原样回传历史密文,以恢复对应的历史思考。 文本和多模态输入使用相同参数;密文只保存思维链,图片、视频等历史输入仍需保留。多段思考或工具调用时,每段密文随所属 assistant 消息分别保存。 Chat Completions 和 DashScope 需开启 enable_thinking=true;回传密文续推时设置 preserve_thinking=true。Responses 和 Anthropic Messages 使用各自的思考参数。

开启方式

接口开启参数摘要字段密文字段
Chat Completionsthinking_display="summarized"choices[i].message.reasoning_contentchoices[i].message.encrypted_content
Responsesreasoning.summary="auto"summary[].textencrypted_content
Anthropic Messagesthinking.display="summarized"thinkingsignature
DashScopeparameters.thinking_display="summarized"output.choices[i].message.reasoning_contentoutput.choices[i].message.encrypted_content
  • Chat Completions
  • Responses
  • Anthropic Messages
  • DashScope
curl
curl -X POST "https://maas.qianwenaiapi.com/compatible-mode/v1/chat/completions" \
  -H "Authorization: Bearer $DASHSCOPE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "qwen4-max",
  "messages": [
    {
      "role": "user",
      "content": "请计算 17×23。"
    }
  ],
  "enable_thinking": true,
  "thinking_display": "summarized",
  "preserve_thinking": true
}'

返回与流式处理

Chat Completions 流式顺序:choices[i].delta.reasoning_content 摘要增量 → 独立 encrypted_content 密文帧 → 正文或工具调用 → 结束。非流式字段位于 choices[i].message。密文一次返回,不要因文本为空而过滤密文帧,也不要为密文单独创建 assistant 消息。 Responses 的 reasoning item 包含 summary(summary_text 元素)和 encrypted_content。摘要按文本增量返回,密文在 reasoning item 完成事件及最终 response 中返回;可从完成快照补齐密文,不要重复追加摘要。 Anthropic Messages 的 thinking block 包含 thinking 摘要和 signature 密文。流式先返回 thinking_delta,再返回完整 signature_delta,关闭 thinking block 后返回正文或工具调用;未产生思考或加密失败时 signature 可能为空字符串。 DashScope 字段位于 output.choices[i].message,流式顺序与 Chat Completions 一致。密文放在 encrypted_content,不能放进 content[].text。中间帧的 finish_reason 为字符串 "null",最终为 "stop";usage 为累计值,不要重复累加。

流式保存摘要、密文与回复

以下为单条回复、无工具调用的 Chat Completions 示例。逐帧处理三个字段,最终只保存一条 assistant 消息。独立的密文帧即使没有文本也必须保留;密文是完整值,不能按文本增量拼接。
Python
import os
from openai import OpenAI

client = OpenAI(
  api_key=os.environ["DASHSCOPE_API_KEY"],
  base_url="https://maas.qianwenaiapi.com/compatible-mode/v1",
)
history = [{"role": "user", "content": "Calculate 17 * 23."}]
stream = client.chat.completions.create(
  model="qwen4-max",
  messages=history,
  stream=True,
  max_tokens=8192,
  extra_body={
    "enable_thinking": True,
    "thinking_display": "summarized",
    "preserve_thinking": True,
  },
)
assistant = {"role": "assistant", "content": "", "reasoning_content": ""}
for chunk in stream:
  if not chunk.choices:
    continue
  delta = chunk.choices[0].delta.model_dump(exclude_none=True)
  assistant["reasoning_content"] += delta.get("reasoning_content") or ""
  assistant["content"] += delta.get("content") or ""
  ciphertext = delta.get("encrypted_content")
  if ciphertext:
    assistant["encrypted_content"] = ciphertext
history.append(assistant)
print("Summary:", assistant["reasoning_content"])
print("Answer:", assistant["content"])
if not assistant.get("encrypted_content"):
  print("No ciphertext was returned; the summary cannot restore full reasoning.")

多轮对话

回传历史时保留完整密文;摘要可省略,Responses 的 summary 可设为空数组。
若某轮没有返回密文,无法用该轮摘要恢复完整历史思考。请保留原始输入与回复,并检查该轮是否产生思考及是否成功启用摘要;不要用摘要伪造或替代密文。
  • Chat Completions
  • Responses
  • Anthropic Messages
  • DashScope
OpenAI Python SDK 的非标准参数 enable_thinking、thinking_display、preserve_thinking 通过 extra_body 传入。
Python
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["DASHSCOPE_API_KEY"],
    base_url="https://maas.qianwenaiapi.com/compatible-mode/v1",
)
questions = [
    "请计算 17×23。",
    "再加 9。"
]
history = []

for turn, question in enumerate(questions, start=1):
    history.append({"role": "user", "content": question})
    response = client.chat.completions.create(
        model="qwen4-max",
        messages=history,
        max_tokens=8192,
        stream=False,
        extra_body={
            "enable_thinking": True,
            "thinking_display": "summarized",
            "preserve_thinking": True,
        },
    )
    assistant_message = response.choices[0].message.model_dump(exclude_none=True)
    history.append(assistant_message)
    print(f"Turn {turn}")
    print("Summary:", assistant_message.get("reasoning_content", ""))
    print("Answer:", assistant_message.get("content") or "")

计费说明

本功能不额外收费,按模型原有输入、输出 Token 计费规则计费。