【大模型】-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 小时前
关于制作技术视频讲解的问卷调查
python
喵手10 小时前
Python爬虫实战:博物馆官网的“展览预告/正在热展”栏目,抓取展览名称、精确展期、具体展厅位置以及票务/预约规则(附CSV导出)!
爬虫·python·爬虫实战·零基础python爬虫教学·博物馆信息采集·采集展览预告/正在热展等·采集数据csv导出
喵手10 小时前
Python爬虫实战:电商实体消歧完整实战 - 从混乱店铺名到标准化知识库的工程化实现,一文带你搞定!
爬虫·python·算法·爬虫实战·零基础python爬虫教学·同名实体消除·从混乱店铺名到标准化知识库
aluluka11 小时前
Emacs折腾日记(三十六)——打造个人笔记系统
笔记·python·emacs
黎子越11 小时前
python相关练习
java·前端·python
小白学大数据11 小时前
实测数据:多进程、多线程、异步协程爬虫速度对比
开发语言·爬虫·python·php
小鸡吃米…11 小时前
机器学习 - 精确率与召回率
人工智能·python·机器学习
sonrisa_11 小时前
Python同一类不同方法中变量值的传递
开发语言·windows·python
逻极12 小时前
OpenClaw「Clawdbot/Moltbot」 深入解析:核心架构深度剖析
python·ai·架构·agent·ai编程·moltbot·openclaw
sayang_shao12 小时前
C++ ONNX Runtime 与 Python Ultralytics 库实现 YOLOv8 模型检测的区别
c++·python·yolo