Langsung ke konten utama
POST
Kirim percakapan melalui API Chat Completions yang kompatibel dengan OpenAI. Konfigurasikan SDK Anda dengan base_url="https://api.cometapi.com/v1" dan API key CometAPI Anda. Contoh dasar, gambar, Streaming, dan output terstruktur menggunakan gpt-6-astra. Contoh Function Calling menggunakan gpt-6.1-sol, dan contoh log-probability menggunakan gpt-4.1.
Gunakan Referensi OpenAI Chat Completions untuk definisi parameter. Untuk tool calling GPT-6 Astra, gunakan Responses, dengan mengikuti panduan model OpenAI.

Peran pesan

Tempatkan instruksi aplikasi dalam pesan developer sebelum pesan pengguna saat menggunakan contoh GPT-6 Astra.

Kirim input Multimodal

Gunakan array bagian konten untuk mengirim gambar beserta teks:
Gunakan image_url.detail untuk memilih detail pemrosesan gambar. Contoh ini menggunakan high.

Stream respons

Untuk menerima output secara bertahap, tetapkan stream ke true. Respons dikirimkan sebagai Server-Sent Events (SSE). Teks tiba dalam objek chat.completion.chunk; contoh singkat ini mencakup chunk penggunaan akhir:
Untuk menyertakan statistik penggunaan token dalam respons streaming, tetapkan stream_options.include_usage ke true. Data penggunaan muncul dalam chunk akhir sebelum [DONE]. Chunk dapat memiliki array choices yang kosong; periksa sebelum mengakses choices[0].

Minta output terstruktur

Gunakan response_format untuk meminta output terstruktur:
Mode JSON Schema menentukan struktur yang diperlukan. Mode JSON Object meminta JSON yang valid tanpa menerapkan schema Anda. Periksa penolakan atau respons yang dibatasi panjangnya sebelum menggunakan hasilnya.

Memanggil tools dan fungsi

Gunakan gpt-6.1-sol dengan definisi fungsi:
Panggilan fungsi mengembalikan finish_reason: "tool_calls" dan array message.tool_calls. Uraikan arguments milik fungsi yang dikodekan dalam JSON, jalankan fungsi Anda, lalu tambahkan pesan assistant dan pesan hasil tool dengan tool_call_id yang sesuai.

Pilih parameter request

Gunakan reasoning_effort dan max_completion_tokens dengan GPT-6 Astra. Contoh Functions dan Logprobs menampilkan opsi request untuk gpt-6.1-sol dan gpt-4.1, secara berurutan.Untuk format request khusus Claude atau Gemini, lihat Anthropic Messages dan Gemini Generate content.
Gunakan max_completion_tokens untuk membatasi Token yang dihasilkan pada contoh GPT-6 Astra. Ini mencakup penalaran dan output yang terlihat, jadi sediakan ruang untuk keduanya. max_tokens adalah parameter lawas.
Gunakan developer untuk instruksi aplikasi dalam contoh GPT-6 Astra. Simpan konten pengguna akhir dalam pesan user dan pertahankan balasan assistant sebelumnya saat melanjutkan percakapan.

FAQ

Bagaimana menangani rate limit?

Saat menemui 429 Too Many Requests, terapkan exponential backoff:

Bagaimana mempertahankan konteks percakapan?

Sertakan seluruh riwayat percakapan dalam array messages:

Apa yang dimaksud dengan finish_reason ?

Bagaimana mengendalikan biaya?

  1. Gunakan max_completion_tokens untuk membatasi panjang output.
  2. Bandingkan harga model dan pilih model yang memenuhi kebutuhan workload Anda.
  3. Jaga Prompt tetap ringkas — hindari konteks yang berulang.
  4. Pantau penggunaan token dalam field respons usage.

Otorisasi

Authorization
string
header
wajib

Bearer token authentication. Use your CometAPI key.

Body

application/json
model
string
default:gpt-6-astra
wajib

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

Contoh:

"gpt-6-astra"

messages
object[]
wajib

Conversation messages, including instructions, user input, assistant replies, and tool results.

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

Sampling temperature. Omit this field for GPT-6 Astra.

Rentang yang diperlukan: 0 <= x <= 2
top_p
number

Nucleus sampling threshold. Omit this field for GPT-6 Astra. For sampling overrides, adjust either top_p or temperature.

Rentang yang diperlukan: 0 <= x <= 1
n
integer

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

stop

Stop string or list of up to four strings, for models that support stop sequences.

max_tokens
integer
usang

Legacy output-token limit. Use max_completion_tokens for the GPT-6 Astra examples.

presence_penalty
number

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.

Rentang yang diperlukan: -2 <= x <= 2
frequency_penalty
number

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

Rentang yang diperlukan: -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

Maximum generated tokens, including visible output and reasoning. Leave enough room for both, as in the image-input example.

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[]

Function definitions for a model that supports tool calling on Chat Completions. The function example uses GPT-5.6 Sol; use Responses for GPT-6 Astra tool calls.

tool_choice

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

logprobs
boolean

Return token log probabilities. Use gpt-4.1 as shown in the Logprobs example.

top_logprobs
integer

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

Rentang yang diperlukan: 0 <= x <= 20
reasoning_effort
string

Reasoning effort supported by the selected model. The GPT-6 Astra examples use low; the GPT-5.6 Sol function example uses none. See the model reference for other supported levels.

stream_options
object

Options for streaming. Only valid when stream is true.

service_tier
enum<string>

Specifies the processing tier.

Opsi yang tersedia:
auto,
default,
flex,
priority

Respons

Successful chat completion response.

id
string

Unique completion identifier.

Contoh:

"chatcmpl_example"

object
enum<string>

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

Opsi yang tersedia:
chat.completion
Contoh:

"chat.completion"

created
integer

Unix timestamp of creation.

Contoh:

1788763703

model
string

The model used (may include version suffix).

Contoh:

"gpt-6-astra"

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 returned.

system_fingerprint
string | null

Model configuration fingerprint, when returned.

Terakhir diubah pada 8 Oktober 2026