OpenAI 和 Anthropic 调用
rb.openai() 和 rb.anthropic() 返回的是官方 SDK 实例,捕获错误的方式与官方 SDK 一致:
import OpenAI from 'openai';
try {
const res = await rb.openai().chat.completions.create({ ... });
} catch (err) {
if (err instanceof OpenAI.APIError) {
console.error('API 错误:', err.status, err.message);
}
}
import Anthropic from '@anthropic-ai/sdk';
try {
const res = await rb.anthropic().messages.create({ ... });
} catch (err) {
if (err instanceof Anthropic.APIError) {
console.error('API 错误:', err.status, err.message);
}
}
图像生成
ImageResponse.json() 会自动检查 HTTP 状态码,失败时抛出错误详情:
try {
const result = await rb.image({
model: 'dall-e-3',
prompt: '', // 空 prompt 会触发错误
}).json();
} catch (err) {
console.error('生成失败:', (err as Error).message);
// 例如: "prompt is required and must not be empty"
}
如需先检查状态码:
const img = rb.image({ model: 'dall-e-3', prompt: '...' });
const status = await img.httpStatus();
if (status !== 200) {
console.warn('异常状态码:', status);
}
const result = await img.json(); // 非 2xx 仍会抛错
Rerank 错误
非 2xx 响应自动抛错,错误信息来自上游返回的 error.message:
try {
const result = await rb.rerank({ ... });
} catch (err) {
console.error('重排失败:', (err as Error).message);
}