Skip to main content

重要提示

⚠️ 部分模型引入了新的「交错思考(Interleaved Thinking)」行为。
在 SiliconFlow 上,通过 Serverless 模型 API 使用 DeepSeek V3.2GLM-4.7 时,模型可能会输出与交错思考相关的结构化内容——最常见于 工具调用(tool-calling) 流程。
为确保正确性、稳定性与最佳性能,请遵循以下指南。

概览

在 SiliconFlow 上,目前支持 交错思考(Interleaved Thinking) 的模型包括:
  • DeepSeek V3.2
  • GLM-4.7
交错思考尤其适用于:
  • Agent 式编排
  • 工具调用场景
  • 编码与调试
  • 需要利用工具中间结果推进的多步骤任务

1. 什么是交错思考(Interleaved Thinking)?

交错思考 模式下,模型可以:
  1. 判断是否需要调用工具
  2. 调用工具
  3. 接收工具返回结果
  4. 基于中间输出继续推进
  5. 决定下一步(继续调用工具或输出最终答案)
这使得多步骤执行更加稳健:工具输出能够影响并驱动后续步骤。

示意图:包含工具结果的 Turn/Step 结构

下图展示了单个 Turn 如何包含多个 Step,以及模型在收到工具结果之后(即你发送 role="tool" 消息后)仍可能继续生成 reasoning_content
为保证工具调用行为正确,必须在整个链路中按原样保留并回放模型返回的 reasoning_content
交错思考(Turn/Step、工具调用/结果、reasoning_content)

2. 工具调用:必须遵守的规则(不可妥协)

DeepSeek V3.2GLM-4.7工具调用(tool calling) 场景中,API 可能会在专用字段中返回额外的结构化输出:
  • reasoning_content
你必须将 reasoning_content 按原样保留,并在后续请求中不做任何修改地回传。

必须保留的内容(包括工具结果之后)

需要明确:必须保留的不仅是 用户消息之后 产生的 reasoning_content,还包括 工具结果之后 产生的 reasoning_content 在启用工具的流程中,模型可能在以下阶段产生推理内容:
  • 在任何工具调用之前
  • 在多次工具调用之间
  • 在接收工具结果之后(即你发送 role="tool" 消息后,模型继续生成)
你必须对上述产生的 所有 reasoning_content 做到按生成结果原样保留并回放 包括但不限于:
  • 工具调用前输出的内容
  • 工具调用之间输出的内容(多步骤工具链)
  • 工具结果之后输出的内容
  • 跨多个轮次产生的所有 reasoning_content 片段(必须保持原始顺序

禁止行为

不要
  • 修改文本
  • “清理”或做任何后处理
  • 合并或拆分片段
  • 调整片段顺序
  • 丢弃 reasoning_content,只保留普通 assistant 文本
否则可能导致:
  • 与工具相关的多步骤行为被破坏
  • 跨工具调用表现不稳定
  • 缓存效率下降、输出质量劣化

3. 客户端处理清单(推荐)

接收响应时:
  • content(或流式场景下的 delta.content)累积普通 assistant 文本
  • reasoning_content(或流式场景下的 delta.reasoning_content)累积交错思考文本
  • tool_calls(或流式场景下的 delta.tool_calls)收集工具调用请求
将 assistant 消息回传给模型时,需要一并包含:
  • content
  • reasoning_content(原样、完整,且顺序与生成一致)
  • tool_calls(按返回结果回传)
**说明:**该规则同时适用于 流式非流式。流式仅影响字段读取方式(delta.*),不影响必须保留与回传的内容要求。

4. 示例:交错思考 + 工具调用(DeepSeek V3.2)

该示例演示在 SiliconFlow 上使用 DeepSeek V3.2
同样的处理模式也适用于 GLM-4.7
from openai import OpenAI
import json

client = OpenAI(
    api_key="YOUR_API_KEY",
    base_url="https://api.siliconflow.com/v1/"
)

tools = [{
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "Get weather information",
        "parameters": {
            "type": "object",
            "properties": {
                "city": {"type": "string"}
            },
            "required": ["city"]
        }
    }
}]

messages = [
    {"role": "system", "content": "You are an assistant"},
    {"role": "user", "content": "What's the weather like in America?"}
]

# Round 1:模型推理并调用工具
response = client.chat.completions.create(
    model="deepseek-ai/DeepSeek-V3.2",
    messages=messages,
    tools=tools,
    stream=True  # 可选;同样规则适用于非流式
)

reasoning_content = ""
content = ""
tool_calls = []

for chunk in response:
    delta = chunk.choices[0].delta

    if getattr(delta, "reasoning_content", None):
        reasoning_content += delta.reasoning_content

    if getattr(delta, "content", None):
        content += delta.content

    if getattr(delta, "tool_calls", None):
        tool_calls.extend(delta.tool_calls)

# 🔑 原样保留 reasoning_content(Round 1)
messages.append({
    "role": "assistant",
    "content": content,
    "reasoning_content": reasoning_content,
    "tool_calls": tool_calls
})

# 工具执行结果(示例)
messages.append({
    "role": "tool",
    "tool_call_id": tool_calls[0]["id"],
    "content": json.dumps({"weather": "Sunny", "temp": "25°C"})
})

# Round 2:在工具结果之后,模型可能产生新的 reasoning_content
response = client.chat.completions.create(
    model="deepseek-ai/DeepSeek-V3.2",
    messages=messages,
    tools=tools,
    stream=True  # 可选
)

reasoning_content_2 = ""
content_2 = ""
tool_calls_2 = []

for chunk in response:
    delta = chunk.choices[0].delta

    if getattr(delta, "reasoning_content", None):
        reasoning_content_2 += delta.reasoning_content

    if getattr(delta, "content", None):
        content_2 += delta.content

    if getattr(delta, "tool_calls", None):
        tool_calls_2.extend(delta.tool_calls)

# 🔑 原样保留 reasoning_content(Round 2,即工具结果之后)
messages.append({
    "role": "assistant",
    "content": content_2,
    "reasoning_content": reasoning_content_2,
    "tool_calls": tool_calls_2
})

5. 改用 GLM-4.7

若要将示例切换为 GLM-4.7,将:
model="deepseek-ai/DeepSeek-V3.2"
替换为:
model="zai-org/GLM-4.7"
交错思考的保留规则完全一致,包括必须保留 工具结果之后 产生的 reasoning_content

总结

在 SiliconFlow 上使用 DeepSeek V3.2GLM-4.7 时:
  1. 交错思考支持多步骤、面向工具的执行
  2. 工具调用 场景下,必须 按原样保留并回放 reasoning_content——包括 工具结果之后 产生的推理内容
  3. 严禁对 reasoning_content 进行修改、丢弃、合并/拆分或重排
遵循以上规则可确保工具使用稳定、多步骤行为一致,并获得更佳的整体效果与性能表现。