Skip to main content
POST
CometAPI định tuyến Chat Completions đến nhiều nhà cung cấp — bao gồm OpenAI, Claude và Gemini — thông qua một giao diện tương thích OpenAI duy nhất. Chuyển đổi giữa các mô hình bằng cách thay đổi tham số model; hầu hết SDK tương thích OpenAI hoạt động khi đặt base_url thành https://api.cometapi.com/v1.
Tham số yêu cầu và trường phản hồi có thể khác nhau đáng kể giữa các nhà cung cấp mô hình. Hãy xem tài liệu chính thức của nhà cung cấp đứng sau mô hình bạn sử dụng bất cứ khi nào cần danh sách tham số đầy đủ hoặc hành vi riêng của nhà cung cấp. Ví dụ: reasoning_effort chỉ áp dụng cho các mô hình suy luận (o-series, GPT-5.1+), và một số mô hình không hỗ trợ logprobs hoặc n > 1.
Đối với các mô hình OpenAI Pro, các mô hình suy luận o-series và các mô hình Codex, hãy sử dụng Responses endpoint thay thế. Các nhóm mô hình này được hỗ trợ đầy đủ hơn trên Responses API.

Vai trò tin nhắn

Đối với các mô hình mới hơn (GPT-4.1, dòng GPT-5, o-series), nên ưu tiên developer hơn system cho các tin nhắn chỉ dẫn. Cả hai đều hoạt động, nhưng developer cung cấp khả năng tuân theo chỉ dẫn mạnh hơn.

Gửi dữ liệu đầu vào Multimodal

Nhiều mô hình hỗ trợ hình ảnh và âm thanh cùng với văn bản. Để gửi tin nhắn Multimodal, hãy sử dụng định dạng mảng cho content:
Tham số detail kiểm soát độ sâu phân tích hình ảnh:
  • low — nhanh hơn, sử dụng ít tokens hơn (chi phí cố định)
  • high — phân tích chi tiết, tiêu tốn nhiều tokens hơn
  • auto — mô hình tự quyết định (mặc định)

Truyền phản hồi theo luồng

Để nhận đầu ra tăng dần, hãy đặt stream thành true. Phản hồi được phân phối dưới dạng Server-Sent Events (SSE), trong đó mỗi sự kiện chứa một đối tượng chat.completion.chunk:
Để bao gồm số liệu thống kê mức sử dụng token trong các phản hồi Streaming, hãy đặt stream_options.include_usage thành true. Dữ liệu sử dụng xuất hiện trong chunk cuối cùng trước [DONE].

Yêu cầu đầu ra có cấu trúc

Để buộc mô hình trả về JSON hợp lệ khớp với một schema cụ thể, hãy sử dụng response_format:
Chế độ JSON Schema (json_schema) đảm bảo đầu ra khớp chính xác với schema của bạn. Chế độ JSON Object (json_object) chỉ đảm bảo JSON hợp lệ — cấu trúc không được ép buộc.

Gọi tools và functions

Để cho phép mô hình gọi các functions bên ngoài, hãy cung cấp định nghĩa tool:
Khi mô hình quyết định gọi một tool, phản hồi sẽ có finish_reason: "tool_calls" và mảng message.tool_calls sẽ chứa tên function cùng các đối số. Sau đó, bạn thực thi function và gửi kết quả trở lại dưới dạng tin nhắn tool với tool_call_id tương ứng.

Lưu ý giữa các nhà cung cấp

  • max_tokens — Tham số cũ. Hoạt động với hầu hết mô hình nhưng đã không còn được khuyến nghị cho các mô hình OpenAI mới hơn.
  • max_completion_tokens — Tham số được khuyến nghị cho các mô hình GPT-4.1, dòng GPT-5 và o-series. Bắt buộc đối với các mô hình suy luận vì bao gồm cả output tokens và reasoning tokens.
CometAPI tự động xử lý việc ánh xạ khi định tuyến đến các nhà cung cấp khác nhau.
  • system — Vai trò chỉ dẫn truyền thống. Hoạt động với tất cả mô hình.
  • developer — Được giới thiệu cùng các mô hình o1. Cung cấp khả năng tuân theo chỉ dẫn mạnh hơn cho các mô hình mới hơn. Chuyển sang hành vi system trên các mô hình cũ hơn.
Sử dụng developer cho các dự án mới nhắm đến các mô hình GPT-4.1+ hoặc o-series.

Câu hỏi thường gặp

Cách xử lý giới hạn tốc độ?

Khi gặp 429 Too Many Requests, hãy triển khai exponential backoff:

Cách duy trì ngữ cảnh hội thoại?

Bao gồm toàn bộ lịch sử hội thoại trong mảng messages:

finish_reason có nghĩagì?

Cách kiểm soát chi phí?

  1. Sử dụng max_completion_tokens để giới hạn độ dài đầu ra.
  2. Sử dụng gpt-5.6-terra để cân bằng giữa năng lực và chi phí, hoặc gpt-5.6-luna cho các khối lượng công việc hiệu quả, quy mô lớn.
  3. Giữ prompt ngắn gọn — tránh ngữ cảnh dư thừa.
  4. Theo dõi mức sử dụng token trong trường phản hồi usage.

Ủy quyền

Authorization
string
header
bắt buộc

Bearer token authentication. Use your CometAPI key.

Nội dung

application/json
model
string
mặc định:gpt-5.6-sol
bắt buộc

Model ID to use for this request. See the Models page for current options.

Ví dụ:

"gpt-4.1"

messages
object[]
bắt buộc

A list of messages forming the conversation. Each message has a role (system, user, assistant, or developer) and content (text string or multimodal content array).

stream
boolean

If true, partial response tokens are delivered incrementally via server-sent events (SSE). The stream ends with a data: [DONE] message.

temperature
number
mặc định:1

Sampling temperature between 0 and 2. Higher values (e.g., 0.8) produce more random output; lower values (e.g., 0.2) make output more focused and deterministic. Recommended to adjust this or top_p, but not both.

Phạm vi bắt buộc: 0 <= x <= 2
top_p
number
mặc định:1

Nucleus sampling parameter. The model considers only the tokens whose cumulative probability reaches top_p. For example, 0.1 means only the top 10% probability tokens are considered. Recommended to adjust this or temperature, but not both.

Phạm vi bắt buộc: 0 <= x <= 1
n
integer
mặc định:1

Number of completion choices to generate for each input message. Defaults to 1.

stop
string

Up to 4 sequences where the API will stop generating further tokens. Can be a string or an array of strings.

max_tokens
integer

Maximum number of tokens to generate in the completion. The total of input + output tokens is capped by the model's context length.

presence_penalty
number
mặc định:0

Number between -2.0 and 2.0. Positive values penalize tokens based on whether they have already appeared, encouraging the model to explore new topics.

Phạm vi bắt buộc: -2 <= x <= 2
frequency_penalty
number
mặc định:0

Number between -2.0 and 2.0. Positive values penalize tokens proportionally to how often they have appeared, reducing verbatim repetition.

Phạm vi bắt buộc: -2 <= x <= 2
logit_bias
object

A JSON object mapping token IDs to bias values from -100 to 100. The bias is added to the model's logits before sampling. Values between -1 and 1 subtly adjust likelihood; -100 or 100 effectively ban or force selection of a token.

user
string

A unique identifier for your end-user. Helps with abuse detection and monitoring.

max_completion_tokens
integer

An upper bound for the number of tokens to generate, including visible output tokens and reasoning tokens. Use this instead of max_tokens for GPT-4.1+, GPT-5 series, and o-series models.

response_format
object

Specifies the output format. Use {"type": "json_object"} for JSON mode, or {"type": "json_schema", "json_schema": {...}} for strict structured output.

tools
object[]

A list of tools the model may call. Currently supports function type tools.

tool_choice
mặc định:auto

Controls how the model selects tools. auto (default): model decides. none: no tools. required: must call a tool.

logprobs
boolean
mặc định:false

Whether to return log probabilities of the output tokens.

top_logprobs
integer

Number of most likely tokens to return at each position (0-20). Requires logprobs to be true.

Phạm vi bắt buộc: 0 <= x <= 20
reasoning_effort
enum<string>

Controls the reasoning effort for o-series and GPT-5.1+ models.

Tùy chọn có sẵn:
low,
medium,
high
stream_options
object

Options for streaming. Only valid when stream is true.

service_tier
enum<string>

Specifies the processing tier.

Tùy chọn có sẵn:
auto,
default,
flex,
priority

Phản hồi

Successful chat completion response.

id
string

Unique completion identifier.

Ví dụ:

"chatcmpl-abc123"

object
enum<string>

Object type. Non-streaming responses use chat.completion.

Tùy chọn có sẵn:
chat.completion
Ví dụ:

"chat.completion"

created
integer

Unix timestamp of creation.

Ví dụ:

1774412483

model
string

The model used (may include version suffix).

Ví dụ:

"gpt-5.4-2026-03-05"

choices
object[]

Array of completion choices.

usage
object

Token accounting for this request. Billing uses these counts.

service_tier
string

Service tier that processed the request, when the provider reports one.

Ví dụ:

"default"

system_fingerprint
string | null

Provider backend configuration fingerprint, when the provider reports one.

Ví dụ:

"fp_490a4ad033"