当前位置: 首页 > news >正文

Qwen3-8B安装与体验-速度很快!

目录

步骤一、下载模型

步骤二、安装模型依赖包

步骤三、写代码

步骤四、运行代码

步骤五、中文提问 & 运行代码


步骤一、下载模型

安装下载命令

pip install modelscope
 

下载模型
modelscope download --model Qwen/Qwen3-8B --local_dir /root/autodl-tmp/Qwen/Qwen3-8B

步骤二、安装模型依赖包

pip install transformers peft diffusers

步骤三、写代码

from modelscope import AutoModelForCausalLM, AutoTokenizermodel_name = "/root/autodl-tmp/Qwen/Qwen3-8B"# load the tokenizer and the model
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name,torch_dtype="auto",device_map="auto"
)
# prepare the model input
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 # Switches between thinking and non-thinking modes. Default is True.
)
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)# conduct text completion
generated_ids = model.generate(**model_inputs,max_new_tokens=32768
)
output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist() # parsing thinking content
try:# rindex finding 151668 (</think>)index = len(output_ids) - output_ids[::-1].index(151668)
except 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:", thinking_content)
print("content:", content)

步骤四、运行代码

python startqw3.py

步骤五、中文提问 & 运行代码

from modelscope import AutoModelForCausalLM, AutoTokenizermodel_name = "/root/autodl-tmp/Qwen/Qwen3-8B"# load the tokenizer and the model
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name,torch_dtype="auto",device_map="auto"
)
# prepare the model input
prompt = "1.我今年42岁是个程序员。2.我三高,高血压高血糖高血脂。3.不算工资收入,我如何在最短的时间内赚够一百万。请用中文思考和回复"
messages = [{"role": "user", "content": prompt}
]
text = tokenizer.apply_chat_template(messages,tokenize=False,add_generation_prompt=True,enable_thinking=True # Switches between thinking and non-thinking modes. Default is True.
)
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)# conduct text completion
generated_ids = model.generate(**model_inputs,max_new_tokens=32768
)
output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist() # parsing thinking content
try:# rindex finding 151668 (</think>)index = len(output_ids) - output_ids[::-1].index(151668)
except 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:", thinking_content)
print("content:", content)

相关文章:

  • 国内无法访问GitHub官网的问题解决
  • 碰到的 MRCPv2 串线以及解决思路
  • C语言Makefile编写与使用指南
  • centos7 安装python3
  • IIC小记
  • Wi-SUN与LoRa和NB-IoT通信技术的对比
  • Femap许可分配和监控
  • API文档生成与测试工具推荐
  • CSS in JS:机遇与挑战的思考
  • 微服务架构详解:从概念到实践
  • 集群与存储-lvs-nat实验
  • Origin将普通散点图升级为清晰的基因分组差异蜂群图
  • 使用 v-print 实现 Vue 项目中的打印功能
  • Windows Server 2019搭建iis服务器
  • 小市值策略复现(A股选股框架回测系统)
  • CORS跨域学习
  • 第十六届蓝桥杯大赛网安组--几道简单题的WP
  • C++中vector的扩容过程是怎样的?
  • 折叠机处理流程
  • DOM 事件的处理通常分为三个阶段:捕获、目标、冒泡【前端示例】
  • 屠呦呦当选美国国家科学院外籍院士
  • 解读|特朗普“助攻”下加拿大自由党“惨胜”,卡尼仍需克服“特鲁多阴影”
  • 外交部亚洲司司长刘劲松向菲方严肃交涉
  • 日本希望再次租借大熊猫,外交部:双方就相关合作保持密切沟通
  • 太好玩了!坐进大卫·霍克尼的敞篷车进入他画笔下的四季
  • 葡萄牙、西班牙突发大范围停电,交通和通信服务受到严重影响