文本生成 (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 | 可选 | 生成的最大 token 数量 |
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
}
}代码示例
Python
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)JavaScript
import OpenAI from 'openai';
// 使用代理 API
const client = new OpenAI({
apiKey: 'your-api-key',
baseURL: 'https://your-proxy-domain.com/v1'
});
async function chat() {
const response = await client.chat.completions.create({
model: 'GLM-4-Air',
messages: [
{ role: 'system', content: '你是一个智能助手' },
{ role: 'user', content: '请介绍一下智谱AI' }
],
temperature: 0.7,
stream: false
});
console.log(response.choices[0].message.content);
}
chat();cURL
curl https://your-proxy-domain.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your-api-key" \
-d '{
"model": "GLM-4-Air",
"messages": [
{"role": "system", "content": "你是一个智能助手"},
{"role": "user", "content": "请介绍一下智谱AI"}
],
"temperature": 0.7
}'