跳轉到主要內容
POST
透過與 OpenAI 相容的聊天補全 API 傳送對話。使用 base_url="https://api.cometapi.com/v1" 和您的 CometAPI API 金鑰設定 SDK。 基本、影像、串流(Streaming)及結構化輸出範例使用 gpt-6-astra。函式呼叫(Function Calling)範例使用 gpt-6.1-sol,而對數機率範例使用 gpt-4.1。
請參閱 OpenAI 聊天補全參考文件 以瞭解參數定義。若要進行 GPT-6 Astra 工具呼叫,請使用 回應,並遵循 OpenAI 模型指南.

訊息角色

使用 GPT-6 Astra 範例時,請將包含應用程式指示的 developer 訊息放在使用者訊息之前。

傳送多模態(Multimodal)輸入

使用內容部分陣列,將影像與文字一併傳送:
使用 image_url.detail 選取影像處理細節。此範例使用 high。

串流回應

若要接收增量輸出,請將 stream 設為 true。回應會以 **伺服器傳送事件(SSE)**傳送。文字會以 chat.completion.chunk 物件送達;此簡略範例包含最終的用量區塊:
若要在串流(Streaming)回應中納入 Token 用量統計資料,請將 stream_options.include_usage 設為 true。用量資料會在 [DONE] 之前的最終區塊中顯示。區塊的 choices 陣列可能為空;存取 choices[0] 前請先檢查。

要求結構化輸出

使用 response_format 來要求結構化輸出:
JSON Schema 模式會指定所需的結構。JSON Object 模式會要求有效的 JSON,但不會強制套用您的結構描述。使用結果前,請先檢查是否遭拒絕,或回應是否受長度限制。

呼叫工具和函式

搭配函式定義使用 gpt-6.1-sol:
函式呼叫會傳回 finish_reason: "tool_calls" 和 message.tool_calls 陣列。剖析函式 JSON 編碼的 arguments,執行函式,然後附加 assistant 訊息及具有相符 tool_call_id 的 tool 結果訊息。

選擇請求參數

將 reasoning_effort 和 max_completion_tokens 與 GPT-6 Astra 搭配使用。函式和 Logprobs 範例分別顯示 gpt-6.1-sol 和 gpt-4.1 的請求選項。如需 Claude 或 Gemini 專屬的請求格式,請參閱 Anthropic 訊息 和 Gemini 產生內容.
使用 max_completion_tokens 限制 GPT-6 Astra 範例生成的 Token。這包括推理和可見輸出,因此請為兩者保留空間。max_tokens 是舊版參數。
在 GPT-6 Astra 範例中,使用 developer 提供應用程式指令。將終端使用者內容保留在 user 訊息中,並在延續對話時保留先前的 assistant 回覆。

常見問答

如何處理速率限制?

遇到 429 Too Many Requests 時,請實作指數退避:

如何維持對話上下文?

在 messages 陣列中包含完整的對話記錄:

finish_reason 代表什麼意思?

如何控制成本?

  1. 使用 max_completion_tokens 限制輸出長度。
  2. 比較模型定價,並選擇符合工作負載需求的模型。
  3. 保持 Prompt 簡潔,避免重複的上下文。
  4. 在 usage 回應欄位中監控 Token 使用量。

授權

Authorization
string
header
必填

Bearer token authentication. Use your CometAPI key.

主體

application/json
model
string
預設值:gpt-6-astra
必填

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

範例:

"gpt-6-astra"

messages
object[]
必填

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.

必填範圍: 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.

必填範圍: 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
已棄用

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.

必填範圍: -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.

必填範圍: -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.

必填範圍: 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.

可用選項:
auto,
default,
flex,
priority

回應

Successful chat completion response.

id
string

Unique completion identifier.

範例:

"chatcmpl_example"

object
enum<string>

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

可用選項:
chat.completion
範例:

"chat.completion"

created
integer

Unix timestamp of creation.

範例:

1788763703

model
string

The model used (may include version suffix).

範例:

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

最後修改於 2026年10月8日