Python ガイド

Python で使う OpenAI Decisions API

OpenAI は Decisions API の SDK を公開していません——client.decisions.create のようなメソッドは存在しません。このページでは、このサイトのエンドポイント(decisions-1 を提供する、呼び出せる OpenAI Decisions API の代替)向けの動く Python を示します。同じ制約付きデシジョンパターンに従います。

更新日

呼び出す

Bearer キーで /api/v1/decisions へ 1 回 POST します。本文は 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、全選択肢の確率、confidence を持ちます——自動実行するかは勝者ではなく confidence で決めてください。

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

プロバイダーを 1 関数の後ろに隠す

呼び出し側が見るのはテキストを受けてラベルを返す関数だけにしましょう——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"

よくある質問

Decisions API の公式 OpenAI SDK 例はありますか?

ありません。OpenAI は Decisions API の SDK メソッドやリクエストスキーマを公開していません——client.decisions.create を示すコードはすべて創作です。このページはプレーンな HTTP を使いますが、どんなプロバイダ SDK も内部ではそうしているものです。

requests の代わりに httpx は使えますか?

はい——エンドポイントは通常の HTTPS POST です。httpx.AsyncClient に同じヘッダー・本文・タイムアウト・ステータス処理を使ってください。

構造化されたコンテキストを送るには?

state は文字列だけでなく JSON オブジェクトや配列を受けます——json 本文に dict をそのまま渡せば、モデルがコンテキストとして読みます。

ブラウザでデシジョンを実行

セットアップ不要——新規訪問者の 2 クレジットでプレイグラウンドから実際に呼べます。