创建聊天补全
使用 CometAPI POST /v1/chat/completions 向聊天模型发送多消息对话,并通过流式输出、temperature 和 max_tokens 控制参数进行调节。
model 参数即可切换模型;只需将 base_url 设置为 https://api.cometapi.com/v1,大多数 OpenAI 兼容 SDK 都可使用。
消息角色
发送多模态输入
许多模型除文本外还支持图像和音频。要发送多模态消息,请对content 使用数组格式:
detail 参数控制图像分析深度:
low— 更快,使用更少的 Token(固定成本)high— 详细分析,消耗更多 Tokenauto— 由模型决定(默认)
流式传输响应
要接收增量输出,请将stream 设为 true。响应将以 服务器发送事件(SSE),其中每个事件包含一个 chat.completion.chunk 对象:
请求结构化输出
要强制模型返回与特定 schema 匹配的有效 JSON,请使用response_format:
json_schema)保证输出与您的 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"