Tạo một chat completion
Sử dụng CometAPI POST /v1/chat/completions để gửi các cuộc hội thoại nhiều tin nhắn đến các mô hình chat với các tùy chọn Streaming, temperature và max_tokens.
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.
Vai trò tin nhắ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 chocontent:
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ơnauto— 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 đặtstream 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:
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ụngresponse_format:
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: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
Hỗ trợ tham số giữa các nhà cung cấp
Hỗ trợ tham số giữa các nhà cung cấp
max_tokens so với max_completion_tokens
max_tokens so với max_completion_tokens
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.
Vai trò system so với developer
Vai trò system so với developer
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 visystemtrên các mô hình cũ hơn.
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ặp429 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ảngmessages:
finish_reason có nghĩagì?
Cách kiểm soát chi phí?
- Sử dụng
max_completion_tokensđể giới hạn độ dài đầu ra. - Sử dụng
gpt-5.6-terrađể cân bằng giữa năng lực và chi phí, hoặcgpt-5.6-lunacho các khối lượng công việc hiệu quả, quy mô lớn. - Giữ prompt ngắn gọn — tránh ngữ cảnh dư thừa.
- Theo dõi mức sử dụng token trong trường phản hồi
usage.
Ủy quyền
Bearer token authentication. Use your CometAPI key.
Nội dung
Model ID to use for this request. See the Models page for current options.
"gpt-4.1"
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).
If true, partial response tokens are delivered incrementally via server-sent events (SSE). The stream ends with a data: [DONE] message.
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.
0 <= x <= 2Nucleus 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.
0 <= x <= 1Number of completion choices to generate for each input message. Defaults to 1.
Up to 4 sequences where the API will stop generating further tokens. Can be a string or an array of strings.
Maximum number of tokens to generate in the completion. The total of input + output tokens is capped by the model's context length.
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.
-2 <= x <= 2Number between -2.0 and 2.0. Positive values penalize tokens proportionally to how often they have appeared, reducing verbatim repetition.
-2 <= x <= 2A 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.
A unique identifier for your end-user. Helps with abuse detection and monitoring.
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.
Specifies the output format. Use {"type": "json_object"} for JSON mode, or {"type": "json_schema", "json_schema": {...}} for strict structured output.
A list of tools the model may call. Currently supports function type tools.
Controls how the model selects tools. auto (default): model decides. none: no tools. required: must call a tool.
Whether to return log probabilities of the output tokens.
Number of most likely tokens to return at each position (0-20). Requires logprobs to be true.
0 <= x <= 20Controls the reasoning effort for o-series and GPT-5.1+ models.
low, medium, high Options for streaming. Only valid when stream is true.
Specifies the processing tier.
auto, default, flex, priority Phản hồi
Successful chat completion response.
Unique completion identifier.
"chatcmpl-abc123"
Object type. Non-streaming responses use chat.completion.
chat.completion "chat.completion"
Unix timestamp of creation.
1774412483
The model used (may include version suffix).
"gpt-5.4-2026-03-05"
Array of completion choices.
Token accounting for this request. Billing uses these counts.
Service tier that processed the request, when the provider reports one.
"default"
Provider backend configuration fingerprint, when the provider reports one.
"fp_490a4ad033"