Chat Completions
Gunakan CometAPI POST /v1/chat/completions untuk mengirim percakapan multi-pesan dan mendapatkan balasan LLM dengan kontrol streaming, temperature, dan max_tokens.
CometAPI merutekan Chat Completions ke beberapa provider — termasuk OpenAI, Claude, dan Gemini — melalui satu antarmuka yang kompatibel dengan OpenAI. Beralih antar model dengan mengubah parameterDocumentation 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; sebagian besar SDK yang kompatibel dengan OpenAI dapat digunakan dengan menetapkan base_url ke https://api.cometapi.com/v1.
Role pesan
| Role | Deskripsi |
|---|---|
system | Menetapkan perilaku dan kepribadian assistant. Ditempatkan di awal percakapan. |
developer | Menggantikan system untuk model yang lebih baru (o1+). Memberikan instruksi yang harus diikuti model terlepas dari input pengguna. |
user | Pesan dari pengguna akhir. |
assistant | Respons model sebelumnya, digunakan untuk mempertahankan riwayat percakapan. |
tool | Hasil dari pemanggilan tool/function. Harus menyertakan tool_call_id yang cocok dengan pemanggilan tool asli. |
Kirim input multimodal
Banyak model mendukung gambar dan audio bersama teks. Untuk mengirim pesan multimodal, gunakan format array untukcontent:
detail mengontrol kedalaman analisis gambar:
low— lebih cepat, menggunakan lebih sedikit token (biaya tetap)high— analisis mendetail, lebih banyak token yang dikonsumsiauto— model yang memutuskan (default)
Stream respons
Untuk menerima output secara bertahap, setelstream ke true. Respons dikirim sebagai Server-Sent Events (SSE), di mana setiap event berisi objek chat.completion.chunk:
Minta output terstruktur
Untuk memaksa model mengembalikan JSON valid yang sesuai dengan schema tertentu, gunakanresponse_format:
json_schema) menjamin output cocok persis dengan schema Anda. Mode JSON Object (json_object) hanya menjamin JSON yang valid — strukturnya tidak dipaksakan.Panggil tools dan fungsi
Untuk mengaktifkan model memanggil fungsi eksternal, berikan definisi tool:finish_reason: "tool_calls" dan array message.tool_calls akan berisi nama fungsi serta argumennya. Anda kemudian menjalankan fungsi tersebut dan mengirimkan hasilnya kembali sebagai pesan tool dengan tool_call_id yang sesuai.
Catatan lintas provider
Dukungan parameter di berbagai provider
Dukungan parameter di berbagai provider
| Parameter | OpenAI GPT | Claude (via compat) | Gemini (via compat) |
|---|---|---|---|
temperature | 0–2 | 0–1 | 0–2 |
top_p | 0–1 | 0–1 | 0–1 |
n | 1–128 | hanya 1 | 1–8 |
stop | Hingga 4 | Hingga 4 | Hingga 5 |
tools | ✅ | ✅ | ✅ |
response_format | ✅ | ✅ (json_schema) | ✅ |
logprobs | ✅ | ❌ | ❌ |
reasoning_effort | o-series, GPT-5.1+ | ❌ | ❌ (gunakan thinking untuk Gemini native) |
max_tokens vs max_completion_tokens
max_tokens vs max_completion_tokens
max_tokens— Parameter lama. Berfungsi dengan sebagian besar model tetapi sudah deprecated untuk model OpenAI yang lebih baru.max_completion_tokens— Parameter yang direkomendasikan untuk model GPT-4.1, seri GPT-5, dan o-series. Diperlukan untuk reasoning models karena mencakup output tokens dan reasoning tokens.
system vs developer role
system vs developer role
system— Role instruksi tradisional. Berfungsi dengan semua model.developer— Diperkenalkan dengan model o1. Menyediakan kepatuhan instruksi yang lebih kuat untuk model yang lebih baru. Akan fallback ke perilakusystempada model yang lebih lama.
developer untuk project baru yang menargetkan model GPT-4.1+ atau o-series.FAQ
Bagaimana menangani rate limit?
Saat menemui429 Too Many Requests, terapkan exponential backoff:
Bagaimana mempertahankan konteks percakapan?
Sertakan riwayat percakapan lengkap dalam arraymessages:
Apa arti finish_reason?
| Value | Arti |
|---|---|
stop | Penyelesaian alami atau mencapai stop sequence. |
length | Mencapai batas max_tokens atau max_completion_tokens. |
tool_calls | Model memanggil satu atau lebih tool/function call. |
content_filter | Output difilter karena kebijakan konten. |
Bagaimana mengontrol biaya?
- Gunakan
max_completion_tokensuntuk membatasi panjang output. - Pilih model yang hemat biaya (misalnya,
gpt-5.4-miniataugpt-5.4-nanountuk tugas yang lebih sederhana). - Buat prompt tetap ringkas — hindari konteks yang berulang.
- Pantau penggunaan token di field respons
usage.
Otorisasi
Bearer token authentication. Use your CometAPI key.
Body
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 Respons
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"