ChatGLM3 模型部署教程ChatGLM3 是智谱 AI 和清华大学 KEG 实验室联合发布的对话预训练模型。ChatGLM3-6B 是 ChatGLM3 系列中的开源模型,在保留了前两代模型对话流畅、部署门槛低等众多优秀特性的基础上,ChatGLM3-6B 引入了如下特性:
基础环境最低要求说明:
| 环境名称 | 版本信息 1 |
|---|---|
| Ubuntu | 22.04.4 LTS |
| Cuda | V12.1.105 |
| Python | 3.10.8 |
| NVIDIA Corporation | RTX 3090 |
租用实例 --》一键使用 --》进入“应用社区”--》搜索并选择“ChatGLM3” 大模型 --》选择 RTX 3090 GPU ,即可在平台进行实例创建。



项目实例 --》点击“WebSSH”--》执行“启动 web 页面”的相关命令


# 切换到项目工作目录
cd /ChatGLM3/basic_demo
# 激活 ChatGLM3 虚拟环境
conda activate ChatGLM3
# 运行 web_demo_gradio.py 文件
python web_demo_gradio.py


将获取到的链接复制到本地浏览器:
# 比如当前获取的地址如下:
http://hn-a.suanjiayun.com:31505


# 切换到 ChatGLM3 项目工作目录
cd /ChatGLM3/finetune_demo
# 激活 ChatGLM3 虚拟环境
conda activate ChatGLM3
# 在 ChatGLM3 环境中安装 requirements.txt 依赖
pip install -r requirements.txt

出现以上报错,需要修改 requirements.txt 文件
vim requirements.txt

鼠标往下滑,找到最后一行 双击键盘上的 d 键,即可快速删除
继续执行依赖安装命令
# 在 ChatGLM3 环境中安装 requirements.txt 依赖
pip install -r requirements.txt



我们使用 AdvertiseGen 数据集来进行微调。从 Google Drive 或者 Tsinghua Cloud 下载处理好的 AdvertiseGen 数据集,将解压后的 AdvertiseGen 目录放到本目录的 /data/ 下, 例如。/ChatGLM3/finetune_demo/data/AdvertiseGen

cd data/AdvertiseGen
tar -xzvf AdvertiseGen.tar.gz
这里的选项解释如下:
x 代表解压。z 代表 gzip 压缩(.gz)。v 代表在解压时显示过程(verbose 模式)。f 代表后面跟着的是文件名。
# 切换到 ChatGLM3 项目工作目录
cd /ChatGLM3/finetune_demo
# 创建 AdvertiseGen_fix.py 文件
vim AdvertiseGen_fix.py
import json
from typing import Union
from pathlib import Path
def _resolve_path(path: Union[str, Path]) -> Path:
return Path(path).expanduser().resolve()
def _mkdir(dir_name: Union[str, Path]):
dir_name = _resolve_path(dir_name)
if not dir_name.is_dir():
dir_name.mkdir(parents=True, exist_ok=False)
def convert_adgen(data_dir: Union[str, Path], save_dir: Union[str, Path]):
def _convert(in_file: Path, out_file: Path):
_mkdir(out_file.parent)
with open(in_file, encoding='utf-8') as fin:
with open(out_file, 'wt', encoding='utf-8') as fout:
for line in fin:
dct = json.loads(line)
sample = {'conversations': [{'role': 'user', 'content': dct['content']},
{'role': 'assistant', 'content': dct['summary']}]}
fout.write(json.dumps(sample, ensure_ascii=False) + '\n')
data_dir = _resolve_path(data_dir)
save_dir = _resolve_path(save_dir)
train_file = data_dir / 'train.json'
if train_file.is_file():
out_file = save_dir / train_file.relative_to(data_dir)
_convert(train_file, out_file)
dev_file = data_dir / 'dev.json'
if dev_file.is_file():
out_file = save_dir / dev_file.relative_to(data_dir)
_convert(dev_file, out_file)
convert_adgen('data/AdvertiseGen/AdvertiseGen', 'data/AdvertiseGen_fix')
# 运行 AdvertiseGen_fix.py 文件
python AdvertiseGen_fix.py
接着,我们仅需要将配置好的参数以命令行的形式传参给程序,就可以使用命令行进行高效微调。
python finetune_hf.py /ChatGLM3/finetune_demo/data/AdvertiseGen_fix /ChatGLM3/basic_demo/THUDM/ZhipuAI/chatglm3-6b configs/lora.yaml

出现以上问题,需要安装 nltk 依赖
# 安装 nltk 依赖
pip install nltk

再次执行微调命令
python finetune_hf.py /ChatGLM3/finetune_demo/data/AdvertiseGen_fix /ChatGLM3/basic_demo/THUDM/ZhipuAI/chatglm3-6b configs/lora.yaml

微调完成


python inference_hf.py output/checkpoint-500/ --prompt "类型#裙*版型#显瘦*材质#网纱*风格#性感*裙型#百褶*裙下摆#压褶*裙长#连衣裙*裙衣门襟#拉链*裙衣门襟#套头*裙款式#拼接*裙款式#拉链*裙款式#木耳边*裙款式#抽褶*裙款式#不规则"

点击此处,立即体验ChatGLM3!
