微信扫码
添加专属顾问
我要投稿
Qwen3,Qwen系列的最新突破,带来革命性的多语言对话和推理能力。核心内容:1. Qwen3的推理能力和多语言支持2. 无缝切换思考模式和非思考模式3. 快速上手指南和代码示例
Qwen3 是 Qwen 系列的最新一代大型语言模型,提供了一系列密集型和专家混合(MoE)模型。基于广泛的训练,Qwen3 在推理能力、指令遵循、代理能力以及多语言支持方面取得了突破性的进展,主要特点如下:
Qwen3-0.6B 具有以下特点:
Qwen3 的代码已集成到最新的 Hugging Face transformers
中,建议您使用最新版本的 transformers
。
如果您使用的是 transformers<4.51.0
,将会遇到以下错误:
KeyError: 'qwen3'
以下是一个代码片段,展示如何使用该模型根据给定输入生成内容:
from transformers import AutoModelForCausalLM, AutoTokenizer
model_name = "Qwen/Qwen3-0.6B"
# 加载分词器和模型
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype="auto",
device_map="auto"
)
# 准备模型输入
prompt = "Give me a short introduction to large language model."
messages = [
{"role": "user", "content": prompt}
]
text = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True,
enable_thinking=True# 切换思考模式和非思考模式,默认为 True。
)
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
# 执行文本补全
generated_ids = model.generate(
**model_inputs,
max_new_tokens=32768
)
output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist()
# 解析思考内容
try:
# rindex 查找 151668 (</think>)
index = len(output_ids) - output_ids[::-1].index(151668)
except ValueError:
index = 0
thinking_content = tokenizer.decode(output_ids[:index], skip_special_tokens=True).strip("\n")
content = tokenizer.decode(output_ids[index:], skip_special_tokens=True).strip("\n")
print("thinking content:", thinking_content)
print("content:", content)
对于部署,您可以使用 vllm>=0.8.5
或 sglang>=0.4.5.post2
创建一个与 OpenAI 兼容的 API 端点:
vllm serve Qwen/Qwen3-0.6B --enable-reasoning --reasoning-parser DeepSeek_r1
python -m sglang.launch_server --model-path Qwen/Qwen3-0.6B --reasoning-parser deepseek-r1
enable_thinking=True
默认情况下,Qwen3 启用了思考能力,类似于 QwQ-32B。这意味着模型将使用其推理能力来提高生成响应的质量。例如,当显示设置 enable_thinking=True
或将其保留为 tokenizer.apply_chat_template
中的默认值时,模型将进入思考模式。
text = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True,
enable_thinking=True # True 是 enable_thinking 的默认值
)
在这种模式下,模型将生成一个 <think>...</think>
块包裹的思考内容,随后是最终响应。
enable_thinking=False
我们提供了一个硬开关,严格禁用模型的思考行为,使其功能与之前的 Qwen2.5-Instruct 模型一致。这种模式特别适用于在需要禁用思考以提高效率的场景中。
text = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True,
enable_thinking=False # 设置 enable_thinking=False 禁用思考模式
)
在这种模式下,模型不会生成任何思考内容,也不会包含 <think>...</think>
快。
我们提供了一个软开关机制,允许用户在 enable_thinking=True
是动态控制模型的行为。具体来说,您可以在用户提示或系统消息中添加 /think
和 /no_think
,以在多轮对话中逐轮切换模型的思考模式。模型将遵循最近一次的指令。
以下是一个多轮对话的示例:
from transformers import AutoModelForCausalLM, AutoTokenizer
classQwenChatbot:
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 = []
defgenerate_response(self, user_input):
messages = self.history + [{"role": "user", "content": user_input}]
text = self.tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True
)
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)
# 更新历史记录
self.history.append({"role": "user", "content": user_input})
self.history.append({"role": "assistant", "content": response})
return response
# 示例用法
if __name__ == "__main__":
chatbot = QwenChatbot()
# 第一次输入(未使用 /think 或 /no_think 标签,默认启用思考模式)
user_input_1 = "How many r's in strawberries?"
print(f"User: {user_input_1}")
response_1 = chatbot.generate_response(user_input_1)
print(f"Bot: {response_1}")
print("----------------------")
# 第二次输入,使用 /no_think
user_input_2 = "Then, how many r's in blueberries? /no_think"
print(f"User: {user_input_2}")
response_2 = chatbot.generate_response(user_input_2)
print(f"Bot: {response_2}")
print("----------------------")
# 第三次输入,使用 /think
user_input_3 = "Really? /think"
print(f"User: {user
53AI,企业落地大模型首选服务商
产品:场景落地咨询+大模型应用平台+行业解决方案
承诺:免费场景POC验证,效果验证后签署服务协议。零风险落地应用大模型,已交付160+中大型企业
2025-04-28
大模型是否有必要本地化部署?
2025-04-28
给 AI 小白的说明书:什么是 Manus?AI Agent为什么重要?
2025-04-28
MCP爆火背后:AI Agent的生产力时代来了吗?
2025-04-28
AI助力!明文密码泄漏无处遁形【大模型应用实践系列二】
2025-04-28
大模型应用实践(一):AI助力Code Review安全漏洞发现
2025-04-28
安全沙箱构筑智能体防护壁垒:解码OpenAI百万悬赏背后的安全困局
2025-04-28
从MCP到超级Agent:这场AI生产力革命将淘汰谁?
2025-04-28
MCP实战之Agent自主决策-让 AI玩转贪吃蛇
2024-08-13
2024-06-13
2024-08-21
2024-09-23
2024-07-31
2024-05-28
2024-08-04
2024-04-26
2024-07-09
2024-09-17