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")

把 provider 藏在一个函数后面

调用方应该只看到一个“文本进、标签出”的函数——而不是 HTTP 细节。等 OpenAI 开放其 Decisions API 时,你只需替换 decide() 的内部实现,所有调用点不动。问题文本、选项和阈值都能沿用。

Python

# Keep the decision behind one function. Swap the HTTP layer
# when OpenAI publishes its schema — callers never change.
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,这也正是任何 provider SDK 内部的封装。

能用 httpx 代替 requests 吗?

可以——端点就是普通的 HTTPS POST。用 httpx.AsyncClient 配上相同的头、请求体、超时和状态处理即可。

如何发送结构化上下文?

state 不只接受字符串,也接受 JSON 对象或数组——在 json 请求体里直接传 dict,模型会把它作为上下文读取。

在浏览器里跑一次决策

跳过配置——在试用台用新访客的 2 个免费积分发起真实调用。