认证并配置基础 URL
您的 API 密钥可立即访问聊天补全端点。通过仪表盘使用 Google 或电子邮件生成密钥,并将其粘贴到您的环境变量中。基础 URL 为 https://api.cheapaiapikeys.com/v1 可与任何标准 OpenAI SDK 客户端配合使用。
- 设置
OPENAI_API_KEY到您的生成字符串。 - 覆盖
base_url指向我们的托管服务器。 - 通过调用验证连接性
GET /v1/models列出可用模型。
curl https://api.cheapaiapikeys.com/v1/chat/completions \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "uncensored",
"messages": [{"role": "user", "content": "List three common regex patterns for validating email addresses"}]
}'
发送您的第一次聊天补全请求
使用标准聊天补全结构发送文本输入并接收生成的文本。我们的单一 uncensored 模型处理请求,不会拒绝合法的成人或争议性话题。您可以调整参数,例如 temperature 和 top_p 用于控制随机性。
from openai import OpenAI
client = OpenAI(base_url="https://api.cheapaiapikeys.com/v1", api_key="YOUR_KEY")
resp = client.chat.completions.create(
model="uncensored",
messages=[{"role": "user", "content": "Summarise this thread without softening it."}],
)
print(resp.choices[0].message.content)
启用流式传输并处理 SSE 事件
通过设置启用实时 token 交付 stream: true 在你的请求中。服务器返回服务器发送事件 (SSE),你必须增量解析。Token 使用详情出现在流的最后一个数据块中。这种方法降低了长响应的感知延迟。
import OpenAI from "openai";
const client = new OpenAI({ baseURL: "https://api.cheapaiapikeys.com/v1", apiKey: process.env.API_KEY });
const resp = await client.chat.completions.create({
model: "uncensored",
messages: [{ role: "user", content: "Draft a villain monologue for my game." }],
});
console.log(resp.choices[0].message.content);
使用函数调用和 JSON 模式
在请求负载中定义工具以启用结构化输出或外部函数执行。使用 response_format: {type: "json_object"} 以强制严格的 JSON 合规性。模型支持 tool_choice 用于精确控制它调用哪些函数。这确保了应用程序逻辑的可靠解析。
理解 Token 限制和定价
定价透明,输入令牌每百万令牌收费 $0.15,输出令牌每百万令牌收费 $0.50。错误和拒绝不消耗余额。上下文窗口总共支持 100,000 个令牌。超过限制的请求将返回 429 速率限制错误。确保您的负载保持在 8 MB 以下以避免主体错误。
处理错误和边界情况
常见错误包括 401(无效密钥)和 402(余额不足)。429 状态表示您已超过每分钟 300 次请求的限制。在速率限制后重试时使用指数退避。检查 error object in the JSON response for specific details. Refunds are not issued, so monitor your usage closely.
流式传输
stream = client.chat.completions.create(
model="uncensored",
messages=[{"role": "user", "content": "Tell the story in second person."}],
stream=True,
)
for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)