聊天完成块对象
聊天完成块对象
chat.completion.chunk 是流式返回(stream: true)时每个 SSE 数据块的格式。
对象示例
{
"id": "chatcmpl-abc123",
"object": "chat.completion.chunk",
"created": 1677652288,
"model": "gpt-4o",
"system_fingerprint": "fp_44709d6fcb",
"choices": [
{
"index": 0,
"delta": {
"role": "assistant",
"content": "你好"
},
"finish_reason": null
}
]
}
最后一个块(finish_reason 不为 null)
{
"id": "chatcmpl-abc123",
"object": "chat.completion.chunk",
"created": 1677652288,
"model": "gpt-4o",
"choices": [
{
"index": 0,
"delta": {},
"finish_reason": "stop"
}
]
}
字段说明
| 字段 | 类型 | 说明 |
|---|---|---|
id |
string | 本次请求的唯一 ID(与所有块相同) |
object |
string | 固定为 "chat.completion.chunk" |
created |
integer | 创建时间(Unix 时间戳) |
model |
string | 实际使用的模型 ID |
system_fingerprint |
string | 系统配置指纹 |
choices[].index |
integer | 候选结果索引(通常为 0) |
choices[].delta.role |
string | 仅第一个 chunk 包含,值为 "assistant" |
choices[].delta.content |
string/null | 当前块的文本内容增量 |
choices[].delta.tool_calls |
array | 工具调用增量(工具调用时出现) |
choices[].finish_reason |
string/null | 结束原因;生成中为 null,结束时为 "stop" 等 |
完整流响应格式
data: {"id":"chatcmpl-abc","object":"chat.completion.chunk","model":"gpt-4o","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":null}]}
data: {"id":"chatcmpl-abc","object":"chat.completion.chunk","model":"gpt-4o","choices":[{"index":0,"delta":{"content":"你"},"finish_reason":null}]}
data: {"id":"chatcmpl-abc","object":"chat.completion.chunk","model":"gpt-4o","choices":[{"index":0,"delta":{"content":"好"},"finish_reason":null}]}
data: {"id":"chatcmpl-abc","object":"chat.completion.chunk","model":"gpt-4o","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}
data: [DONE]
流结束的标志是
data: [DONE],收到此事件后停止读取。