具备思考能力的模型会发出一个 thinking 字段,将其推理过程与最终答案分开。 利用此功能可以审计模型步骤、在 UI 中展示模型“思考中”的动画,或者在仅需要最终回答时完全隐藏推理过程。支持的模型
在 API 调用中启用思考
在对话 (chat) 或生成 (generate) 请求中设置 think 字段。大多数模型接受布尔值(true/false)。 而 GPT-OSS 则需要 low、medium 或 high 其中之一来调整推理过程的长度。 message.thinking(对话端点)或 thinking(生成端点)字段包含推理过程,而 message.content / response 则包含最终答案。curl https://:11434/api/chat -d '{
"model": "qwen3",
"messages": [{
"role": "user",
"content": "How many letter r are in strawberry?"
}],
"think": true,
"stream": false
}'
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)
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 将被忽略。
流式传输推理过程
思考流在答案令牌 (tokens) 之前交织推理令牌。检测到第一个 thinking 块时渲染“正在思考”部分,然后在 message.content 到达时切换到最终回复。
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='')
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 替换为 medium 或 high)。
对于支持的模型,CLI 和 API 默认启用思考功能。