API 参考文档
poeti.ai 提供完全兼容 OpenAI 格式的统一 AI 接口。无需修改现有代码,只需替换 Base URL 和 API Key 即可接入 100+ AI 模型。
🔗 Base URL:
https://api.poeti.ai
协议版本:
v1
所有端点一览
认证方式
所有 API 请求需在 HTTP Header 中携带 Bearer Token:
// HTTP Header
Authorization: Bearer sk-poeti-xxxxxxxxxxxx
聊天补全
核心接口,兼容 OpenAI Chat Completions API 格式。
POST
/v1/chat/completions
请求参数
请求示例
# 基础请求示例
curl https://api.poeti.ai/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $POETI_API_KEY" \
-d '{
"model": "gpt-4o",
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "解释量子纠缠"}
],
"temperature": 0.7,
"max_tokens": 1024
}'
响应格式
// 非流式响应 (stream: false)
{
"id": "chatcmpl-poeti-a1b2c3d4",
"object": "chat.completion",
"created": 1712678400,
"model": "gpt-4o",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "量子纠缠是一种量子力学现象..."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 28,
"completion_tokens": 312,
"total_tokens": 340
}
}
💡 响应头:每个请求会返回
X-Trace-ID 响应头,可用于在 请求日志中查找具体调用记录。
流式响应(Stream)
设置 stream: true 后,服务器通过 Server-Sent Events (SSE) 逐块返回内容:
# Python 流式示例
stream = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "写一首关于春天的诗"}],
stream=True
)
for chunk in stream:
delta = chunk.choices[0].delta.content
if delta is not None:
print(delta, end="", flush=True)
// SSE 流数据格式
data: {"id":"chatcmpl-...","choices":[{"delta":{"content":"量"},"index":0}]}
data: {"id":"chatcmpl-...","choices":[{"delta":{"content":"子"},"index":0}]}
data: {"id":"chatcmpl-...","choices":[{"delta":{},"finish_reason":"stop","index":0}]}
data: [DONE]
模型列表
获取当前平台支持的所有 AI 模型。
GET
/v1/models
# 获取所有可用模型
curl https://api.poeti.ai/v1/models \
-H "Authorization: Bearer $POETI_API_KEY"
响应格式
{
"object": "list",
"data": [
{
"id": "gpt-4o",
"object": "model",
"created": 1712678400,
"owned_by": "openai"
},
{
"id": "claude-3-5-sonnet-20241022",
"object": "model",
"created": 1712678400,
"owned_by": "anthropic"
}
]
}
完整的模型列表(含价格和上下文长度)请参见 支持的模型。
API Keys 管理
创建和管理访问 poeti.ai 平台的 API Keys。
GET
/api/v1/api-keys
# 获取当前用户的所有 API Key
curl https://api.poeti.ai/api/v1/api-keys \
-H "Authorization: Bearer $POETI_API_KEY"
// 响应示例
{
"code": 200,
"data": [
{
"id": 1,
"name": "生产环境 Key",
"key_prefix": "sk-poeti-a1b2...",
"is_active": true,
"total_requests": 15420,
"total_cost": 12.84,
"created_at": "2026-01-15T08:00:00Z"
}
]
}
POST
/api/v1/api-keys
# 创建新 API Key
curl -X POST https://api.poeti.ai/api/v1/api-keys \
-H "Authorization: Bearer $POETI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"name": "我的应用",
"monthly_budget": 50.0,
"rate_limit": 100
}'
用量统计
查询 Token 用量和成本统计。
GET
/api/v1/usage
# 查询本月用量
curl "https://api.poeti.ai/api/v1/usage?date_from=2026-04-01&date_to=2026-04-30" \
-H "Authorization: Bearer $POETI_API_KEY"
// 响应示例
{
"code": 200,
"data": {
"total_requests": 8432,
"total_prompt_tokens": 1240580,
"total_completion_tokens": 845230,
"total_tokens": 2085810,
"total_cost_usd": 24.56,
"by_model": [
{
"model": "gpt-4o",
"requests": 3210,
"tokens": 980450,
"cost_usd": 14.30
}
]
}
}
请求追踪 API
v2.6.0 新增查询每条请求的详细追踪记录,包含成本分解、延迟、所用 Key 等信息。
✅ v2.6.0 新功能:每次 API 调用都会自动生成 Trace 记录。响应头包含
X-Trace-ID,可用于快速定位单条请求。
GET
/api/v1/request-traces
# 查询最近 20 条失败请求
curl "https://api.poeti.ai/api/v1/request-traces?status=error&page_size=20" \
-H "Authorization: Bearer $POETI_API_KEY"
// 响应示例
{
"code": 200,
"data": {
"items": [
{
"trace_id": "trace-poeti-7f8e9d0a",
"model": "gpt-4o",
"provider": "openai",
"status": "success",
"prompt_tokens": 128,
"completion_tokens": 256,
"cost_usd": 0.00512,
"latency_ms": 843,
"routing_strategy": "comprehensive",
"created_at": "2026-04-09T14:23:18Z"
}
],
"total": 15420,
"page": 1,
"page_size": 20
}
}
GET
/api/v1/request-traces/stats
// 聚合统计响应
{
"total_requests": 15420,
"success_count": 15210,
"error_count": 210,
"total_tokens": 8450230,
"total_cost_usd": 42.18,
"avg_latency_ms": 723,
"avg_cost_per_request": 0.00274
}
错误码参考
所有错误响应遵循统一格式,包含 HTTP 状态码和业务错误码。
// 错误响应格式
{
"error": {
"code": "invalid_api_key",
"message": "The provided API key is invalid or has been revoked.",
"status": 401
}
}
重试策略
import time, openai
def chat_with_retry(client, **kwargs):
max_retries = 3
wait = 1
for attempt in range(max_retries):
try:
return client.chat.completions.create(**kwargs)
except openai.RateLimitError:
if attempt == max_retries - 1: raise
time.sleep(wait)
wait *= 2 # 指数退避
except openai.APIStatusError as e:
if e.status_code >= 500:
time.sleep(wait)
wait *= 2
else:
raise