ChatGLM2 PyCharm运行环境部署
1、资料下载
ChatGLM2 源码下载地址:
https://github.com/THUDM/ChatGLM2-6B
git clone https://github.com/THUDM/ChatGLM2-6B
数据模型下载(需求全部下载):
https://huggingface.co/THUDM/chatglm2-6b/tree/main
git clone https://huggingface.co/THUDM/chatglm2-6b
有个清华大学云盘的模型地址,没试过
https://cloud.tsinghua.edu.cn/d/674208019e314311ab5c/?p=%2Fchatglm2-6b%2F&mode=list
2、Windows PyCharm环境部署
在下载的ChatGLM2-6B-main源码目录下新建个“THUDM\chatglm2-6b”目录,将模型文件都扔到这个目录下
创建python虚拟环境,需要安装PyCharm和Miniconda3,可以参考博客《YOLOv5 安装开发环境》
我们只需要安装这两个软件就行了。
然后创建我们的虚拟环境“ChatGLM”
conda create -n ChatGLM python==3.10
创建完成之后,直接用PyCharm打开我们的ChatGLM2-6B-main工程,然后设置Python解释器为我们刚刚创建的ChatGLM。
打开PyCharm终端,执行下面命令,安装依赖包
pip install -r requirements.txt -i https://mirror.baidu.com/pypi/simple
通过下面代码,来调用 ChatGLM2-6B 模型来生成对话(进入python)
python
>>> from transformers import AutoTokenizer, AutoModel
>>> tokenizer=AutoTokenizer.from_pretrained("THUDM\\chatglm2-6b",trust_remote_code=True)
>>> model=AutoModel.from_pretrained("THUDM\\chatglm2-6b", trust_remote_code=True, device='cpu')
>>> model = model.eval()
>>> response, history = model.chat(tokenizer, "你好", history=[])
>>> print(response)
你好6B,很高兴见到你,欢迎问我任何问题。
>>> response, history = model.chat(tokenizer, "晚上睡不着应该怎么办", history=history)
>>> print(response)
晚上睡不着可能会让你感到焦虑或不舒服,但以下是一些可以帮助你入睡的方法:
执行结果如下:
注意:如果使用GPU,device可以设置成“cuda”,cpu回答问题比较慢,博主这里回答第一个问题花了6分钟左右,第二个问题20多分钟。
如果使用GPU的话,需要安装cuda(请根据自己电脑cuda版本,安装对应cuda)
pip install torch==2.2.1+cu121 torchvision==0.17.1+cu121 -f https://download.pytorch.org/whl/torch_stable.html