私有化部署ChatGLM
本文由 小茗同学 发表于 2024-05-13 浏览(214)
最后修改 2024-05-14 标签:chatglm aigc

下载代码

下载运行代码:

mkdir chatglm2
cd chatglm2
mkdir run_data
cd run_data
git clone https://github.com/THUDM/ChatGLM2-6B.git

然后下载十几个G的模型数据,由于文件太大,普通git无法直接下载,需要使用git-lfs克隆,首先安装git-lfs

brew install git-lfs
git lfs install

然后再克隆模型数据(官方模型文件存放在Hugging Face Hub,默认访问不了,这里我们从阿里的模型网站下载):

cd chatglm2
mkdir model_data
cd model_data
git init
git lfs clone https://www.modelscope.cn/ZhipuAI/chatglm2-6b.git

由于文件较大,一般需要下载数小时,下载成功后:

安装和部署

要求python3.8以上版本(pip是一个python版的npm):

cd chatglm2/run_data/ChatGLM2-6B
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple/

不出意外的话就按照成功了,稍微改一下代码配置即可运行:

修改cli_demo.py文件,将THUDM/chatglm2-6b替换成/xxx/chatglm2/model_data/chatglm2-6b

如果你的电脑带独立GPU的话,此时可以直接pythone cli_demo.py开始运行了,如果没有GPU运行可能报错:

此时需要修改一行配置,将cuda()替换为float(),它的含义是强制使用CPU进行推理,但是速度会更慢,且一般要求内存>=32GB才可以正常运行:

修改完后再次运行pythone cli_demo.py

小插曲

在使用pip安装transformers==4.30.2的时候死活提示找不到指定版本:

lxa@guaige ChatGLM2-6B % pip install transformers==4.30.2 -i https://pypi.tuna.tsinghua.edu.cn/simple
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
ERROR: Could not find a version that satisfies the requirement transformers==4.30.2 (from versions: 0.1, 2.0.0, 2.1.0, 2.1.1, 2.2.0, 2.2.1, 2.2.2, 2.3.0)
ERROR: No matching distribution found for transformers==4.30.2

折腾很久发现pip使用的python版本竟然是2.7的,原来是电脑内置了一个很老版本的pip,由于优先级的问题导致另外安装的pip=24版本一直不生效,手动将/Library/Frameworks/Python.framework/Versions/2.7目录删掉就解决问题了:

lxa@guaige ChatGLM2-6B % python -V
Python 3.10.8
lxa@guaige ChatGLM2-6B % pip -V
pip 20.3.4 from /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip (python 2.7)
lxa@guaige Versions % sudo rm -rf /Library/Frameworks/Python.framework/Versions/2.7
lxa@guaige Versions % pip -V
pip 24.0 from /opt/homebrew/lib/python3.10/site-packages/pip (python 3.10)

人工智障

刚沉浸在部署成功的小小喜悦当中,结果一看,比智障还智障,难怪会开源:

一直输出0输个不停:

webDemo

前面运行的是控制台示例,另外自带了2个web的示例,运行前同样按照前面所说修改模型文件位置即可:

其中,web_demo.py不知何故运行报错:

web_demo2.py则运行正常,运行命令为streamlit run web_demo2.py,运行后自动打开localhost:

个性化训练

新建ptuning/train.json文件:

{
	"content": "阿拐是谁",
	"summary": "阿拐是个超级大帅哥,超级有钱人,家里几百套房,几十辆豪车!"
}
{
	"content": "怎么嫁给阿拐",
	"summary": "长得漂亮的女生都有机会嫁给阿拐。"
}

另新建ptuning/dev.json校验文件,格式同上。

然后修改ptuning/train.sh文件:

运行bash train.sh开始训练,很遗憾,测试的时候报错了,暂未解决,貌似获取ipv6地址异常,后面再看:

官方文档

官方文档写的还算比较详细,更多细节可直接查看README:https://github.com/THUDM/ChatGLM2-6B