【大模型】-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后,引入模型就会下载到环境变量目录

相关推荐
江华森10 分钟前
深入理解 Flask 实现原理
后端·python·flask
FriendshipT22 分钟前
Ultralytics:解读SPPF模块
人工智能·pytorch·python·深度学习·目标检测
AOwhisky41 分钟前
Python 基础语法(上篇 + 下篇)——综合自测题
linux·windows·python
埃博拉酱1 小时前
Pip/Conda 混用导致的 CRT 版本冲突问题:[WinError 1114] 动态链接库(DLL)初始化例程失败
windows·python
刘小八1 小时前
LangGraph 人机交互实战:Interrupt、人工审批与工作流恢复
人工智能·python·人机交互
YMWM_1 小时前
python-venv虚拟环境
linux·开发语言·python
Tbisnic2 小时前
23.大模型开发:深度学习----CNN 卷积神经网络 与 RNN 循环神经网络
人工智能·python·rnn·深度学习·cnn·ai大模型
CoderYanger2 小时前
视频切割脚本(Python版)
linux·开发语言·windows·后端·python·程序人生·职场和发展
迷途呀2 小时前
新闻头条后端:新闻缓存模块
前端·redis·python·缓存·fastapi
想会飞的蒲公英2 小时前
逻辑回归为什么叫回归,却用来做分类
人工智能·python·分类·回归·逻辑回归