文本生成 (Chat Completions)
GLM-4 系列大语言模型对话接口,完全兼容 OpenAI Chat Completions API 格式,支持流式输出和函数调用。
API 端点
POST
/chat/completions创建对话补全
请求参数
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
model | string | 必填 | 模型名称:GLM-4-Plus, GLM-4-Air, GLM-4-AirX, GLM-4-Long, GLM-4-FlashX, GLM-4-Flash |
messages | array | 必填 | 对话消息列表,每条消息包含 role 和 content |
temperature | number | 可选 | 采样温度,范围 0-1,默认 0.7 |
max_tokens | integer | 可选 | 最大生成 tokens 数量 |
stream | boolean | 可选 | 是否使用流式输出,默认 false |
top_p | number | 可选 | 核采样参数,默认 0.9 |
tools | array | 可选 | 可用的工具列表(函数调用) |
请求示例
请求示例
{
"model": "GLM-4-Air",
"messages": [
{"role": "system", "content": "你是一个智能助手"},
{"role": "user", "content": "请介绍一下智谱AI"}
],
"temperature": 0.7,
"top_p": 0.9,
"stream": false
}响应示例
响应示例
{
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1677652288,
"model": "GLM-4-Air",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "智谱AI是一家专注于大模型与认知智能技术的公司..."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 20,
"completion_tokens": 100,
"total_tokens": 120
}
}代码示例
from openai import OpenAI
# 使用代理 API
client = OpenAI(
api_key="your-api-key",
base_url="https://your-proxy-domain.com/v1"
)
response = client.chat.completions.create(
model="GLM-4-Air",
messages=[
{"role": "system", "content": "你是一个智能助手"},
{"role": "user", "content": "请介绍一下智谱AI"}
],
temperature=0.7,
stream=False
)
print(response.choices[0].message.content)