从 智谱清言 官方 GitHub README 翻译整理的快速上手指南。
ChatGLM-6B 是由智谱AI与清华大学开源的中英双语对话语言模型,基于 General Language Model (GLM) 架构,拥有 62 亿参数。它采用了与 ChatGPT 相似的技术路线,针对中文问答和对话进行了深度优化。
该模型最大的亮点在于低门槛部署:通过模型量化技术,INT4 量化级别下最低仅需 6GB 显存即可在消费级显卡上运行。模型权重对学术研究完全开放,填写问卷登记后也允许免费商业使用。
ChatGLM-6B 主要面向以下场景:
# 1. 克隆仓库
git clone https://github.com/THUDM/ChatGLM-6B.git
cd ChatGLM-6B
# 2. 安装依赖
pip install -r requirements.txt
# 3. 安装 PyTorch(根据 CUDA 版本选择)
# CUDA 11.6
pip install torch==1.13.1+cu116 --extra-index-url https://download.pytorch.org/whl/cu116
# CUDA 11.7
pip install torch==1.13.1+cu117 --extra-index-url https://download.pytorch.org/whl/cu117
模型权重会自动从 Hugging Face 下载,国内访问受限时可使用镜像站点:
# 设置镜像
export HF_ENDPOINT=https://hf-mirror.com
创建 cli_demo.py:
from transformers import AutoTokenizer, AutoModel
tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True)
model = AutoModel.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True).half().cuda()
model = model.eval()
# 多轮对话
history = []
while True:
query = input("\n用户: ")
if query == "exit":
break
response, history = model.chat(tokenizer, query, history=history)
print(f"ChatGLM: {response}")
运行:
python cli_demo.py
# 启动 Gradio 界面
python web_demo.py
访问 http://localhost:7860 即可在浏览器中对话。
ptuning/README.md 包含基于 P-Tuning v2 的详细微调教程注意事项:
翻译整理自 GitHub README,原文:https://github.com/THUDM/ChatGLM-6B