GLM-4.7 是什麼
GLM-4.7 是 Z.ai / Zhipu AI 最新的旗艦級開源基礎大語言模型(模型名稱 glm-4.7)。它被定位為面向開發者的「思考型」模型,特別強化了 程式設計/代理式任務執行、多步推理、工具調用,以及長上下文工作流程。此次發布重點突出了大型上下文處理能力(最高 200K 上下文)、高最大輸出(最高 128K tokens),以及面向代理式流程的專用「思考」模式。
主要特性
- 代理式/工具使用能力提升: 內建思考模式(「Interleaved Thinking」、「Preserved Thinking」、回合級控制),讓模型能在行動前先思考、在多輪對話中保留推理,並在調用工具或執行多步任務時更穩定。這是為了支援更穩健的代理工作流程(終端機、工具鏈、網頁瀏覽)。
- 程式設計與終端機能力: 在程式設計基準測試與終端機自動化任務上有顯著提升——廠商基準測試顯示,相較於 GLM-4.6,在 SWE-bench 與 Terminal Bench 指標上有明顯進步。這代表在代理環境中,多輪程式碼生成、命令排序與錯誤恢復能力更強。
- 「Vibe coding」/前端輸出品質: 生成的 HTML、投影片與簡報在預設 UI/版面品質方面有所改進(版面更整潔、尺寸更合理、視覺預設更佳)。
- 長上下文工作流程: 具備 200K token 上下文視窗與上下文快取工具;適合多檔程式碼庫、長篇文件,以及多輪代理工作階段。
基準表現
GLM-4.7 的發布方/維護者與社群基準表顯示,相較於 GLM-4.6 有顯著提升,且在程式設計、代理式與工具使用任務上,對比其他同時代模型也具有競爭力。以下為部分數據(來源:官方 Hugging Face / Z.AI 公布表格):
- LiveCodeBench-v6(程式設計代理基準): 84.9(標示為開源 SOTA)。
- SWE-bench Verified(程式設計): 73.8%(較 GLM-4.6 的 68.0% 提升)。
- SWE-bench Multilingual: 66.7%(較 GLM-4.6 提升 +12.9%)。
- Terminal Bench 2.0(代理式終端機操作): 41.0%(相較 4.6 有顯著 +16.5% 提升)。
- HLE(結合工具的複雜推理): 搭配工具使用時為 42.8%(相較先前版本據報有大幅提升)。
- τ²-Bench(互動式工具調用): 87.4(據報為開源 SOTA)。
典型使用案例與示例場景
- 代理式程式設計助手: 自主或半自主的程式碼生成、多輪程式碼修復、終端機自動化與 CI/CD 腳本撰寫。
- 工具驅動代理: 網頁瀏覽、API 編排、多步工作流程(支援 preserved thinking 與 function calling)。
- 前端與 UI 生成: 自動建立網站骨架、投影片、海報,且具備更佳的美觀度與版面配置。
- 研究與長上下文任務: 長文件上的文件摘要、文獻整合,以及檢索增強生成(200k token 視窗在此很有幫助)。
- 互動式教育代理/程式設計導師: 具備保留推理能力的多輪教學,在整個對話期間記住先前的推理區塊。
如何存取並使用 GLM 4.7 API
步驟 1:註冊以取得 API Key
登入 cometapi.com。如果您尚未成為我們的使用者,請先註冊。登入您的 CometAPI 控制台。取得介面存取憑證 API key。於個人中心的 API token 處點擊「Add Token」,取得 token key:sk-xxxxx 並提交。
步驟 2:向 MiniMax M2.1 API 發送請求
選擇「glm-4.7」端點來發送 API 請求,並設定請求主體。請求方法與請求主體可從我們網站的 API 文件取得。我們的網站也提供 Apifox 測試供您使用。將 <YOUR_API_KEY> 替換為您帳戶中的實際 CometAPI key。呼叫位置: Chat 風格的 API。
將您的問題或請求插入 content 欄位——這就是模型會回應的內容。處理 API 回應以取得生成的答案。
步驟 3:取得並驗證結果
處理 API 回應以取得生成的答案。處理完成後,API 會回傳任務狀態和

![以下以「GLM-4-7B-Flash」(常見記法為 GLM-4-7B-Flash,您寫的 GLM-4.7-Flash 多半指此型號)為例,說明本機離線使用的常見方式。請先在 Hugging Face 或 ModelScope 找到對應的模型倉庫並接受授權,後續將 <model-id> 替換為實際倉庫名;GLM 系列通常需要 trust_remote_code。
一、環境與依賴
- 建議:Python 3.10+,CUDA 11.8+/12.x(若用 NVIDIA GPU),PyTorch 對應版本
- 可選加速:flash-attn(需符合 GPU 架構與 CUDA 版本)
- 安裝基本依賴
pip install --upgrade torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
pip install --upgrade transformers accelerate sentencepiece safetensors
pip install --upgrade bitsandbytes # 如需4-bit量化推理
pip install flash-attn --no-build-isolation # 可選(GPU 支援時)
二、使用 Transformers 直接推理
- 半精度/自動分配到 GPU
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
model_id = "<model-id>" # 例如:THUDM/glm-4-7b-flash(請以實際倉庫名為準)
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
model_id,
torch_dtype=torch.bfloat16, # 或 torch.float16(依 GPU 支援)
device_map="auto",
trust_remote_code=True
).eval()
# 若模型提供 chat 介面(GLM 常見)
response, history = model.chat(tokenizer, "你好,給我一段 GLM 本機使用的示例。", history=[])
print(response)
# 或使用 chat template(通用方式)
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "介紹一下 GLM-4-7B-Flash 的本機部署方法。"}
]
input_ids = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)
with torch.no_grad():
outputs = model.generate(
input_ids,
max_new_tokens=512,
temperature=0.7,
top_p=0.9
)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
- 4-bit 量化(降低顯存佔用)
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
quant_cfg = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_use_double_quant=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype="bfloat16"
)
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
model_id,
quantization_config=quant_cfg,
device_map="auto",
trust_remote_code=True
).eval()
- Apple Silicon(MPS)簡述
import torch
device = torch.device("mps") if torch.backends.mps.is_available() else torch.device("cpu")
# 直接 .to(device) 或 device_map="auto" 由 PyTorch 分配(性能不及 CUDA,僅作輕量測試)
三、用 vLLM 跑本地 OpenAI 相容服務
- 安裝與啟動
pip install "vllm>=0.5.0"
python -m vllm.entrypoints.openai.api_server --model <model-id> --trust-remote-code --max-model-len 4096 --dtype auto
- 調用(curl)
curl http://127.0.0.1:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "<model-id>",
"messages": [{"role":"user","content":"用要點說明如何本機使用 GLM-4-7B-Flash"}],
"temperature": 0.7
}'
- 調用(Python,openai 相容)
pip install openai
from openai import OpenAI
client = OpenAI(base_url="http://127.0.0.1:8000/v1", api_key="EMPTY")
resp = client.chat.completions.create(
model="<model-id>",
messages=[{"role":"user","content":"請示範本機推理指令"}],
temperature=0.7
)
print(resp.choices[0].message.content)
四、常見注意事項
- 模型倉庫與授權:到 Hugging Face 或 ModelScope 搜尋「GLM-4-7B-Flash」或相近名稱,閱讀說明並接受授權。不同倉庫可能提供原始、量化(如 GPTQ/AWQ/INT4)或「Flash/Long」等變體。
- trust_remote_code:GLM 系列常需設為 True 以載入自定義模型邏輯與 chat 介面。
- 顯存需求(粗略參考):7B FP16/BF16 約需 12–16GB;4-bit 量化約 6–8GB。實際取決於序列長度與張量並行策略。
- 加速:若安裝並啟用 flash-attn,需確保 CUDA/驅動/GPU 架構相容;否則請移除或改用預設注意力實作。
- 長文本與上下文:若為「Flash」或「Long」變體,請依倉庫說明設置 max_position_embeddings 或相應啟動參數(如 vLLM 的 --max-model-len)。
- Windows 使用者:建議 CUDA + WSL2 或原生 CUDA 環境;bitsandbytes 在某些 Windows/CUDA 組合上需額外配置。
五、快速故障排查
- ImportError/ModuleNotFoundError:升級 transformers、accelerate、safetensors;確認 trust_remote_code=True。
- CUDA OOM:嘗試 4-bit 量化、減小 batch/序列長度、關閉梯度計算(eval、no_grad)、或使用 vLLM+paged attention。
- 推理很慢:確認是否在 CPU 上;如可用,安裝 flash-attn;調整 max_new_tokens、temperature/top_p;或改用量化權重。
如您提供具體的模型倉庫名稱(<model-id>)與硬體環境(GPU/顯存/作業系統),可進一步給出精確的安裝與啟動指令。](https://resource.cometapi.com/How to Use GLM-4.7-Flash Locally.webp)
