| 19 | |
| 20 | |
| 21 | def call(state, questions, retries=5): |
| 22 | body = json.dumps({"model": MODEL, "state": state, "questions": questions}).encode() |
| 23 | req = urllib.request.Request(API, data=body, method="POST", headers={"Authorization": f"Bearer {KEY}", "Content-Type": "application/json"}) |
| 24 | delay = 1.0 |
| 25 | for attempt in range(retries): |
| 26 | try: |
| 27 | with urllib.request.urlopen(req, timeout=60) as r: |
| 28 | return json.load(r) |
| 29 | except urllib.error.HTTPError as e: |
| 30 | if e.code in (429, 529) and attempt < retries - 1: |
| 31 | time.sleep(delay); delay *= 2; continue |
| 32 | raise RuntimeError(f"HTTP {e.code}: {e.read().decode(errors='replace')[:200]}") |
| 33 | raise RuntimeError("retries exhausted") |
| 34 | |
| 35 | |
| 36 | def run(jobs, workers=8): |