一、背景
最近GPT不僅發(fā)布了GPT-4,而且解除封印可以聯(lián)網(wǎng)了。不得不贊嘆AI更新迭代的速度真快,都跟不上節(jié)奏了。但是大家也注意到了吧,隨著GPT的每次更新,OpenAI對(duì)其開(kāi)放使用的限制也越來(lái)越大。之前國(guó)內(nèi)網(wǎng)隨便訪問(wèn)GPT3,現(xiàn)在動(dòng)不動(dòng)就封號(hào)
所以,今天就來(lái)教大家部署國(guó)內(nèi)清華大學(xué)開(kāi)源的ChatGLM-6B。簡(jiǎn)單介紹下,ChatGLM是對(duì)話語(yǔ)言模型,對(duì)中文問(wèn)答和對(duì)話進(jìn)行了優(yōu)化。當(dāng)前訓(xùn)練模型有62億參數(shù),后續(xù)還會(huì)推出1300億參數(shù)的大模型,期待國(guó)內(nèi)的ChatGLM能越做越強(qiáng)大。
1. ChatGLM的開(kāi)源地址
ChatGLM的開(kāi)源地址: THUDM/ChatGLM-6B
2. ChatGLM-6B模型
https://huggingface.co/THUDM/chatglm-6b/tree/main
廢話不多說(shuō)了,直接上效果,以下是由ChatGLM中文對(duì)話的結(jié)果
(文末給大家準(zhǔn)備了ChatGLM的免費(fèi)體驗(yàn)地址 和 算力平臺(tái)免費(fèi)體驗(yàn)方式,一定看到文章結(jié)尾哦)
二、服務(wù)器選型
2.1. 運(yùn)行要求
硬件需求
量化等級(jí) | 最低 GPU 顯存(推理) | 最低 GPU 顯存(高效參數(shù)微調(diào)) |
---|---|---|
FP16(無(wú)量化) | 13 GB | 14 GB |
INT8 | 8 GB | 9 GB |
INT4 | 6 GB | 7 GB |
2.2. 服務(wù)器選型
官方說(shuō)明ChatGLM對(duì)硬件的配置要求至少13G的顯存
要準(zhǔn)備的東西如下:
一臺(tái)GPU云服務(wù)器(16GB顯存,32G內(nèi)存)
云服務(wù)器上已安裝好顯卡驅(qū)動(dòng)cuda和pytorch框架(平臺(tái)都有現(xiàn)成的鏡像,直接安裝即可)
再來(lái)說(shuō)說(shuō)服務(wù)器廠商的選擇,GPU服務(wù)器比較貴,所以小卷對(duì)比了一些大廠和小廠的GPU規(guī)格,這里只看配置符合要求且價(jià)錢合適的
廠商 | 配置 | 價(jià)錢 | 優(yōu)勢(shì) |
---|---|---|---|
阿里云 | 4核-15G內(nèi)存-16顯存NVIDIA T4 | 1878/月 | 大廠服務(wù),但是價(jià)錢太貴 |
騰訊云 | 10核-40G- NVIDIA T4 | 8.68/小時(shí) | 大廠服務(wù),但獨(dú)占1顆GPU價(jià)錢略高 |
華為云 | 8核-32G-16顯存NVIDIA T4 | 3542/月 | 大太貴 |
mistGPU | 8核-32G-24G顯存NVIDIA 3090 | 4.5/小時(shí) | 缺點(diǎn):只有1GB免費(fèi)存儲(chǔ) |
攬睿星舟 | 10核-40G-24G顯存NVIDIA 3090 | 1.9/小時(shí) | 推薦,配置高且價(jià)錢低,現(xiàn)在NVIDIA 3090有特價(jià) |
我們這里使用攬睿星舟這個(gè)算力平臺(tái)的服務(wù)器,價(jià)錢就是優(yōu)勢(shì)哦。需要注意的是,GPU服務(wù)器要選按量計(jì)費(fèi),就是你用的時(shí)候按使用時(shí)長(zhǎng)計(jì)費(fèi),不用時(shí)關(guān)掉就不會(huì)計(jì)費(fèi)
三、服務(wù)器配置
這一步購(gòu)買服務(wù)器并安裝環(huán)境,比較簡(jiǎn)單
3.1. 注冊(cè)使用
官網(wǎng)地址:https://www.lanrui-ai.com/register?invitation_code=4126597164
3.2. 購(gòu)買服務(wù)器并安裝鏡像
在網(wǎng)站的算力市場(chǎng)購(gòu)買需要的服務(wù)器配置,這里我選的是3090-24G這款,點(diǎn)擊使用按鈕進(jìn)入鏡像安裝界面
運(yùn)行環(huán)境鏡像選【公共鏡像/pytorchofficial-1.12.1-cuda11.6-cudnn8-devel】
等待5分鐘左右,工作空間就創(chuàng)建好了,點(diǎn)擊 進(jìn)入-> JupyterLab進(jìn)入服務(wù)器,接下來(lái)就準(zhǔn)備ChatGLM的安裝就行了
四、部署ChatGLM
4.1. conda環(huán)境安裝
wget -c https://repo.anaconda.com/archive/Anaconda3-2021.05-Linux-x86_64.sh
cd /home/user
bash Anaconda3-2021.05-Linux-x86_64.sh
一路選擇yes
配置環(huán)境變量
vim /etc/profile
export PATH=/root/anaconda3/bin:$PATH
刷新環(huán)境變量
source /etc/profile
服務(wù)驗(yàn)證
conda -V
鏡像源配置
vim ~/.condarc
channels:
- http://mirrors.aliyun.com/anaconda/cloud/stackless
- https://mirrors.aliyun.com/anaconda/cloud/simpleitk
- https://mirrors.aliyun.com/anaconda/cloud/rdkit
- https://mirrors.aliyun.com/anaconda/cloud/rapidsai
- https://mirrors.aliyun.com/anaconda/cloud/qiime2
- https://mirrors.aliyun.com/anaconda/cloud/pyviz
- https://mirrors.aliyun.com/anaconda/cloud/pytorch3d
- https://mirrors.aliyun.com/anaconda/cloud/pytorch-test
- https://mirrors.aliyun.com/anaconda/cloud/pytorch
- https://mirrors.aliyun.com/anaconda/cloud/psi4
- https://mirrors.aliyun.com/anaconda/cloud/plotly
- https://mirrors.aliyun.com/anaconda/cloud/omnia
- https://mirrors.aliyun.com/anaconda/cloud/ohmeta
- https://mirrors.aliyun.com/anaconda/cloud/numba
- https://mirrors.aliyun.com/anaconda/cloud/msys2
- https://mirrors.aliyun.com/anaconda/cloud/mordred-descriptor
- https://mirrors.aliyun.com/anaconda/cloud/menpo
- https://mirrors.aliyun.com/anaconda/cloud/matsci
- https://mirrors.aliyun.com/anaconda/cloud/intel
- https://mirrors.aliyun.com/anaconda/cloud/idaholab
- https://mirrors.aliyun.com/anaconda/cloud/fermi
- https://mirrors.aliyun.com/anaconda/cloud/fastai
- https://mirrors.aliyun.com/anaconda/cloud/dglteam
- https://mirrors.aliyun.com/anaconda/cloud/deepmodeling
- https://mirrors.aliyun.com/anaconda/cloud/conda-forge
- https://mirrors.aliyun.com/anaconda/cloud/caffe2
- https://mirrors.aliyun.com/anaconda/cloud/c4aarch64
- https://mirrors.aliyun.com/anaconda/cloud/bioconda
- https://mirrors.aliyun.com/anaconda/cloud/biobakery
- https://mirrors.aliyun.com/anaconda/cloud/auto
- https://mirrors.aliyun.com/anaconda/cloud/Paddle
- https://mirrors.aliyun.com/anaconda/pkgs/r
- https://mirrors.aliyun.com/anaconda/pkgs/msys2
- https://mirrors.aliyun.com/anaconda/pkgs/main
- https://mirrors.aliyun.com/anaconda/pkgs/free
show_channel_urls: true
ssl_verify: true
allow_conda_downgrades: true
pip配置
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com
4.2. 下載ChatGLM源代碼
進(jìn)入Jupyter的頁(yè)面后,可以看到2個(gè)目錄,對(duì)目錄做下說(shuō)明:
- data目錄,存放數(shù)據(jù),平臺(tái)共享的
- imported_models目錄,存放預(yù)訓(xùn)練模型,等會(huì)需要下載
點(diǎn)擊data目錄下,可以看到ChatGLM-6B文件夾,里面是ChatGLM的源代碼。
注第一次是空的需要手動(dòng)下載源碼
如果沒(méi)有ChatGLM-6B目錄,那么我們這一步需要下載代碼,操作如下:
頁(yè)面打開(kāi)一個(gè)Terminal終端,在Terminal終端執(zhí)行命令
git clone https://github.com/THUDM/ChatGLM-6B.git
4.3. 下載模型
由于模型文件大需要執(zhí)行以下命令再拉取chatglm-6b模型
mkdir /home/user/imported_models/model/
cd /home/user/imported_models/model/
apt install git-lfs
git lfs install
git clone https://huggingface.co/THUDM/chatglm-6b
4.4. 安裝依賴
- 1.執(zhí)行命令切換到ChatGLM-6B的目錄
cd ChatGLM-6B
- 2.接著修改requirements.txt文件,把后續(xù)所有需要的依賴都加上,下面的配置加在文件末尾即可,如果文件里已加上這3個(gè)依賴,無(wú)需再修改。
vim requirements.txt
chardet
streamlit
streamlit-chat
- 3.加完之后save保存下文件,如圖
- 4.接著命令行執(zhí)行下面命令安裝依賴
#使用默認(rèn)鏡像源下載會(huì)超時(shí),這里用了清華的pip鏡像源地址
創(chuàng)建虛擬環(huán)境
conda create -n ChatGLM python==3.10
conda init bash
重新打開(kāi)一個(gè)新的窗口執(zhí)行激活環(huán)境
conda activate ChatGLM
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple/
這一步可能會(huì)執(zhí)行報(bào)錯(cuò)
.ERROR: Could not install packages due to an OSError: Missing dependencies for SOCKS support
解決方法:切換到root用戶后再執(zhí)行命令
# 切換root用戶
sudo su
# 重新執(zhí)行
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple/
4.4. 啟動(dòng)腳本修改
因?yàn)槟P褪菃为?dú)文件夾存儲(chǔ)的,所以需要修改啟動(dòng)腳本中讀模型文件的代碼
為了能從公網(wǎng)訪問(wèn)我們的ChatGLM,需要修改監(jiān)聽(tīng)地址為0.0.0.0,端口為27777,這個(gè)是攬睿星舟平臺(tái)的調(diào)試地址
修改步驟:
- 1.修改web_demo2.py文件中的模型路徑,替換為模型的絕對(duì)路徑,修改方法如下:
vim web_demo2.py
修改前的代碼
tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True)
model = AutoModel.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True).half().cuda()
修改后的代碼
tokenizer = AutoTokenizer.from_pretrained("/home/user/imported_models/model/chatglm-6b", trust_remote_code=True)
model = AutoModel.from_pretrained("/home/user/imported_models/model/chatglm-6b", trust_remote_code=True).half().cuda()
修改完后ctrl + s保存一下
4.5. 啟動(dòng)ChatGLM
在ChatGLM-6B目錄下,命令行執(zhí)行
python3 -m streamlit run ./web_demo2.py --server.port 27777 --server.address 0.0.0.0
啟動(dòng)ChatGLM的webui界面
看到http://0.0.0.0:27777字樣說(shuō)明成功啟動(dòng)了
五、使用
我們需要從瀏覽器訪問(wèn)剛部署的服務(wù),回到攬睿星舟平臺(tái)
在工作空間頁(yè)面上點(diǎn)擊自定義服務(wù)拷貝調(diào)試鏈接,然后把拷貝的鏈接在瀏覽器上打開(kāi)
然后你就可以在這個(gè)頁(yè)面開(kāi)始對(duì)話了
注意第一次對(duì)話時(shí),程序會(huì)加載模型文件,耗時(shí)會(huì)比較長(zhǎng),可以從剛才啟動(dòng)的命令行查看加載進(jìn)度。
等第一次加載完成后,后面再對(duì)話,響應(yīng)就很快了
六、對(duì)話效果
到這一步,所有安裝部署過(guò)程就成功完成了,我們來(lái)看看效果吧,拷貝的鏈接在手機(jī)端也能打開(kāi),下面是手機(jī)端的效果
七、關(guān)閉服務(wù)
因?yàn)槲覀兊姆?wù)按使用量收費(fèi)的,所以不用時(shí)在頁(yè)面上點(diǎn)擊停止運(yùn)行即可
八、重啟服務(wù)
想重新運(yùn)行服務(wù)的時(shí)候,點(diǎn)工作空間頁(yè)面上的啟動(dòng)按鈕。工作空間重新創(chuàng)建后,進(jìn)入紅色Jupyter,通過(guò)命令行再次啟動(dòng)
實(shí)例重啟后conda環(huán)境需要重新安裝
cd /home/user
bash Anaconda3-2021.05-Linux-x86_64.sh
一路選擇yes
配置環(huán)境變量
vim /etc/profile
export PATH=/root/anaconda3/bin:$PATH
刷新環(huán)境變量
source /etc/profile
服務(wù)驗(yàn)證
conda -V
cd /home/user/data/ChatGLM-6B/
# 創(chuàng)建虛擬環(huán)境
conda create -n ChatGLM python==3.10
conda init bash
# 重新打開(kāi)一個(gè)新的窗口執(zhí)行激活環(huán)境
conda activate ChatGLM
安裝依賴
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple/
啟動(dòng)ChatGLM
python3 -m streamlit run ./web_demo2.py --server.port 27777 --server.address 0.0.0.0
如果報(bào)錯(cuò)請(qǐng)參考下面的異常匯總文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-521432.html
六、異常匯總
6.1. not found icetk
ImportError: This modeling file requires the following packages that were not found in your environment: icetk. Run pip install icetk
解決方案文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-521432.html
cd data/ChatGLM-6B/
pip install icetk
python3 -m streamlit run ./web_demo2.py --server.port 27777 --server.address 0.0.0.0
到了這里,關(guān)于云服務(wù)器部署開(kāi)源ChatGLM-6B,讓你也能擁有自己的ChatGPT的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!