ChatCompletions 格式
根据对话历史创建模型响应。支持流式和非流式响应。兼容 OpenAI Chat Completions API。
注意: 完整路径为 POST /v1/chat/completions。OpenAI SDK 的 base_url 需填写完整路径(含 /v1)。支持 usage.include(响应返回费用)、models(回退模型序列)与 provider.sort(路由偏好),见下方请求体说明。
示例模型
下文均以 gpt-4(GPT-4)为例;更换其他模型时,请替换 model 参数,并参考控制台模型说明调整 size、quality 等字段。
创建聊天补全
POST
https://token.easyapi.com/v1/chat/completions根据对话历史创建模型响应,兼容 OpenAI Chat Completions API,支持流式与非流式。
请求参数
| 名称 | 位置 | 类型 | 必选 | 说明 |
|---|---|---|---|---|
Authorization | header | string | 是 | EasyAPI API Key。格式:Bearer `API_KEY` |
Content-Type | header | string | 是 | 请求体格式 |
请求体
| 名称 | 类型 | 必选 | 说明 |
|---|---|---|---|
model | string | 是 | 模型 ID |
messages | array<object> | 是 | 对话消息列表 |
temperature | number | 否 | 采样温度,默认 1,范围 0–2 |
top_p | number | 否 | 核采样参数,默认 1,范围 0–1 |
n | integer | 否 | 生成数量,默认 1,范围 ≥ 1 |
stream | boolean | 否 | 是否流式响应,默认 false |
stream_options | object | 否 | 流式响应附加选项 |
stop | string | array<string> | 否 | 停止序列,字符串或字符串数组 |
max_tokens | integer | 否 | 最大生成 Token 数 |
max_completion_tokens | integer | 否 | 最大补全 Token 数 |
presence_penalty | number | 否 | 存在惩罚,默认 0,范围 -2–2 |
frequency_penalty | number | 否 | 频率惩罚,默认 0,范围 -2–2 |
logit_bias | object | 否 | 调整指定 token 出现概率 |
user | string | 否 | 终端用户标识,便于滥用监测 |
tools | array<object> | 否 | 可调用的工具定义列表 |
tool_choice | string | object | 否 | 工具选择策略,字符串或对象 |
response_format | object | 否 | 响应格式,如 JSON Schema |
seed | integer | 否 | 随机种子,用于尽量复现结果 |
reasoning_effort | string | 否 | medium |
modalities | array<string> | 否 | 输出模态,如 text、audio |
audio | object | 否 | 音频输出相关配置 |
usage | object | 否 | 设为 usage.include = true 时,响应的 usage 对象会多一个 cost 字段(本次调用费用,USD);流式请求在最后一个带 usage 的块里返回。最终扣费以 GET /v1/generation 为准 |
models | array<string> | 否 | 回退模型序列(最多 5 个)。主模型没有可用渠道,或上游失败且尚未开始输出时,按顺序换用下一个模型重试;仅限本 Key 允许的模型 |
provider | object | 否 | 同优先级渠道内的路由偏好:provider.sort 取 price 选最便宜的渠道;latency 选实测首字延迟最低的(非流式按端到端耗时);throughput 选实测输出 tokens/s 最高的。latency 与 throughput 用该模型在各渠道的真实请求实测,没有实测数据时退回渠道测速。模型名后缀 :floor 等价 sort=price,:nitro 等价 sort=throughput |
示例
{
"model": "gpt-4",
"messages": [
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "Hello!"
}
],
"temperature": 0.7,
"stream": false,
"usage": {
"include": true
}
}返回示例
{
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1718452800,
"model": "gpt-4",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Hello! How can I help you today?"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 20,
"completion_tokens": 12,
"total_tokens": 32,
"prompt_tokens_details": {
"cached_tokens": 0,
"text_tokens": 20,
"audio_tokens": 0,
"image_tokens": 0
},
"completion_tokens_details": {
"text_tokens": 12,
"audio_tokens": 0,
"reasoning_tokens": 0
},
"cost": 0.000124
},
"system_fingerprint": "fp_abc123"
}