Ask the judge LLM whether the model's response matches the reference.
(
sample: dict, ref_answer: str, client: AsyncOpenAI, sem: asyncio.Semaphore
)
| 201 | |
| 202 | |
| 203 | async def judge_one( |
| 204 | sample: dict, ref_answer: str, client: AsyncOpenAI, sem: asyncio.Semaphore |
| 205 | ) -> dict: |
| 206 | """Ask the judge LLM whether the model's response matches the reference.""" |
| 207 | doc_id = sample["doc_id"] |
| 208 | response_text = sample["resps"][0][0] |
| 209 | response_tail = response_text[-TAIL_CHARS:] |
| 210 | |
| 211 | prompt = JUDGE_PROMPT.format(reference=ref_answer, response_tail=response_tail) |
| 212 | |
| 213 | for attempt in range(MAX_RETRIES): |
| 214 | async with sem: |
| 215 | try: |
| 216 | completion = await client.chat.completions.create( |
| 217 | model=JUDGE_MODEL, |
| 218 | messages=[{"role": "user", "content": prompt}], |
| 219 | max_tokens=8, |
| 220 | temperature=0, |
| 221 | ) |
| 222 | judgment = completion.choices[0].message.content.strip() |
| 223 | break |
| 224 | except Exception as e: |
| 225 | if "429" in str(e) and attempt < MAX_RETRIES - 1: |
| 226 | delay = RETRY_BASE_DELAY * (2**attempt) |
| 227 | await asyncio.sleep(delay) |
| 228 | continue |
| 229 | print(f" doc_id={doc_id}: API error: {e}", file=sys.stderr) |
| 230 | judgment = "ERROR" |
| 231 | break |
| 232 | |
| 233 | correct = judgment.upper().startswith("YES") |
| 234 | return {"doc_id": doc_id, "correct": correct, "judgment": judgment} |
| 235 | |
| 236 | |
| 237 | def find_samples(eval_dir: str) -> Path | None: |