创建聊天补全
使用 CometAPI POST /v1/chat/completions 向聊天模型发送多消息对话,支持流式输出(Streaming)、结构化输出和工具调用。
base_url="https://api.cometapi.com/v1" 和您的 CometAPI API 密钥配置 SDK。
基础、图像、流式输出(Streaming)和结构化输出示例使用 gpt-6-astra。函数调用(Function Calling)示例使用 gpt-5.6-sol,对数概率示例使用 gpt-4.1。
消息角色
发送多模态(Multimodal)输入
使用内容部分数组,将图像与文本一同发送:image_url.detail 选择图像处理细节。此示例使用 high。
流式传输响应
要接收增量输出,请将stream 设置为 true。响应通过以下方式传送 服务器发送事件(SSE)。文本以 chat.completion.chunk 对象的形式到达;这个简略示例包含最终的用量数据块:
请求结构化输出
使用response_format 请求结构化输出:
调用工具和函数
使用gpt-5.6-sol 并提供函数定义:
finish_reason: "tool_calls" 和一个 message.tool_calls 数组。解析函数的 JSON 编码 arguments,执行函数,然后追加助手消息以及包含匹配的 tool 的 tool_call_id 结果消息。
选择请求参数
特定模型参数
特定模型参数
reasoning_effort 和 max_completion_tokens 与 GPT-6 Astra 搭配使用。函数和 Logprobs 示例分别展示了 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 Astra 示例中,使用 GPT-6 作为应用程序指令。将最终用户内容保留在 user 消息中,并在继续对话时保留先前的助手回复。常见问题
如何处理速率限制?
遇到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.