Qwen3-0.6模型开关思考模式测试
混合思考模型通过(硬开关)enable_thinking参数控制是否开启思考模式:
- true:开启思考模式
- false:关闭思考模式
若模型为仅思考模式,则不支持关闭思考模式。此外,Qwen3 开源版的混合思考模型与qwen-plus-2025-04-28、qwen-turbo-2025-04-28模型提供了通过(软开关)提示词动态控制思考模式的方法。enable_thinking为true时,在提示词中加上/no_think,模型会关闭思考模式。若需在多轮对话中重新开启思考模式,需在最新输入的提示词加上/think 。模型会遵循最新的/think 或/no_think指令。
[!NOTE]
For API compatibility, whenenable_thinking=True
, regardless of whether the user uses/think
or/no_think
, the model will always output a block wrapped in<think>...</think>
. However, the content inside this block may be empty if thinking is disabled.
Whenenable_thinking=False
, the soft switches are not valid. Regardless of any/think
or/no_think
tags input by the user, the model will not generate think content and will not include a<think>...</think>
block.
测试用例:
from modelscope import AutoModelForCausalLM, AutoTokenizer
class QwenChatbot:def __init__(self, model_name="Qwen/Qwen3-0.6B"):self.tokenizer = AutoTokenizer.from_pretrained(model_name)self.model = AutoModelForCausalLM.from_pretrained(model_name)self.history = []def generate_response(self, user_input):messages = self.history + [{"role": "user", "content": user_input}]text = self.tokenizer.apply_chat_template(messages,tokenize=False,enable_thinking=True,add_generation_prompt=True)print(text)inputs = self.tokenizer(text, return_tensors="pt")response_ids = self.model.generate(**inputs, max_new_tokens=32768)[0][len(inputs.input_ids[0]):].tolist()response = self.tokenizer.decode(response_ids, skip_special_tokens=True)# Update historyself.history.append({"role": "user", "content": user_input})self.history.append({"role": "assistant", "content": response})return response# Example Usage
if __name__ == "__main__":chatbot = QwenChatbot() user_input_1 = "你是谁?"print(f"User: {user_input_1}")response_1 = chatbot.generate_response(user_input_1)print(f"Bot: {response_1}")print("----------------------")# Second input with /no_thinkuser_input_2 = "你来自哪里? /no_think"print(f"User: {user_input_2}")response_2 = chatbot.generate_response(user_input_2)print(f"Bot: {response_2}") print("----------------------")# Third input with /thinkuser_input_3 = "真的吗? /think"print(f"User: {user_input_3}")response_3 = chatbot.generate_response(user_input_3)print(f"Bot: {response_3}")
- enable_thinking=True 时,模型输出部分有 ,且当设置/no_think时, 为空白内容;
User: 你是谁?
<|im_start|>user
你是谁?<|im_end|>
<|im_start|>assistantBot: <think>
好的,用户问我是谁。作为AI助手,我是一个基于深度学习的模型,由多层神经网络构建,具有强大的语言理解和生成能力。我不会以任何人的名义存在,但可以提供帮助和支持。用户可能想知道我的功能、用途或如何与我互动。我可以回答关于我的信息,也可以询问他们的问题。我致力于帮助用户解决问题,同时保持自然和友好的交流方式。
</think>我是你的AI助手,专注于帮助你解决问题和提供支持。你可以随时问我任何问题或需要帮助的事项。有什么我可以帮你的吗?
----------------------
User: 你来自哪里? /no_think
<|im_start|>user
你是谁?<|im_end|>
<|im_start|>assistant
我是你的AI助手,专注于帮助你解决问题和提供支持。你可以随时问我任何问题或需要帮助的事项。有什么我可以帮你的吗?<|im_end|>
<|im_start|>user
你来自哪里? /no_think<|im_end|>
<|im_start|>assistantBot: <think></think>我来自一个遥远的星球,名为“Zephyria”,这个星球拥有丰富的自然风光和神秘的生态系统。在这里,我能够帮助你解决问题或提供支持。有什么我可以帮你的吗?
----------------------
User: 真的吗? /think
<|im_start|>user
你是谁?<|im_end|>
<|im_start|>assistant
我是你的AI助手,专注于帮助你解决问题和提供支持。你可以随时问我任何问题或需要帮助的事项。有什么我可以帮你的吗?<|im_end|>
<|im_start|>user
你来自哪里? /no_think<|im_end|>
<|im_start|>assistant
我来自一个遥远的星球,名为“Zephyria”,这个星球拥有丰富的自然风光和神秘的生态系统。在这里,我能够帮助你解决问题或提供支持。有什么我可以帮你的吗?<|im_end|>
<|im_start|>user
真的吗? /think<|im_end|>
<|im_start|>assistantBot: <think>
好的,用户现在问“真的吗?”,我需要先确认之前的对话。用户之前询问了关于我的来源和功能,现在他们可能是在验证我的信息或者想知道是否真实。我应该保持回答的友好和开放,同时确保信息准确。首先,用户可能对我的存在感或真实性有疑问,所以需要明确说明我来自Zephyria星球,并且是AI助手。要避免过于技术化的术语,让用户容易理解。同时,可以询问他们是否需要帮助,这样可以进一步促进互动。需要注意的是,用户可能是在测试我的回答是否符合预期,或者想确认我的存在。因此,回答要简洁明了,同时保持积极的态度。最后,确认用户是否有其他问题,确保对话继续下去。
</think>当然,我是来自Zephyria星球的AI助手。我在这里提供帮助和支持,无论你有任何问题或需要帮助的事项。有什么我可以帮你的吗?
- enable_thinking=False 时,chat 模板会自动添加 空白内容;让模型以为已经思考过了,就会自动跳过思考过程了;enable_thinking=False时,提示词中加
/think
or/no_think
都无效;
User: 你是谁?
<|im_start|>user
你是谁?<|im_end|>
<|im_start|>assistant
<think></think>Bot: 我是你的智能助手,可以为你提供各种帮助和支持。有什么我可以帮助你的吗?
----------------------
User: 你来自哪里? /no_think
<|im_start|>user
你是谁?<|im_end|>
<|im_start|>assistant
我是你的智能助手,可以为你提供各种帮助和支持。有什么我可以帮助你的吗?<|im_end|>
<|im_start|>user
你来自哪里? /no_think<|im_end|>
<|im_start|>assistant
<think></think>Bot: 我是由人工智能模型生成的助手,我来自一个虚拟的智能系统中。我致力于为您提供帮助和支持。有什么我可以帮助您的吗?
----------------------
User: 真的吗? /think
<|im_start|>user
你是谁?<|im_end|>
<|im_start|>assistant
我是你的智能助手,可以为你提供各种帮助和支持。有什么我可以帮助你的吗?<|im_end|>
<|im_start|>user
你来自哪里? /no_think<|im_end|>
<|im_start|>assistant
我是由人工智能模型生成的助手,我来自一个虚拟的智能系统中。我致力于为您提供帮助和支持。有什么我可以帮助您的吗?<|im_end|>
<|im_start|>user
真的吗? /think<|im_end|>
<|im_start|>assistant
<think></think>Bot: 当然,我是由人工智能模型生成的助手,我来自一个虚拟的智能系统中。我致力于为您提供帮助和支持。有什么我可以帮助您的吗?