一、下载本地仓库
git clone https://github.com/THUDM/ChatGLM2-6B
cd ChatGLM2-6B
二、安装pip
curl https://bootstrap.pypa.io/get-pip.py | python3
#查看版本
pip3 --version
#查看python3安装包列表
pip3 list
参考:http://www.manongjc.com/detail/62-zuwklvrkupvaytn.html
三、安装依赖
pip3 install -r requirements.txt
pip3 install protobuf transformers==4.30.2 cpm_kernels torch>=2.0 gradio mdtex2html sentencepiece accelerate
四、从本地加载模型
brew install git-lfs
git lfs install
python3 -m pip install urllib3==1.26.6
#如果你从 Hugging Face Hub 上下载 checkpoint 的速度较慢,可以只下载模型实现
GIT_LFS_SKIP_SMUDGE=1 git clone https://huggingface.co/THUDM/chatglm2-6b
然后从这里手动下载模型参数文件
chatglm2-6b-int4 |
并将下载文件放到mac目录:/Users/用户名/apps/model/chatglm2-6b-int4。
如果对比tokenizer.model 文件跟Git上的大小,如果大小不一致,需要从git上下载过来。
参考:python 问题修复ImportError: urllib3 v2.0 only supports OpenSSL 1.1.1+_liwenchao1995的博客-优快云博客
五、网页版demo
打开Users/用户名/apps/ChatGLM2-6B/web_demo.py.修改代码
## 添加进去就行,控制内存池的高水位线比率,不然执行GLM后一段时间会因为内存占用过高报错
import os
os.environ['PYTORCH_MPS_HIGH_WATERMARK_RATIO'] = '0.0'
tokenizer = AutoTokenizer.from_pretrained("/Users/yanhongzhang/apps/model/chatglm2-6b-int4", trust_remote_code=True)
model = AutoModel.from_pretrained("/Users/yanhongzhang/apps/model/chatglm2-6b-int4", trust_remote_code=True).float()
#修改后,开启共享
demo.queue().launch(share=True, inbrowser=True)
pip3 install gradio
pip3 list
pip3 install torch torchvision torchaudio
python3 web_demo.py
六、API部署
pip install fastapi uvicorn
python api.py
curl -X POST "http://127.0.0.1:8000" \
-H 'Content-Type: application/json' \
-d '{"prompt": "你好", "history": []}'
七:相关参考
https://zhuanlan.zhihu.com/p/636711594
https://xie.infoq.cn/article/f71813ca7b3cedd231600453a
https://www.bilibili.com/read/cv22604870
https://github.com/THUDM/ChatGLM2-6B
https://www.dianjilingqu.com/761793.html