Skip to content

支持思考的模型会输出一个 thinking 字段,将推理轨迹(reasoning trace)与最终答案分开。

利用此功能可以审计模型的思考步骤、在 UI 中展示模型正在“思考”的状态,或者在仅需要最终回答时完全隐藏推理轨迹。

支持的模型

在 API 调用中启用思考

在 chat 或 generate 请求中设置 think 字段。大多数模型接受布尔值 (true/false) 或等级 (low, medium, high, max),其中 max 请求最高的思考级别。

GPT-OSS 则要求使用 lowmediumhigh 其中之一来调节推理轨迹长度。

message.thinking(chat 端点)或 thinking(generate 端点)字段包含推理轨迹,而 message.content / response 包含最终答案。

cURL

shell
curl http://localhost:11434/api/chat -d '{
  "model": "qwen3",
  "messages": [{
    "role": "user",
    "content": "How many letter r are in strawberry?"
  }],
  "think": true,
  "stream": false
}'

Python

python
from ollama import chat

response = chat(
  model='qwen3',
  messages=[{'role': 'user', 'content': 'How many letter r are in strawberry?'}],
  think=True,
  stream=False,
)

print('Thinking:\n', response.message.thinking)
print('Answer:\n', response.message.content)

JavaScript

javascript
import ollama from 'ollama'

const response = await ollama.chat({
  model: 'deepseek-r1',
  messages: [{ role: 'user', content: 'How many letter r are in strawberry?' }],
  think: true,
  stream: false,
})

console.log('Thinking:\n', response.message.thinking)
console.log('Answer:\n', response.message.content)

注意

GPT-OSS 要求将 think 设置为 "low""medium""high"。对于该模型,传递 true/false 将被忽略。

流式传输推理轨迹

思考流在回答 token 之前包含推理 token。检测到第一个包含 thinking 的 chunk 时渲染“思考”部分,然后在 message.content 到达时切换到最终回复。

Python

python
from ollama import chat

stream = chat(
  model='qwen3',
  messages=[{'role': 'user', 'content': 'What is 17 × 23?'}],
  think=True,
  stream=True,
)

in_thinking = False

for chunk in stream:
  if chunk.message.thinking and not in_thinking:
    in_thinking = True
    print('Thinking:\n', end='')

  if chunk.message.thinking:
    print(chunk.message.thinking, end='')
  elif chunk.message.content:
    if in_thinking:
      print('\n\nAnswer:\n', end='')
      in_thinking = False
    print(chunk.message.content, end='')

JavaScript

javascript
import ollama from 'ollama'

async function main() {
  const stream = await ollama.chat({
    model: 'qwen3',
    messages: [{ role: 'user', content: 'What is 17 × 23?' }],
    think: true,
    stream: true,
  })

  let inThinking = false

  for await (const chunk of stream) {
    if (chunk.message.thinking && !inThinking) {
      inThinking = true
      process.stdout.write('Thinking:\n')
    }

    if (chunk.message.thinking) {
      process.stdout.write(chunk.message.thinking)
    } else if (chunk.message.content) {
      if (inThinking) {
        process.stdout.write('\n\nAnswer:\n')
        inThinking = false
      }
      process.stdout.write(chunk.message.content)
    }
  }
}

main()

CLI 快速参考

  • 单次运行启用思考:ollama run deepseek-r1 --think "Where should I visit in Lisbon?"
  • 禁用思考:ollama run deepseek-r1 --think=false "Summarize this article"
  • 在使用思考模型时隐藏轨迹:ollama run deepseek-r1 --hidethinking "Is 9.9 bigger or 9.11?"
  • 在交互式会话中,使用 /set think/set nothink 进行切换。
  • GPT-OSS 仅接受等级:ollama run gpt-oss --think=low "Draft a headline"(根据需要将 low 替换为 mediumhigh)。

注意

对于支持的模型,在 CLI 和 API 中默认启用思考功能。