建立聊天補全
使用 CometAPI POST /v1/chat/completions,透過串流、temperature 和 max_tokens 控制項,將多訊息對話傳送至聊天模型。
model 參數即可切換模型;大多數 OpenAI 相容 SDK 只要將 base_url 設為 https://api.cometapi.com/v1 即可運作。
訊息角色
傳送多模態輸入
許多模型除了文字外也支援圖片和音訊。若要傳送多模態訊息,請對content 使用陣列格式:
detail 參數可控制圖片分析深度:
low— 速度更快,使用較少 Token(固定成本)high— 詳細分析,消耗較多 Tokenauto— 由模型決定(預設)
串流回應
若要接收增量輸出,請將stream 設為 true。回應會以 **Server-Sent Events(SSE)**傳遞,其中每個事件都包含一個 chat.completion.chunk 物件:
請求結構化輸出
若要強制模型傳回符合特定結構描述的有效 JSON,請使用response_format:
json_schema)可保證輸出完全符合您的結構描述。JSON Object 模式(json_object)僅保證有效 JSON,不會強制結構。呼叫工具和函式
若要讓模型能夠呼叫外部函式,請提供工具定義:finish_reason: "tool_calls",而 message.tool_calls 陣列將包含函式名稱和引數。接著執行該函式,並將結果以帶有相符 tool_call_id 的 tool 訊息傳回。
跨供應商注意事項
各供應商的參數支援情況
各供應商的參數支援情況
max_tokens 與 max_completion_tokens
max_tokens 與 max_completion_tokens
max_tokens— 舊版參數。可搭配大多數模型使用,但已不建議用於較新的 OpenAI 模型。max_completion_tokens— GPT-4.1、GPT-5 系列和 o-series 模型建議使用的參數。推理模型必須使用,因為它同時包含輸出 Token 和推理 Token。
system 與 developer 角色
system 與 developer 角色
system— 傳統的指示角色。適用於所有模型。developer— 隨 o1 模型推出。為較新模型提供更強的指示遵循能力。在較舊模型上會回退為system行為。
developer。常見問題
如何處理速率限制?
遇到429 Too Many Requests 時,請實作指數退避:
如何維持對話上下文?
在messages 陣列中包含完整對話歷程:
finish_reason 代表什麼?
如何控制成本?
- 使用
max_completion_tokens限制輸出長度。 - 使用
gpt-5.6-terra平衡智慧與成本,或使用gpt-5.6-luna處理高效率的大量工作負載。 - 保持 Prompt 簡潔,避免重複的上下文。
- 在
usage回應欄位中監控 Token 使用量。
授權
Bearer token authentication. Use your CometAPI key.
主體
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 回應
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"