跳转到主要内容

创建聊天补全

POST /v1/chat/completions
根据输入的消息列表生成模型回复,支持非流式和流式响应。 本文页只写入 2026-03-23 已在 Crazyrouter 生产环境复核过的通用行为。

认证

Authorization: Bearer YOUR_API_KEY

核心参数

参数类型必填说明
modelstring模型名称,如 gpt-5.4claude-sonnet-4-6gemini-3-pro-preview
messagesarray消息列表
streamboolean是否启用流式输出
max_tokensinteger最大输出 Token 数
temperaturenumber采样温度
response_formatobject结构化输出格式约束
toolsarray工具列表
tool_choicestring|object工具选择策略
stream_optionsobject流式附加选项,例如 include_usage
不同模型对可选参数的支持程度不同。写客户端时应优先依赖通用字段,不要假设所有模型都完整支持同一组高级参数。

非流式请求

cURL
curl https://crazyrouter.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
    "model": "gpt-5.4",
    "messages": [
      {
        "role": "system",
        "content": "You are a helpful assistant."
      },
      {
        "role": "user",
        "content": "Explain artificial intelligence in one sentence."
      }
    ],
    "max_tokens": 64
  }'
当前生产返回的典型结构:
{
  "object": "chat.completion",
  "model": "gpt-5.4",
  "choices": [
    {
      "message": {
        "role": "assistant",
        "content": "...",
        "reasoning_content": null,
        "tool_calls": null
      },
      "finish_reason": "stop"
    }
  ]
}

需要特别注意

  • message.content 是最稳定的最终文本字段
  • message.tool_calls 只在工具调用时出现
  • message.reasoning_content 可能存在键,但当前不应视为稳定必有内容

流式请求

cURL
curl https://crazyrouter.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
    "model": "gpt-5.4",
    "messages": [
      {
        "role": "user",
        "content": "Explain AI in one short sentence."
      }
    ],
    "stream": true,
    "stream_options": {
      "include_usage": true
    },
    "max_tokens": 64
  }'
本轮生产复核确认:
  • 流式返回 chat.completion.chunk
  • SSE 以 data: ... 形式逐块发送
  • 流结束时仍是 data: [DONE]
当前收到的 SSE 片段形态类似:
data: {"object":"chat.completion.chunk","model":"gpt-5.4","choices":[{"delta":{"role":"assistant","content":"AI","reasoning_content":null,"tool_calls":null}}]}
data: [DONE]

Python 示例

Python
from openai import OpenAI

client = OpenAI(
    api_key="YOUR_API_KEY",
    base_url="https://crazyrouter.com/v1"
)

stream = client.chat.completions.create(
    model="gpt-5.4",
    messages=[
        {"role": "user", "content": "Explain AI in one short sentence."}
    ],
    stream=True,
    stream_options={"include_usage": True},
    max_tokens=64
)

for chunk in stream:
    delta = chunk.choices[0].delta
    if delta.content is not None:
        print(delta.content, end="")

当前建议

  • 普通文本对话:/v1/chat/completions
  • 需要 reasoning 摘要或 OpenAI 风格 web search:优先 /v1/responses
  • 需要更复杂的能力判定时,先看对应能力页,不要只看本页
相关页面: