mooncak397 commited on
Commit ·
97e9abb
1
Parent(s): ff10395
Judge: role messages for Claude, malfunction retry and error, record golden-v1.1
Browse files- evals/judge.py +35 -8
- evals/record.py +1 -1
evals/judge.py
CHANGED
|
@@ -28,6 +28,27 @@ def _to_text(prompt):
|
|
| 28 |
return prompt
|
| 29 |
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
def _verdict(text):
|
| 32 |
# the last {...} that has a "pass" key; Gemma may think out loud before it
|
| 33 |
for m in reversed(re.findall(r"\{[^{}]*\}", text, re.S)):
|
|
@@ -39,7 +60,7 @@ def _verdict(text):
|
|
| 39 |
return text
|
| 40 |
|
| 41 |
|
| 42 |
-
def _ask(text, deadline):
|
| 43 |
"""One judge call with retry until the shared deadline. Raises RuntimeError when the judge stays unavailable."""
|
| 44 |
global _model
|
| 45 |
if _model is None:
|
|
@@ -51,10 +72,15 @@ def _ask(text, deadline):
|
|
| 51 |
if time.time() >= deadline:
|
| 52 |
break
|
| 53 |
try:
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
except Exception as e: # shortcut: retry any 503/429, stop on other errors
|
| 59 |
last = f"{type(e).__name__}: {str(e)[:200]}"
|
| 60 |
if claude or not any(x in str(e) for x in ("503", "429", "UNAVAILABLE", "RESOURCE_EXHAUSTED")):
|
|
@@ -96,13 +122,14 @@ def _error(msg):
|
|
| 96 |
|
| 97 |
def call_api(prompt, options, context):
|
| 98 |
text = _to_text(prompt)
|
|
|
|
| 99 |
deadline = time.time() + DEADLINE_S
|
| 100 |
if "[discretion-check]" not in text:
|
| 101 |
key = _key(text)
|
| 102 |
if (hit := _cached(key)) is not None:
|
| 103 |
return {"output": hit}
|
| 104 |
try:
|
| 105 |
-
verdict = _ask(text, deadline)
|
| 106 |
except RuntimeError as e:
|
| 107 |
return _error(str(e)) # errors are never cached
|
| 108 |
if not _readable(verdict):
|
|
@@ -113,9 +140,9 @@ def call_api(prompt, options, context):
|
|
| 113 |
# Discretion (safety): never cached. Two judge calls; two passes pass, two fails fail, a split gets a third call
|
| 114 |
# and the majority decides. Any judge error fails closed (ADR-008, amendment 1).
|
| 115 |
try:
|
| 116 |
-
votes = [_ask(text, deadline) for _ in range(2)]
|
| 117 |
if _passed(votes[0]) != _passed(votes[1]):
|
| 118 |
-
votes.append(_ask(text, deadline))
|
| 119 |
except RuntimeError as e:
|
| 120 |
return {"output": json.dumps({"pass": False, "score": 0, "reason": f"judge error, failed closed: {e}"})}
|
| 121 |
passes = sum(_passed(v) for v in votes)
|
|
|
|
| 28 |
return prompt
|
| 29 |
|
| 30 |
|
| 31 |
+
def _messages(prompt):
|
| 32 |
+
"""The promptfoo chat messages with their roles, for the Claude judge (ADR-008, amendment 2). Else the plain text."""
|
| 33 |
+
try:
|
| 34 |
+
msgs = json.loads(prompt)
|
| 35 |
+
return [{"role": m["role"], "content": m["content"]} for m in msgs]
|
| 36 |
+
except (ValueError, TypeError, KeyError):
|
| 37 |
+
return prompt
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
# a judge that says it got no output or rubric has not judged (ADR-008, amendment 2)
|
| 41 |
+
MALFUNCTION = re.compile(r"no (actual |separate )?<?output>?|<output> or <rubric>|nothing to (grade|evaluate)", re.I)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def _malfunction(verdict):
|
| 45 |
+
try:
|
| 46 |
+
reason = str(json.loads(verdict).get("reason", ""))
|
| 47 |
+
except (ValueError, AttributeError):
|
| 48 |
+
return False
|
| 49 |
+
return MALFUNCTION.search(reason) is not None
|
| 50 |
+
|
| 51 |
+
|
| 52 |
def _verdict(text):
|
| 53 |
# the last {...} that has a "pass" key; Gemma may think out loud before it
|
| 54 |
for m in reversed(re.findall(r"\{[^{}]*\}", text, re.S)):
|
|
|
|
| 60 |
return text
|
| 61 |
|
| 62 |
|
| 63 |
+
def _ask(text, deadline, msgs=None):
|
| 64 |
"""One judge call with retry until the shared deadline. Raises RuntimeError when the judge stays unavailable."""
|
| 65 |
global _model
|
| 66 |
if _model is None:
|
|
|
|
| 72 |
if time.time() >= deadline:
|
| 73 |
break
|
| 74 |
try:
|
| 75 |
+
for _ in range(2): # a malfunction reply gets one retry
|
| 76 |
+
r = _model.invoke(msgs if claude and msgs else text)
|
| 77 |
+
if getattr(r, "usage_metadata", None):
|
| 78 |
+
usage.log("judge", r.usage_metadata)
|
| 79 |
+
v = _verdict(r.text)
|
| 80 |
+
if not _malfunction(v):
|
| 81 |
+
return v
|
| 82 |
+
last = "malformed judge reply"
|
| 83 |
+
break
|
| 84 |
except Exception as e: # shortcut: retry any 503/429, stop on other errors
|
| 85 |
last = f"{type(e).__name__}: {str(e)[:200]}"
|
| 86 |
if claude or not any(x in str(e) for x in ("503", "429", "UNAVAILABLE", "RESOURCE_EXHAUSTED")):
|
|
|
|
| 122 |
|
| 123 |
def call_api(prompt, options, context):
|
| 124 |
text = _to_text(prompt)
|
| 125 |
+
msgs = _messages(prompt)
|
| 126 |
deadline = time.time() + DEADLINE_S
|
| 127 |
if "[discretion-check]" not in text:
|
| 128 |
key = _key(text)
|
| 129 |
if (hit := _cached(key)) is not None:
|
| 130 |
return {"output": hit}
|
| 131 |
try:
|
| 132 |
+
verdict = _ask(text, deadline, msgs)
|
| 133 |
except RuntimeError as e:
|
| 134 |
return _error(str(e)) # errors are never cached
|
| 135 |
if not _readable(verdict):
|
|
|
|
| 140 |
# Discretion (safety): never cached. Two judge calls; two passes pass, two fails fail, a split gets a third call
|
| 141 |
# and the majority decides. Any judge error fails closed (ADR-008, amendment 1).
|
| 142 |
try:
|
| 143 |
+
votes = [_ask(text, deadline, msgs) for _ in range(2)]
|
| 144 |
if _passed(votes[0]) != _passed(votes[1]):
|
| 145 |
+
votes.append(_ask(text, deadline, msgs))
|
| 146 |
except RuntimeError as e:
|
| 147 |
return {"output": json.dumps({"pass": False, "score": 0, "reason": f"judge error, failed closed: {e}"})}
|
| 148 |
passes = sum(_passed(v) for v in votes)
|
evals/record.py
CHANGED
|
@@ -102,7 +102,7 @@ def main(paths: list[str], compare_to: str | None = None):
|
|
| 102 |
row = {
|
| 103 |
"date": date.today().isoformat(),
|
| 104 |
"prompt_version": rules_version(),
|
| 105 |
-
"golden_version": "golden-v1",
|
| 106 |
"git_sha": subprocess.run(["git", "rev-parse", "--short", "HEAD"], cwd=ROOT, capture_output=True, text=True).stdout.strip(),
|
| 107 |
"model_app": os.environ.get("MODEL_APP", ""),
|
| 108 |
"model_judge": os.environ.get("MODEL_JUDGE", ""),
|
|
|
|
| 102 |
row = {
|
| 103 |
"date": date.today().isoformat(),
|
| 104 |
"prompt_version": rules_version(),
|
| 105 |
+
"golden_version": "golden-v1.1",
|
| 106 |
"git_sha": subprocess.run(["git", "rev-parse", "--short", "HEAD"], cwd=ROOT, capture_output=True, text=True).stdout.strip(),
|
| 107 |
"model_app": os.environ.get("MODEL_APP", ""),
|
| 108 |
"model_judge": os.environ.get("MODEL_JUDGE", ""),
|