模型简介
模型特点
模型能力
使用案例
license: apache-2.0 base_model:
- Qwen/Qwen3-235B-A22B library_name: transformers
Qwen3-235B-A22B-AWQ
由Eric Hartford上传
复制自Modelscope https://www.modelscope.cn/models/swift/Qwen3-235B-A22B-AWQ
原始模型 https://huggingface.co/Qwen/Qwen3-235B-A22B
Modelscope AWQ 模型卡片:
import torch from modelscope import AutoModelForCausalLM, AutoTokenizer
model_name = "swift/Qwen3-235B-A22B-AWQ"
加载分词器和模型
tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForCausalLM.from_pretrained( model_name, torch_dtype=torch.float16, device_map="auto" )
准备模型输入
prompt = "给我一个关于大语言模型的简短介绍。" 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 () 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)
原始模型卡片
Qwen3-235B-A22B
Qwen3 亮点
Qwen3 是Qwen系列最新一代的大语言模型,提供了一系列密集和混合专家(MoE)模型。基于广泛的训练,Qwen3在推理、指令遵循、代理能力和多语言支持方面取得了突破性进展,具有以下关键特点:
- 独特支持在单一模型中无缝切换思考模式(用于复杂的逻辑推理、数学和编码)和非思考模式(用于高效的通用对话),确保在各种场景下的最佳性能。
- 显著增强的推理能力,在数学、代码生成和常识逻辑推理方面超越了之前的QwQ(思考模式)和Qwen2.5指令模型(非思考模式)。
- 卓越的人类偏好对齐,在创意写作、角色扮演、多轮对话和指令遵循方面表现出色,提供更自然、吸引人和沉浸式的对话体验。
- 在代理能力方面的专长,能够在思考和非思考模式下精确集成外部工具,并在复杂的基于代理的任务中实现开源模型的领先性能。
- 支持100多种语言和方言,具有强大的多语言指令遵循和翻译能力。
模型概述
Qwen3-235B-A22B 具有以下特点:
- 类型:因果语言模型
- 训练阶段:预训练和后训练
- 参数数量:总计235B,激活22B
- 非嵌入参数数量:234B
- 层数:94
- 注意力头数(GQA):Q为64,KV为4
- 专家数量:128
- 激活专家数量:8
- 上下文长度:原生支持32,768,通过YaRN支持131,072 tokens。
更多细节,包括基准评估、硬件要求和推理性能,请参考我们的博客、GitHub和文档。
快速开始
Qwen3-MoE的代码已集成到最新版本的Hugging Face transformers
中,建议使用最新版本的transformers
。
如果使用transformers<4.51.0
,会遇到以下错误:
KeyError: 'qwen3_moe'
以下代码片段展示了如何根据给定输入使用模型生成内容。
from transformers import AutoModelForCausalLM, AutoTokenizer
model_name = "Qwen/Qwen3-235B-A22B"
# 加载分词器和模型
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 # 在思考模式和非思考模式之间切换。默认为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)
print("内容:", content)
对于部署,可以使用sglang>=0.4.6.post1
或vllm>=0.8.5
创建与OpenAI兼容的API端点:
- SGLang:
python -m sglang.launch_server --model-path Qwen/Qwen3-235B-A22B --reasoning-parser qwen3 --tp 8
- vLLM:
vllm serve Qwen/Qwen3-235B-A22B --enable-reasoning --reasoning-parser deepseek_r1
对于本地使用,Ollama、LMStudio、MLX-LM、llama.cpp和KTransformers等应用程序也已支持Qwen3。
在思考模式和非思考模式之间切换
[!TIP]
enable_thinking
开关在SGLang和vLLM创建的API中也可用。 请参考我们的SGLang和vLLM用户文档。
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 # enable_thinking的默认值为True
)
在此模式下,模型将生成包裹在<think>...</think>
块中的思考内容,然后是最终响应。
[!NOTE] 对于思考模式,使用
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 # 设置enable_thinking=False禁用思考模式
)
在此模式下,模型不会生成任何思考内容,也不会包含<think>...</think>
块。
[!NOTE] 对于非思考模式,建议使用
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-235B-A22B"):
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()
# 第一个输入(不带/think或/no_think标签,默认启用思考模式)
user_input_1 = "草莓中有多少个r?"
print(f"用户: {user_input_1}")
response_1 = chatbot.generate_response(user_input_1)
print(f"机器人: {response_1}")
print("----------------------")
# 第二个输入带/no_think
user_input_2 = "那么,蓝莓中有多少个r? /no_think"
print(f"用户: {user_input_2}")
response_2 = chatbot.generate_response(user_input_2)
print(f"机器人: {response_2}")
print("----------------------")
# 第三个输入带/think
user_input_3 = "真的吗? /think"
print(f"用户: {user_input_3}")
response_3 = chatbot.generate_response(user_input_3)
print(f"机器人: {response_3}")
[!NOTE] 为了API兼容性,当
enable_thinking=True
时,无论用户使用/think
还是/no_think
,模型总是会输出一个包裹在<think>...</think>
中的块。但是,如果思考被禁用,该块中的内容可能为空。 当enable_thinking=False
时,软开关无效。无论用户输入任何/think
或/no_think
标签,模型都不会生成思考内容,也不会包含<think>...</think>
块。
代理使用
Qwen3在工具调用能力方面表现出色。我们建议使用Qwen-Agent以充分利用Qwen3的代理能力。Qwen-Agent内部封装了工具调用模板和工具调用解析器,大大降低了编码复杂度。
要定义可用的工具,可以使用MCP配置文件、使用Qwen-Agent的集成工具或自行集成其他工具。
from qwen_agent.agents import Assistant
# 定义LLM
llm_cfg = {
'model': 'Qwen3-235B-A22B',
# 使用阿里云Model Studio提供的端点:
# 'model_type': 'qwen_dashscope',
# 'api_key': os.getenv('DASHSCOPE_API_KEY'),
# 使用与OpenAI API兼容的自定义端点:
'model_server': 'http://localhost:8000/v1', # api_base
'api_key': 'EMPTY',
# 其他参数:
# 'generate_cfg': {
# # 添加:当响应内容为`<think


