JavaScript 指南
JavaScript 版 OpenAI Decisions API
OpenAI 尚未为其 Decisions API 发布 SDK——没有 client.decisions.create 可抄。本页展示针对本站端点的可用 JavaScript 代码——该端点是可调用的 OpenAI Decisions API 替代方案,由 decisions-1 提供服务,遵循相同的受限决策模式。
更新于
发起调用
向 /api/v1/decisions 发一次 POST,携带 Bearer 密钥。请求体为 model、state 和 questions——1 到 6 个问题,类型为 noul、choice 或 score。把 DECISIONS_API_KEY 设为控制台里的密钥。
用 fetch——Node 18+、Deno 和浏览器里的同一个 API。每个调用都设超时(AbortSignal.timeout);挂起的决策应该快速失败,而不是卡住流水线。
JavaScript
const res = await fetch('https://decisions-api.net/api/v1/decisions', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.DECISIONS_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'decisions-1',
state: 'I was charged twice for my subscription this morning.',
questions: {
refund: {
type: 'noul',
instructions: 'Is the customer asking for money back?',
},
},
}),
})
const { answers } = await res.json()
console.log(answers.refund.noul)读取概率
answers 以你的问题 ID 为键返回。noul 答案是陈述为真的概率。choice 答案带胜出的 choice、每个选项的概率和置信度——用置信度而不只是胜者来决定是否自动执行。
JavaScript
// noul: probability the statement is true
if (answers.refund.noul >= 0.8) routeToRefunds()
// choice: winning label + per-option probabilities + confidence
const team = answers.team
console.log(team.choice, team.probabilities, team.confidence)超时与重试
429 和 502 值得短退避重试——失败的调用不计费。402 表示余额不足:去充值,不要重试。422 是校验错误;报错信息会指明字段,应该修正请求体而不是重试。
JavaScript
async function decide(body, attempts = 3) {
for (let i = 0; i < attempts; i++) {
const res = await fetch('https://decisions-api.net/api/v1/decisions', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.DECISIONS_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(body),
signal: AbortSignal.timeout(30000),
})
if (res.status === 429 || res.status === 502) {
await new Promise(r => setTimeout(r, 2 ** i * 1000))
continue
}
if (res.status === 402) throw new Error('out of credits')
if (!res.ok) throw new Error(`decision call failed: ${res.status}`)
return (await res.json()).answers
}
throw new Error('decision call failed')把 provider 藏在一个函数后面
调用方应该只看到一个“文本进、标签出”的函数——而不是 HTTP 细节。等 OpenAI 开放其 Decisions API 时,你只需替换 decide() 的内部实现,所有调用点不动。问题文本、选项和阈值都能沿用。
JavaScript
// Keep the decision behind one function. Swap the HTTP layer
// when OpenAI publishes its schema — callers never change.
export async function routeTicket(text) {
const answers = await decide({
model: 'decisions-1',
state: text,
questions: {
team: {
type: 'choice',
instructions: 'Which team should own this ticket?',
criteria: {
payments: 'Checkout or billing.',
frontend: 'Rendering or browser behavior.',
account: 'Login or permissions.',
},
},
},
})
const team = answers.team
return team.confidence >= 0.7 ? team.choice : 'triage'
}常见问题
OpenAI 官方有 Decisions API 的 SDK 示例吗?
没有。OpenAI 尚未发布其 Decisions API 的 SDK 方法或请求模式——任何展示 client.decisions.create 的代码都是虚构的。本页使用纯 HTTP,这也正是任何 provider SDK 内部的封装。
能用 axios 或其他 HTTP 客户端代替 fetch 吗?
可以——端点就是普通的 HTTPS POST。axios、undici 或 got 都一样;保持相同的超时和状态处理。
如何发送结构化上下文?
state 不只接受字符串,也接受 JSON 对象或数组——在 json 请求体里直接传 dict,模型会把它作为上下文读取。
在浏览器里跑一次决策
跳过配置——在试用台用新访客的 2 个免费积分发起真实调用。