README.md 13.6 KB
Newer Older
D
duzx16 已提交
1
# ChatGLM-6B
A
Aohan Zeng 已提交
2

D
duzx16 已提交
3
## 介绍
A
Aohan Zeng 已提交
4

X
xiao9905 已提交
5 6 7
ChatGLM-6B 是一个开源的、支持中英双语的对话语言模型,基于 [General Language Model (GLM)](https://github.com/THUDM/GLM) 架构,具有 62 亿参数。结合模型量化技术,用户可以在消费级的显卡上进行本地部署(INT4 量化级别下最低只需 6GB 显存)。
ChatGLM-6B 使用了和 ChatGPT 相似的技术,针对中文问答和对话进行了优化。经过约 1T 标识符的中英双语训练,辅以监督微调、反馈自助、人类反馈强化学习等技术的加持,62 亿参数的 ChatGLM-6B 已经能生成相当符合人类偏好的回答。更多信息请参考我们的[博客](https://chatglm.cn/blog)

S
Shaw 已提交
8
不过,由于 ChatGLM-6B 的规模较小,目前已知其具有相当多的[**局限性**](#局限性),如事实性/数学逻辑错误,可能生成有害/有偏见内容,较弱的上下文能力,自我认知混乱,以及对英文指示生成与中文指示完全矛盾的内容。请大家在使用前了解这些问题,以免产生误解。更大的基于1300亿参数[GLM-130B](https://github.com/THUDM/GLM-130B)的ChatGLM正在内测开发中。
D
duzx16 已提交
9

D
duzx16 已提交
10 11
*Read this in [English](README_en.md).*

D
duzx16 已提交
12
## 更新信息
D
duzx16 已提交
13
**[2023/03/23]** 增加API部署(感谢 [@LemonQu-GIT](https://github.com/LemonQu-GIT))。增加Embedding量化模型[ChatGLM-6B-INT4-QE](https://huggingface.co/THUDM/chatglm-6b-int4-qe)。增加对基于Apple Silicon的Mac上GPU加速的支持。
D
duzx16 已提交
14

A
Aohan Zeng 已提交
15
**[2023/03/19]** 增加流式输出接口 `stream_chat`,已更新到网页版和命令行 Demo。修复输出中的中文标点。增加量化后的模型 [ChatGLM-6B-INT4](https://huggingface.co/THUDM/chatglm-6b-int4)
D
duzx16 已提交
16

D
duzx16 已提交
17 18
## 友情链接
以下是部分基于本仓库开发的开源项目:
D
duzx16 已提交
19
* [ChatGLM-MNN](https://github.com/wangzhaode/ChatGLM-MNN): 一个基于 MNN 的 ChatGLM-6B C++ 推理实现,支持根据显存大小自动分配计算任务给 GPU 和 CPU
D
duzx16 已提交
20 21
* [ChatGLM-Tuning](https://github.com/mymusise/ChatGLM-Tuning): 基于 LoRA 对 ChatGLM-6B 进行微调

D
duzx16 已提交
22 23 24 25
以下是部分针对本项目的教程/文档:
* [Windows部署文档](https://github.com/ZhangErling/ChatGLM-6B/blob/main/deployment_windows.md)

如果你有其他好的项目/教程的话,欢迎参照上述格式添加到README中并提出 [PR](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request-from-a-fork).
26

D
duzx16 已提交
27 28 29
## 使用方式

### 硬件需求
A
Aohan Zeng 已提交
30 31

| **量化等级**    | **最低 GPU 显存** |
A
Aohan Zeng 已提交
32 33 34 35
| -------------- | ----------------- |
| FP16(无量化)   | 13 GB             |
| INT8           | 10 GB              |
| INT4           | 6 GB               |
D
duzx16 已提交
36

A
Aohan Zeng 已提交
37 38 39
### 环境安装

使用 pip 安装依赖:`pip install -r requirements.txt`,其中 `transformers` 库版本推荐为 `4.26.1`,但理论上不低于 `4.23.1` 即可。
D
duzx16 已提交
40 41 42

### 代码调用 

A
Aohan Zeng 已提交
43
可以通过如下代码调用 ChatGLM-6B 模型来生成对话:
D
duzx16 已提交
44

A
Aohan Zeng 已提交
45
```python
A
Aohan Zeng 已提交
46 47 48
>>> 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()
D
duzx16 已提交
49
>>> model = model.eval()
A
Aohan Zeng 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
>>> response, history = model.chat(tokenizer, "你好", history=[])
>>> print(response)
你好👋!我是人工智能助手 ChatGLM-6B,很高兴见到你,欢迎问我任何问题
>>> response, history = model.chat(tokenizer, "晚上睡不着应该怎么办", history=history)
>>> print(response)
晚上睡不着可能会让你感到焦虑或不舒服,但以下是一些可以帮助你入睡的方法:

1. 制定规律的睡眠时间表:保持规律的睡眠时间表可以帮助你建立健康的睡眠习惯,使你更容易入睡尽量在每天的相同时间上床,并在同一时间起床
2. 创造一个舒适的睡眠环境:确保睡眠环境舒适,安静,黑暗且温度适宜可以使用舒适的床上用品,并保持房间通风
3. 放松身心:在睡前做些放松的活动,例如泡个热水澡,听些轻柔的音乐,阅读一些有趣的书籍等,有助于缓解紧张和焦虑,使你更容易入睡
4. 避免饮用含有咖啡因的饮料:咖啡因是一种刺激性物质,会影响你的睡眠质量尽量避免在睡前饮用含有咖啡因的饮料,例如咖啡,茶和可乐
5. 避免在床上做与睡眠无关的事情:在床上做些与睡眠无关的事情,例如看电影,玩游戏或工作等,可能会干扰你的睡眠
6. 尝试呼吸技巧:深呼吸是一种放松技巧,可以帮助你缓解紧张和焦虑,使你更容易入睡试着慢慢吸气,保持几秒钟,然后缓慢呼气

如果这些方法无法帮助你入睡,你可以考虑咨询医生或睡眠专家,寻求进一步的建议
D
duzx16 已提交
65
```
A
Aohan Zeng 已提交
66
完整的模型实现可以在 [Hugging Face Hub](https://huggingface.co/THUDM/chatglm-6b) 上查看。如果你从 Hugging Face Hub 上下载checkpoint的速度较慢,也可以从[这里](https://cloud.tsinghua.edu.cn/d/fb9f16d6dc8f482596c2/)手动下载。
D
duzx16 已提交
67 68 69

### Demo

D
duzx16 已提交
70
我们提供了一个基于 [Gradio](https://gradio.app) 的网页版 Demo 和一个命令行 Demo。使用时首先需要下载本仓库:
A
Aohan Zeng 已提交
71

D
duzx16 已提交
72 73 74 75
```shell
git clone https://github.com/THUDM/ChatGLM-6B
cd ChatGLM-6B
```
D
duzx16 已提交
76 77 78

#### 网页版 Demo

X
xiao9905 已提交
79
![web-demo](resources/web-demo.gif)
D
duzx16 已提交
80

A
Aohan Zeng 已提交
81
首先安装 Gradio:`pip install gradio`,然后运行仓库中的 [web_demo.py](web_demo.py)
D
duzx16 已提交
82 83 84 85 86

```shell
python web_demo.py
```

A
Aohan Zeng 已提交
87
程序会运行一个 Web Server,并输出地址。在浏览器中打开输出的地址即可使用。最新版 Demo 实现了打字机效果,速度体验大大提升。注意,由于国内 Gradio 的网络访问较为缓慢,启用 `demo.queue().launch(share=True, inbrowser=True)` 时所有网络会经过 Gradio 服务器转发,导致打字机体验大幅下降,现在默认启动方式已经改为 `share=False`,如有需要公网访问的需求,可以重新修改为 `share=True` 启动。
D
duzx16 已提交
88

D
duzx16 已提交
89
感谢 [@AdamBear](https://github.com/AdamBear) 实现了基于 Streamlit 的网页版 Demo,运行方式见[#117](https://github.com/THUDM/ChatGLM-6B/pull/117).
D
duzx16 已提交
90

D
duzx16 已提交
91 92 93 94
#### 命令行 Demo

![cli-demo](resources/cli-demo.png)

D
duzx16 已提交
95
运行仓库中 [cli_demo.py](cli_demo.py)
D
duzx16 已提交
96 97 98 99 100 101 102

```shell
python cli_demo.py
```

程序会在命令行中进行交互式的对话,在命令行中输入指示并回车即可生成回复,输入`clear`可以清空对话历史,输入`stop`终止程序。

D
duzx16 已提交
103
### API部署
D
duzx16 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
首先需要安装额外的依赖`pip install fastapi uvicorn`,然后运行仓库中的[api.py](api.py)
```shell
python api.py
```
默认部署在本地的8000端口,通过POST方法进行调用
```shell
curl -X POST "http://127.0.0.1:8000" \
     -H 'Content-Type: application/json' \
     -d '{"prompt": "你好", "history": []}'
```
得到的返回值为
```shell
{
  "response":"你好👋!我是人工智能助手 ChatGLM-6B,很高兴见到你,欢迎问我任何问题。",
  "history":[["你好","你好👋!我是人工智能助手 ChatGLM-6B,很高兴见到你,欢迎问我任何问题。"]],
  "status":200,
  "time":"2023-03-23 21:38:40"
}
```

D
duzx16 已提交
124 125
## 低成本部署
### 模型量化
A
Aohan Zeng 已提交
126
默认情况下,模型以 FP16 精度加载,运行上述代码需要大概 13GB 显存。如果你的 GPU 显存有限,可以尝试以量化方式加载模型,使用方法如下:
D
duzx16 已提交
127 128

```python
A
Aohan Zeng 已提交
129
# 按需修改,目前只支持 4/8 bit 量化
D
duzx16 已提交
130 131 132
model = AutoModel.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True).half().quantize(4).cuda()
```

A
Aohan Zeng 已提交
133 134
进行 2 至 3 轮对话后,8-bit 量化下 GPU 显存占用约为 10GB,4-bit 量化下仅需 6GB 占用。随着对话轮数的增多,对应消耗显存也随之增长,由于采用了相对位置编码,理论上 ChatGLM-6B 支持无限长的 context-length,但总长度超过 2048(训练长度)后性能会逐渐下降。

D
duzx16 已提交
135
模型量化会带来一定的性能损失,经过测试,ChatGLM-6B 在 4-bit 量化下仍然能够进行自然流畅的生成。使用 [GPT-Q](https://arxiv.org/abs/2210.17323) 等量化方案可以进一步压缩量化精度/提升相同量化精度下的模型性能,欢迎大家提出对应的 Pull Request。
D
duzx16 已提交
136

A
Aohan Zeng 已提交
137
**[2023/03/19]** 量化过程需要在内存中首先加载 FP16 格式的模型,消耗大概 13GB 的内存。如果你的内存不足的话,可以直接加载量化后的模型,仅需大概 5.2GB 的内存:
D
duzx16 已提交
138 139 140 141
```python
model = AutoModel.from_pretrained("THUDM/chatglm-6b-int4", trust_remote_code=True).half().cuda()
```

D
duzx16 已提交
142 143 144 145 146 147 148
**[2023/03/24]** 我们进一步提供了对Embedding量化后的模型,模型参数仅占用4.3 GB显存:
```python
model = AutoModel.from_pretrained("THUDM/chatglm-6b-int4-qe", trust_remote_code=True).half().cuda()
```



A
Aohan Zeng 已提交
149 150
### CPU 部署
如果你没有 GPU 硬件的话,也可以在 CPU 上进行推理,但是推理速度会更慢。使用方法如下(需要大概 32GB 内存)
D
duzx16 已提交
151 152 153
```python
model = AutoModel.from_pretrained("THUDM/chatglm-6b", trust_remote_code=True).float()
```
D
duzx16 已提交
154

D
duzx16 已提交
155
**[2023/03/19]** 如果你的内存不足,可以直接加载量化后的模型:
156
```python
D
duzx16 已提交
157
model = AutoModel.from_pretrained("THUDM/chatglm-6b-int4",trust_remote_code=True).float()
158 159
```

D
duzx16 已提交
160
如果遇到了报错 `Could not find module 'nvcuda.dll'` 或者 `RuntimeError: Unknown platform: darwin` (MacOS) 的话请参考这个[Issue](https://github.com/THUDM/ChatGLM-6B/issues/6#issuecomment-1470060041).
D
duzx16 已提交
161

D
duzx16 已提交
162
### Mac 上的 GPU 加速
D
duzx16 已提交
163
对于搭载了Apple Silicon的Mac(以及MacBook),可以使用 MPS 后端来在 GPU 上运行 ChatGLM-6B。首先需要参考 Apple 的 [官方说明](https://developer.apple.com/metal/pytorch) 安装 PyTorch-Nightly。然后将模型仓库 clone 到本地(需要先[安装Git LFS](https://docs.github.com/zh/repositories/working-with-files/managing-large-files/installing-git-large-file-storage)
D
duzx16 已提交
164
```shell
D
duzx16 已提交
165
git lfs install
D
duzx16 已提交
166 167 168 169 170 171 172 173
git clone https://huggingface.co/THUDM/chatglm-6b
```
将代码中的模型加载改为从本地加载,并使用 mps 后端
```python
model = AutoModel.from_pretrained("your local path", trust_remote_code=True).half().to('mps')
```
即可使用在 Mac 上使用 GPU 加速模型推理。

A
Aohan Zeng 已提交
174
## ChatGLM-6B 示例
X
xiao9905 已提交
175

A
Aohan Zeng 已提交
176
以下是一些使用 `web_demo.py` 得到的示例截图。更多 ChatGLM-6B 的可能,等待你来探索发现!
X
xiao9905 已提交
177 178

<details><summary><b>自我认知</b></summary>
X
xiao9905 已提交
179 180 181

![](examples/self-introduction.png)

X
xiao9905 已提交
182 183 184 185
</details>

<details><summary><b>提纲写作</b></summary>

X
xiao9905 已提交
186 187
![](examples/blog-outline.png)

X
xiao9905 已提交
188 189 190 191
</details>

<details><summary><b>文案写作</b></summary>

X
xiao9905 已提交
192 193 194 195
![](examples/ad-writing-2.png)

![](examples/comments-writing.png)

X
xiao9905 已提交
196 197 198 199
</details>

<details><summary><b>邮件写作助手</b></summary>

X
xiao9905 已提交
200 201 202 203
![](examples/email-writing-1.png)

![](examples/email-writing-2.png)

X
xiao9905 已提交
204 205 206 207
</details>

<details><summary><b>信息抽取</b></summary>

X
xiao9905 已提交
208 209
![](examples/information-extraction.png)

X
xiao9905 已提交
210 211 212 213
</details>

<details><summary><b>角色扮演</b></summary>

X
xiao9905 已提交
214 215
![](examples/role-play.png)

X
xiao9905 已提交
216 217
</details>

X
xiao9905 已提交
218
<details><summary><b>评论比较</b></summary>
X
xiao9905 已提交
219

X
xiao9905 已提交
220 221
![](examples/sport.png)

X
xiao9905 已提交
222 223 224 225
</details>

<details><summary><b>旅游向导</b></summary>

X
xiao9905 已提交
226 227 228 229
![](examples/tour-guide.png)

</details>

X
xiao9905 已提交
230 231
## 局限性

A
Aohan Zeng 已提交
232
由于 ChatGLM-6B 的小规模,其能力仍然有许多局限性。以下是我们目前发现的一些问题:
X
xiao9905 已提交
233

A
Aohan Zeng 已提交
234
- 模型容量较小:6B 的小容量,决定了其相对较弱的模型记忆和语言能力。在面对许多事实性知识任务时,ChatGLM-6B 可能会生成不正确的信息;它也不擅长逻辑类问题(如数学、编程)的解答。
X
xiao9905 已提交
235 236 237 238 239 240 241 242
    <details><summary><b>点击查看例子</b></summary>
    
    ![](limitations/factual_error.png)
    
    ![](limitations/math_error.png)
    
    </details>
  
A
Aohan Zeng 已提交
243
- 产生有害说明或有偏见的内容:ChatGLM-6B 只是一个初步与人类意图对齐的语言模型,可能会生成有害、有偏见的内容。(内容可能具有冒犯性,此处不展示)
X
xiao9905 已提交
244

X
xiao9905 已提交
245 246
- 英文能力不足:ChatGLM-6B 训练时使用的指示/回答大部分都是中文的,仅有极小一部分英文内容。因此,如果输入英文指示,回复的质量远不如中文,甚至与中文指示下的内容矛盾,并且出现中英夹杂的情况。

X
xiao9905 已提交
247 248 249 250 251 252 253 254 255 256 257
- 易被误导,对话能力较弱:ChatGLM-6B 对话能力还比较弱,而且 “自我认知” 存在问题,并很容易被误导并产生错误的言论。例如当前版本的模型在被误导的情况下,会在自我认知上发生偏差。
    <details><summary><b>点击查看例子</b></summary>

    ![](limitations/self-confusion_google.jpg)
    
    ![](limitations/self-confusion_openai.jpg)
    
    ![](limitations/self-confusion_tencent.jpg)
    
    </details>

A
Aohan Zeng 已提交
258 259 260 261
## 协议

本仓库的代码依照 [Apache-2.0](LICENSE) 协议开源,ChatGLM-6B 模型的权重的使用则需要遵循 [Model License](MODEL_LICENSE)

D
duzx16 已提交
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
## 引用

如果你觉得我们的工作有帮助的话,请考虑引用下列论文

```
@inproceedings{
  zeng2023glm-130b,
  title={{GLM}-130B: An Open Bilingual Pre-trained Model},
  author={Aohan Zeng and Xiao Liu and Zhengxiao Du and Zihan Wang and Hanyu Lai and Ming Ding and Zhuoyi Yang and Yifan Xu and Wendi Zheng and Xiao Xia and Weng Lam Tam and Zixuan Ma and Yufei Xue and Jidong Zhai and Wenguang Chen and Zhiyuan Liu and Peng Zhang and Yuxiao Dong and Jie Tang},
  booktitle={The Eleventh International Conference on Learning Representations (ICLR)},
  year={2023},
  url={https://openreview.net/forum?id=-Aw0rrrPUF}
}
```
```
@inproceedings{du2022glm,
  title={GLM: General Language Model Pretraining with Autoregressive Blank Infilling},
  author={Du, Zhengxiao and Qian, Yujie and Liu, Xiao and Ding, Ming and Qiu, Jiezhong and Yang, Zhilin and Tang, Jie},
  booktitle={Proceedings of the 60th Annual Meeting of the Association for Computational Linguistics (Volume 1: Long Papers)},
  pages={320--335},
  year={2022}
}
A
Aohan Zeng 已提交
284
```