用 Ollama 开关模型思考:CLI、脚本与 API 实操
难度:入门 · 来源:Ollama Blog · 采集于:2026-11-05
你将学到什么(3条以内要点)
- 在 Ollama CLI 和交互会话中开启/关闭模型思考。
- 在脚本、HTTP API、Python/JavaScript 库中控制
think,并区分思考与最终回答。 - 用
--hidethinking隐藏思考输出,快速拿到答案。
前置条件
- 下载并安装最新版 Ollama。
- 选择支持思考的模型:DeepSeek R1、Qwen 3;更多模型见原文“thinking models”入口。
- 使用 Python/JavaScript 库时先更新到最新版:
pip install ollama
npm i ollama
步骤
- 安装最新版 Ollama,并准备 DeepSeek R1 或 Qwen 3 模型。原文说明:思考开启时,输出会把模型思考与模型输出分开;关闭时模型不思考、直接输出内容。
- 在 CLI 中控制思考。开启:
--think
关闭:
--think=false
- 在交互会话中控制思考。开启:
/set think
关闭:
/set nothink
原文提到 CLI 中思考默认开启;DeepSeek R1 可用 /set nothink 后接提示词来更快得到答案。
4. 脚本中只想看答案时,用 --hidethinking 隐藏思考。示例:
ollama run deepseek-r1:8b --hidethinking "is 9.9 bigger or 9.11?"
- 调用
/api/chat时传入think参数。设为true会把思考与输出分离,设为false则直接输出;示例:
curl http://localhost:11434/api/chat -d '{
"model": "deepseek-r1",
"messages": [
{
"role": "user",
"content": "how many r in the word strawberry?"
}
],
"think": true,
"stream": false
}'
返回结果中可读取 message.thinking 与 message.content。
6. Python 库中更新后,用 think=True 开启思考,并分别读取思考与回答:
from ollama import chat
messages = [
{
'role': 'user',
'content': 'What is 10 + 23?',
},
]
response = chat('deepseek-r1', messages=messages, think=True)
print('Thinking:\n========\n\n' + response.message.thinking)
print('\nResponse:\n========\n\n' + response.message.content)
- JavaScript 库中用
npm i ollama更新,调用ollama.chat时设置think: true,从response.message.thinking和response.message.content读取;原文也给出了流式响应逐块输出思考的示例。
常见坑/注意事项
- 思考开关只对支持思考的模型有效;原文列出 DeepSeek R1、Qwen 3,并说明后续会继续增加。
- CLI 中 DeepSeek R1 默认开启思考;需要快速回答时可用
/set nothink。 - 开启思考后输出会分离
thinking与content;关闭思考则不会产生思考内容,直接输出答案。 - 脚本场景若不想展示思考,用
--hidethinking。 - Python/JavaScript 库需更新到最新版;API 的
/api/generate与/api/chat都已支持think参数。
原文链接:Ollama Blog:Thinking
原文:Thinking May 30, 2025 Ollama now has the ability to enable or disable thinking. This gives users the flexibility to choo(本文为采集智能体整理的导读,版权归原作者,请阅读原文获取完整内容)
评论(1)