解锁对话新体验:ChatGLM3 模型微调教程(第一版本)

ChatGLM3 是一个对话预训练大模型,由清华大学 KEG 实验室和智谱 AI 共同就研究发布。其中ChatGLM3-6B 是 ChatGLM3 系列中的开源模型,不仅有前两代模型对话流畅、部署门槛低等众多优秀特性,还增加引入了如下特性:

  1. 更强大的基础模型: ChatGLM3-6B 的基础模型 ChatGLM3-6B-Base ,采用了更多样的训练数据、更充分的训练步数和更合理的训练策略。通过评测显示在语义、数学、推理、代码、知识等不同角度的数据集上,该基础模型在 10B 以下的基础模型中拥有最强性能 。
  2. 更完整的功能支持: ChatGLM3-6B 采用了全新设计的 Prompt 格式 ,除了正常的多轮对话外,同时也支持工具调用(Function Call)、代码执行(Code Interpreter)和 Agent 任务等复杂场景。
  3. 更全面的开源序列: 除了对话模型 ChatGLM3-6B 外,还开源了基础模型 ChatGLM3-6B-Base 、长文本对话模型 ChatGLM3-6B-32K 和进一步强化了对于长文本理解能力的 ChatGLM3-6B-128K。

一、大模型 ChatGLM3 微调步骤

基础环境最低要求:

环境名称 版本信息 1
Ubuntu 22.04.4 LTS
Cuda V12.1.105
Python 3.10.8
NVIDIA Corporation RTX 3090

1. 结束当前运行(按键盘上的 Ctrl + C)

2. 安装模型依赖库

  • 切换到项目目录、激活 ChatGLM3 虚拟环境、安装 requirements.txt 依赖

    切换到 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

3. 准备数据集

3.1 创建文件夹 data 以及 子文件夹 AdvertiseGen
  • 创建 data 文件夹
  • 同样的方式创建 AdvertiseGen 子文件夹
3.2 上传数据集

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

3.3 解压数据集
复制代码
cd data/AdvertiseGen
tar -xzvf AdvertiseGen.tar.gz

这里的选项解释如下:

  • x 代表解压。
  • z 代表 gzip 压缩(.gz)。
  • v 代表在解压时显示过程(verbose 模式)。
  • f 代表后面跟着的是文件名。
3.4 转换数据格式
  • 创建 AdvertiseGen_fix.py 文件

    切换到 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 文件

    运行 AdvertiseGen_fix.py 文件

    python AdvertiseGen_fix.py

4. 使用命令行开始微调,我们使用 lora 进行微调

接着,我们仅需要将配置好的参数以命令行的形式传参给程序,就可以使用命令行进行高效微调。

复制代码
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

微调完成

5. 微调验证以及推理

  • 找到模型保存点路径
  • 执行推理命令

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

相关推荐
serve the people几秒前
Agent知识库怎么解决海量文档数据的向量索引过度消耗内存的问题
人工智能
云飞云共享云桌面3 分钟前
佛山某机械加工设备工厂10个SolidWorks共享一台服务器的软硬件
大数据·运维·服务器·前端·网络·人工智能·性能优化
木棉知行者5 分钟前
(二)Python基本语句
开发语言·python
傻啦嘿哟7 分钟前
2026版基于Python的旅游景点推荐系统:技术解析与实现路径
开发语言·python
一水鉴天8 分钟前
整体设计 定稿 之17 从三种“闭”概念到 色调/文字/字体 中 三种字体(宋体/斜体/粗体)
人工智能
小陈phd8 分钟前
RAG从入门到精通(十四)——评估技术
人工智能·python
卡次卡次110 分钟前
注意点:多线程与多进程与在并行读-->并行cpu或者GPU处理--->并行写 的架构中,如何选择
linux·网络·python
一晌小贪欢11 分钟前
Python-12 Python生成器与yield:惰性求值的艺术
开发语言·python·python基础·python3·python小白·python生成器
jerryinwuhan14 分钟前
稿件整理以及意见
人工智能
简单的话*14 分钟前
Logback 日志按月归档并保留 180 天,超期自动清理的配置实践
java·前端·python