Ollama 新版功能:AI 思維鏈控制
1、概述
Ollama 現在可以啓用或禁用思考功能。這使用戶可以靈活地針對不同的應用程序和用例選擇模型的思考行爲。
當開啓思考時,輸出會將模型的思考和模型的輸出分離;當關閉思考時,模型不會思考,直接輸出內容。
支持思考的模型:
-
• DeepSeek R1
-
• Qwen3
2、CLI 命令行中使用
CLI 中缺省是啓用思考模式的,如:
ollama run deepseek-r1
如果要禁用思考模式,可以在互動環境時輸入 /set nothink
在互動環節輸入:/set think 以啓用思考模式
也可以直接在執行命令的時候設置思考模式
ollama run deepseek-r1 --think # 開啓思考模式
ollama run deepseek-r1 --think=false #禁用思考模式
對於直接使用腳本進行推理時,可以使用 --hidethinking,這會啓用思維模型但只想返回最終的結果,而不包括思考過程。
ollama run deepseek-r1:8b --hidethinking "9.9與9.11哪一個更大?"
3、Rest API
Ollama 的生成 API(/api/generate)和聊天 API(/api/chat)均已更新,以支持思考。
新增了一個 think 參數,可以設置爲 true 或 false 用於啓用模型的思考過程。當該 think 參數設置爲 true 時,輸出將把模型的思考與模型的輸出分離。這可以幫助用戶打造全新的應用體驗,例如通過圖形界面以動畫形式呈現思考過程,或者讓遊戲中的 NPC 在輸出前顯示思考氣泡。當該 think 參數設置爲 false 時,模型將不會思考,直接輸出內容。
使用 Ollama 聊天 API 並啓用思考的示例
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
}'
輸出
{"model":"deepseek-r1",
"created_at":"2025-05-29T09:35:56.836222Z",
"message":
{"role": "assistant",
"content": "The word \"strawberry\" contains **three** instances of the letter 'R' ..."
"thinking": "First, the question is: \"how many r in the word strawberry?\" I need to count the number of times the letter 'r' appears in the word \"strawberry\". Let me write down the word:...",
"done_reason":"stop",
"done":true,
"total_duration":47975065417,
"load_duration":29758167,
"prompt_eval_count":10,
"prompt_eval_duration":174191542,
"eval_count":2514,
"eval_duration":47770692833
}
}
4、Python 庫
請更新到最新的 Ollama Python 庫。
pip install ollama
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)
5、JavaScript 庫
請更新到最新的 Ollama JavaScript 庫。
npm i ollama
import ollama from 'ollama'
async function main() {
const response = await ollama.chat({
model: 'deepseek-r1',
messages: [
{
role: 'user',
content: 'What is 10 + 23',
},
],
stream: false,
think: true,
})
console.log('Thinking:\n========\n\n' + response.message.thinking)
console.log('\nResponse:\n========\n\n' + response.message.content + '\n\n')
}
main()
思考式流式響應示例
import ollama from 'ollama'
async function main() {
const response = await ollama.chat({
model: 'deepseek-r1',
messages: [
{
role: 'user',
content: 'What is 10 + 23',
},
],
stream: true,
think: true,
})
let startedThinking = false
let finishedThinking = false
for await (const chunk of response) {
if (chunk.message.thinking && !startedThinking) {
startedThinking = true
process.stdout.write('Thinking:\n========\n\n')
} else if (chunk.message.content && startedThinking && !finishedThinking) {
finishedThinking = true
process.stdout.write('\n\nResponse:\n========\n\n')
}
if (chunk.message.thinking) {
process.stdout.write(chunk.message.thinking)
} else if (chunk.message.content) {
process.stdout.write(chunk.message.content)
}
}
}
main()
本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源:https://mp.weixin.qq.com/s/0zG5H6FkFOeerfnVLoa-Og