mooncak397 Claude Opus 5.5 commited on
Commit ·
59bc960
1
Parent(s): 07ff8cf
evals: stop at once on a daily-quota 429 (PerDay), mark the case quota-day, run.py prints the retry time
Browse filesThe graph attaches the caught exception to its log record; the provider reads it, writes
evals/results/.quota-day and makes no more calls. run.py drops the incomplete result file and exits.
Tests follow with the resume commit.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- .gitignore +2 -0
- app/graph.py +2 -1
- evals/provider.py +34 -1
- evals/run.py +5 -0
.gitignore
CHANGED
|
@@ -6,3 +6,5 @@ __pycache__/
|
|
| 6 |
node_modules/
|
| 7 |
evals/.judge-cache.jsonl
|
| 8 |
evals/.usage.jsonl
|
|
|
|
|
|
|
|
|
| 6 |
node_modules/
|
| 7 |
evals/.judge-cache.jsonl
|
| 8 |
evals/.usage.jsonl
|
| 9 |
+
evals/results/.quota-day
|
| 10 |
+
evals/results/*.partial.jsonl
|
app/graph.py
CHANGED
|
@@ -90,7 +90,8 @@ def build_graph(model):
|
|
| 90 |
return out, None
|
| 91 |
except Exception as e: # refusal, parse error, transport: the user gets a fixed text, not a 500
|
| 92 |
busy = _transport(e)
|
| 93 |
-
|
|
|
|
| 94 |
return None, (BUSY if busy else idk)[lang]
|
| 95 |
|
| 96 |
def entry(state: State):
|
|
|
|
| 90 |
return out, None
|
| 91 |
except Exception as e: # refusal, parse error, transport: the user gets a fixed text, not a 500
|
| 92 |
busy = _transport(e)
|
| 93 |
+
# the exception rides along for evals/provider.py (daily-quota stop); the log text has no message content
|
| 94 |
+
log.warning("model_error class=%s busy=%s", type(e).__name__, busy, extra={"error": e})
|
| 95 |
return None, (BUSY if busy else idk)[lang]
|
| 96 |
|
| 97 |
def entry(state: State):
|
evals/provider.py
CHANGED
|
@@ -1,9 +1,12 @@
|
|
| 1 |
"""promptfoo Python provider: runs the real graph and returns JSON (plan section 7)."""
|
| 2 |
import json
|
|
|
|
| 3 |
import os
|
| 4 |
import sys
|
| 5 |
import time
|
|
|
|
| 6 |
from pathlib import Path
|
|
|
|
| 7 |
|
| 8 |
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
| 9 |
|
|
@@ -15,6 +18,7 @@ from app.texts import BUSY # noqa: E402
|
|
| 15 |
from evals import usage # noqa: E402
|
| 16 |
|
| 17 |
_graph = None
|
|
|
|
| 18 |
|
| 19 |
|
| 20 |
class CountCalls(BaseCallbackHandler):
|
|
@@ -31,14 +35,41 @@ class CountCalls(BaseCallbackHandler):
|
|
| 31 |
usage.log("app", u)
|
| 32 |
|
| 33 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
def call_api(prompt, options, context):
|
| 35 |
global _graph
|
|
|
|
|
|
|
| 36 |
_graph = _graph or build_graph(get_model())
|
| 37 |
waits = 0
|
|
|
|
| 38 |
for attempt in range(6):
|
| 39 |
-
counter = CountCalls()
|
|
|
|
| 40 |
try:
|
| 41 |
s = _graph.invoke({"messages": [("user", prompt)]}, config={"callbacks": [counter]})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
if s["messages"][-1].text not in BUSY.values():
|
| 43 |
break
|
| 44 |
# the graph turns 429/5xx into the BUSY text (ADR-010); for the eval that is an error, not an answer
|
|
@@ -50,6 +81,8 @@ def call_api(prompt, options, context):
|
|
| 50 |
time.sleep(min(15 * 2**attempt, 90))
|
| 51 |
continue
|
| 52 |
return {"error": f"{type(e).__name__}: {str(e)[:300]}"}
|
|
|
|
|
|
|
| 53 |
else:
|
| 54 |
return {"error": "rate limited after 6 tries"}
|
| 55 |
out = {"route": s["route"], "lang": s["lang"], "answer": s["messages"][-1].text,
|
|
|
|
| 1 |
"""promptfoo Python provider: runs the real graph and returns JSON (plan section 7)."""
|
| 2 |
import json
|
| 3 |
+
import logging
|
| 4 |
import os
|
| 5 |
import sys
|
| 6 |
import time
|
| 7 |
+
from datetime import datetime, timedelta
|
| 8 |
from pathlib import Path
|
| 9 |
+
from zoneinfo import ZoneInfo
|
| 10 |
|
| 11 |
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
|
| 12 |
|
|
|
|
| 18 |
from evals import usage # noqa: E402
|
| 19 |
|
| 20 |
_graph = None
|
| 21 |
+
STOP = Path(__file__).resolve().parent / "results/.quota-day" # written on a daily-quota 429; run.py stops the run
|
| 22 |
|
| 23 |
|
| 24 |
class CountCalls(BaseCallbackHandler):
|
|
|
|
| 35 |
usage.log("app", u)
|
| 36 |
|
| 37 |
|
| 38 |
+
class ModelErrors(logging.Handler):
|
| 39 |
+
"""Collects the exceptions that app/graph.py's _call caught and logged."""
|
| 40 |
+
|
| 41 |
+
def __init__(self):
|
| 42 |
+
super().__init__()
|
| 43 |
+
self.errors = []
|
| 44 |
+
|
| 45 |
+
def emit(self, record):
|
| 46 |
+
if getattr(record, "error", None) is not None:
|
| 47 |
+
self.errors.append(record.error)
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def _quota_reset() -> str:
|
| 51 |
+
"""Gemini daily quotas reset at midnight Pacific time; shown in local time."""
|
| 52 |
+
pt = datetime.now(ZoneInfo("America/Los_Angeles"))
|
| 53 |
+
reset = (pt + timedelta(days=1)).replace(hour=0, minute=0, second=0, microsecond=0)
|
| 54 |
+
return reset.astimezone().strftime("%Y-%m-%d %H:%M %Z")
|
| 55 |
+
|
| 56 |
+
|
| 57 |
def call_api(prompt, options, context):
|
| 58 |
global _graph
|
| 59 |
+
if STOP.exists(): # the daily quota is gone: no more calls in this run
|
| 60 |
+
return {"error": "quota-day: run stopped"}
|
| 61 |
_graph = _graph or build_graph(get_model())
|
| 62 |
waits = 0
|
| 63 |
+
graph_log = logging.getLogger("app.graph")
|
| 64 |
for attempt in range(6):
|
| 65 |
+
counter, errors = CountCalls(), ModelErrors()
|
| 66 |
+
graph_log.addHandler(errors)
|
| 67 |
try:
|
| 68 |
s = _graph.invoke({"messages": [("user", prompt)]}, config={"callbacks": [counter]})
|
| 69 |
+
if any("PerDay" in str(e) for e in errors.errors): # retrying a daily quota only burns time
|
| 70 |
+
STOP.parent.mkdir(exist_ok=True)
|
| 71 |
+
STOP.write_text(f"quota-day hit at {datetime.now():%Y-%m-%d %H:%M}; retry after {_quota_reset()}\n")
|
| 72 |
+
return {"error": "quota-day: daily request limit reached", "metadata": {"llm_calls": counter.n}}
|
| 73 |
if s["messages"][-1].text not in BUSY.values():
|
| 74 |
break
|
| 75 |
# the graph turns 429/5xx into the BUSY text (ADR-010); for the eval that is an error, not an answer
|
|
|
|
| 81 |
time.sleep(min(15 * 2**attempt, 90))
|
| 82 |
continue
|
| 83 |
return {"error": f"{type(e).__name__}: {str(e)[:300]}"}
|
| 84 |
+
finally:
|
| 85 |
+
graph_log.removeHandler(errors)
|
| 86 |
else:
|
| 87 |
return {"error": "rate limited after 6 tries"}
|
| 88 |
out = {"route": s["route"], "lang": s["lang"], "answer": s["messages"][-1].text,
|
evals/run.py
CHANGED
|
@@ -20,6 +20,7 @@ GROUPS = {"fact": "fact", "facts": "fact", "unk": "unk", "unknown": "unk", "disc
|
|
| 20 |
"off": "off", "offtopic": "off", "inj": "inj", "injection": "inj", "con": "con", "contact": "con"}
|
| 21 |
SAFETY = ["disc", "inj", "con"]
|
| 22 |
OUT = ROOT / "evals/results"
|
|
|
|
| 23 |
|
| 24 |
|
| 25 |
def golden() -> dict[str, dict]:
|
|
@@ -56,7 +57,11 @@ def run_eval(out: Path, ids=None, judge=True, delay=None, replay_from=None):
|
|
| 56 |
cmd += ["-r", "file://replay.py"]
|
| 57 |
out.parent.mkdir(exist_ok=True)
|
| 58 |
out.unlink(missing_ok=True)
|
|
|
|
| 59 |
subprocess.run(cmd, cwd=ROOT, env=env) # promptfoo exits non-zero when a case fails: not an error here
|
|
|
|
|
|
|
|
|
|
| 60 |
if not out.exists():
|
| 61 |
sys.exit(f"promptfoo wrote no result file: {out}")
|
| 62 |
|
|
|
|
| 20 |
"off": "off", "offtopic": "off", "inj": "inj", "injection": "inj", "con": "con", "contact": "con"}
|
| 21 |
SAFETY = ["disc", "inj", "con"]
|
| 22 |
OUT = ROOT / "evals/results"
|
| 23 |
+
STOP = OUT / ".quota-day" # the same file as evals/provider.py STOP
|
| 24 |
|
| 25 |
|
| 26 |
def golden() -> dict[str, dict]:
|
|
|
|
| 57 |
cmd += ["-r", "file://replay.py"]
|
| 58 |
out.parent.mkdir(exist_ok=True)
|
| 59 |
out.unlink(missing_ok=True)
|
| 60 |
+
STOP.unlink(missing_ok=True)
|
| 61 |
subprocess.run(cmd, cwd=ROOT, env=env) # promptfoo exits non-zero when a case fails: not an error here
|
| 62 |
+
if STOP.exists(): # evals/provider.py hit the daily quota: this result file is incomplete
|
| 63 |
+
out.unlink(missing_ok=True)
|
| 64 |
+
sys.exit(f"Run stopped: {STOP.read_text().strip()}")
|
| 65 |
if not out.exists():
|
| 66 |
sys.exit(f"promptfoo wrote no result file: {out}")
|
| 67 |
|