【大模型】chatglm3-6b 本地推理

chatglm3-6b介绍

  • ChatGLM3-6B 是 ChatGLM 系列最新一代的开源模型,在保留了前两代模型对话流畅、部署门槛低等众多优秀特性的基础上,ChatGLM3-6B 引入了如下特性:

  • 更强大的基础模型: ChatGLM3-6B 的基础模型 ChatGLM3-6B-Base 采用了更多样的训练数据、更充分的训练步数和更合理的训练策略。在语义、数学、推理、代码、知识等不同角度的数据集上测评显示,ChatGLM3-6B-Base 具有在 10B 以下的预训练模型中最强的性能。
    更完整的功能支持: ChatGLM3-6B 采用了全新设计的 Prompt 格式,除正常的多轮对话外。同时原生支持工具调用(Function Call)、代码执行(Code Interpreter)和 Agent 任务等复杂场景。

  • 更全面的开源序列: 除了对话模型 ChatGLM3-6B 外,还开源了基础模型 ChatGLM-6B-Base、长文本对话模型 ChatGLM3-6B-32K。以上所有权重对学术研究完全开放,在填写问卷进行登记后亦允许免费商业使用。

官网

官网:https://www.chatglm.cn/
github: https://github.com/THUDM/ChatGLM

模型下载

国内使用 modelscope API下载,很简单:

pip install modelscope

from modelscope import snapshot_download
model_dir = snapshot_download("ZhipuAI/chatglm3-6b", revision = "v1.0.0")

下载后模型保存在 cache 中,mv 到当前路径:

mv /root/.cache/modelscope/hub/ZhipuAI/ .

推理

GPU推理

from modelscope import AutoTokenizer, AutoModel, snapshot_download
#model_dir = snapshot_download("ZhipuAI/chatglm3-6b", revision = "v1.0.0")
model_dir = "ZhipuAI/chatglm3-6b"
tokenizer = AutoTokenizer.from_pretrained(model_dir, trust_remote_code=True)
model = AutoModel.from_pretrained(model_dir, trust_remote_code=True).half().cuda()
model = model.eval()
response, history = model.chat(tokenizer, "你好", history=[])
print(response)
response, history = model.chat(tokenizer, "晚上睡不着应该怎么办", history=history)
print(response)

CPU 推理

from modelscope import AutoTokenizer, AutoModel, snapshot_download
#model_dir = snapshot_download("ZhipuAI/chatglm3-6b", revision = "v1.0.0")
model_dir = "ZhipuAI/chatglm3-6b"
tokenizer = AutoTokenizer.from_pretrained(model_dir, trust_remote_code=True)
#model = AutoModel.from_pretrained(model_dir, trust_remote_code=True).half().cuda()
model = AutoModel.from_pretrained(model_dir, trust_remote_code=True, device_map="cpu").float()
model = model.eval()
response, history = model.chat(tokenizer, "你好", history=[])
print(response)
response, history = model.chat(tokenizer, "晚上睡不着应该怎么办", history=history)
print(response)

更多使用方法

请参考:https://github.com/THUDM/ChatGLM

猜你喜欢

转载自blog.csdn.net/zengNLP/article/details/135030109