POST /v1/chat/completions
通过流、工具、视觉和模型别名完成与 OpenAI 兼容的聊天。
POST /v1/chat/completions
要求
curl https://api.model-gate.com/v1/chat/completions \
-H "Authorization: Bearer mg_live_..." \
-H "Content-Type: application/json" \
-d '{
"model": "ch-47",
"messages": [
{"role": "user", "content": "Explain this API in one sentence."}
],
"stream": false
}'
这 model 可以是规范 ID 或启用的别名。 Model Gate 在定价之前解析别名并向上游发送规范 ID。
回复
{
"id": "chatcmpl_example",
"object": "chat.completion",
"model": "claude-opus-4.7",
"choices": [
{
"index": 0,
"message": {"role": "assistant", "content": "Model Gate provides one compatible API for multiple AI models."},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 14,
"completion_tokens": 17,
"total_tokens": 31
}
}
流媒体请求
放 "stream": true。上游服务器发送的事件将被传递,而无需重写成功的事件负载。
异步请求
对于支持的端点,添加 "async": true 和 "stream": false。立即响应是 HTTP 202 并包含 request_id 和 Partner API 结果 URL。
错误
提供者非 2xx 状态、正文和内容类型将保持不变。本地生成的验证错误使用模型门错误包络。
| 地位 | 意义 | 行动 |
|---|---|---|
400 | 无效请求 | 检查请求正文和模型 ID。 |
401 | 无效或冻结的密钥 | 验证或轮换 API 密钥。 |
429 | 超出速率或并发限制 | 尊重 Retry-After 并通过退避重试。 |
5xx | 临时上游或网关错误 | 使用指数退避重试幂等请求。 |