Python rehberi
Python'da OpenAI Decisions API
OpenAI, Decisions API'si için bir SDK yayınlamadı — kopyalanacak client.decisions.create yok. Bu sayfa, bu sitenin uç noktası için çalışan Python gösterir — decisions-1 sunan ve aynı kısıtlı karar örüntüsünü izleyen, çağrılabilir bir OpenAI Decisions API alternatifi.
Güncellendi
Çağrıyı yapın
Bearer anahtarıyla /api/v1/decisions'a tek POST. Gövde; model, state ve questions'tır — her biri noul, choice veya score tipte 1–6 soru. DECISIONS_API_KEY'i paneldeki bir anahtara ayarlayın.
requests kullanın (async için httpx — çağrı biçimi aynıdır). Her çağrıya timeout koyun; takılan bir karar hızlıca başarısız olmalı, pipeline'ı kilitlememeli.
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"])Olasılıkları okuyun
answers, soru kimliklerinizle anahtarlanmış olarak döner. Bir noul yanıtı, ifadenin doğru olma olasılığıdır. Bir choice yanıtı; kazanan choice'u, her seçenek için bir olasılık ve bir güven değeri taşır — otomatik davranıp davranmayacağınıza yalnız kazanana değil, güvene göre karar verin.
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"])Timeout'lar ve yeniden denemeler
429 ve 502 kısa bir backoff denemesini hak eder — başarısız çağrılar ücretlendirilmez. 402 bakiyenin boş olduğu anlamına gelir: yükleyin, tekrar denemeyin. 422 doğrulama hatasıdır; mesaj alanı belirtir, yani tekrar denemek yerine gövdeyi düzeltin.
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")Çağrıyı bir helper'a sarın
Çağıranlar metin alıp etiket döndüren bir fonksiyon görmeli — HTTP ayrıntılarını değil. Zaman aşımını, 402 kontrolünü ve retry döngüsünü decide() içine bir kez koyun; her çağrı noktası temiz kalır.
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"SSS
Decisions API için resmi bir OpenAI SDK örneği var mı?
Hayır. OpenAI, Decisions API'si için SDK metodları veya istek şeması yayımlamadı — client.decisions.create gösteren her şey uydurmadır. Bu sayfa düz HTTP kullanır.
requests yerine httpx kullanabilir miyim?
Evet — uç nokta düz bir HTTPS POST'tur. httpx.AsyncClient'ı aynı başlıklar, gövde, timeout ve durum işlemeyle kullanın.
Yapılandırılmış bağlamı nasıl gönderirim?
state yalnızca string değil, JSON objesi veya dizi de kabul eder — json gövdesinde doğrudan dict geçirin, model onu bağlam olarak okur.
Tarayıcıdan bir karar çalıştırın
Kurulum yok — yeni ziyaretçilere 1 ücretsiz krediyle oyun alanında gerçek bir çağrı yapın.