Responses

Cấu trúc response object trả về từ Chat Completion API.

Chat Completion Object

Non-streaming response trả về object hoàn chỉnh:

json
{
  "id": "chatcmpl-abc123def456",
  "object": "chat.completion",
  "created": 1711684800,
  "model": "openai/gpt-4o",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "Đây là câu trả lời từ AI."
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 42,
    "completion_tokens": 128,
    "total_tokens": 170
  }
}

Response Fields

id string

ID duy nhất cho request. Format: chatcmpl-...

object string

Luôn là "chat.completion" (hoặc "chat.completion.chunk" cho streaming).

model string

Model thực tế đã xử lý request.

choices array

Mảng kết quả. Mỗi choice có index, message, và finish_reason.

usage object

Thông tin tokens đã sử dụng: prompt_tokens, completion_tokens, total_tokens.

Finish Reasons

Giá trịMô tả
stopHoàn thành tự nhiên hoặc gặp stop sequence
lengthĐạt giới hạn max_tokens
tool_callsModel yêu cầu gọi tool/function
content_filterNội dung bị chặn bởi bộ lọc an toàn

Tool Calls Response

Khi model quyết định gọi function, message sẽ chứa tool_calls:

json
{
  "choices": [
    {
      "message": {
        "role": "assistant",
        "content": null,
        "tool_calls": [
          {
            "id": "call_abc123",
            "type": "function",
            "function": {
              "name": "get_weather",
              "arguments": "{\"city\":\"Hà Nội\"}"
            }
          }
        ]
      },
      "finish_reason": "tool_calls"
    }
  ]
}
💡 Gửi kết quả tool_call về model Sau khi thực thi function, gửi kết quả với role: "tool"tool_call_id khớp để model tiếp tục trả lời.

Streaming Chunk Format

Khi dùng streaming, mỗi chunk có cấu trúc tương tự nhưng dùng delta thay cho message:

json
{
  "id": "chatcmpl-abc123",
  "object": "chat.completion.chunk",
  "choices": [
    {
      "index": 0,
      "delta": {
        "content": "token"
      },
      "finish_reason": null
    }
  ]
}