zRzRzRzRzRzRzR commited on
Commit
e2259f1
·
1 Parent(s): 32978ec
Files changed (2) hide show
  1. README.md +289 -21
  2. configuration.json +0 -1
README.md CHANGED
@@ -7,39 +7,307 @@ pipeline_tag: text-generation
7
  library_name: transformers
8
  ---
9
 
10
- # GLM-4-9B-Chat-0414
11
-
12
  ## Introduction
13
 
14
- Based on our latest technological advancements, we have trained a `GLM-4-0414` series model. During pretraining, we incorporated more code-related and reasoning-related data. In the alignment phase, we optimized the model specifically for agent capabilities. As a result, the model's performance in agent tasks such as tool use, web search, and coding has been significantly improved.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
- ## Inference Code
17
 
18
- Make Sure Using `transforemrs>=4.51.3`.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
  ```python
 
 
 
21
  from transformers import AutoModelForCausalLM, AutoTokenizer
22
 
23
- MODEL_PATH = "THUDM/GLM-4-9B-Chat-0414"
24
 
25
  tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
26
  model = AutoModelForCausalLM.from_pretrained(MODEL_PATH, device_map="auto")
27
 
28
- message = [{"role": "user", "content": "hello!"}]
 
 
 
 
 
29
 
30
- inputs = tokenizer.apply_chat_template(
31
- message,
32
- return_tensors="pt",
33
- add_generation_prompt=True,
34
- return_dict=True,
35
- ).to(model.device)
 
 
 
 
 
36
 
37
- generate_kwargs = {
38
- "input_ids": inputs["input_ids"],
39
- "attention_mask": inputs["attention_mask"],
40
- "max_new_tokens": 128,
41
- "do_sample": False,
42
- }
43
- out = model.generate(**generate_kwargs)
44
- print(tokenizer.decode(out[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  ```
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  library_name: transformers
8
  ---
9
 
 
 
10
  ## Introduction
11
 
12
+ The GLM family welcomes new members, the **GLM-4-32B-0414** series models, featuring 32 billion parameters. Its performance is comparable to OpenAI’s GPT series and DeepSeek’s V3/R1 series. It also supports very user-friendly local deployment features. GLM-4-32B-Base-0414 was pre-trained on 15T of high-quality data, including substantial reasoning-type synthetic data. This lays the foundation for subsequent reinforcement learning extensions. In the post-training stage, we employed human preference alignment for dialogue scenarios. Additionally, using techniques like rejection sampling and reinforcement learning, we enhanced the models performance in instruction following, engineering code, and function calling, thus strengthening the atomic capabilities required for agent tasks. GLM-4-32B-0414 achieves good results in engineering code, Artifact generation, function calling, search-based Q&A, and report generation. In particular, on several benchmarks, such as code generation or specific Q&A tasks, GLM-4-32B-Base-0414 achieves comparable performance with those larger models like GPT-4o and DeepSeek-V3-0324 (671B).
13
+
14
+ **GLM-Z1-32B-0414** is a reasoning model with deep thinking capabilities. This was developed based on GLM-4-32B-0414 through cold start, extended reinforcement learning, and further training on tasks including mathematics, code, and logic. Compared to the base model, GLM-Z1-32B-0414 significantly improves mathematical abilities and the capability to solve complex tasks. During training, we also introduced general reinforcement learning based on pairwise ranking feedback, which enhances the model's general capabilities.
15
+
16
+ **GLM-Z1-Rumination-32B-0414** is a deep reasoning model with rumination capabilities (against OpenAI's Deep Research). Unlike typical deep thinking models, the rumination model is capable of deeper and longer thinking to solve more open-ended and complex problems (e.g., writing a comparative analysis of AI development in two cities and their future development plans). Z1-Rumination is trained through scaling end-to-end reinforcement learning with responses graded by the ground truth answers or rubrics and can make use of search tools during its deep thinking process to handle complex tasks. The model shows significant improvements in research-style writing and complex tasks.
17
+
18
+ Finally, **GLM-Z1-9B-0414** is a surprise. We employed all the aforementioned techniques to train a small model (9B). GLM-Z1-9B-0414 exhibits excellent capabilities in mathematical reasoning and general tasks. Its overall performance is top-ranked among all open-source models of the same size. Especially in resource-constrained scenarios, this model achieves an excellent balance between efficiency and effectiveness, providing a powerful option for users seeking lightweight deployment.
19
+
20
+ ## Showcase
21
+
22
+ ### Animation Generation
23
+
24
+ <table>
25
+ <tr>
26
+ <td style="text-align: center; font-size: 16px; font-weight: bold; padding: 10px; width: 420px;">
27
+ GLM-Z1-32B-0414
28
+ </td>
29
+ <td style="text-align: center; font-size: 16px; font-weight: bold; padding: 10px; width: 420px;">
30
+ GLM-4-32B-0414
31
+ </td>
32
+ </tr>
33
+ <tr>
34
+ <td style="vertical-align: top; padding: 10px; width: 420px;">
35
+ <video src="https://github.com/user-attachments/assets/849ff9fd-b54d-4c74-9ee5-3412e1a09e32"
36
+ style="width: 400px; height: 300px; object-fit: contain;" autoplay loop muted playsinline></video>
37
+ <div style="margin-top: 10px; font-size: 14px; color: #333; width: 400px;">
38
+ write a Python program that shows a ball bouncing inside a spinning hexagon. The ball should be affected by gravity and friction, and it must bounce off the rotating walls realistically
39
+ </div>
40
+ </td>
41
+ <td style="vertical-align: top; padding: 10px; width: 420px;">
42
+ <video src="https://github.com/user-attachments/assets/8dccdb9d-cc44-4732-b438-74a4e3cb9dfb"
43
+ style="width: 400px; height: 300px; object-fit: contain;" autoplay loop muted playsinline></video>
44
+ <div style="margin-top: 10px; font-size: 14px; color: #333; width: 400px;">
45
+ Use HTML to simulate the scenario of a small ball released from the center of a rotating hexagon. Consider the collision between the ball and the hexagon's edges, the gravity acting on the ball, and assume all collisions are perfectly elastic. (Prompt translated from Chinese)
46
+ </div>
47
+ </td>
48
+ </tr>
49
+ </table>
50
+
51
+ ### Web Design
52
+
53
+ <table>
54
+ <tr>
55
+ <td style="text-align: center; font-size: 16px; font-weight: bold; padding: 10px; width: 420px;">
56
+ GLM-4-32B-0414
57
+ </td>
58
+ <td style="text-align: center; font-size: 16px; font-weight: bold; padding: 10px; width: 420px;">
59
+ GLM-4-32B-0414
60
+ </td>
61
+ </tr>
62
+ <tr>
63
+ <td style="vertical-align: top; padding: 10px; width: 420px;">
64
+ <img src="https://github.com/user-attachments/assets/bd9c1fc1-c784-4e8f-9c76-5f7389a715f1"/>
65
+ <div style="margin-top: 10px; font-size: 14px; color: #333; width: 400px;">
66
+ Design a drawing board that supports custom function plotting, allowing adding and deleting custom functions, and assigning colors to functions. (Prompt translated from Chinese)
67
+ </div>
68
+ </td>
69
+ <td style="vertical-align: top; padding: 10px; width: 420px;">
70
+ <img src="https://github.com/user-attachments/assets/7ad12d52-9229-4278-8d1b-ffbf43e99070"/>
71
+ <div style="margin-top: 10px; font-size: 14px; color: #333; width: 400px;"> Design a UI for a mobile machine learning platform, which should include interfaces for training tasks, storage management, and personal statistics. The personal statistics interface should use charts to display the user's resource usage over a period. Use Tailwind CSS to style the page, and display these 3 mobile interfaces tiled on a single HTML page. (Prompt translated from Chinese) </div>
72
+ </td>
73
+ </tr>
74
+ </table>
75
+
76
+ ### SVG Generation
77
+
78
+ <table>
79
+ <tr>
80
+ <td style="text-align: center; font-size: 16px; font-weight: bold; padding: 10px; width: 420px;">
81
+ GLM-4-32B-0414
82
+ </td>
83
+ <td style="text-align: center; font-size: 16px; font-weight: bold; padding: 10px; width: 420px;">
84
+ GLM-4-32B-0414
85
+ </td>
86
+ </tr>
87
+ <tr>
88
+ <td style="vertical-align: top; padding: 10px; width: 420px;">
89
+ <img src="https://github.com/user-attachments/assets/9407e4c1-1876-4ab5-838c-839836fb418a"/>
90
+ <div style="margin-top: 10px; font-size: 14px; color: #333; width: 400px;">
91
+ Create a misty Jiangnan scene using SVG. (Prompt translated from Chinese)
92
+ </div>
93
+ </td>
94
+ <td style="vertical-align: top; padding: 10px; width: 420px;">
95
+ <img src="https://github.com/user-attachments/assets/bcce8c5a-cedf-45c8-b666-ddb023d5b49c"/>
96
+ <div style="margin-top: 10px; font-size: 14px; color: #333; width: 400px;"> Use SVG to illustrate the training process of an LLM. (Prompt translated from Chinese) </div>
97
+ </td>
98
+ </tr>
99
+ </table>
100
+
101
 
102
+ ### Search-Based Writing
103
 
104
+ For search-based writing tasks, we use the following system prompt to have the model respond based on search results:
105
+
106
+ ```
107
+ 请根据所给搜索返回结果对用户问题进行作答。
108
+
109
+ ## 注意
110
+ 1. 充分利用和整理收集到的信息,而不是简单的复制粘贴,生成符合用户要求且有深度的专业答案。
111
+ 2. 所提供信息充分的情况下,你的回答需尽可能延长,从用户意图角度出发,提供具有足够信息量和多角度的回复。
112
+ 3. 另外,并非所有的搜索结果都与用户问题密切相关,请仔细的甄别、筛选和利用。
113
+ 4. 客观类问答的答案通常非常简短,你可以适当补充一到两句相关信息,以丰富内容。
114
+ 5. 请确保你的回复格式美观、可读性强。对于多实体对比或列举,善用列表格式来帮助用户更好的理解信息。
115
+ 6. 除非用户要求,否则你回答的语言请于用户提问语言保持一致。
116
+ 7. 在适当情况下在句子末尾使用例如:【0†source】的格式引用搜索结果。
117
+ ```
118
+
119
+ When using, you can obtain search results through methods such as `RAG` or `WebSearch`, and wrap them in `observation`, for example:
120
+
121
+ ```json
122
+ [
123
+ {
124
+ "role": "user",
125
+ "content": "Explore the common characteristics of children's literature, with a focus on its narrative techniques and thematic tendencies. This includes narrative techniques: common approaches in children's literature such as first-person, third-person, omniscient narrator, and interactive narration, and their influence on young readers. It also includes thematic tendencies: recurring themes in children's literature such as growth, adventure, friendship, and family, with an analysis of how these themes impact children's cognitive and emotional development. Additionally, other universal features such as the use of personification, repetitive language, symbolism and metaphor, and educational value should be considered. Please provide a detailed analytical report based on academic research, classic examples of children's literature, and expert opinions."
126
+ },
127
+ {
128
+ "role": "observation",
129
+ "content": "【{id}†{title}†{url}】\n{content}"
130
+ },
131
+ ...
132
+ ]
133
+ ```
134
+ For the above prompt, we use an internal or external search model to obtain the search results. Using the format shown above, we can generate the following analysis report:
135
+
136
+ <div style="height: 400px; width: 100%; overflow: auto; border: 5px solid #ddd; padding: 20px;">
137
+
138
+
139
+ </div>
140
+
141
+ ### Function Call
142
+
143
+ GLM-4-32B-0414 supports calling external tools in JSON format. This can be done via HuggingFace Transformers, vLLM, or sgLang.
144
+ The message format for tool calling is as follows:
145
+ ```json=
146
+ {
147
+ "role": "asssitant",
148
+ "metadata": function_name,
149
+ "content": json.dumps(call_arguments, ensure_ascii=False)
150
+ }
151
+ ```
152
+
153
+ The message format for tool execution results is as follows:
154
+
155
+ ```json=
156
+ {
157
+ "role": "observation",
158
+ "content": json.dumps(tool_response, ensure_ascii=False) if not isinstance(tool_response, str) else tool_response
159
+ }
160
+ ```
161
+
162
+ The following example demonstrates the process of GLM-4-9B-0414 calling a tool and generating a final response using HuggingFace Transformers.
163
 
164
  ```python
165
+ import json
166
+ import re
167
+ import ast
168
  from transformers import AutoModelForCausalLM, AutoTokenizer
169
 
170
+ MODEL_PATH = "THUDM/GLM-4-9B-0414"
171
 
172
  tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
173
  model = AutoModelForCausalLM.from_pretrained(MODEL_PATH, device_map="auto")
174
 
175
+ def is_function_call(single_message):
176
+ """Determine whether the current system message is a function call."""
177
+ pattern = re.compile(r'([^\n`]*?)\n({.*?})(?=\w*\n|$)', re.DOTALL)
178
+ matches = pattern.findall(single_message)
179
+ if not matches:
180
+ return False
181
 
182
+ func_name, args_str = matches[0]
183
+ func_name = func_name.strip()
184
+ try:
185
+ parsed_args = json.loads(args_str)
186
+ except json.JSONDecodeError:
187
+ try:
188
+ parsed_args = ast.literal_eval(args_str)
189
+ except:
190
+ return False
191
+
192
+ return {"name": func_name, "arguments": parsed_args}
193
 
194
+ def realtime_aqi(city):
195
+ """Weather Query Tool"""
196
+ if '北京' in city.lower():
197
+ return json.dumps({'city': '北京', 'aqi': '10', 'unit': 'celsius'}, ensure_ascii=False)
198
+ elif '上海' in city.lower():
199
+ return json.dumps({'city': '上海', 'aqi': '72', 'unit': 'fahrenheit'}, ensure_ascii=False)
200
+ else:
201
+ return json.dumps({'city': city, 'aqi': 'unknown'}, ensure_ascii=False)
202
+
203
+ def build_system_prompt(tools):
204
+ """Construct system prompt based on the list of available tools."""
205
+ if tools is None:
206
+ tools = []
207
+ value = "# 可用工具"
208
+ contents = []
209
+ for tool in tools:
210
+ content = f"\n\n## {tool['function']['name']}\n\n{json.dumps(tool['function'], ensure_ascii=False, indent=4)}"
211
+ content += "\n在调用上述函数时,请使用 Json 格式表示调用的参数。"
212
+ contents.append(content)
213
+ value += "".join(contents)
214
+ return value
215
+
216
+ tools = [
217
+ {
218
+ "type": "function",
219
+ "function": {
220
+ "name": "realtime_aqi",
221
+ "description": "天气预报。获取实时空气质量。当前空气质量,PM2.5,PM10信息",
222
+ "parameters": {
223
+ "type": "object",
224
+ "properties": {
225
+ "city": {
226
+ "description": "城市名"
227
+ }
228
+ },
229
+ "required": [
230
+ "city"
231
+ ]
232
+ }
233
+ }
234
+ }
235
+ ]
236
+
237
+ system_prompt = build_system_prompt(tools)
238
+
239
+ message = [
240
+ {"role": "system", "content": system_prompt},
241
+ {"role": "user", "content": "北京和上海今天的天气情况"}
242
+ ]
243
+ print(f"User Message: {message[-1]['content']}")
244
+
245
+ while True:
246
+ inputs = tokenizer.apply_chat_template(
247
+ message,
248
+ return_tensors="pt",
249
+ add_generation_prompt=True,
250
+ return_dict=True,
251
+ ).to(model.device)
252
+
253
+ generate_kwargs = {
254
+ "input_ids": inputs["input_ids"],
255
+ "attention_mask": inputs["attention_mask"],
256
+ "max_new_tokens": 1024,
257
+ "do_sample": True,
258
+ }
259
+ out = model.generate(**generate_kwargs)
260
+ generate_resp = tokenizer.decode(out[0][inputs["input_ids"].shape[1]:-1], skip_special_tokens=False)
261
+ stop_sequence = tokenizer.decode(out[0][-1:], skip_speical_tokens=False)
262
+ if stop_sequence == "<|user|>":
263
+ print(f"Assistant Response: {generate_resp.strip()}")
264
+ break
265
+
266
+ function_calls = []
267
+ for m in generate_resp.split("<|assistant|>"):
268
+ fc_decode = is_function_call(m.strip())
269
+ if fc_decode:
270
+ message.append({"role": "assistant", "metadata": fc_decode['name'], "content": json.dumps(fc_decode['arguments'], ensure_ascii=False)})
271
+ print(f"Function Call: {fc_decode}")
272
+ function_calls.append(fc_decode)
273
+ else:
274
+ message.append({"role": "assistant", "content": m})
275
+ print(f"Assistant Response: {m.strip()}")
276
+
277
+ for fc in function_calls:
278
+ function_response = realtime_aqi(
279
+ city=fc["arguments"]["city"],
280
+ )
281
+ print(f"Function Response: {function_response}")
282
+ message.append({"role": "observation", "content": function_response})
283
  ```
284
+
285
+ ## Evaluation Results
286
+
287
+ <div style="text-align: center;">
288
+ <img src="https://raw.githubusercontent.com/THUDM/GLM-4/refs/heads/main/resources/Bench-32B.png" style="width: 80%;" />
289
+ </div>
290
+
291
+ ### GLM-4-0414 Series
292
+
293
+ | 模型 | IFEval | BFCL-v3 (Overall) | BFCL-v3 (MultiTurn) | TAU-Bench (Retail) | TAU-Bench (Airline) | SimpleQA | HotpotQA |
294
+ | ---------------- | ------ | ----------------- | ------------------- | ------------------ | ------------------- | -------- | -------- |
295
+ | Qwen2.5-Max | 85.6 | 50.9 | 30.5 | 58.3 | 22.0 | 79.0 | 52.8 |
296
+ | GPT-4o-1120 | 81.9 | 69.6 | 41.0 | 62.8 | 46.0 | 82.8 | 63.9 |
297
+ | DeepSeek-V3-0324 | 83.4 | 66.2 | 35.8 | 60.7 | 32.4 | 82.6 | 54.6 |
298
+ | DeepSeek-R1 | 84.3 | 57.5 | 12.4 | 33.0 | 37.3 | 83.9 | 63.1 |
299
+ | GLM-4-32B-0414 | 87.6 | 69.6 | 41.5 | 68.7 | 51.2 | 88.1 | 63.8 |
300
+
301
+ > For `SimpleQA` and `HotpotQA`, we sampled nearly 500 test cases from each test set, provided all models with basic `search` and `click` tools, ensured other settings remained consistent, and averaged the results over 3 runs.
302
+
303
+ | Model | Framework | [SWE-bench Verified](https://openai.com/index/introducing-swe-bench-verified/) | [SWE-bench Verified mini](https://github.com/mariushobbhahn/SWEBench-verified-mini) |
304
+ |---|---|---|---|
305
+ | GLM-4-32B-0414 | Moatless<sup>[1]</sup> | 33.8 | 38.0 |
306
+ | GLM-4-32B-0414 | Agentless<sup>[2]</sup> | 30.7 | 34.0 |
307
+ | GLM-4-32B-0414 | OpenHands<sup>[3]</sup> | 27.2 | 28.0 |
308
+
309
+ [1] [Moatless v0.0.3](https://github.com/aorwall/moatless-tools) used the following parameters: `response_format="react", thoughts_in_action=False, max_interations=30`. No retries on failed trajectories; other settings are default.
310
+
311
+ [2] [Agentless v1.5.0](https://github.com/OpenAutoCoder/Agentless) used [BGE](https://github.com/FlagOpen/FlagEmbedding/blob/master/README.md) as the embedding model and [FAISS](https://github.com/facebookresearch/faiss) for similarity search. To speed up patch verification while maintaining performance, the timeout for running a single instance was changed from the default 300s to 180s.
312
+
313
+ [3] [OpenHands v0.29.1](https://github.com/All-Hands-AI/OpenHands/tree/main) did not use YaRN context extension but limited runs to a maximum of 60 iterations and summarized the history to prevent exceeding the 32K context limit. Summarization was configured as `llm_config="condenser", keep_first=1, max_size=32`. No retries on failed trajectories.
configuration.json DELETED
@@ -1 +0,0 @@
1
- {"framework":"Pytorch","task":"text-generation"}