基础模型: Qwen/Qwen3-32B
语言:
- 英语
库名称: transformers
许可证链接: https://huggingface.co/Qwen/Qwen3-32B/blob/main/LICENSE
许可证: apache-2.0
标签:
- qwen3
- qwen
- unsloth
- transformers
[!注意]
通过YaRN实现128K上下文长度支持。
Qwen3-32B
Qwen3亮点
Qwen3是Qwen系列最新一代大语言模型,提供全面的密集和混合专家(MoE)模型套件。基于广泛训练,Qwen3在推理、指令遵循、代理能力和多语言支持方面取得突破性进展,具有以下关键特性:
- 独特支持在单一模型内无缝切换思维模式(用于复杂逻辑推理、数学和编码)和非思维模式(用于高效通用对话),确保在各种场景下获得最佳性能。
- 显著增强推理能力,在数学、代码生成和常识逻辑推理方面超越之前的QwQ(思维模式)和Qwen2.5指令模型(非思维模式)。
- 卓越的人类偏好对齐,在创意写作、角色扮演、多轮对话和指令遵循方面表现出色,提供更自然、吸引人和沉浸式的对话体验。
- 擅长代理能力,在思维和非思维模式下精确集成外部工具,在复杂代理任务中实现开源模型领先性能。
- 支持100多种语言和方言,具备强大的多语言指令遵循和翻译能力。
模型概览
Qwen3-32B具有以下特性:
- 类型:因果语言模型
- 训练阶段:预训练和后训练
- 参数数量:32.8B
- 非嵌入参数数量:31.2B
- 层数:64
- 注意力头数(GQA):Q为64,KV为8
- 上下文长度:原生32,768,使用YaRN可达131,072 tokens。
更多详情,包括基准评估、硬件要求和推理性能,请参考我们的博客、GitHub和文档。
快速开始
Qwen3的代码已集成到最新版Hugging Face transformers
中,建议使用最新版transformers
。
使用transformers<4.51.0
时,会遇到以下错误:
KeyError: 'qwen3'
以下代码片段展示如何基于给定输入使用模型生成内容:
from transformers import AutoModelForCausalLM, AutoTokenizer
model_name = "Qwen/Qwen3-32B"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype="auto",
device_map="auto"
)
prompt = "给我一个关于大语言模型的简短介绍。"
messages = [
{"role": "user", "content": prompt}
]
text = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True,
enable_thinking=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:
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)
print("内容:", content)
部署时,可使用vllm>=0.8.5
或sglang>=0.4.5.post2
创建兼容OpenAI的API端点:
思维与非思维模式切换
[!提示]
enable_thinking
开关在vLLM和SGLang创建的API中同样可用。
更多详情请参考我们的文档。
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
)
在此模式下,模型将生成包裹在<think>...</think>
块中的思维内容,后跟最终响应。
[!注意]
对于思维模式,使用Temperature=0.6
、TopP=0.95
、TopK=20
和MinP=0
(generation_config.json
中的默认设置)。不要使用贪婪解码,否则可能导致性能下降和无限重复。更多详细指导请参考最佳实践部分。
enable_thinking=False
我们提供硬开关严格禁用模型的思维行为,使其功能与之前的Qwen2.5-Instruct模型对齐。此模式在需要禁用思维以提升效率的场景中特别有用。
text = tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True,
enable_thinking=False
)
在此模式下,模型不会生成任何思维内容,也不会包含<think>...</think>
块。
[!注意]
对于非思维模式,建议使用Temperature=0.7
、TopP=0.8
、TopK=20
和MinP=0
。更多详细指导请参考最佳实践部分。
高级用法:通过用户输入切换思维和非思维模式
我们提供软开关机制,允许用户在enable_thinking=True
时动态控制模型行为。具体来说,可在用户提示或系统消息中添加/think
和/no_think
在多轮对话中逐轮切换模型的思维模式。模型将遵循最近一次指令。
以下是多轮对话示例:
from transformers import AutoModelForCausalLM, AutoTokenizer
class QwenChatbot:
def __init__(self, model_name="Qwen/Qwen3-32B"):
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,
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()
user_input_1 = "草莓中有多少个r?"
print(f"用户: {user_input_1}")
response_1 = chatbot.generate_response(user_input_1)
print(f"机器人: {response_1}")
print("----------------------")
user_input_2 = "那么,蓝莓中有多少个r? /no_think"
print(f"用户: {user_input_2}")
response_2 = chatbot.generate_response(user_input_2)
print(f"机器人: {response_2}")
print("----------------------")
user_input_3 = "真的吗? /think"