Skip to content

Commit bd5bfb2

Browse files
committed
Init commit
0 parents  commit bd5bfb2

17 files changed

+910
-0
lines changed

DEPLOYMENT.md

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
## 低成本部署
2+
3+
### 模型量化
4+
5+
默认情况下,模型以 FP16 精度加载,运行上述代码需要大概 13GB 显存。如果你的 GPU 显存有限,可以尝试以量化方式加载模型,使用方法如下:
6+
7+
```python
8+
model = AutoModel.from_pretrained("THUDM/chatglm3-6b",trust_remote_code=True).quantize(4).cuda()
9+
```
10+
11+
模型量化会带来一定的性能损失,经过测试,ChatGLM3-6B 在 4-bit 量化下仍然能够进行自然流畅的生成。
12+
13+
### CPU 部署
14+
15+
如果你没有 GPU 硬件的话,也可以在 CPU 上进行推理,但是推理速度会更慢。使用方法如下(需要大概 32GB 内存)
16+
```python
17+
model = AutoModel.from_pretrained("THUDM/chatglm3-6b", trust_remote_code=True).float()
18+
```
19+
20+
### Mac 部署
21+
22+
对于搭载了 Apple Silicon 或者 AMD GPU 的 Mac,可以使用 MPS 后端来在 GPU 上运行 ChatGLM3-6B。需要参考 Apple 的 [官方说明](https://developer.apple.com/metal/pytorch) 安装 PyTorch-Nightly(正确的版本号应该是2.x.x.dev2023xxxx,而不是 2.x.x)。
23+
24+
目前在 MacOS 上只支持[从本地加载模型](README.md#从本地加载模型)。将代码中的模型加载改为从本地加载,并使用 mps 后端:
25+
```python
26+
model = AutoModel.from_pretrained("your local path", trust_remote_code=True).to('mps')
27+
```
28+
29+
加载半精度的 ChatGLM3-6B 模型需要大概 13GB 内存。内存较小的机器(比如 16GB 内存的 MacBook Pro),在空余内存不足的情况下会使用硬盘上的虚拟内存,导致推理速度严重变慢。
30+
31+
### 多卡部署
32+
如果你有多张 GPU,但是每张 GPU 的显存大小都不足以容纳完整的模型,那么可以将模型切分在多张GPU上。首先安装 accelerate: `pip install accelerate`,然后通过如下方法加载模型:
33+
```python
34+
from utils import load_model_on_gpus
35+
model = load_model_on_gpus("THUDM/chatglm3-6b", num_gpus=2)
36+
```
37+
即可将模型部署到两张 GPU 上进行推理。你可以将 `num_gpus` 改为你希望使用的 GPU 数。默认是均匀切分的,你也可以传入 `device_map` 参数来自己指定。

MODEL_LICENSE

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
The ChatGLM3-6B License
2+
3+
1. 定义
4+
5+
“许可方”是指分发其软件的 ChatGLM3-6B 模型团队。
6+
7+
“软件”是指根据本许可提供的 ChatGLM3-6B 模型参数。
8+
9+
2. 许可授予
10+
11+
根据本许可的条款和条件,许可方特此授予您非排他性、全球性、不可转让、不可再许可、可撤销、免版税的版权许可。
12+
13+
上述版权声明和本许可声明应包含在本软件的所有副本或重要部分中。
14+
15+
3.限制
16+
17+
您不得出于任何军事或非法目的使用、复制、修改、合并、发布、分发、复制或创建本软件的全部或部分衍生作品。
18+
19+
您不得利用本软件从事任何危害国家安全和国家统一、危害社会公共利益、侵犯人身权益的行为。
20+
21+
4.免责声明
22+
23+
本软件“按原样”提供,不提供任何明示或暗示的保证,包括但不限于对适销性、特定用途的适用性和非侵权性的保证。 在任何情况下,作者或版权持有人均不对任何索赔、损害或其他责任负责,无论是在合同诉讼、侵权行为还是其他方面,由软件或软件的使用或其他交易引起、由软件引起或与之相关 软件。
24+
25+
5. 责任限制
26+
27+
除适用法律禁止的范围外,在任何情况下且根据任何法律理论,无论是基于侵权行为、疏忽、合同、责任或其他原因,任何许可方均不对您承担任何直接、间接、特殊、偶然、示范性、 或间接损害,或任何其他商业损失,即使许可人已被告知此类损害的可能性。
28+
29+
6.争议解决
30+
31+
本许可受中华人民共和国法律管辖并按其解释。 因本许可引起的或与本许可有关的任何争议应提交北京市海淀区人民法院。
32+
33+
请注意,许可证可能会更新到更全面的版本。 有关许可和版权的任何问题,请通过 license@zhipuai.cn 与我们联系。
34+
35+
1. Definitions
36+
37+
“Licensor” means the ChatGLM3-6B Model Team that distributes its Software.
38+
39+
“Software” means the ChatGLM3-6B model parameters made available under this license.
40+
41+
2. License Grant
42+
43+
Subject to the terms and conditions of this License, the Licensor hereby grants to you a non-exclusive, worldwide, non-transferable, non-sublicensable, revocable, royalty-free copyright license to use the Software.
44+
45+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
46+
47+
3. Restriction
48+
49+
You will not use, copy, modify, merge, publish, distribute, reproduce, or create derivative works of the Software, in whole or in part, for any military, or illegal purposes.
50+
51+
You will not use the Software for any act that may undermine China's national security and national unity, harm the public interest of society, or infringe upon the rights and interests of human beings.
52+
53+
4. Disclaimer
54+
55+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
56+
57+
5. Limitation of Liability
58+
59+
EXCEPT TO THE EXTENT PROHIBITED BY APPLICABLE LAW, IN NO EVENT AND UNDER NO LEGAL THEORY, WHETHER BASED IN TORT, NEGLIGENCE, CONTRACT, LIABILITY, OR OTHERWISE WILL ANY LICENSOR BE LIABLE TO YOU FOR ANY DIRECT, INDIRECT, SPECIAL, INCIDENTAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES, OR ANY OTHER COMMERCIAL LOSSES, EVEN IF THE LICENSOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
60+
61+
6. Dispute Resolution
62+
63+
This license shall be governed and construed in accordance with the laws of People’s Republic of China. Any dispute arising from or in connection with this License shall be submitted to Haidian District People's Court in Beijing.
64+
65+
Note that the license is subject to update to a more comprehensive version. For any questions related to the license and copyright, please contact us at license@zhipuai.cn.

PROMPT.md

+198
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
## ChatGLM3 对话格式
2+
为了避免用户输入的注入攻击,以及统一 Code Interpreter,Tool & Agent 等任务的输入,ChatGLM3 采用了全新的对话格式。
3+
4+
### 规定
5+
#### 整体结构
6+
ChatGLM3 对话的格式由若干对话组成,其中每个对话包含对话头和内容,一个典型的多轮对话结构如下
7+
```text
8+
<|system|>
9+
You are ChatGLM3, a large language model trained by Zhipu.AI. Follow the user's instructions carefully. Respond using markdown.
10+
<|user|>
11+
Hello
12+
<|assistant|>
13+
Hello, I'm ChatGLM3. What can I assist you today?
14+
```
15+
16+
#### 对话头
17+
对话头占完整的一行,格式为
18+
```text
19+
<|role|>{metadata}
20+
```
21+
其中 `<|role|>` 部分使用 special token 表示,无法从文本形式被 tokenizer 编码以防止注入。metadata 部分采用纯文本表示,为可选内容。
22+
* `<|system|>`:系统信息,设计上可穿插于对话中,**但目前规定仅可以出现在开头**
23+
* `<|user|>`:用户
24+
- 不会连续出现多个来自 `<|user|>` 的信息
25+
* `<|assistant|>`:AI 助手
26+
- 在出现之前必须有一个来自 `<|user|>` 的信息
27+
* `<|observation|>`:外部的返回结果
28+
- 必须在 `<|assistant|>` 的信息之后
29+
30+
### 样例场景
31+
#### 多轮对话
32+
* 有且仅有 `<|user|>``<|assistant|>``<|system|>` 三种 role
33+
```text
34+
<|system|>
35+
You are ChatGLM3, a large language model trained by Zhipu.AI. Follow the user's instructions carefully. Respond using markdown.
36+
<|user|>
37+
Hello
38+
<|assistant|>
39+
Hello, I'm ChatGLM3. What can I assist you today?
40+
```
41+
42+
#### 工具调用
43+
````
44+
<|system|>
45+
Answer the following questions as best as you can. You have access to the following tools:
46+
[
47+
{
48+
"name": "get_current_weather",
49+
"description": "Get the current weather in a given location",
50+
"parameters": {
51+
"type": "object",
52+
"properties": {
53+
"location": {
54+
"type": "string",
55+
"description": "The city and state, e.g. San Francisco, CA",
56+
},
57+
"unit": {"type": "string"},
58+
},
59+
"required": ["location"],
60+
},
61+
}
62+
]
63+
<|user|>
64+
今天北京的天气怎么样?
65+
<|assistant|>
66+
好的,让我们来查看今天的天气
67+
<|assistant|>get_current_weather
68+
```python
69+
tool_call(location="beijing", unit="celsius")
70+
```
71+
<|observation|>
72+
{"temperature": 22}
73+
<|assistant|>
74+
根据查询结果,今天北京的气温为 22 摄氏度。
75+
````
76+
77+
#### 代码执行
78+
*`<|user|>``<|assistant|>``<|system|>``<|observation|>` 四种 role。其中 `<|assistant|>` 的 metadata 只有 interpreter。
79+
80+
`````text
81+
<|system|>
82+
你是一位智能AI助手,你叫ChatGLM3,你连接着一台电脑,但请注意不能联网。在使用Python解决任务时,你可以运行代码并得到结果,如果运行结果有错误,你需要尽可能对代码进行改进。你可以处理用户上传到电脑上的文件,文件默认存储路径是/mnt/data/。
83+
<|user|>
84+
#File: /mnt/data/metadata.jsonl
85+
#Size: 35380
86+
#File uploaded
87+
文件中是否存在缺失值或异常值?
88+
<|assistant|>
89+
首先,我将读取您提供的文件,并查看其内容以确定是否存在缺失值或异常值。
90+
91+
我们从加载文件开始:
92+
<|assistant|>interpreter
93+
```python
94+
import json
95+
96+
# Load the uploaded file
97+
data = []
98+
with open("/mnt/data/metadata.jsonl", 'r') as file:
99+
for line in file:
100+
data.append(json.loads(line))
101+
102+
# Display the first few records to get a sense of the data
103+
data[:5]
104+
```
105+
<|observation|>
106+
```result
107+
[{'file_name': 'aceinthehole.png',
108+
'name': 'Ace in the Hole',
109+
'type': 'survivor',
110+
'description': 'Lady Luck always seems to be throwing something good your way.'},
111+
{'file_name': 'adrenaline.png',
112+
'name': 'Adrenaline',
113+
'type': 'survivor',
114+
'description': 'You are fuelled by unexpected energy when on the verge of escape.'},
115+
{'file_name': 'aftercare.png',
116+
'name': 'Aftercare',
117+
'type': 'survivor',
118+
'description': "Unlocks potential in one's aura reading ability."},
119+
{'file_name': 'agitation.png',
120+
'name': 'Agitation',
121+
'type': 'killer',
122+
'description': 'You get excited in anticipation of hooking your prey.'},
123+
{'file_name': 'alert.png',
124+
'name': 'Alert',
125+
'type': 'survivor',
126+
'description': 'Your acute senses are on high alert.'}]
127+
```
128+
<|assistant|>
129+
该文件看起来包含有关某些条目的元数据,每个条目有以下字段:
130+
131+
- `file_name`: 文件名称
132+
- `name`: 名称
133+
- `type`: 类型(例如 "survivor" 或 "killer")
134+
- `description`: 描述
135+
136+
接下来,我将检查这些字段是否存在缺失值。
137+
<|assistant|>interpreter
138+
```python
139+
# Check for missing values in each column
140+
missing_values = {key: sum(1 for item in data if key not in item) for key in data[0].keys()}
141+
142+
missing_values
143+
```
144+
<|observation|>
145+
```result
146+
{'file_name': 0, 'name': 0, 'type': 0, 'description': 0}
147+
```
148+
<|assistant|>
149+
每个字段都没有缺失值。这意味着每个条目都完整。
150+
151+
接下来,我们可以检查是否存在任何潜在的异常值。这通常需要更深入的分析和对数据的了解,但我可以从一些基本的统计和分布开始进行检查。为了这样做,我们可以查看“type”字段的值分布,看看是否有任何意外的类别。
152+
<|assistant|>interpreter
153+
```python
154+
# Check the distribution of values in the 'type' column
155+
type_distribution = {key: sum(1 for item in data if item['type'] == key) for key in set(item['type'] for item in data)}
156+
157+
type_distribution
158+
```
159+
<|observation|>
160+
```result
161+
{'survivor': 116, 'killer': 103}
162+
```
163+
<|assistant|>
164+
“type”字段有两个值:“survivor”和“killer”。其中,“survivor”有116个条目,而“killer”有103个条目。
165+
166+
从这个初步的检查中,我们没有发现任何明显的异常值。
167+
168+
如果您有任何特定的标准或其他字段需要检查,我可以为您提供更详细的分析。您还希望我检查什么呢?
169+
<|user|>
170+
帮我画一个爱心
171+
<|assistant|>interpreter
172+
```python
173+
import numpy as np
174+
import matplotlib.pyplot as plt
175+
176+
# 爱心形状的参数方程
177+
def heart(t):
178+
x = 16 * np.sin(t) ** 3
179+
y = 13 * np.cos(t) - 5 * np.cos(2 * t) - 2 * np.cos(3 * t) - np.cos(4 * t)
180+
return x, y
181+
182+
t = np.linspace(0, 2 * np.pi, 1000)
183+
x, y = heart(t)
184+
185+
plt.figure(figsize=(6, 6))
186+
plt.plot(x, y, color='red')
187+
plt.axis('equal')
188+
plt.axis('off')
189+
plt.show()
190+
```
191+
<|observation|>
192+
```result
193+
【image】
194+
```
195+
<|assistant|>
196+
这是一个爱心形状。我使用了参数方程来描述这个形状,并使用matplotlib进行了绘制。如果您有任何其他需求或问题,请随时告诉我。
197+
<|user|> # End
198+
````

0 commit comments

Comments
 (0)