Chat Completions API 完整指南
ChatGLM / GLM 支持 OpenAI 兼容 Chat Completions。Xikapi 示例统一使用:
text
https://xikapi.com/v1/chat/completions基础调用
bash
curl https://xikapi.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $XIKAPI_API_KEY" \
-d '{
"model": "glm-5.1-air",
"messages": [
{ "role": "system", "content": "You are a helpful assistant." },
{ "role": "user", "content": "Explain in three sentences what ChatGLM is best suited for" }
]
}'JavaScript
javascript
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.XIKAPI_API_KEY,
baseURL: "https://xikapi.com/v1",
});
const completion = await client.chat.completions.create({
model: "glm-5.1-air",
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "Introduce the ChatGLM-compatible calling method" },
],
});
console.log(completion.choices[0].message.content);Python
python
from openai import OpenAI
client = OpenAI(
api_key="YOUR_XIKAPI_API_KEY",
base_url="https://xikapi.com/v1",
)
completion = client.chat.completions.create(
model="glm-5.1-air",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Introduce the ChatGLM-compatible calling method"},
],
)
print(completion.choices[0].message.content)多轮对话
json
{
"model": "glm-5.1-air",
"messages": [
{ "role": "user", "content": "My system needs knowledge-base Q&A" },
{ "role": "assistant", "content": "Start with document chunking and retrieval." },
{ "role": "user", "content": "What are good document chunking practices?" }
]
}流式输出
json
{
"model": "glm-5.1-air",
"stream": true,
"messages": [
{ "role": "user", "content": "Write a short article about API integration" }
]
}接入建议
- API Key 只放在服务端
- 显式设置输出长度和超时
- 视觉模型和推理模型要单独压测
- 流式输出要处理异常中断和重试