如何认识Qwen3?

360影视 动漫周边 2025-05-11 22:23 1

摘要:Qwen3是阿里巴巴集团发布的第三代大型语言模型系列,旨在推动人工智能在通用人工智能(AGI)和超级人工智能(ASI)方向的发展。Qwen3是Qwen系列中最新一代的大规模语言模型,提供了一系列密集型和专家混合(MoE)模型。基于广泛的训练,Qwen3 在推理

Qwen3是阿里巴巴集团发布的第三代大型语言模型系列,旨在推动人工智能在通用人工智能(AGI)和超级人工智能(ASI)方向的发展。Qwen3是Qwen系列中最新一代的大规模语言模型,提供了一系列密集型和专家混合(MoE)模型。基于广泛的训练,Qwen3 在推理、指令执行、代理能力和多语言支持方面实现了突破性进展,具有以下关键特性:

在单一模型内无缝切换思考模式(用于复杂的逻辑推理、数学和编程)和非思考模式(用于高效的通用对话),确保在各种场景下的最佳性能。显著增强其推理能力,在数学、代码生成和常识逻辑推理方面超越了之前的 QwQ(在思考模式下)和 Qwen2.5 指令模型(在非思考模式下)。优越的人类偏好对齐,擅长创意写作、角色扮演、多轮对话和指令执行,提供更加自然、吸引人和沉浸式的对话体验。在代理能力方面的专长,能够在思考和非思考模式下与外部工具精确集成,并在复杂代理任务中达到开源模型中的领先性能。支持100多种语言和方言,具有强大的多语言指令执行和翻译能力。

Qwen3-0.6B 具有以下特点:

类型:因果语言模型训练阶段:预训练 & 后训练参数数量:0.6B非嵌入参数数量:0.44B层数:28注意力头数(GQA):Q 为 16,KV 为 8上下文长度:32,768

下面的代码片段展示了如何根据给定输入使用该模型生成内容。

from transformers import AutoModelForCausalLM, AutoTokenizermodel_name = "Qwen/Qwen3-0.6B"# 加载分词器和模型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:# 索引查找 151668 ()index = len(output_ids) - output_ids[::-1].indexexcept ValueError:index = 0thinking_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)

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=False

我们提供了一个硬开关,可以严格禁用模型的思考行为,使其功能与之前的 Qwen2.5-Instruct 模型保持一致。这种模式在需要通过禁用思考来提高效率的场景中特别有用。

text = tokenizer.apply_chat_template(messages,tokenize=False,add_generation_prompt=True,enable_thinking=False #设置enable_thinking=False禁用思考)

在这种模式下,模型不会生成任何思考内容,并且不会包含 ... 块。

keyboard_arrow_down

我们提供了一种软开关机制,允许用户在 enable_thinking=True 时动态控制模型的行为。具体来说,您可以在用户提示或系统消息中添加 /think 和 /no_think 来逐轮切换模型的思考模式。在多轮对话中,模型将遵循最新的指令。

以下是一个多轮对话的例子:

from transformers import AutoModelForCausalLM, AutoTokenizerclass 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,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]):].tolistresponse = 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 Usageif __name__ == "__main__":chatbot = QwenChatbot# First input (without /think or /no_think tags, thinking mode is enabled by default)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),使用Temperature=0.6,TopP=0.95,TopK=20,以及MinP=0。不要使用贪婪解码,因为它可能导致性能下降和无尽重复。对于非思考模式(enable_thinking=False),建议使用Temperature=0.7,TopP=0.8,TopK=20,以及MinP=0。对于支持的框架,您可以在0到2之间调整presence_penalty参数以减少无尽重复。但是,使用较高的值偶尔会导致语言混杂和模型性能轻微下降。充足的输出长度: 对于大多数查询,我们建议使用32,768个令牌的输出长度。对于高度复杂的问题,如数学和编程竞赛中的问题,我们建议将最大输出长度设置为38,912个令牌。这为模型提供了足够的空间来生成详细而全面的回答,从而提高其整体性能。标准化输出格式:我们在基准测试时推荐使用提示来标准化模型输出。数学问题: 在提示中包括“请逐步推理,并将最终答案放在\boxed{}内。”选择题: 向提示中添加以下JSON结构以标准化回答:“请仅在answer字段中显示您的选择字母,例如,"answer": "C"。”

来源:做个明媚的女子

相关推荐