OpenAI 兼容接口
回端AI 提供 OpenAI 兼容接口。只要你的 SDK 支持自定义 baseURL,通常只需要替换 Base URL 和 API Key。
Base URL
Section titled “Base URL”https://api.ai.retterm.cn/v1请不要把控制台地址、文档站地址或支付页面地址写成 API Base URL。
Node.js
Section titled “Node.js”import OpenAI from "openai";
const client = new OpenAI({ apiKey: process.env.RETTERMAI_API_KEY, baseURL: "https://api.ai.retterm.cn/v1"});
const completion = await client.chat.completions.create({ model: process.env.RETTERMAI_MODEL, messages: [{ role: "user", content: "Give me one concise onboarding tip." }]});
console.log(completion.choices[0]?.message?.content);建议在服务端读取 RETTERMAI_API_KEY 和 RETTERMAI_MODEL,不要把它们写死在代码里。
Python
Section titled “Python”import osfrom openai import OpenAI
client = OpenAI( api_key=os.environ["RETTERMAI_API_KEY"], base_url="https://api.ai.retterm.cn/v1",)
completion = client.chat.completions.create( model=os.environ["RETTERMAI_MODEL"], messages=[{"role": "user", "content": "Give me one concise onboarding tip."}],)
print(completion.choices[0].message.content)curl https://api.ai.retterm.cn/v1/chat/completions \ -H "Authorization: Bearer $RETTERMAI_API_KEY" \ -H "Content-Type: application/json" \ -d '{ "model": "'"$RETTERMAI_MODEL"'", "messages": [{ "role": "user", "content": "ping" }] }'- 在服务端读取
RETTERMAI_API_KEY,不要把 Key 暴露到浏览器。 - 使用控制台「模型状态」里显示的模型名,不要猜测模型名。
- 请求超时时可以短暂重试,但不要无限重试。
- 对 429、502、503、504 使用指数退避重试。
- 对 401、402、403 先检查 Key、余额和账号权限,不要反复重试。
聊天接口的成功响应会包含 choices。如果你使用的是 OpenAI SDK,读取方式通常和 OpenAI 官方接口一致:
completion.choices[0]?.message?.content如果响应不是 200,请根据 常见错误 中的状态码说明处理。