help.khb.com 对话模型 推理模型 (Reasoning)

推理模型 (Reasoning)

Reasoning models use Chain-of-Thought to solve math, code, and complex logic problems. Supports DeepSeek-R1 and more.

1. 概述

Reasoning models 是基于深度学习的 AI 系统,通过逻辑推理、知识关联和上下文分析解决复杂任务。典型应用包括数学问题求解、代码生成、逻辑判断和多步推理场景。这类模型通常具有以下特征:

  • 结构化思考:使用思维链 (Chain-of-Thought) 等技术分解复杂问题
  • 知识整合:结合领域知识库与常识推理能力
  • 自我修正机制:通过反馈循环提升结果可靠性
  • 多模态处理:部分先进模型支持文本、代码、公式的混合输入

2. 支持的模型

平台支持的推理模型可在 模型广场 查询。

3. 使用建议

3.1 API 参数

输入参数

  • 思维链最大长度 (thinking_budget):模型用于内部推理的 token 数,调整此参数可控制思维链过程的长度。
  • 最大响应长度 (max_tokens):用于限制最终输出给用户的 token 数,用户可正常配置以控制最大响应长度。
  • 最大上下文长度 (context_length):这是最大总内容长度,不是请求参数,不需要用户设置。

解耦推理模型的思维链过程与响应长度后,输出行为将遵循以下规则:

  • 如果”思考阶段”生成的 token 数达到 thinking_budget,原生支持该参数的 Qwen3 系列推理模型将强制停止思维链推理。其他推理模型可能会继续输出思考内容。
  • 如果最大响应长度超过 max_tokens 限制或上下文长度超过 context_length 限制,响应内容将被截断。响应中 finish_reason 字段将被标记为 length,表示输出因长度限制而终止。

返回参数

  • reasoning_content:推理链内容,与 content 同级。
  • content:最终答案内容。

3.2 DeepSeek-R1 使用建议

  • 将 temperature 设置在 0.5-0.7 范围内(推荐 0.6),以防止无休止重复或不一致的输出。
  • top_p 值设为 0.95。
  • 避免添加系统提示;所有指令应包含在用户提示中。
  • 对于数学问题,建议在 prompt 中加入指令,如:”Please reason step by step, and put your final answer within \boxed.”
  • 评估模型性能时,建议进行多次测试并取平均值。

4. OpenAI 请求示例

4.1 流式请求

from openai import OpenAI

url = 'https://khb.net.cn/v1/'
api_key = 'your-api-key'

client = OpenAI(
    base_url=url,
    api_key=api_key
)

content = ""
reasoning_content = ""
messages = [
    {"role": "user", "content": "Who are the legendary athletes of the Olympic Games?"}
]

response = client.chat.completions.create(
    model="Pro/deepseek-ai/DeepSeek-V3.2",
    messages=messages,
    stream=True,
    max_tokens=4096,
    extra_body={
        "enable_thinking": True,
        "thinking_budget": 1024
    }
)

for chunk in response:
    if chunk.choices[0].delta.content:
        content += chunk.choices[0].delta.content
    if chunk.choices[0].delta.reasoning_content:
        reasoning_content += chunk.choices[0].delta.reasoning_content

# Round 2
messages.append({"role": "assistant", "content": content})
messages.append({'role': 'user', 'content': "Go on"})
response = client.chat.completions.create(
    model="Pro/deepseek-ai/DeepSeek-V3.2",
    messages=messages,
    stream=True
)

4.2 非流式请求

from openai import OpenAI

url = 'https://khb.net.cn/v1/'
api_key = 'your-api-key'

client = OpenAI(
    base_url=url,
    api_key=api_key
)

messages = [
    {"role": "user", "content": "Who are the legendary athletes of the Olympic Games?"}
]

response = client.chat.completions.create(
    model="Pro/deepseek-ai/DeepSeek-V3.2",
    messages=messages,
    stream=False,
    max_tokens=4096,
    extra_body={
        "enable_thinking": True,
        "thinking_budget": 1024
    }
)

content = response.choices[0].message.content
reasoning_content = response.choices[0].message.reasoning_content

# Round 2
messages.append({"role": "assistant", "content": content})
messages.append({'role': 'user', 'content': "Go on"})
response = client.chat.completions.create(
    model="Pro/deepseek-ai/DeepSeek-V3.2",
    messages=messages,
    stream=False
)

5. 注意事项

  • API Key:确保使用正确的 API Key 进行身份验证。
  • 流式模式:流式模式适用于需要逐步接收响应的场景,非流式模式适用于需要一次性获取完整响应的场景。

6. 常见问题

如何获取 API Key?
请访问 KHB 算力平台 注册并获取 API Key。

如何处理长文本?
您可以调整 max_tokens 参数来控制输出的长度。

Related Post