File size: 6,974 Bytes
d49e060
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
"""OpenAI-compatible Judge client with a durable append-only cache.

Two request shapes, chosen by model name. Reasoning models are served on
``/responses`` instead of ``/chat/completions`` -- on the gateway this benchmark
was scored with, a ``gpt-5.x`` model has no chat/completions cluster at all, so
sending one there fails rather than falling back -- and they take an output-token
budget and a reasoning effort in place of a temperature. Everything else uses
``/chat/completions``.
"""

from __future__ import annotations

import hashlib
import json
import random
import threading
import time
from pathlib import Path

# Chat models: deterministic decoding.
JUDGE_TEMPERATURE = 0.0
JUDGE_MAX_TOKENS = 2048

# Reasoning models: no temperature knob. Thinking is disabled so the Judge is
# scoring rather than deliberating, and the token budget is higher because the
# reasoning envelope counts against it even at effort "none".
JUDGE_MAX_OUTPUT_TOKENS = 4096
JUDGE_REASONING_EFFORT = "none"


def uses_responses_endpoint(model: str) -> bool:
    return model.startswith(("gpt-5", "gpt-6", "o1", "o3", "o4"))


class JudgeRequestError(RuntimeError):
    pass


class JudgeClient:
    def __init__(
        self,
        *,
        api_key: str,
        base_url: str,
        model: str,
        prompt_version: str,
        cache_path: Path,
        timeout: float = 360.0,
        max_attempts: int = 6,
    ) -> None:
        if not api_key:
            raise ValueError("JUDGE_API_KEY is required")
        from openai import OpenAI

        self.model = model
        self.prompt_version = prompt_version
        self.timeout = timeout
        self.max_attempts = max_attempts
        self._use_responses = uses_responses_endpoint(model)
        self.cache_path = cache_path
        self.cache_path.parent.mkdir(parents=True, exist_ok=True)
        self._client = OpenAI(api_key=api_key, base_url=base_url, timeout=timeout)
        self._lock = threading.Lock()
        self._cache: dict[str, str] = {}
        self._load_cache()

    def _key(self, prompt: str) -> str:
        # The decoding parameters belong in the key, and the two endpoints do not
        # share a set: keying a /responses verdict on a temperature it never used
        # would let a cached chat verdict answer a reasoning-model request.
        if self._use_responses:
            params: dict[str, object] = {
                "endpoint": "responses",
                "max_output_tokens": JUDGE_MAX_OUTPUT_TOKENS,
                "reasoning_effort": JUDGE_REASONING_EFFORT,
            }
        else:
            params = {
                "endpoint": "chat.completions",
                "temperature": JUDGE_TEMPERATURE,
                "max_tokens": JUDGE_MAX_TOKENS,
            }
        payload = json.dumps(
            {
                "model": self.model,
                "prompt_version": self.prompt_version,
                "prompt": prompt,
                **params,
            },
            ensure_ascii=False,
            sort_keys=True,
            separators=(",", ":"),
        )
        return hashlib.sha256(payload.encode("utf-8")).hexdigest()

    def _load_cache(self) -> None:
        if not self.cache_path.is_file():
            return
        with self.cache_path.open(encoding="utf-8") as handle:
            for line_number, line in enumerate(handle, 1):
                if not line.strip():
                    continue
                try:
                    record = json.loads(line)
                    self._cache[record["key"]] = record["response"]
                except (json.JSONDecodeError, KeyError, TypeError):
                    # An interrupted final append must not destroy earlier cache entries.
                    if line_number > 1:
                        continue

    def _cache_put(self, key: str, response: str) -> None:
        with self._lock:
            if key in self._cache:
                return
            self._cache[key] = response
            with self.cache_path.open("a", encoding="utf-8") as handle:
                handle.write(json.dumps({"key": key, "response": response}, ensure_ascii=False))
                handle.write("\n")
                handle.flush()

    def _complete_via_chat(self, prompt: str) -> str:
        response = self._client.chat.completions.create(
            model=self.model,
            messages=[{"role": "user", "content": prompt}],
            temperature=JUDGE_TEMPERATURE,
            max_tokens=JUDGE_MAX_TOKENS,
        )
        return response.choices[0].message.content or ""

    def _complete_via_responses(self, prompt: str) -> str:
        response = self._client.responses.create(
            model=self.model,
            input=prompt,
            max_output_tokens=JUDGE_MAX_OUTPUT_TOKENS,
            reasoning={"effort": JUDGE_REASONING_EFFORT},
        )
        # Walk the output blocks rather than reading output_text: with reasoning
        # enabled the array also carries reasoning items, and a provider that
        # ignores effort="none" would otherwise fold thinking into the verdict.
        parts = []
        for block in response.output or []:
            if getattr(block, "type", None) != "message":
                continue
            for item in getattr(block, "content", None) or []:
                if getattr(item, "type", None) == "output_text":
                    parts.append(getattr(item, "text", "") or "")
        return "".join(parts)

    def complete(self, prompt: str) -> tuple[str, bool]:
        """Return (response_text, served_from_cache)."""
        key = self._key(prompt)
        with self._lock:
            cached = self._cache.get(key)
        if cached is not None:
            return cached, True

        error: BaseException | None = None
        for attempt in range(self.max_attempts):
            try:
                content = (
                    self._complete_via_responses(prompt) if self._use_responses
                    else self._complete_via_chat(prompt)
                )
                self._cache_put(key, content)
                return content, False
            except BaseException as exc:
                error = exc
                status_code = getattr(exc, "status_code", None)
                retryable = status_code == 429 or (
                    isinstance(status_code, int) and 500 <= status_code < 600
                )
                # Connection/transport failures usually have no HTTP status.
                if status_code is None:
                    retryable = True
                if retryable and attempt + 1 < self.max_attempts:
                    time.sleep(min(2 ** (attempt + 1), 60) + random.uniform(0, 1))
                    continue
                break
        raise JudgeRequestError(
            f"Judge request failed after {self.max_attempts} attempts: {error}"
        ) from error

    @property
    def cache_entries(self) -> int:
        return len(self._cache)