Base URL
所有 API 请求的基础 URL:https://aiapi.xiaoliu.fun/v1
认证方式
所有 API 请求都需要在请求头中包含 API Key:Authorization: Bearer sk-your-api-key-here
请求格式
所有 API 请求都使用 JSON 格式:curl -X POST "https://aiapi.xiaoliu.fun/v1/chat/completions" \
-H "Authorization: <YOUR_API_KEY>" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4",
"messages": [
{
"role": "system",
"content": "string"
}
]
}'
响应格式
API 响应也使用 JSON 格式:{
"id": "string",
"object": "chat.completion",
"created": 0,
"model": "string",
"choices": [
{
"index": 0,
"message": {
"role": "system",
"content": "string",
"name": "string",
"tool_calls": [
{
"id": "string",
"type": "function",
"function": {
"name": "string",
"arguments": "string"
}
}
],
"tool_call_id": "string",
"reasoning_content": "string"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 0,
"completion_tokens": 0,
"total_tokens": 0,
"prompt_tokens_details": {
"cached_tokens": 0,
"text_tokens": 0,
"audio_tokens": 0,
"image_tokens": 0
},
"completion_tokens_details": {
"text_tokens": 0,
"audio_tokens": 0,
"reasoning_tokens": 0
}
},
"system_fingerprint": "string"
}
错误处理
API 使用标准的 HTTP 状态码:| 状态码 | 说明 |
|---|---|
| 200 | 请求成功 |
| 400 | 请求参数错误 |
| 401 | 认证失败(API Key 无效) |
| 429 | 请求频率超限 |
| 500 | 服务器错误 |
{
"error": {
"message": "string",
"type": "string",
"param": "string",
"code": "string"
}
}
流式响应
大多数文本生成接口支持流式响应,实时返回生成的内容:client = openai.OpenAI(
api_key="sk-your-api-key-here",
base_url="https://aiapi.xiaoliu.fun/v1"
)
stream = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "讲个故事"}],
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")
SDK 和工具
官方 SDK
API 兼容 OpenAI SDK,可直接使用:pip install openai