Chat Completions

OpenAI 聊天格式,适用于所有模型。

POST试一试

发送一段对话,得到下一条消息。这是大多数工具使用的格式:OpenAI SDK、Vercel AI SDK、LangChain、OpenClaw 和 Hermes。

较长的回答请用 stream: true 流式输出,再加上 stream_options: { "include_usage": true },就能在最后一个数据块中拿到费用。参见流式输出。

如需零数据保留,请发送 provider: { "zdr": true, "data_collection": "deny" }。这样调用只会发往不保留你发送内容、也不用其训练的服务商;如果该模型此刻没有这样的服务商可用,调用会返回错误,而不会转给会保留数据的服务商。直接提供的 Abliterated Large v2 按其厂商条款不保留任何内容,会忽略这个字段。同样直接提供的 Kimi K3.1 Pro 按 Kimi 的条款会保留发送给它的内容,所以带有其中任一字段的调用会返回 400 zdr_unavailable。binference.io 上的 AI 对话会对每个有这类服务商的模型发送它。

用 Authorization: Bearer binf_... 或 x-api-key 发送你的密钥。

请求体

这里列出的是大多数调用会用到的字段。OpenAI 格式的其他字段都会按原样转发。

  • modelstring必填

    来自 GET /models 的模型 ID,比如 anthropic/claude-sonnet-5.5。加上 :online 开启联网搜索,或加上 :nitro、:floor 或 :exacto 来影响由哪个服务商运行。

  • messagesobject[]必填

    到目前为止的对话,按时间从早到晚排列。

    • role"system" | "user" | "assistant" | "tool"必填

      这条消息由谁发出。

    • contentstring | object[]必填

      文本,或由多个部分组成:文本和图像 URL。图像和文件请以 URL 发送,不要内联数据:请求体最大 4 MB。

  • max_tokensinteger

    回答的最大长度,以 Token 计。请设置它:调用运行期间会预留它最长可能回答的费用,不设上限时,预留的就是模型的全部输出长度。也可以用 max_completion_tokens。

  • streamboolean

    以服务器发送事件的形式,边生成边发送回答。超过一句话的回答都建议使用:流式调用最长可运行 30 分钟,其他调用 13 分钟。

  • stream_optionsobject

    { "include_usage": true } 会在最后增加一个带 usage 的数据块,其中包括本次调用的费用。

  • temperaturenumber

    随机性,0 到 2。越低越可预测。

  • toolsobject[]

    模型可以调用的函数,采用该格式自己的结构。由你的代码运行它们,并把结果发回。也支持联网搜索和网页抓取。

  • tool_choicestring | object

    "auto"、"none"、"required",或按名称指定一个函数。

  • response_formatobject

    结构化输出:{ "type": "json_schema", "json_schema": { ... } } 让回答符合你的 schema。

  • reasoningobject

    用于推理模型:{ "effort": "low" | "medium" | "high" }。推理 Token 按输出计费。

  • modelsstring[]

    备用模型,在第一个模型繁忙或不可用时按顺序尝试。预留金额按其中最贵的模型计算。

返回

  • idstring

    回答的 ID。

  • choicesobject[]

    回答:message.content、可能有的 message.tool_calls,以及 finish_reason。

  • usageobject

    Token 数和本次调用的费用。

    • prompt_tokensinteger

      读取的 Token 数。

    • completion_tokensinteger

      生成的 Token 数,包括推理。

    • costnumber

      本次调用的扣费金额,单位为美元。正是从余额中扣除的金额。

响应头

  • x-binference-call-idstring

    这次调用在你调用记录页面上的 ID。联系我们时请提供它。

  • x-generation-idstring

    模型服务商为这次回答生成的 ID。

  • retry-afterinteger

    出现 429 或 503 时:重新发送前需要等待的整秒数。

  • retry-after-msinteger

    同样的等待时间,以毫秒计。OpenAI 和 Anthropic SDK 会读取它。

请求
curl https://binference.io/api/v1/chat/completions \  -H "Authorization: Bearer $BINF_API_KEY" \  -H "Content-Type: application/json" \  -d '{    "model": "anthropic/claude-sonnet-5.5",    "messages": [      {        "role": "system",        "content": "You are a concise trading assistant."      },      {        "role": "user",        "content": "Summarize the last 24 hours of trades in one line."      }    ],    "max_tokens": 256  }'
返回
{  "id": "gen-1790755652-kQ3hTz",  "object": "chat.completion",  "created": 1790755652,  "model": "anthropic/claude-sonnet-5.5",  "choices": [    {      "index": 0,      "message": {        "role": "assistant",        "content": "12 trades today: 9 wins, net +3.4 BNB, biggest move on $NOVA."      },      "finish_reason": "stop"    }  ],  "usage": {    "prompt_tokens": 31,    "completion_tokens": 22,    "total_tokens": 53,    "cost": 0.000338  }}
流式输出(stream: true)
data: {"id":"gen-1790755652-kQ3hTz","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"role":"assistant","content":"12 trades"}}]}data: {"id":"gen-1790755652-kQ3hTz","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":" today: 9 wins"}}]}data: {"id":"gen-1790755652-kQ3hTz","object":"chat.completion.chunk","choices":[{"index":0,"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":31,"completion_tokens":22,"total_tokens":53,"cost":0.000338}}data: [DONE]