Skip to content

具備思考能力的模型會產生一個 thinking 欄位,將其推理軌跡(reasoning trace)與最終答案分離。

利用此功能來稽核模型的步驟、在使用者介面 (UI) 中呈現模型的「思考」過程,或在僅需要最終回應時完全隱藏軌跡。

支援的模型

在 API 調用中啟用思考

在對話或生成請求中設置 think 欄位。大多數模型接受布林值 (true/false) 或等級 (low, medium, high, max),其中 max 表示請求最高的思考層級。

GPT-OSS 則要求使用 lowmediumhigh 其中之一來調整軌跡長度。

message.thinking(對話端點)或 thinking(生成端點)欄位包含推理軌跡,而 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 將被忽略。

流式傳輸推理軌跡

思考串流會在答案標記之前交錯插入推理標記。偵測第一個 thinking 片段以渲染「思考」區塊,然後在 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 中預設啟用思考功能。