跳到内容

交错思考

简介

交错思考允许模型在工具调用之间进行推理,从而在收到工具结果后实现更复杂的决策。此功能可帮助模型在中间插入推理步骤来链接多个工具调用,并根据中间结果做出细致的决策。

重要提示:交错思考会增加 token 使用量和响应延迟。启用此功能时,请考虑您的预算和性能要求。

交错思考如何工作

通过交错思考,模型可以

  • 在决定下一步做什么之前,对工具调用的结果进行推理
  • 在中间插入推理步骤来链接多个工具调用
  • 根据中间结果做出更细致的决策
  • 为其工具选择过程提供透明的推理

支持的模型

vLLM 目前支持以下交错思考模型

模型系列 推理解析器名称
moonshotai/Kimi-K2-Thinking kimi_k2
MiniMaxAI/MiniMax-M2 minimax_m2

示例用法

要将交错思考与工具调用一起使用,请指定一个支持此功能且在您的聊天补全请求中启用了工具调用的模型。以下是一个示例

代码
"""
vllm serve MiniMaxAI/MiniMax-M2 \
  --tensor-parallel-size 4 \
  --tool-call-parser minimax_m2 \
  --reasoning-parser minimax_m2 \
  --enable-auto-tool-choice
"""
import json

from openai import OpenAI

client = OpenAI(base_url="https://:8000/v1",     api_key="dummy")


def get_current_weather(location: str, unit: "str"):
    """Get the current weather in a given location"""
    if unit == "celsius":
        return f"The current temperature in {location} is 22°C."
    else:
        return f"The current temperature in {location} is 72°F."


tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Get the current weather in a given     location",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": "City and state, e.g.,     'San Francisco, CA'",
                    },
                    "unit": {"type": "string", "enum":     ["celsius", "fahrenheit"]},
                },
                "required": ["location", "unit"],
            },
        },
    }
]
messages = [{"role": "user", "content": "What's the weather in Fahrenheit like in San Francisco?"}]
response = client.chat.completions.create(
    model=client.models.list().data[0].id,
    messages=messages,
    tools=tools,
    tool_choice="auto",
)

tool_call = response.choices[0].message.tool_calls[0].function

messages.append(
    {
        "role": "assistant",
        "tool_calls": response.choices[0].message.tool_calls,
        "reasoning": response.choices[0].message.reasoning, # append reasoning
    }
)

# Simulate tool execution
available_tools = {"get_weather": get_current_weather}

completion_tool_calls = response.choices[0].message.tool_calls
for call in completion_tool_calls:
    tool_to_call = available_tools[call.function.name]
    args = json.loads(call.function.arguments)
    result = tool_to_call(**args)
    messages.append(
        {
            "role": "tool",
            "content": result,
            "tool_call_id": call.id,
            "name": call.function.name,
        }
    )
response_2 = client.chat.completions.create(
    model=client.models.list().data[0].id,
    messages=messages,
    tools=tools,
    tool_choice="auto",
)
print(response_2.choices[0].message.content)

此示例演示了如何使用天气检索函数设置带有工具调用的交错思考。模型在生成最终响应之前会对其工具结果进行推理。