🚀 GLM-4-9B-Chat
GLM-4-9B-Chat 是智谱 AI 推出的 GLM-4 系列预训练模型的开源版本。它在语义、数学、推理、代码和知识等数据集评估中表现出色,超越了 Llama-3-8B。除多轮对话外,还具备网页浏览、代码执行、自定义工具调用和长上下文推理等高级功能。
🚀 快速开始
如需更多推理代码和要求,请访问我们的 GitHub 页面。
请严格按照 依赖项 进行安装,否则将无法正常运行。
使用 Transformers 库(4.46.0 及更高版本)进行推理:
from transformers import AutoModelForCausalLM, AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained('THUDM/glm-4-9b-chat-hf')
model = AutoModelForCausalLM.from_pretrained('THUDM/glm-4-9b-chat-hf', device_map="auto")
message = [
{
"role": "system",
"content": "Answer the following question."
},
{
"role": "user",
"content": "How many legs does a cat have?"
}
]
inputs = tokenizer.apply_chat_template(
message,
return_tensors='pt',
add_generation_prompt=True,
return_dict=True,
).to(model.device)
input_len = inputs['input_ids'].shape[1]
generate_kwargs = {
"input_ids": inputs['input_ids'],
"attention_mask": inputs['attention_mask'],
"max_new_tokens": 128,
"do_sample": False,
}
out = model.generate(**generate_kwargs)
print(tokenizer.decode(out[0][input_len:], skip_special_tokens=True))
使用 vLLM 库(0.6.4 及更高版本)进行推理:
from transformers import AutoTokenizer
from vllm import LLM, SamplingParams
max_model_len, tp_size = 131072, 1
model_name = "THUDM/glm-4-9b-chat-hf"
prompt = [{"role": "user", "content": "what is your name?"}]
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
llm = LLM(
model=model_name,
tensor_parallel_size=tp_size,
max_model_len=max_model_len,
trust_remote_code=True,
enforce_eager=True,
)
stop_token_ids = [151329, 151336, 151338]
sampling_params = SamplingParams(temperature=0.95, max_tokens=1024, stop_token_ids=stop_token_ids)
inputs = tokenizer.apply_chat_template(prompt, tokenize=False, add_generation_prompt=True)
outputs = llm.generate(prompts=inputs, sampling_params=sampling_params)
print(outputs[0].outputs[0].text)
✨ 主要特性
- 高性能表现:在语义、数学、推理、代码和知识等数据集评估中,GLM-4-9B 及其人工偏好对齐版本 GLM-4-9B-Chat 表现超越 Llama-3-8B。
- 高级功能丰富:除多轮对话外,GLM-4-9B-Chat 还具备网页浏览、代码执行、自定义工具调用(Function Call)和长上下文推理(支持高达 128K 上下文)等高级功能。
- 多语言支持:支持日语、韩语和德语等 26 种语言。
- 长上下文模型:推出支持 1M 上下文长度(约 200 万汉字)的 GLM-4-9B-Chat-1M 模型。
- 多模态能力:基于 GLM-4-9B 的多模态模型 GLM-4V-9B 在 1120*1120 的高分辨率下具备中英双语对话能力,在多种多模态评估中表现优于 GPT-4-turbo-2024-04-09、Gemini 1.0 Pro、Qwen-VL-Max 和 Claude 3 Opus。
📚 详细文档
基准测试
综合基准测试
我们在一些经典任务上对 GLM-4-9B-Chat 模型进行了评估,结果如下:
模型 |
AlignBench-v2 |
MT-Bench |
IFEval |
MMLU |
C-Eval |
GSM8K |
MATH |
HumanEval |
NCB |
Llama-3-8B-Instruct |
5.12 |
8.00 |
68.58 |
68.4 |
51.3 |
79.6 |
30.0 |
62.2 |
24.7 |
ChatGLM3-6B |
3.97 |
5.50 |
28.1 |
66.4 |
69.0 |
72.3 |
25.7 |
58.5 |
11.3 |
GLM-4-9B-Chat |
6.61 |
8.35 |
69.0 |
72.4 |
75.6 |
79.6 |
50.6 |
71.8 |
32.2 |
长上下文测试
多语言测试
在六个多语言数据集上对 GLM-4-9B-Chat 和 Llama-3-8B-Instruct 进行了测试,测试结果和每个数据集选择的相应语言如下表所示:
数据集 |
Llama-3-8B-Instruct |
GLM-4-9B-Chat |
语言 |
M-MMLU |
49.6 |
56.6 |
所有语言 |
FLORES |
25.0 |
28.8 |
ru, es, de, fr, it, pt, pl, ja, nl, ar, tr, cs, vi, fa, hu, el, ro, sv, uk, fi, ko, da, bg, no |
MGSM |
54.0 |
65.3 |
zh, en, bn, de, es, fr, ja, ru, sw, te, th |
XWinograd |
61.7 |
73.1 |
zh, en, fr, jp, ru, pt |
XStoryCloze |
84.7 |
90.7 |
zh, en, ar, es, eu, hi, id, my, ru, sw, te |
XCOPA |
73.3 |
80.1 |
zh, et, ht, id, it, qu, sw, ta, th, tr, vi |
函数调用测试
在 Berkeley Function Calling Leaderboard 上进行测试,结果如下:
模型 |
总体准确率 |
AST 摘要 |
执行摘要 |
相关性 |
Llama-3-8B-Instruct |
58.88 |
59.25 |
70.01 |
45.83 |
gpt-4-turbo-2024-04-09 |
81.24 |
82.14 |
78.61 |
88.75 |
ChatGLM3-6B |
57.88 |
62.18 |
69.78 |
5.42 |
GLM-4-9B-Chat |
81.00 |
80.26 |
84.40 |
87.92 |
此仓库是 GLM-4-9B-Chat 的模型仓库,支持 128K
上下文长度。
📄 许可证
GLM-4 模型的权重可在 许可证 的条款下使用。
🔖 引用
如果您认为我们的工作有用,请考虑引用以下论文:
@misc{glm2024chatglm,
title={ChatGLM: A Family of Large Language Models from GLM-130B to GLM-4 All Tools},
author={Team GLM and Aohan Zeng and Bin Xu and Bowen Wang and Chenhui Zhang and Da Yin and Diego Rojas and Guanyu Feng and Hanlin Zhao and Hanyu Lai and Hao Yu and Hongning Wang and Jiadai Sun and Jiajie Zhang and Jiale Cheng and Jiayi Gui and Jie Tang and Jing Zhang and Juanzi Li and Lei Zhao and Lindong Wu and Lucen Zhong and Mingdao Liu and Minlie Huang and Peng Zhang and Qinkai Zheng and Rui Lu and Shuaiqi Duan and Shudan Zhang and Shulin Cao and Shuxun Yang and Weng Lam Tam and Wenyi Zhao and Xiao Liu and Xiao Xia and Xiaohan Zhang and Xiaotao Gu and Xin Lv and Xinghan Liu and Xinyi Liu and Xinyue Yang and Xixuan Song and Xunkai Zhang and Yifan An and Yifan Xu and Yilin Niu and Yuantao Yang and Yueyan Li and Yushi Bai and Yuxiao Dong and Zehan Qi and Zhaoyu Wang and Zhen Yang and Zhengxiao Du and Zhenyu Hou and Zihan Wang},
year={2024},
eprint={2406.12793},
archivePrefix={arXiv},
primaryClass={id='cs.CL' full_name='Computation and Language' is_active=True alt_name='cmp-lg' in_archive='cs' is_general=False description='Covers natural language processing. Roughly includes material in ACM Subject Class I.2.7. Note that work on artificial languages (programming languages, logics, formal systems) that does not explicitly address natural-language issues broadly construed (natural-language processing, computational linguistics, speech, text retrieval, etc.) is not appropriate for this area.'}
}
⚠️ 重要提示
如果您使用此仓库中的权重,请更新到 transformers>=4.46.0 ,这些权重与旧版本的 transformers 库 不兼容。
💡 使用建议
请严格按照 依赖项 进行安装,否则模型将无法正常运行。在使用 vLLM 库进行推理时,如果遇到 OOM 现象,建议降低 max_model_len
或增加 tp_size
;对于 GLM-4-9B-Chat-1M-HF,若遇到 OOM 现象,建议启用 enable_chunked_prefill=True
和 max_num_batched_tokens=8192
等参数。