Chat Completions API
完全兼容 OpenAI Chat Completions API 格式。
请求
POST https://api.tokenflying.top/v1/chat/completionsHeaders
| 字段 | 值 |
|---|---|
Content-Type | application/json |
Authorization | Bearer sk-你的ApiKey |
Body
json
{
"model": "gpt-5.5",
"messages": [
{"role": "system", "content": "你是一个有用的助手"},
{"role": "user", "content": "你好"}
],
"temperature": 0.7,
"max_tokens": 2048,
"stream": false
}参数说明
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
model | string | ✅ | 模型名称,见 模型列表 |
messages | array | ✅ | 对话消息列表 |
temperature | number | ❌ | 随机性,0-2,默认 1 |
max_tokens | integer | ❌ | 最大输出 token 数 |
stream | boolean | ❌ | 是否流式输出,默认 false |
top_p | number | ❌ | 核采样参数,0-1 |
响应
json
{
"id": "chatcmpl-xxx",
"object": "chat.completion",
"created": 1234567890,
"model": "gpt-5.5",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "你好!有什么可以帮你的?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 20,
"completion_tokens": 15,
"total_tokens": 35
}
}流式输出
设置 "stream": true,响应以 SSE (Server-Sent Events) 格式返回:
data: {"id":"chatcmpl-xxx","choices":[{"delta":{"content":"你"},"index":0}]}
data: {"id":"chatcmpl-xxx","choices":[{"delta":{"content":"好"},"index":0}]}
data: [DONE]Python 流式示例
python
from openai import OpenAI
client = OpenAI(
api_key="sk-你的ApiKey",
base_url="https://api.tokenflying.top/v1"
)
stream = client.chat.completions.create(
model="gpt-5.5",
messages=[{"role": "user", "content": "写一首诗"}],
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")多轮对话
将历史消息一起传入 messages 数组:
json
{
"model": "gpt-5.5",
"messages": [
{"role": "user", "content": "我叫小明"},
{"role": "assistant", "content": "你好小明!"},
{"role": "user", "content": "我叫什么?"}
]
}