Appearance
API 快速开始
XAI.KG 兼容 OpenAI API 调用格式。当前 API 调用请使用流式输出,也就是请求里显式传入 "stream": true。
流式聊天
bash
curl -N https://api.xai.kg/v1/chat/completions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-5.5",
"messages": [
{ "role": "user", "content": "Reply exactly: pong" }
],
"stream": true
}'1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
JavaScript 示例
js
import OpenAI from 'openai'
const client = new OpenAI({
apiKey: process.env.XAI_KG_API_KEY,
baseURL: 'https://api.xai.kg/v1'
})
const stream = await client.chat.completions.create({
model: 'gpt-5.5',
messages: [{ role: 'user', content: 'Reply exactly: pong' }],
stream: true
})
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content ?? '')
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Python 示例
python
from openai import OpenAI
client = OpenAI(
api_key="YOUR_API_KEY",
base_url="https://api.xai.kg/v1",
)
stream = client.chat.completions.create(
model="gpt-5.5",
messages=[{"role": "user", "content": "Reply exactly: pong"}],
stream=True,
)
for chunk in stream:
print(chunk.choices[0].delta.content or "", end="")1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15