チャット補完を作成する
CometAPI POST /v1/chat/completions を使用して、ストリーミング、構造化出力、ツール呼び出しに対応した複数メッセージの会話をチャットモデルに送信します。
base_url="https://api.cometapi.com/v1" と CometAPI API キーで設定します。
基本、画像、ストリーミング、構造化出力の例では gpt-6-astra を使用します。関数呼び出しの例では gpt-5.6-sol を、対数確率の例では gpt-4.1 を使用します。
に従ってください。
マルチモーダル入力を送信する
コンテンツパートの配列を使用して、テキストとともに画像を送信します。image_url.detail を使用します。この例では high を使用します。
レスポンスをストリーミングする
増分出力を受け取るには、stream を true に設定します。レスポンスは次の形式で配信されます Server-Sent Events(SSE)。テキストは chat.completion.chunk オブジェクトで届きます。この短縮された例には、最終的な使用量チャンクが含まれています:
構造化出力をリクエストする
構造化出力をリクエストするには、response_format を使用します:
ツールと関数を呼び出す
関数定義とともにgpt-5.6-sol を使用します:
finish_reason: "tool_calls" と message.tool_calls 配列が返されます。関数の JSON エンコード済みの arguments を解析し、関数を実行してから、assistant メッセージと、一致する tool を持つ tool_call_id 結果メッセージを追加します。
リクエストパラメータを選択する
モデル固有のパラメータ
モデル固有のパラメータ
reasoning_effort と max_completion_tokens を使用します。Functions と Logprobs の例では、それぞれ GPT-6、gpt-5.6-sol、gpt-4.1 のリクエストオプションを示しています。Claude または Gemini 固有のリクエスト形式については、次を参照してください Anthropic メッセージ および Gemini コンテンツ生成.max_tokens と max_completion_tokens
max_tokens と max_completion_tokens
max_completion_tokens を使用して、GPT-6 Astra の例で生成されるトークン(Token)を制限します。これには推論と表示される出力が含まれるため、両方のための余裕を確保してください。max_tokens はレガシーパラメータです。指示ロール
指示ロール
developer を GPT-6 Astra の例におけるアプリケーション指示に使用します。エンドユーザーのコンテンツは user メッセージに保持し、会話を継続する場合は以前の assistant の応答を維持してください。よくある質問
レート制限にはどのように対処しますか?
429 Too Many Requests が発生した場合は、指数バックオフを実装します:
会話コンテキストを維持するには?
会話履歴全体をmessages 配列に含めます:
finish_reason とは何を意味しますか?
コストを管理するには?
max_completion_tokensを使用して出力長を制限します。- モデルの価格を比較し、ワークロード要件を満たすモデルを選択します。
- プロンプト(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-6-astra"
Conversation messages, including instructions, user input, assistant replies, and tool results.
If true, partial response tokens are delivered incrementally via server-sent events (SSE). The stream ends with a data: [DONE] message.
Sampling temperature. Omit this field for GPT-6 Astra.
0 <= x <= 2Nucleus sampling threshold. Omit this field for GPT-6 Astra. For sampling overrides, adjust either top_p or temperature.
0 <= x <= 1Number of completion choices to generate for each input message. Defaults to 1.
Stop string or list of up to four strings, for models that support stop sequences.
Legacy output-token limit. Use max_completion_tokens for the GPT-6 Astra examples.
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.
Maximum generated tokens, including visible output and reasoning. Leave enough room for both, as in the image-input example.
Specifies the output format. Use {"type": "json_object"} for JSON mode, or {"type": "json_schema", "json_schema": {...}} for strict structured output.
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.
Controls how the model selects tools. auto (default): model decides. none: no tools. required: must call a tool.
Return token log probabilities. Use gpt-4.1 as shown in the Logprobs example.
Number of most likely tokens to return at each position (0-20). Requires logprobs to be true.
0 <= x <= 20Reasoning 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.
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_example"
Object type. Non-streaming responses use chat.completion.
chat.completion "chat.completion"
Unix timestamp of creation.
1788763703
The model used (may include version suffix).
"gpt-6-astra"
Array of completion choices.
Token accounting for this request. Billing uses these counts.
Service tier that processed the request, when returned.
Model configuration fingerprint, when returned.