根据你的请求格式匹配实现该格式的页面来使用 CometAPI 文本模型文档。对于兼容 OpenAI 的聊天,先从聊天补全或响应开始;对于提供商原生格式,请使用对应的提供商页面。
选择一个文本与聊天 API
创建聊天补全
使用 messages 数组发送兼容 OpenAI 的聊天消息。
创建模型响应
通过 Responses API 使用推理、多模态输出和内置工具。
创建 Message
使用提供商原生字段调用兼容 Claude 的 Messages 工作流。
调用文本模型
使用来自模型页面或模型目录中的任意支持文本的 model ID。下面的示例调用的是兼容 OpenAI 的聊天补全端点。
这些示例使用占位符 your-model-id。在运行请求之前,请将其替换为模型页面或模型目录中可用的文本 model ID。
import os
import requests
response = requests.post(
"https://api.cometapi.com/v1/chat/completions",
headers={
"Authorization": "Bearer " + os.environ["COMETAPI_KEY"],
"Content-Type": "application/json",
},
json={
"model": "your-model-id",
"messages": [
{
"role": "user",
"content": "Write one sentence about CometAPI.",
}
],
},
timeout=30,
)
response.raise_for_status()
result = response.json()
print(result["choices"][0]["message"]["content"])
const response = await fetch("https://api.cometapi.com/v1/chat/completions", {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.COMETAPI_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "your-model-id",
messages: [
{
role: "user",
content: "Write one sentence about CometAPI.",
},
],
}),
});
if (!response.ok) {
throw new Error(await response.text());
}
const result = await response.json();
console.log(result.choices[0].message.content);
curl https://api.cometapi.com/v1/chat/completions \
-H "Authorization: Bearer $COMETAPI_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "your-model-id",
"messages": [
{
"role": "user",
"content": "Write one sentence about CometAPI."
}
]
}'
响应示例
成功的响应可能如下所示。字段值会因模型和请求而异:
{
"id": "chatcmpl_example",
"object": "chat.completion",
"created": 1779960520,
"model": "your-model-id",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "CometAPI lets developers route model requests through one API surface."
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 12,
"completion_tokens": 14,
"total_tokens": 26
}
}
示例模型记录
此示例模型目录响应展示了 /api/models 的响应封装结构,以及一个文本模型记录的结构。它不是完整的模型列表。
curl https://api.cometapi.com/api/models
{
"success": true,
"page": 1,
"page_size": 20,
"total": 302,
"data": [
{
"created": 1773798949,
"id": "your-text-model-id",
"code": "your-text-model-id",
"provider": "ExampleProvider",
"provider_code": "example",
"name": "Example text model",
"model_type": "text",
"features": [
"text-to-text"
],
"endpoints": "{\n \"openai-chat\": {\n \"path\": \"/v1/chat/completions\",\n \"method\": \"POST\"\n }\n}",
"pricing": {
"currency": "USD / M Tokens",
"input": 0.5,
"output": 1.5,
"per_request": null,
"per_second": null
}
}
]
}
常见错误
发送 Authorization: Bearer $COMETAPI_KEY。
对于 OpenAI 兼容请求,请使用 https://api.cometapi.com/v1。
先移除可选字段,然后逐个将这些字段加回去。
错误代码与重试策略
在修正请求体之前,不要重试。
在 API key 存在且有效之前,不要重试。
重试前请检查基础 URL、路径和 model ID。
使用指数退避进行重试,并降低并发度。
对暂时性的提供商或服务错误,使用退避策略重试。
定价和模型目录
模型页面
了解 CometAPI 如何在文档中公开 model ID。