【大模型】-modelscope魔搭

魔搭社区,类似huggingface,是中文库

1.需要设置环境变量,和huggingface一样,这样魔搭社区的模型就会下载到下面的目录

复制代码
setx MODELSCOPE_CACHE "D:\modelscope\models"
 setx MODELSCOPE_DATASETS_CACHE "D:\langChain\modelscope\datasets"

2.下载魔搭对应的框架modelscope , huggingface对应的框架是transformers

pip install modelscope

如何使用这个Qwen/Qwen3-235B-A22B模型呢。魔搭有代码,直接copy

代码

python 复制代码
from modelscope import AutoModelForCausalLM, AutoTokenizer

model_name = "Qwen/Qwen3-235B-A22B"

# 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 = 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)

上面from modelscope import AutoModelForCausalLM, AutoTokenizer后,引入模型就会下载到环境变量目录

相关推荐
寻星探路21 分钟前
【深度长文】万字攻克网络原理:从 HTTP 报文解构到 HTTPS 终极加密逻辑
java·开发语言·网络·python·http·ai·https
ValhallaCoder3 小时前
hot100-二叉树I
数据结构·python·算法·二叉树
猫头虎4 小时前
如何排查并解决项目启动时报错Error encountered while processing: java.io.IOException: closed 的问题
java·开发语言·jvm·spring boot·python·开源·maven
八零后琐话4 小时前
干货:程序员必备性能分析工具——Arthas火焰图
开发语言·python
青春不朽5125 小时前
Scrapy框架入门指南
python·scrapy
MZ_ZXD0016 小时前
springboot旅游信息管理系统-计算机毕业设计源码21675
java·c++·vue.js·spring boot·python·django·php
全栈老石6 小时前
Python 异步生存手册:给被 JS async/await 宠坏的全栈工程师
后端·python
梨落秋霜7 小时前
Python入门篇【模块/包】
python
阔皮大师8 小时前
INote轻量文本编辑器
java·javascript·python·c#