欢迎回来

登录 EAKE AI,继续您的智能之旅

忘记密码?
还没有账号?立即注册

智谱 GLM API 使用手册

2026-05-08 · 使用手册

一、概述

智谱 AI 推出的 GLM(General Language Model)系列大模型,提供从轻量到旗舰的完整模型矩阵。兼容 OpenAI API 格式,支持文本生成、对话、函数调用、图像理解、文生图、代码执行等能力。GLM-4V 多模态模型支持超高分辨率图像理解。

二、认证

Authorization: Bearer xxxxxxxxxx.xxxxxxxx

API Key 在 智谱开放平台 创建。

三、Base URL

https://open.bigmodel.cn/api/paas/v4

四、模型列表

模型ID上下文最大输出说明
glm-5128K16K最新旗舰
glm-4-plus128K8K上一代旗舰
glm-4-flash128K8K免费模型
glm-4-air128K8K高性价比
glm-4-long1M8K超长上下文
glm-4v-plus8K4K多模态旗舰
glm-4v-flash8K4K多模态免费
cogview-4--文生图
embedding-3--文本嵌入

五、使用 OpenAI SDK 调用

from openai import OpenAI

client = OpenAI(
    api_key="your-zhipu-api-key",
    base_url="https://open.bigmodel.cn/api/paas/v4"
)

response = client.chat.completions.create(
    model="glm-5",
    messages=[
        {"role": "system", "content": "你是智谱AI助手。"},
        {"role": "user", "content": "你好!"}
    ],
    temperature=0.7,
    top_p=0.7,
    max_tokens=2048,
    stream=False
)
print(response.choices[0].message.content)

六、原生 SDK 调用

from zhipuai import ZhipuAI

client = ZhipuAI(api_key="your-api-key")

response = client.chat.completions.create(
    model="glm-5",
    messages=[{"role": "user", "content": "你好"}]
)
print(response.choices[0].message.content)

七、流式输出

response = client.chat.completions.create(
    model="glm-5",
    messages=[{"role": "user", "content": "写一首关于春天的诗"}],
    stream=True
)

for chunk in response:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="")

八、Function Calling

tools = [{
    "type": "function",
    "function": {
        "name": "query_weather",
        "description": "查询天气",
        "parameters": {
            "type": "object",
            "properties": {
                "location": {"type": "string", "description": "城市"}
            },
            "required": ["location"]
        }
    }
}]

response = client.chat.completions.create(
    model="glm-5",
    messages=[{"role": "user", "content": "北京天气"}],
    tools=tools
)

if response.choices[0].finish_reason == "tool_calls":
    tool_call = response.choices[0].message.tool_calls[0]
    # 执行函数并返回结果
    result = query_weather(json.loads(tool_call.function.arguments)["location"])
    
    follow_up = client.chat.completions.create(
        model="glm-5",
        messages=[
            {"role": "user", "content": "北京天气"},
            response.choices[0].message,
            {"role": "tool", "tool_call_id": tool_call.id, "content": result}
        ],
        tools=tools
    )

九、多模态(图像理解)

response = client.chat.completions.create(
    model="glm-4v-plus",
    messages=[{
        "role": "user",
        "content": [
            {"type": "image_url", "image_url": {"url": "https://example.com/photo.jpg"}},
            {"type": "text", "text": "详细描述这张图片"}
        ]
    }]
)

十、文生图(CogView)

response = client.images.generations(
    model="cogview-4",
    prompt="一只在水墨画风格的山间行走的猫",
    size="1024x1024"
)
print(response.data[0].url)

十一、定价

模型输入价格输出价格
glm-5¥30/1M tokens¥90/1M tokens
glm-4-plus¥25/1M tokens¥75/1M tokens
glm-4-flash免费免费
glm-4-air¥1/1M tokens¥1/1M tokens
glm-4-long¥1/1M tokens¥1/1M tokens
cogview-4¥0.05/张-

十二、速率限制

模型RPM(免费)RPM(付费)
glm-4-flash10100
glm-4-air560
glm-5560

十三、错误码

状态码含义处理
400参数错误检查请求格式
401认证失败检查API Key
429速率限制退避重试
1301内容合规拦截修改输入
1302模型过载稍后重试

评论区

发表评论