Chat Completions
Chat Completions 是最常用的对话接口,适合普通聊天、编程助手、摘要、分类、工具调用和多轮对话。
Endpoint
Section titled “Endpoint”POST /v1/chat/completions完整地址:
https://www.routeapi.ai/v1/chat/completionscurl https://www.routeapi.ai/v1/chat/completions \ -H "Authorization: Bearer $ROUTEAPI_KEY" \ -H "Content-Type: application/json" \ -d '{ "model": "gpt-4o", "messages": [ { "role": "user", "content": "请用一句话介绍 RouteAPI" } ] }'| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
model | string | 是 | 模型 ID,必须是当前账户可用模型 |
messages | array | 是 | 对话消息列表 |
stream | boolean | 否 | 是否使用 SSE 流式输出 |
temperature | number | 否 | 采样温度 |
top_p | number | 否 | nucleus sampling 参数 |
max_tokens | number | 否 | 最大输出 token 数 |
tools | array | 否 | 工具定义 |
tool_choice | string/object | 否 | 工具选择策略 |
response_format | object | 否 | JSON mode 或 JSON Schema |
stream_options | object | 否 | 流式输出附加选项 |
messages 格式
Section titled “messages 格式”[ { "role": "system", "content": "你是一个严谨的技术助手。" }, { "role": "user", "content": "解释什么是 API 网关。" }]常见 role:
| Role | 说明 |
|---|---|
system | 系统提示词 |
user | 用户输入 |
assistant | 模型历史回复 |
tool | 工具调用结果 |
{ "id": "chatcmpl_xxx", "object": "chat.completion", "created": 1730000000, "model": "gpt-4o", "choices": [ { "index": 0, "message": { "role": "assistant", "content": "RouteAPI 是一个统一管理多家 AI 模型供应商的 API 网关。" }, "finish_reason": "stop" } ], "usage": { "prompt_tokens": 24, "completion_tokens": 18, "total_tokens": 42 }}- 同一个参数在不同模型服务中的支持程度可能不同。
- 如果请求包含
tools、response_format、stream_options等可选参数,请先确认所选模型支持对应能力。 - 明确传入
0或false的可选参数应被视为用户显式设置。