跳转到主要内容

聊天完成对象

聊天完成 API 返回两种对象类型:非流式响应返回 ChatCompletion 对象,流式响应返回 ChatCompletionChunk 对象。

Chat Completion 对象

非流式请求返回的完整响应对象。
{
  "id": "chatcmpl-abc123",
  "object": "chat.completion",
  "created": 1709123456,
  "model": "gpt-4o",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "你好!有什么可以帮助你的吗?"
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 12,
    "completion_tokens": 15,
    "total_tokens": 27
  },
  "system_fingerprint": "fp_abc123"
}

字段说明

字段类型说明
idstring本次补全的唯一标识符
objectstring固定为 chat.completion
createdinteger创建时间的 Unix 时间戳
modelstring实际使用的模型名称
choicesarray补全结果列表,长度由请求参数 n 决定
choices[].indexinteger结果在列表中的索引
choices[].messageobject模型生成的消息
choices[].message.rolestring固定为 assistant
choices[].message.contentstring|null消息文本内容
choices[].message.tool_callsarray|null模型请求调用的工具列表
choices[].finish_reasonstring停止原因:stoplengthtool_callscontent_filter
usageobjectToken 用量统计
usage.prompt_tokensinteger输入 Token 数
usage.completion_tokensinteger输出 Token 数
usage.total_tokensinteger总 Token 数
system_fingerprintstring|null系统指纹

Chat Completion Chunk 对象(流式)

流式请求时,服务器以 SSE(Server-Sent Events)格式逐块返回 ChatCompletionChunk 对象。
{
  "id": "chatcmpl-abc123",
  "object": "chat.completion.chunk",
  "created": 1709123456,
  "model": "gpt-4o",
  "choices": [
    {
      "index": 0,
      "delta": {
        "role": "assistant",
        "content": "你"
      },
      "finish_reason": null
    }
  ]
}

字段说明

字段类型说明
idstring本次补全的唯一标识符,所有 chunk 共享同一 id
objectstring固定为 chat.completion.chunk
createdinteger创建时间的 Unix 时间戳
modelstring实际使用的模型名称
choicesarray增量结果列表
choices[].indexinteger结果在列表中的索引
choices[].deltaobject增量消息内容
choices[].delta.rolestring仅在第一个 chunk 中出现,值为 assistant
choices[].delta.contentstring增量文本内容
choices[].delta.tool_callsarray增量工具调用
choices[].finish_reasonstring|null最后一个 chunk 中为停止原因,其余为 null
usageobject|null仅在 stream_options.include_usagetrue 时,最后一个 chunk 包含用量

流式传输格式

流式响应遵循 SSE 协议,每个事件以 data: 前缀开头,流结束时发送 data: [DONE]
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: [DONE]
Crazyrouter 兼容 OpenAI 的完整 Chat Completion 对象格式,支持所有标准字段。上游模型返回的额外字段也会透传给客户端。