# GLM-5

GLM-5，由智谱AI（Z.AI）于2026年2月发布，是一款 **7440亿参数的专家混合（Mixture-of-Experts）模型** 语言模型，每个标记只激活40B参数。它在推理、编码和代理任务上达到了开源领域的最佳表现——在SWE-bench Verified上得分77.8%，可与Claude Opus 4.5和GPT-5.2等前沿模型比肩。该模型可在 **MIT 许可证** 提供。

## 主要特性

* **744B 总量 / 40B 激活** — 256 专家 MoE，具有高效的路由
* **前沿的编码性能** — SWE-bench Verified 77.8%，SWE-bench 多语言 73.3%
* **深度推理** — AIME 2026 得分 92.7%，HMMT 2025年11月 得分 96.9%，内置思考模式
* **代理能力** — 原生工具调用、函数执行和长远任务规划
* **20万+ 上下文窗口** — 处理大规模代码库和长文档
* **MIT 许可证** — 完全开放的权重，允许商业使用

## 要求

自托管 GLM-5 是一项严肃的工程——FP8 检查点需要 **约860GB 显存**.

| 组件   | 最低（FP8）      | 推荐            |
| ---- | ------------ | ------------- |
| GPU  | 8× H100 80GB | 8× H200 141GB |
| 显存   | 640GB        | 1,128GB       |
| 内存   | 256GB        | 512GB         |
| 磁盘   | 1.5TB NVMe   | 2TB NVMe      |
| CUDA | 12.0+        | 12.4+         |

**Clore.ai 建议**： 对于大多数用户， **通过 API 访问 GLM-5** （Z.AI、OpenRouter）。只有在你能租用 8× H100/H200（在 Clore.ai 上约 $24–48/天）的情况下，自托管才有意义。

## API 访问（对多数用户推荐）

从 Clore.ai 机器或任何地方使用 GLM-5 最实用的方式：

### 通过 Z.AI 平台

```python
from openai import OpenAI

client = OpenAI(
    api_key="your-zai-api-key",
    base_url="https://api.z.ai/v1"
)

response = client.chat.completions.create(
    model="glm-5",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "使用 aiohttp 和 BeautifulSoup 编写一个 Python 异步网页爬虫"}
    ],
    temperature=1.0,
    max_tokens=4096
)
print(response.choices[0].message.content)
```

### 通过 OpenRouter

```python
from openai import OpenAI

client = OpenAI(
    api_key="your-openrouter-key",
    base_url="https://openrouter.ai/api/v1"
)

response = client.chat.completions.create(
    model="zai-org/glm-5",
    messages=[
        {"role": "user", "content": "解释 GLM-5 中使用的 MoE 架构"}
    ],
    max_tokens=2048
)
print(response.choices[0].message.content)
```

## vLLM 设置（自托管）

对于可以访问 Clore.ai 上高端多 GPU 机器的用户：

```bash
# 安装 vLLM（需 nightly 以支持 GLM-5）
pip install -U vllm --pre --extra-index-url https://wheels.vllm.ai/nightly

# 安装最新 transformers（必需）
pip install git+https://github.com/huggingface/transformers.git
```

### 在 8× H200 GPU 上提供 FP8 服务

```bash
vllm serve zai-org/GLM-5-FP8 \
  --tensor-parallel-size 8 \
  --speculative-config.method mtp \
  --speculative-config.num_speculative_tokens 1 \
  --tool-call-parser glm47 \
  --reasoning-parser glm45 \
  --enable-auto-tool-choice \
  --served-model-name glm-5-fp8 \
  --gpu-memory-utilization 0.85
```

### 查询服务器

```python
from openai import OpenAI

client = OpenAI(base_url="http://localhost:8000/v1", api_key="EMPTY")

# 使用思考模式（默认）
response = client.chat.completions.create(
    model="glm-5-fp8",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "求解：找出所有使 p^2 + 2 也是素数的素数 p"}
    ],
    temperature=1.0,
    max_tokens=4096
)
print(response.choices[0].message.content)

# 不使用思考模式（更快、响应更短）
response = client.chat.completions.create(
    model="glm-5-fp8",
    messages=[
        {"role": "user", "content": "用 Rust 写一个快速排序"}
    ],
    temperature=1.0,
    max_tokens=4096,
    extra_body={
        "chat_template_kwargs": {"enable_thinking": False}
    }
)
print(response.choices[0].message.content)
```

## SGLang 替代方案

SGLang 也支持 GLM-5，在某些硬件上可能提供更好的性能：

```bash
# 使用 Docker（Hopper GPU）
docker pull lmsysorg/sglang:glm5-hopper

# 启动服务器
python3 -m sglang.launch_server \
  --model-path zai-org/GLM-5-FP8 \
  --tp-size 8 \
  --tool-call-parser glm47 \
  --reasoning-parser glm45 \
  --speculative-algorithm EAGLE \
  --speculative-num-steps 3 \
  --speculative-eagle-topk 1 \
  --speculative-num-draft-tokens 4 \
  --mem-fraction-static 0.85 \
  --served-model-name glm-5-fp8
```

## Docker 快速开始

```bash
# 带有 GLM-5 支持的 vLLM Docker 镜像
docker run --gpus all -p 8000:8000 \
  --ipc=host \
  -v ~/.cache/huggingface:/root/.cache/huggingface \
  vllm/vllm-openai:glm5 zai-org/GLM-5-FP8 \
  --tensor-parallel-size 8 \
  --tool-call-parser glm47 \
  --reasoning-parser glm45 \
  --enable-auto-tool-choice \
  --served-model-name glm5 \
  --trust-remote-code
```

## 工具调用示例

GLM-5 具有原生工具调用支持——非常适合构建代理类应用：

```python
from openai import OpenAI

client = OpenAI(base_url="http://localhost:8000/v1", api_key="EMPTY")

tools = [{
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "获取某城市的当前天气",
        "parameters": {
            "type": "object",
            "required": ["city"],
            "properties": {
                "city": {"type": "string", "description": "城市名称"}
            }
        }
    }
}]

response = client.chat.completions.create(
    model="glm-5-fp8",
    messages=[{"role": "user", "content": "东京的天气怎么样？"}],
    tools=tools,
    tool_choice="auto"
)
print(response.choices[0].message.tool_calls)
```

## 给 Clore.ai 用户的提示

* **先 API，再自托管**： GLM-5 需要 8× H200（每台约 141GB，Clore.ai 上约 $24–48/天）。对于偶尔使用，Z.AI API 或 OpenRouter 更加经济。仅在需要持续吞吐或数据隐私时才考虑自托管。
* **考虑改用 GLM-4.7**： 如果 8× H200 太过昂贵或繁重，前代的 GLM-4.7（355B，总激活 32B）可在 4× H200 或 4× H100（约 $12–24/天）上运行，且仍能提供出色的性能。
* **使用 FP8 权重**： 始终使用 `zai-org/GLM-5-FP8` — 与 BF16 相同的质量但几乎减半的内存占用。BF16 版本需要 16× GPU。
* **监控显存使用**: `watch nvidia-smi` — 长上下文查询可能会使内存峰值增加。设置 `--gpu-memory-utilization 0.85` 以保留余量。
* **思考模式的权衡**： 思考模式能在复杂任务上产生更好的结果，但会消耗更多的令牌和时间。对于简单查询，可在启动时通过禁用来关闭它，方法是 `enable_thinking: false`.

## # 使用固定种子以获得一致结果

| 问题                      | 解决方案                                                                   |
| ----------------------- | ---------------------------------------------------------------------- |
| `OutOfMemoryError` 在启动时 | 确保你有 8× H200（每块 141GB）。FP8 需要约 860GB 总显存。                              |
| 缓慢的下载（约 800GB）          | 使用 `huggingface-cli download zai-org/GLM-5-FP8` 与 `--local-dir` 以恢复下载。 |
| vLLM 版本不匹配              | GLM-5 需要 vLLM nightly。通过以下方式安装： `pip install -U vllm --pre`.           |
| 工具调用无效                  | 添加 `--tool-call-parser glm47 --enable-auto-tool-choice` 来提供服务命令。       |
| DeepGEMM 错误             | 为 FP8 安装 DeepGEMM：使用 `install_deepgemm.sh` 脚本（来自 vLLM 仓库）。             |
| 思考模式输出为空                | 设置 `temperature=1.0` — 思考模式需要非零温度。                                     |

## 延伸阅读

* [HuggingFace 上的 GLM-5](https://huggingface.co/zai-org/GLM-5)
* [GLM-5 FP8 检查点](https://huggingface.co/zai-org/GLM-5-FP8)
* [Z.AI 平台](https://chat.z.ai)
* [Z.AI API 文档](https://docs.z.ai/guides/llm/glm-5)
* [vLLM GLM-5 配方](https://docs.vllm.ai/projects/recipes/en/latest/GLM/GLM5.html)
* [GLM-5 技术博客](https://z.ai/blog/glm-5)
* [Slime RL 基础设施](https://github.com/THUDM/slime)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.clore.ai/guides/guides_v2-zh/yu-yan-mo-xing/glm5.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
