Skip to content

GLM-5 / GLM-5.1

GLM-5 和 GLM-5.1 通过 OpenAI 兼容的 Chat Completions 接口调用。客户端无需使用智谱专用协议,只需要把 model 设置为 glm-5glm-5.1

请求地址

http
POST /v1/chat/completions

支持模型

模型说明
glm-5GLM-5 标准模型
glm-5.1GLM-5.1 标准模型

实际可用模型以账户权限和平台配置为准。

请求示例

bash
curl https://cubicspaces.cloud/v1/chat/completions \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "glm-5",
    "messages": [
      { "role": "user", "content": "你好,简单回复一句。" }
    ],
    "temperature": 0.2,
    "stream": false
  }'

响应示例

GLM 模型可能在 message.reasoning_content 中返回思考内容,最终回复仍读取 message.content

json
{
  "id": "chatcmpl_123",
  "object": "chat.completion",
  "created": 1778309051,
  "model": "glm-5",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "reasoning_content": "用户要求简单回复一句,因此直接给出简短问候。",
        "content": "你好!"
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 11,
    "completion_tokens": 174,
    "total_tokens": 185,
    "completion_tokens_details": {
      "reasoning_tokens": 171
    }
  }
}

流式输出

bash
curl https://cubicspaces.cloud/v1/chat/completions \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "glm-5.1",
    "messages": [
      { "role": "user", "content": "用一句话介绍 GLM。" }
    ],
    "stream": true,
    "stream_options": {
      "include_usage": true
    }
  }'

流式响应遵循 SSE 格式。每个 data: 块中可能出现:

  • choices[0].delta.reasoning_content:思考内容片段。
  • choices[0].delta.content:最终回复片段。
  • usage:当 stream_options.include_usagetrue 时,末尾可能返回用量信息。

常用参数

参数类型说明
modelstringglm-5glm-5.1
messagesarrayOpenAI Chat 消息数组
temperaturenumber采样温度
top_pnumbernucleus sampling 参数
max_tokensnumber最大输出 token 数
streamboolean是否流式输出
stream_optionsobject流式附加配置,例如 include_usage
toolsarray工具调用定义
tool_choicestring/object工具选择策略

SDK 示例

python
from openai import OpenAI

client = OpenAI(
    api_key="YOUR_API_KEY",
    base_url="https://cubicspaces.cloud/v1"
)

response = client.chat.completions.create(
    model="glm-5",
    messages=[
        {"role": "user", "content": "你好,简单回复一句。"}
    ],
    temperature=0.2
)

message = response.choices[0].message
print(message.content)
js
import OpenAI from "openai";

const client = new OpenAI({
  apiKey: "YOUR_API_KEY",
  baseURL: "https://cubicspaces.cloud/v1"
});

const response = await client.chat.completions.create({
  model: "glm-5",
  messages: [{ role: "user", content: "你好,简单回复一句。" }],
  temperature: 0.2
});

console.log(response.choices[0].message.content);

注意事项

  • GLM 接入使用 OpenAI 兼容通道,接口路径固定为 /v1/chat/completions
  • 业务侧展示最终答案时优先读取 choices[0].message.content
  • 如需展示推理过程,可额外读取 reasoning_content;不需要展示时可以忽略。
  • 流式解析时要同时处理 delta.reasoning_contentdelta.content