跳转到主要内容

创建聊天补全

POST /v1/chat/completions
根据输入的消息列表生成模型回复。支持流式和非流式两种响应模式。

认证

在请求头中传入 API Key:
Authorization: Bearer YOUR_API_KEY

请求参数

参数类型必填默认值说明
modelstring-模型名称,如 gpt-4oclaude-sonnet-4-20250514gemini-2.5-flash
messagesarray-消息列表,每条消息包含 rolecontent
temperaturenumber1采样温度,范围 0-2。值越高输出越随机
top_pnumber1核采样参数,范围 0-1。与 temperature 二选一使用
ninteger1生成的回复数量
streambooleanfalse是否启用流式输出
stream_optionsobjectnull流式选项。设置 {"include_usage": true} 可在最后一个 chunk 中返回用量
stopstring|arraynull停止生成的标记,最多 4 个
max_tokensinteger模型默认最大生成 Token 数
presence_penaltynumber0存在惩罚,范围 -2.0 到 2.0
frequency_penaltynumber0频率惩罚,范围 -2.0 到 2.0
logprobsbooleanfalse是否返回 logprobs
top_logprobsintegernull返回的 top logprobs 数量,0-20
response_formatobjectnull指定输出格式,如 {"type": "json_object"}
seedintegernull随机种子,用于可复现输出
toolsarraynull可用工具列表(Function Calling)
tool_choicestring|object”auto”工具选择策略
userstringnull终端用户标识

messages 格式

[
  {"role": "system", "content": "你是一个有帮助的助手。"},
  {"role": "user", "content": "你好"},
  {"role": "assistant", "content": "你好!有什么可以帮助你的?"},
  {"role": "user", "content": "介绍一下你自己"}
]
支持的 role 值:systemuserassistanttool

非流式请求

curl https://crazyrouter.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
    "model": "gpt-4o",
    "messages": [
      {"role": "system", "content": "你是一个有帮助的助手。"},
      {"role": "user", "content": "用一句话解释什么是人工智能"}
    ],
    "temperature": 0.7
  }'

非流式响应

{
  "id": "chatcmpl-abc123",
  "object": "chat.completion",
  "created": 1709123456,
  "model": "gpt-4o",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "人工智能是让计算机模拟人类智能行为的技术,使机器能够学习、推理和解决问题。"
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 28,
    "completion_tokens": 32,
    "total_tokens": 60
  }
}

流式请求

curl https://crazyrouter.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -d '{
    "model": "gpt-4o",
    "messages": [
      {"role": "system", "content": "你是一个有帮助的助手。"},
      {"role": "user", "content": "用一句话解释什么是人工智能"}
    ],
    "stream": true,
    "stream_options": {"include_usage": true}
  }'

流式响应

data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1709123456,"model":"gpt-4o","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":null}]}

data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1709123456,"model":"gpt-4o","choices":[{"index":0,"delta":{"content":"人工智能"},"finish_reason":null}]}

data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1709123456,"model":"gpt-4o","choices":[{"index":0,"delta":{"content":"是让"},"finish_reason":null}]}

data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1709123456,"model":"gpt-4o","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}

data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1709123456,"model":"gpt-4o","choices":[],"usage":{"prompt_tokens":28,"completion_tokens":32,"total_tokens":60}}

data: [DONE]
设置 stream_options.include_usagetrue 后,最后一个 chunk 会包含完整的 usage 信息。
max_tokens 参数限制的是输出 Token 数,不包括输入。如果输出被截断,finish_reason 会返回 length