【笔记】在Mac上部署ChatGLM模型

前言

在Mac上部署ChatGLM模型

准备工作

  • Git Large File Storage(Git LFS)
1
brew install git-lfs

下载模型

  • 模型20多G,网速较好的情况下也需要耐心等待20分钟
1
GIT_CURL_VERBOSE=1 git clone https://huggingface.co/THUDM/chatglm-6b

下载依赖

1
pip3 install protobuf "transformers==4.27.1" "cpm_kernels" "torch>=1.10" gradio mdtex2html sentencepiece accelerate

下载MacM1适配的Torch

1
pip3 install --pre torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/nightly/cpu

编写Python代码调用模型

1
2
3
4
5
6
7
8
9
10
11
12
from transformers import AutoTokenizer, AutoModel


# 加载ChatGLM模型,指定ChatGLM模型的绝对路径(MacOS只能本地加载模型)
chatglm_path = '/root/model/chatglm-6b'
tokenizer = AutoTokenizer.from_pretrained(chatglm_path, trust_remote_code=True, revision="v0.1.0")
model = AutoModel.from_pretrained(chatglm_path, trust_remote_code=True, revision="v0.1.0").float().to('mps')
model = model.eval()

# 询问ChatGLM
response, history = model.chat(tokenizer, "你是谁", history=[])
print(response)
1
我是一个名为 ChatGLM-6B 的人工智能助手,是基于清华大学 KEG 实验室和智谱 AI 公司于 2023 年共同训练的语言模型开发的。我的任务是针对用户的问题和要求提供适当的答复和支持。

完成

参考文献

简书——Jane Wu