返回顶部
热门问答 更多热门问答
技术文章 更多技术文章

Qwen3来啦

[复制链接]
链载Ai 显示全部楼层 发表于 2 小时前 |阅读模式 打印 上一主题 下一主题

Qwen3 特点

Qwen3 是 Qwen 系列的最新一代大型语言模型,提供了一系列密集型和专家混合(MoE)模型。基于广泛的训练,Qwen3 在推理能力、指令遵循、代理能力以及多语言支持方面取得了突破性的进展,主要特点如下:

  • 支持在单一模型内无缝切换思考模式(用于复杂的逻辑推理、数学和编程)和非思考模式(用于高效、通用的对话),确保在各种场景下都能实现最佳性能。
  • 推理能力显著提升,在思考模式下超越了之前的 QwQ(思考模式)和 Qwen2.5 指令模型(非思考模式),在数学、代码生成和常识逻辑推理方面表现出色。
  • 更好地符合人类偏好,在创意写作、角色扮演、多轮对话和指令遵循方面表现出色,能够提供更自然、引人入胜且沉浸式的对话体验。
  • 强大的代理能力,能够在思考和非思考模式下精准地与外部工具集成,并在复杂的基于代理的任务中实现开源模型中的领先性能。
  • 支持 100 多种语言和方言,具备强大的多语言指令遵循和翻译能力。

模型概览

Qwen3-0.6B具有以下特点:

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


快速上手

Qwen3 的代码已集成到最新的 Hugging Facetransformers中,建议您使用最新版本的transformers

如果您使用的是transformers<4.51.0,将会遇到以下错误:

KeyError: 'qwen3'

以下是一个代码片段,展示如何使用该模型根据给定输入生成内容:

fromtransformersimportAutoModelForCausalLM, 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)
exceptValueError:
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.5sglang>=0.4.5.post2创建一个与 OpenAI 兼容的 API 端点:

  • vLLM:
    vllm serve Qwen/Qwen3-0.6B --enable-reasoning --reasoning-parser DeepSeek_r1
  • SGLang:
    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,以在多轮对话中逐轮切换模型的思考模式。模型将遵循最近一次的指令。

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

fromtransformersimportAutoModelForCausalLM, 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})

returnresponse

# 示例用法
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

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

链载AI是专业的生成式人工智能教程平台。提供Stable Diffusion、Midjourney AI绘画教程,Suno AI音乐生成指南,以及Runway、Pika等AI视频制作与动画生成实战案例。从提示词编写到参数调整,手把手助您从入门到精通。
  • 官方手机版

  • 微信公众号

  • 商务合作

  • Powered by Discuz! X3.5 | Copyright © 2025-2025. | 链载Ai
  • 桂ICP备2024021734号 | 营业执照 | |广西笔趣文化传媒有限公司|| QQ