Skip to content
Python
9.8 KB
Raw Blame
"""Generic LLM judge — wraps any ModelAdapter to evaluate outputs.

The judge formats a prompt template with variables, sends it to the model,
and parses the structured response. Used by all scoring functions.
"""

import json
import os
import re
from pathlib import Path

import anthropic
import openai
from google import genai
from google.genai import types
from mistralai.client import Mistral

PROMPTS_DIR = Path(__file__).parent / "prompts"

_VERDICT_SCHEMA = {
    "type": "object",
    "properties": {
        "verdict": {"type": "string", "enum": ["pass", "fail"]},
        "reasoning": {"type": "string"},
    },
    "required": ["verdict", "reasoning"],
    "additionalProperties": False,
}

def _detect_provider(model: str) -> str:
    """Return 'anthropic', 'google', 'openai', or 'mistral' from the model name."""
    name = model.lower()
    if name.startswith("claude"):
        return "anthropic"
    if name.startswith("gemini"):
        return "google"
    if name.startswith(("gpt", "o1", "o3", "o4", "o5")):
        return "openai"
    if name.startswith("mistral"):
        return "mistral"
    raise ValueError(f"Unknown judge provider for model: {model!r}")

class Judge:
    """LLM-as-judge that evaluates agent outputs against rubric criteria."""

    def __init__(self, model: str = "claude-sonnet-4-6"):
        """Initialize with a model ID. Picks the SDK client based on the model prefix.

        Args:
            model: Model ID (e.g. 'claude-sonnet-4-6', 'gemini-3-flash-preview',
                'gpt-5.4', 'mistral-medium-3.5').
        """
        self.model = model
        self.provider = _detect_provider(model)
        if self.provider == "anthropic":
            self.client = anthropic.Anthropic(max_retries=1)
        elif self.provider == "google":
            self.client = genai.Client()
        elif self.provider == "openai":
            self.client = openai.OpenAI()
        else:  # mistral
            self.client = Mistral(
                api_key=os.environ["MISTRAL_API_KEY"],
                timeout_ms=600_000,
            )

    def evaluate(
        self, prompt_template: str, variables: dict, temperature: float = 0.0, _retries: int = 2,
    ) -> dict:
        """Send a formatted prompt to the judge and parse the JSON response.

        Args:
            prompt_template: A prompt string with {variable} placeholders.
            variables: Dict of values to format into the template.
            temperature: Sampling temperature (default 0.0).

        Returns:
            Parsed JSON dict from the judge's response.
        """
        prompt = prompt_template.format(**variables)
        if self.provider == "anthropic":
            return self._evaluate_anthropic(prompt, temperature, _retries)
        if self.provider == "google":
            return self._evaluate_google(prompt, temperature, _retries)
        if self.provider == "openai":
            return self._evaluate_openai(prompt, temperature, _retries)
        return self._evaluate_mistral(prompt, temperature, _retries)

    def _evaluate_anthropic(self, prompt: str, temperature: float, _retries: int) -> dict:
        last_err: Exception | None = None
        for attempt in range(_retries):
            kwargs = {
                "model": self.model,
                "max_tokens": 16384,
                "temperature": temperature,
                "messages": [{"role": "user", "content": prompt}],
            }
            # Use output_config on every attempt except the last.
            if attempt < _retries - 1:
                kwargs["output_config"] = {
                    "format": {
                        "type": "json_schema",
                        "schema": _VERDICT_SCHEMA,
                    }
                }
            try:
                response = self.client.messages.create(**kwargs)
            except anthropic.InternalServerError as e:
                # 500s on the structured-output path have been observed to
                # succeed when retried without output_config.
                last_err = e
                continue

            if response.stop_reason == "max_tokens":
                input_tokens = response.usage.input_tokens if response.usage else "unknown"
                raise ValueError(
                    f"Judge response truncated (stop_reason=max_tokens, "
                    f"input_tokens={input_tokens}, max_tokens={16384}). "
                    f"The agent output is likely too large for the judge context window. "
                    f"Ensure criteria have deliverables lists to scope output."
                )

            text = response.content[0].text
            try:
                return self._parse_json(text)
            except (ValueError, json.JSONDecodeError) as e:
                last_err = e
        raise ValueError(
            f"Judge returned unparseable response after {_retries} attempts: {last_err}"
        )
    
    def _evaluate_google(self, prompt: str, temperature: float, _retries: int) -> dict:
        last_err: Exception | None = None
        for attempt in range(_retries):
            config_kwargs = dict(
                temperature=temperature,
                max_output_tokens=16384,
                response_mime_type="application/json",
            )
            # Constrain to the verdict schema on early attempts; drop it on the last.
            if attempt < _retries - 1:
                config_kwargs["response_schema"] = _VERDICT_SCHEMA
            try:
                response = self.client.models.generate_content(
                    model=self.model,
                    contents=prompt,
                    config=types.GenerateContentConfig(**config_kwargs),
                )
            except Exception as e:
                last_err = e
                continue
            text = response.text or ""
            try:
                return self._parse_json(text)
            except (ValueError, json.JSONDecodeError) as e:
                last_err = e
        raise ValueError(
            f"Judge returned unparseable response after {_retries} attempts: {last_err}"
        )

    def _evaluate_openai(self, prompt: str, temperature: float, _retries: int) -> dict:
        last_err: Exception | None = None
        for attempt in range(_retries):
            kwargs = {
                "model": self.model,
                "input": prompt,
                "max_output_tokens": 16384,
                "temperature": temperature,
            }
            if attempt < _retries - 1:
                kwargs["text"] = {
                    "format": {
                        "type": "json_schema",
                        "name": "verdict",
                        "schema": _VERDICT_SCHEMA,
                        "strict": True,
                    }
                }
            try:
                response = self.client.responses.create(**kwargs)
            except Exception as e:
                last_err = e
                continue
            text = response.output_text or ""
            try:
                return self._parse_json(text)
            except (ValueError, json.JSONDecodeError) as e:
                last_err = e
        raise ValueError(
            f"Judge returned unparseable response after {_retries} attempts: {last_err}"
        )

    def _evaluate_mistral(self, prompt: str, temperature: float, _retries: int) -> dict:
        last_err: Exception | None = None
        for attempt in range(_retries):
            kwargs = {
                "model": self.model,
                "messages": [{"role": "user", "content": prompt}],
                "temperature": temperature,
                "max_tokens": 16384,
            }
            if attempt < _retries - 1:
                kwargs["response_format"] = {"type": "json_object"}
            try:
                response = self.client.chat.complete(**kwargs)
            except Exception as e:
                last_err = e
                continue
            text = response.choices[0].message.content or ""
            try:
                return self._parse_json(text)
            except (ValueError, json.JSONDecodeError) as e:
                last_err = e
        raise ValueError(
            f"Judge returned unparseable response after {_retries} attempts: {last_err}"
        )

    def evaluate_from_file(self, prompt_name: str, variables: dict) -> dict:
        """Load a prompt template from prompts/ dir and evaluate.

        Args:
            prompt_name: Filename (without .md) in the prompts directory.
            variables: Dict of values to format into the template.

        Returns:
            Parsed JSON dict from the judge's response.
        """
        path = PROMPTS_DIR / f"{prompt_name}.txt"
        template = path.read_text(encoding="utf-8")
        return self.evaluate(prompt_template=template, variables=variables)

    @staticmethod
    def _parse_json(text: str) -> dict:
        """Extract JSON from model response, handling markdown fences."""
        # Try to find JSON in code fences first
        match = re.search(r"```(?:json)?\s*\n?(.*?)\n?```", text, re.DOTALL)
        if match:
            try:
                return json.loads(match.group(1).strip())
            except json.JSONDecodeError:
                pass  # Fall through to brace matching

        # Try to find a JSON object by matching balanced braces
        for i, ch in enumerate(text):
            if ch == '{':
                depth = 0
                for j in range(i, len(text)):
                    if text[j] == '{':
                        depth += 1
                    elif text[j] == '}':
                        depth -= 1
                    if depth == 0:
                        try:
                            return json.loads(text[i:j + 1])
                        except json.JSONDecodeError:
                            break  # Try next opening brace
                        break

        raise ValueError(f"No JSON found in judge response: {text[:200]}")