Chat Completions
Sử dụng CometAPI POST /v1/chat/completions để gửi các cuộc hội thoại nhiều tin nhắn và nhận phản hồi từ LLM với các tùy chọn Streaming, temperature và max_tokens.
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 model bằng cách thay đổi tham sốDocumentation Index
Fetch the complete documentation index at: https://apidoc.cometapi.com/llms.txt
Use this file to discover all available pages before exploring further.
model; hầu hết các SDK tương thích OpenAI đều hoạt động bằng cách đặt base_url thành https://api.cometapi.com/v1.
Vai trò của message
| Role | Description |
|---|---|
system | Thiết lập hành vi và tính cách của assistant. Được đặt ở đầu cuộc hội thoại. |
developer | Thay thế system cho các model mới hơn (o1+). Cung cấp các chỉ dẫn mà model phải tuân theo bất kể đầu vào của người dùng. |
user | Các tin nhắn từ người dùng cuối. |
assistant | Các phản hồi trước đó của model, được dùng để duy trì lịch sử hội thoại. |
tool | Kết quả từ các lệnh gọi tool/function. Phải bao gồm tool_call_id khớp với lệnh gọi tool ban đầu. |
Gửi đầu vào multimodal
Nhiều model hỗ trợ hình ảnh và âm thanh cùng với văn bản. Để gửi các message multimodal, hãy 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, dùng ít token hơn (chi phí cố định)high— phân tích chi tiết, tiêu tốn nhiều token hơnauto— model tự quyết định (mặc định)
Stream phản hồi
Để nhận đầu ra tăng dần, hãy đặtstream thành true. Phản hồi được gửi dưới dạng Server-Sent Events (SSE), trong đó mỗi event chứa một đối tượng chat.completion.chunk:
Yêu cầu đầu ra có cấu trúc
Để buộc model 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 đặt.Gọi tools và functions
Để cho phép model gọi các function 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 một message tool với tool_call_id tương ứng.
Ghi chú 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
| Tham số | OpenAI GPT | Claude (qua compat) | Gemini (qua compat) |
|---|---|---|---|
temperature | 0–2 | 0–1 | 0–2 |
top_p | 0–1 | 0–1 | 0–1 |
n | 1–128 | chỉ 1 | 1–8 |
stop | Tối đa 4 | Tối đa 4 | Tối đa 5 |
tools | ✅ | ✅ | ✅ |
response_format | ✅ | ✅ (json_schema) | ✅ |
logprobs | ✅ | ❌ | ❌ |
reasoning_effort | o-series, GPT-5.1+ | ❌ | ❌ (dùng thinking cho Gemini native) |
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 model nhưng đã bị deprecated đối với các model OpenAI mới hơn.max_completion_tokens— Tham số được khuyến nghị cho GPT-4.1, dòng GPT-5 và các model o-series. Bắt buộc đối với các model reasoning vì nó bao gồm cả output tokens và reasoning tokens.
role system so với developer
role system so với developer
system— Role chỉ dẫn truyền thống. Hoạt động với tất cả model.developer— Được giới thiệu cùng các model o1. Cung cấp khả năng tuân theo chỉ dẫn mạnh hơn cho các model mới hơn. Tự động quay về hành visystemtrên các model cũ hơn.
developer cho các dự án mới nhắm tới GPT-4.1+ hoặc các model o-series.Câu hỏi thường gặp
Cách xử lý giới hạn tốc độ?
Khi gặp lỗi429 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ĩa là gì?
| Giá trị | Ý nghĩa |
|---|---|
stop | Hoàn tất tự nhiên hoặc chạm đến chuỗi dừng. |
length | Đã đạt giới hạn max_tokens hoặc max_completion_tokens. |
tool_calls | Model đã gọi một hoặc nhiều công cụ/hàm. |
content_filter | Đầu ra đã bị lọc do chính sách nội dung. |
Cách kiểm soát chi phí?
- Sử dụng
max_completion_tokensđể giới hạn độ dài đầu ra. - Chọn các model tiết kiệm chi phí (ví dụ:
gpt-5.4-minihoặcgpt-5.4-nanocho các tác vụ đơn giản hơ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"
chat.completion "chat.completion"
Unix timestamp of creation.
1774412483
The model used (may include version suffix).
"gpt-5.4-2025-07-16"
Array of completion choices.
"default"
"fp_490a4ad033"