Python 指南
Python 版 OpenAI Decisions API
OpenAI 尚未为其 Decisions API 发布 SDK——没有 client.decisions.create 可抄。本页展示针对本站端点的可用 Python 代码——该端点是可调用的 OpenAI Decisions API 替代方案,由 decisions-1 提供服务,遵循相同的受限决策模式。
更新于
发起调用
向 /api/v1/decisions 发一次 POST,携带 Bearer 密钥。请求体为 model、state 和 questions——1 到 6 个问题,类型为 noul、choice 或 score。把 DECISIONS_API_KEY 设为控制台里的密钥。
用 requests(异步则用 httpx——调用形态完全相同)。每个调用都设超时;挂起的决策应该快速失败,而不是卡住流水线。
Python
import os
import requests
res = requests.post(
"https://decisions-api.net/api/v1/decisions",
headers={"Authorization": f"Bearer {os.environ['DECISIONS_API_KEY']}"},
json={
"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?",
}
},
},
timeout=30,
)
res.raise_for_status()
answers = res.json()["answers"]
print(answers["refund"]["noul"])读取概率
answers 以你的问题 ID 为键返回。noul 答案是陈述为真的概率。choice 答案带胜出的 choice、每个选项的概率和置信度——用置信度而不只是胜者来决定是否自动执行。
Python
answers = res.json()["answers"]
# noul: probability the statement is true
if answers["refund"]["noul"] >= 0.8:
route_to_refunds()
# choice: winning label + per-option probabilities + confidence
team = answers["team"]
print(team["choice"], team["probabilities"], team["confidence"])超时与重试
429 和 502 值得短退避重试——失败的调用不计费。402 表示余额不足:去充值,不要重试。422 是校验错误;报错信息会指明字段,应该修正请求体而不是重试。
Python
import time
import requests
def decide(body, attempts=3):
for i in range(attempts):
try:
res = requests.post(
"https://decisions-api.net/api/v1/decisions",
headers={"Authorization": f"Bearer {os.environ['DECISIONS_API_KEY']}"},
json=body,
timeout=30,
)
if res.status_code in (429, 502):
time.sleep(2 ** i)
continue
if res.status_code == 402:
raise RuntimeError("out of credits")
res.raise_for_status()
return res.json()["answers"]
except requests.Timeout:
time.sleep(2 ** i)
raise RuntimeError("decision call failed")把调用封装成一个 helper
调用方看到的应该是一个「文本进、标签出」的函数——而不是 HTTP 细节。把超时、402 检查和重试循环一次性放进 decide(),每个调用点都保持干净。
Python
# Keep the call behind one function — timeouts, 402s,
# and retries live inside it, not at every call site.
def route_ticket(text: str) -> str:
answers = 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.",
},
}
},
})
team = answers["team"]
return team["choice"] if team["confidence"] >= 0.7 else "triage"常见问题
OpenAI 官方有 Decisions API 的 SDK 示例吗?
没有。OpenAI 尚未发布其 Decisions API 的 SDK 方法或请求模式——任何展示 client.decisions.create 的代码都是虚构的。本页使用纯 HTTP。
能用 httpx 代替 requests 吗?
可以——端点就是普通的 HTTPS POST。用 httpx.AsyncClient 配上相同的头、请求体、超时和状态处理即可。
如何发送结构化上下文?
state 不只接受字符串,也接受 JSON 对象或数组——在 json 请求体里直接传 dict,模型会把它作为上下文读取。
在浏览器里跑一次决策
跳过配置——在试用台用新访客的 1 个免费积分发起真实调用。