Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

31.6 KB
Raw Blame
"""
Evaluation and grading functionality.

This module provides tools for evaluating model outputs against reference answers.
"""

import json
from loguru import logger
from pathlib import Path
import re
import time
from typing import Any, Callable, Dict, List, Optional

from langchain_core.messages.human import HumanMessage

from ..config.llm_config import LLMNotConfiguredError, get_llm
from ..config.openai_endpoint_binding import (
    resolve_openai_endpoint_override_key,
)
from ..config.thread_settings import get_setting_from_snapshot
from ..llm.providers.base import normalize_provider
from ..utilities.json_utils import get_llm_response_text
from ..security.network_utils import is_private_ip
from ..utilities.url_utils import endpoint_origin, same_endpoint_origin
from .templates import BROWSECOMP_GRADER_TEMPLATE, SIMPLEQA_GRADER_TEMPLATE


# Default evaluation configuration using Claude 3.7 Sonnet via OpenRouter
DEFAULT_EVALUATION_CONFIG = {
    "model_name": "anthropic/claude-3.7-sonnet",  # Correct model ID for OpenRouter
    "provider": "openai_endpoint",  # Use OpenRouter
    "openai_endpoint_url": "https://openrouter.ai/api/v1",  # OpenRouter URL
    "temperature": 0,  # Zero temp for consistent evaluation
    # Note: max_tokens removed as it's not supported by LDR's get_llm()
}


def normalize_grader_provider(provider):
    """The grader provider ``provider`` names, normalised as ``get_llm`` does.

    A provider that normalises to nothing (``None``, ``""``, whitespace,
    ``'""'``) returns the grader default, ``openai_endpoint``. The start
    routes and the grader treat it as an unset
    ``benchmark.evaluation.provider``. The ``benchmark_functions`` runners
    call this only for a truthy provider: there a quotes-only or whitespace
    provider selects ``openai_endpoint`` with the research endpoint URL
    (the ``endpoint_url`` argument or ``llm.openai_endpoint.url``) when one
    is set, and only ``None`` or ``""`` leaves the grader on
    ``benchmark.evaluation.endpoint_url``.
    """
    return normalize_provider(provider) or DEFAULT_EVALUATION_CONFIG["provider"]


GRADER_KEY_ERROR_MESSAGE = (
    "The benchmark grader endpoint is on a different origin (scheme, host "
    "or port) than the endpoint its API key was configured for, so no key "
    "is sent to it. Set benchmark.evaluation.api_key for the endpoint in "
    "benchmark.evaluation.endpoint_url, or point the grader at the "
    "research endpoint (llm.openai_endpoint.url). A grader on a local or "
    "private address (for example http://localhost:11434/v1) runs without "
    "a key; for another keyless endpoint, set benchmark.evaluation.api_key "
    "to any placeholder value."
)


def grader_endpoint_url(settings_snapshot: Optional[Dict[str, Any]] = None):
    """The configured grader endpoint (``benchmark.evaluation.endpoint_url``)."""
    url = get_setting_from_snapshot(
        "benchmark.evaluation.endpoint_url",
        None,
        settings_snapshot=settings_snapshot,
    )
    if isinstance(url, str) and url.strip():
        return url.strip()
    return DEFAULT_EVALUATION_CONFIG["openai_endpoint_url"]


def resolve_grader_endpoint_api_key(
    endpoint_url: str,
    explicit_key: Optional[str] = None,
    settings_snapshot: Optional[Dict[str, Any]] = None,
) -> str:
    """Resolve the API key for an ``openai_endpoint`` grader at ``endpoint_url``.

    Each key is only sent to the endpoint it was configured for:

    1. ``explicit_key`` (an ``api_key`` in a custom evaluation config): the
       caller's key for this run's grader, returned as is and bound to the
       grader's resolved endpoint — ``benchmark.evaluation.endpoint_url``
       unless the evaluation config names its own ``openai_endpoint_url``
       (call-supplied or derived from ``llm.openai_endpoint.url``).
    2. ``benchmark.evaluation.api_key``, when ``endpoint_url`` has the same
       origin (scheme, host, port) as ``benchmark.evaluation.endpoint_url``.
    3. ``llm.openai_endpoint.api_key``, when ``endpoint_url`` has the same
       origin as ``llm.openai_endpoint.url`` (or as a set
       ``LDR_LLM_OPENAI_ENDPOINT_URL``). A key from
       ``LDR_LLM_OPENAI_ENDPOINT_API_KEY`` follows only the env URL's
       origin, or with none set the saved URL's — see
       ``resolve_openai_endpoint_override_key``.
    4. Otherwise no key. If a configured key was withheld and the grader
       endpoint is not on a local/private address, raise instead.

    Returns:
        The key to bind to ``endpoint_url``, or ``""`` when none applies.

    Raises:
        LLMNotConfiguredError: a configured key exists but none belongs to
            ``endpoint_url``; message ``GRADER_KEY_ERROR_MESSAGE``.
    """
    if isinstance(explicit_key, str) and explicit_key.strip():
        return explicit_key.strip()

    grader_key = get_setting_from_snapshot(
        "benchmark.evaluation.api_key",
        "",
        settings_snapshot=settings_snapshot,
    )
    grader_key = grader_key.strip() if isinstance(grader_key, str) else ""
    grader_key_withheld = False
    if grader_key:
        if same_endpoint_origin(
            endpoint_url, grader_endpoint_url(settings_snapshot)
        ):
            return grader_key
        grader_key_withheld = True

    key, research_key_withheld = resolve_openai_endpoint_override_key(
        endpoint_url, settings_snapshot=settings_snapshot
    )
    if key:
        return key
    if (grader_key_withheld or research_key_withheld) and not (
        _is_local_endpoint(endpoint_url)
    ):
        raise LLMNotConfiguredError(GRADER_KEY_ERROR_MESSAGE)
    # Keyless: nothing configured, or a local/private grader (Ollama, vLLM
    # on the LAN) that typically needs no key. No configured key is sent.
    return ""


def _is_local_endpoint(endpoint_url) -> bool:
    """True for a grader endpoint on a loopback or private address.

    Decided from the URL text only (no DNS lookup): ``localhost``, private,
    loopback and link-local IP literals, and ``.local`` names.
    """
    origin = endpoint_origin(endpoint_url)
    if origin is None:
        return False
    return is_private_ip(origin[1])


def _evaluation_llm_kwargs(
    custom_config: Optional[Dict[str, Any]] = None,
    settings_snapshot: Optional[Dict[str, Any]] = None,
) -> Dict[str, Any]:
    """Build the ``get_llm`` keyword arguments for the grader.

    Raises:
        LLMNotConfiguredError: see :func:`resolve_grader_endpoint_api_key`.
    """
    # Start with default config (Claude 3.7 Sonnet via OpenRouter)
    config = DEFAULT_EVALUATION_CONFIG.copy()

    # Override with any custom settings
    if custom_config:
        config.update(custom_config)

    logger.info(
        f"Getting evaluation LLM with provider={config['provider']}, model={config['model_name']}"
    )

    # Remove any parameters that LDR's get_llm doesn't support
    # This ensures compatibility with LDR's implementation
    ldr_supported_params = {
        "model_name",
        "temperature",
        "provider",
        "openai_endpoint_url",
        "api_key",
    }

    filtered_config = {
        k: v for k, v in config.items() if k in ldr_supported_params
    }

    # ``api_key`` is accepted in a custom config as the grader's own key;
    # get_llm takes it as ``openai_endpoint_api_key``.
    explicit_key = filtered_config.pop("api_key", None)

    # Normalise as get_llm does and hand get_llm that value, so a quoted
    # '"openai_endpoint"' takes the grader-endpoint path below instead of
    # reaching get_llm with the hard-coded default URL. A provider that
    # normalises to nothing is the grader default, as if it were unset.
    provider = normalize_grader_provider(filtered_config.get("provider"))
    filtered_config["provider"] = provider

    if provider == "openai_endpoint":
        # A config without its own URL (e.g. a request-supplied
        # {"provider": "openai_endpoint"}) grades against the configured
        # grader endpoint, not the hard-coded default.
        if not (custom_config or {}).get("openai_endpoint_url"):
            filtered_config["openai_endpoint_url"] = grader_endpoint_url(
                settings_snapshot
            )
        if settings_snapshot is None:
            logger.warning(
                "No settings snapshot provided for benchmark grader. "
                "API key must be provided via settings_snapshot for thread safety."
            )
        grader_key = resolve_grader_endpoint_api_key(
            filtered_config["openai_endpoint_url"],
            explicit_key=explicit_key,
            settings_snapshot=settings_snapshot,
        )
        if not grader_key:
            logger.warning(
                "Using openai_endpoint provider for the benchmark grader but "
                "no API key found. Set the benchmark.evaluation.api_key "
                "setting if the grader endpoint requires one."
            )
        filtered_config["openai_endpoint_api_key"] = grader_key

    return filtered_config


def check_grader_endpoint_key(
    custom_config: Optional[Dict[str, Any]] = None,
    settings_snapshot: Optional[Dict[str, Any]] = None,
) -> None:
    """Fail fast if the grader would have no key for its endpoint.

    Runs the same resolution as :func:`get_evaluation_llm` without building
    a client, so a benchmark can be refused before any research runs.

    Raises:
        LLMNotConfiguredError: with ``GRADER_KEY_ERROR_MESSAGE``.
    """
    _evaluation_llm_kwargs(custom_config, settings_snapshot)


def get_evaluation_llm(
    custom_config: Optional[Dict[str, Any]] = None,
    settings_snapshot: Optional[Dict[str, Any]] = None,
):
    """
    Get an LLM for evaluation purposes using Claude 3.7 Sonnet via OpenRouter
    by default, which can be overridden with custom settings.

    Args:
        custom_config: Optional custom configuration that overrides defaults
        settings_snapshot: Optional settings snapshot for thread-safe access

    Returns:
        An LLM instance for evaluation
    """
    filtered_config = _evaluation_llm_kwargs(custom_config, settings_snapshot)

    # Get the LLM using LDR's existing function. Thread settings_snapshot
    # through — without it, get_llm's snapshot-less PEP only permits local
    # default providers, so a cloud grader (e.g. openai) would be refused
    # with PolicyDeniedError. The snapshot lets get_llm evaluate the policy
    # and permit the configured grader when the user's scope allows it.
    return get_llm(**filtered_config, settings_snapshot=settings_snapshot)


def extract_answer_from_response(
    response: str, dataset_type: str = "simpleqa"
) -> Dict[str, str]:
    """
    Extract structured information from LDR's response.

    Args:
        response: Response from LDR
        dataset_type: Type of dataset

    Returns:
        Dictionary with extracted answer and confidence
    """
    # Clean up citations — strip both ASCII "[N]" and lenticular "【N】"
    # so a lenticular citation (some LLMs emit them) doesn't survive into
    # the graded answer text and skew the match.
    response = re.sub(r"[\[【]\d+[\]】]", "", response)

    # Extract differently based on dataset type
    if dataset_type.lower() == "browsecomp":
        # Extract the final answer from structured response
        answer_match = re.search(r"Exact Answer:\s*(.*?)(?:\n|$)", response)
        exact_answer = answer_match.group(1).strip() if answer_match else "None"

        # Extract confidence
        confidence_match = re.search(r"Confidence:\s*(\d+)%", response)
        confidence = confidence_match.group(1) if confidence_match else "100"

        return {"extracted_answer": exact_answer, "confidence": confidence}

    # For SimpleQA, return the whole response as the answer
    return {
        "extracted_answer": response,
        "confidence": "100",  # SimpleQA doesn't have confidence scores
    }


def _select_grader_template(dataset_type: str) -> str:
    """Pick the grading template for a dataset type."""
    return (
        BROWSECOMP_GRADER_TEMPLATE
        if dataset_type.lower() == "browsecomp"
        else SIMPLEQA_GRADER_TEMPLATE
    )


def _build_grading_prompt(template: str, result_data: Dict[str, Any]) -> str:
    """Format the grading prompt for one result."""
    return template.format(
        question=result_data.get("problem", ""),
        correct_answer=result_data.get("correct_answer", ""),
        response=result_data.get("response", ""),
    )


def _call_grader_llm(evaluation_llm, grading_prompt: str):
    """Invoke synchronously and normalize supported text/content-block responses."""
    if hasattr(evaluation_llm, "invoke") and callable(evaluation_llm.invoke):
        prompt = (
            [HumanMessage(content=grading_prompt)]
            if hasattr(evaluation_llm, "chat_messages")
            else grading_prompt
        )
        response = evaluation_llm.invoke(prompt)
    else:
        response = evaluation_llm(grading_prompt)
    return get_llm_response_text(response)


async def _acall_grader_llm(evaluation_llm, grading_prompt: str):
    """Await the async interface, retaining the injected sync-only fallback."""
    if hasattr(evaluation_llm, "ainvoke") and callable(evaluation_llm.ainvoke):
        prompt = (
            [HumanMessage(content=grading_prompt)]
            if hasattr(evaluation_llm, "chat_messages")
            else grading_prompt
        )
        response = await evaluation_llm.ainvoke(prompt)
        return get_llm_response_text(response)
    return _call_grader_llm(evaluation_llm, grading_prompt)


def _parse_grading_response(
    grading_response: str, dataset_type: str
) -> Dict[str, Any]:
    """Extract the grading fields from the grader's raw response."""
    if dataset_type.lower() == "browsecomp":
        # BrowseComp-specific extraction
        extracted_answer_match = re.search(
            r"extracted_final_answer:\s*(.*?)(?:\n|$)", grading_response
        )
        extracted_answer = (
            extracted_answer_match.group(1).strip()
            if extracted_answer_match
            else "None"
        )

        reasoning_match = re.search(
            r"reasoning:\s*(.*?)(?:\n\n|\ncorrect:|\Z)",
            grading_response,
            re.DOTALL,
        )
        reasoning = reasoning_match.group(1).strip() if reasoning_match else ""

        correct_match = re.search(
            r"correct:\s*(yes|no)", grading_response, re.IGNORECASE
        )
        is_correct = (
            (correct_match.group(1).lower() == "yes")
            if correct_match
            else False
        )

        confidence_match = re.search(r"confidence:\s*(\d+)", grading_response)
        confidence = confidence_match.group(1) if confidence_match else "100"
    else:
        # SimpleQA extraction
        extracted_answer_match = re.search(
            r"Extracted Answer:\s*(.*?)(?:\n|$)", grading_response
        )
        extracted_answer = (
            extracted_answer_match.group(1).strip()
            if extracted_answer_match
            else "None"
        )

        reasoning_match = re.search(
            r"Reasoning:\s*(.*?)(?:\nCorrect:|\Z)",
            grading_response,
            re.DOTALL,
        )
        reasoning = reasoning_match.group(1).strip() if reasoning_match else ""

        correct_match = re.search(
            r"Correct:\s*(yes|no)", grading_response, re.IGNORECASE
        )
        is_correct = (
            (correct_match.group(1).lower() == "yes")
            if correct_match
            else False
        )

        confidence = "100"  # SimpleQA doesn't have confidence

    return {
        "extracted_by_grader": extracted_answer,
        "reasoning": reasoning,
        "is_correct": is_correct,
        "graded_confidence": confidence,
        "grader_response": grading_response,
    }


def _grading_error_result(exc: Exception) -> Dict[str, Any]:
    """Build the single-result payload for a failed grading call."""
    return {
        "grading_error": str(exc),
        "is_correct": False,
        "graded_confidence": "0",
        "grader_response": f"Grading failed: {exc!s}",
    }


def grade_single_result(
    result_data: Dict[str, Any],
    dataset_type: str = "simpleqa",
    evaluation_config: Optional[Dict[str, Any]] = None,
    settings_snapshot: Optional[Dict[str, Any]] = None,
) -> Dict[str, Any]:
    """
    Grade a single benchmark result using LLM.

    The synchronous entry point uses ``invoke`` without running the async
    core on a temporary event loop. Callers already on an event loop can
    await the corresponding ``_async`` entry point. Resource cleanup uses
    the shared close helper; it is separate from invocation dispatch.

    Args:
        result_data: Dictionary containing result data with keys: id, problem, correct_answer, response, extracted_answer
        dataset_type: Type of dataset
        evaluation_config: Optional custom config for evaluation LLM
        settings_snapshot: Optional settings snapshot for thread-safe access

    Returns:
        Dictionary with grading results
    """
    # Get evaluation LLM
    evaluation_llm = get_evaluation_llm(evaluation_config, settings_snapshot)

    try:
        grading_prompt = _start_single_grading(result_data, dataset_type)

        eval_llm_start = time.time()
        grading_response = _call_grader_llm(evaluation_llm, grading_prompt)
        _log_grading_call_done(eval_llm_start)

        return _parse_grading_response(grading_response, dataset_type)

    except Exception as e:
        logger.exception("Error grading single result")
        return _grading_error_result(e)
    finally:
        from ..utilities.resource_utils import safe_close

        safe_close(evaluation_llm, "grader LLM")


async def grade_single_result_async(
    result_data: Dict[str, Any],
    dataset_type: str = "simpleqa",
    evaluation_config: Optional[Dict[str, Any]] = None,
    settings_snapshot: Optional[Dict[str, Any]] = None,
) -> Dict[str, Any]:
    """Async core of :func:`grade_single_result`: one awaited ainvoke (#5854).

    For callers that already run on an event loop. Mirrors the sync
    entry point; only the LLM call line differs.
    """
    # Get evaluation LLM
    evaluation_llm = get_evaluation_llm(evaluation_config, settings_snapshot)

    try:
        grading_prompt = _start_single_grading(result_data, dataset_type)

        eval_llm_start = time.time()
        grading_response = await _acall_grader_llm(
            evaluation_llm, grading_prompt
        )
        _log_grading_call_done(eval_llm_start)

        return _parse_grading_response(grading_response, dataset_type)

    except Exception as e:
        logger.exception("Error grading single result")
        return _grading_error_result(e)
    finally:
        from ..utilities.resource_utils import safe_close

        safe_close(evaluation_llm, "grader LLM")


def _start_single_grading(
    result_data: Dict[str, Any], dataset_type: str
) -> str:
    """Log the start of a single grading call and build its prompt."""
    template = _select_grader_template(dataset_type)
    question = result_data.get("problem", "")
    logger.info(f"Grading single result: {question[:50]}...")

    grading_prompt = _build_grading_prompt(template, result_data)
    logger.info(
        f"Starting grading LLM call (prompt length: {len(grading_prompt)} chars)..."
    )
    return grading_prompt


def _log_grading_call_done(eval_llm_start: float) -> None:
    """Log how long the grading LLM call took."""
    eval_llm_elapsed = time.time() - eval_llm_start
    logger.info(f"Grading LLM call completed in {eval_llm_elapsed:.2f}s")


def grade_results(
    results_file: str,
    output_file: str,
    dataset_type: str = "simpleqa",
    evaluation_config: Optional[Dict[str, Any]] = None,
    progress_callback: Optional[Callable[[int, int, Dict], None]] = None,
    settings_snapshot: Optional[Dict[str, Any]] = None,
) -> List[Dict[str, Any]]:
    """
    Grade benchmark results using LLM.

    This synchronous entry point calls the grader through ``invoke``
    without running the async core on a temporary loop. Shared resource
    cleanup retains its existing behavior. Callers already on a loop
    should await :func:`grade_results_async` instead.

    Args:
        results_file: Path to results file
        output_file: Path to save graded results
        dataset_type: Type of dataset
        evaluation_config: Optional custom config for evaluation LLM
        progress_callback: Optional callback for progress updates
        settings_snapshot: Optional snapshot so the grader LLM is
            constructed under the user's egress policy. Without it
            the LLM PEP takes its snapshot-less path, which fails
            closed with PolicyDeniedError for any non-local provider.

    Returns:
        List of graded results
    """
    # Get evaluation LLM
    evaluation_llm = get_evaluation_llm(evaluation_config, settings_snapshot)

    try:
        return _grade_results_inner(
            evaluation_llm,
            results_file,
            output_file,
            dataset_type,
            progress_callback,
        )
    finally:
        from ..utilities.resource_utils import safe_close

        safe_close(evaluation_llm, "grader LLM")


async def grade_results_async(
    results_file: str,
    output_file: str,
    dataset_type: str = "simpleqa",
    evaluation_config: Optional[Dict[str, Any]] = None,
    progress_callback: Optional[Callable[[int, int, Dict], None]] = None,
    settings_snapshot: Optional[Dict[str, Any]] = None,
) -> List[Dict[str, Any]]:
    """Async core of :func:`grade_results`: awaited ainvoke calls (#5854).

    For callers that already run on an event loop.
    """
    # Get evaluation LLM
    evaluation_llm = get_evaluation_llm(evaluation_config, settings_snapshot)

    try:
        return await _grade_results_inner_async(
            evaluation_llm,
            results_file,
            output_file,
            dataset_type,
            progress_callback,
        )
    finally:
        from ..utilities.resource_utils import safe_close

        safe_close(evaluation_llm, "grader LLM")


def _load_results_for_grading(
    results_file: str, output_file: str
) -> List[Dict[str, Any]]:
    """Load the results to grade and clear any previous output file."""
    results = []
    with open(results_file, "r", encoding="utf-8") as f:
        for line in f:
            if line.strip():
                results.append(json.loads(line))

    # Remove output file if it exists
    output_path = Path(output_file)
    if output_path.exists():
        # nosemgrep: semgrep.rules.path-traversal-risk, reason: CLI/Python grading API intentionally replaces its caller-selected output file; web grading uses grade_single_result
        output_path.unlink()

    return results


def _append_graded_line(output_file: str, payload: Dict[str, Any]) -> None:
    """Append one graded (or errored) result to the output file."""
    with open(output_file, "a", encoding="utf-8") as f:
        f.write(json.dumps(payload) + "\n")


def _start_batch_item(
    template: str,
    result: Dict[str, Any],
    idx: int,
    total: int,
    progress_callback: Optional[Callable[[int, int, Dict], None]],
) -> str:
    """Report progress for one batch item and build its grading prompt."""
    # Call progress callback if provided
    if progress_callback:
        progress_callback(
            idx,
            total,
            {"status": "grading", "index": idx, "total": total},
        )

    question = result.get("problem", "")
    logger.info(f"Grading {idx + 1}/{total}: {question[:50]}...")

    return _build_grading_prompt(template, result)


def _finish_batch_item(
    result: Dict[str, Any],
    grading_response: str,
    dataset_type: str,
    output_file: str,
    idx: int,
    total: int,
    progress_callback: Optional[Callable[[int, int, Dict], None]],
    graded_results: List[Dict[str, Any]],
) -> bool:
    """Parse, record and report one successfully graded batch item."""
    parsed = _parse_grading_response(grading_response, dataset_type)
    is_correct = parsed["is_correct"]

    # Format graded result
    graded_result = result.copy()
    graded_result.update(parsed)

    graded_results.append(graded_result)

    # Write to output file
    _append_graded_line(output_file, graded_result)

    # Call progress callback if provided
    if progress_callback:
        progress_callback(
            idx,
            total,
            {
                "status": "graded",
                "is_correct": is_correct,
                "result": graded_result,
            },
        )

    return is_correct


def _fail_batch_item(
    result: Dict[str, Any],
    exc: Exception,
    output_file: str,
    idx: int,
    total: int,
    progress_callback: Optional[Callable[[int, int, Dict], None]],
    graded_results: List[Dict[str, Any]],
) -> None:
    """Record and report one batch item whose grading raised."""
    # Handle error
    error_result = result.copy()
    error_result["grading_error"] = str(exc)

    _append_graded_line(output_file, error_result)

    graded_results.append(error_result)

    # Call progress callback if provided
    if progress_callback:
        progress_callback(
            idx,
            total,
            {
                "status": "error",
                "error": str(exc),
                "result": error_result,
            },
        )


def _log_grading_summary(correct_count: int, total: int) -> None:
    """Log the final accuracy of a grading run."""
    accuracy = correct_count / total if total else 0
    logger.info(f"Grading complete. Accuracy: {accuracy:.3f}")
    logger.info(f"Correct: {correct_count}/{total}")


def _grade_results_inner(
    evaluation_llm,
    results_file: str,
    output_file: str,
    dataset_type: str,
    progress_callback: Optional[Callable[[int, int, Dict], None]],
) -> List[Dict[str, Any]]:
    """Inner implementation of grade_results, separated for cleanup."""
    template = _select_grader_template(dataset_type)
    results = _load_results_for_grading(results_file, output_file)

    graded_results: List[Dict[str, Any]] = []
    correct_count = 0
    total = len(results)

    # Process each result
    for idx, result in enumerate(results):
        grading_prompt = _start_batch_item(
            template, result, idx, total, progress_callback
        )

        try:
            grading_response = _call_grader_llm(evaluation_llm, grading_prompt)
            if _finish_batch_item(
                result,
                grading_response,
                dataset_type,
                output_file,
                idx,
                total,
                progress_callback,
                graded_results,
            ):
                correct_count += 1
        except Exception as e:
            logger.exception(f"Error grading result {idx + 1}")
            _fail_batch_item(
                result,
                e,
                output_file,
                idx,
                total,
                progress_callback,
                graded_results,
            )

    _log_grading_summary(correct_count, total)

    return graded_results


async def _grade_results_inner_async(
    evaluation_llm,
    results_file: str,
    output_file: str,
    dataset_type: str,
    progress_callback: Optional[Callable[[int, int, Dict], None]],
) -> List[Dict[str, Any]]:
    """Async counterpart of :func:`_grade_results_inner`.

    Only the LLM call line differs.
    """
    template = _select_grader_template(dataset_type)
    results = _load_results_for_grading(results_file, output_file)

    graded_results: List[Dict[str, Any]] = []
    correct_count = 0
    total = len(results)

    # Process each result
    for idx, result in enumerate(results):
        grading_prompt = _start_batch_item(
            template, result, idx, total, progress_callback
        )

        try:
            grading_response = await _acall_grader_llm(
                evaluation_llm, grading_prompt
            )
            if _finish_batch_item(
                result,
                grading_response,
                dataset_type,
                output_file,
                idx,
                total,
                progress_callback,
                graded_results,
            ):
                correct_count += 1
        except Exception as e:
            logger.exception(f"Error grading result {idx + 1}")
            _fail_batch_item(
                result,
                e,
                output_file,
                idx,
                total,
                progress_callback,
                graded_results,
            )

    _log_grading_summary(correct_count, total)

    return graded_results


def human_evaluation(
    results_file: str, output_file: str, interactive: bool = True
) -> List[Dict[str, Any]]:
    """
    Allow for human evaluation of results.

    Args:
        results_file: Path to results file
        output_file: Path to save human-graded results
        interactive: Whether to run in interactive console mode

    Returns:
        List of human-graded results
    """
    # Load results
    results = []
    with open(results_file, "r", encoding="utf-8") as f:
        for line in f:
            if line.strip():
                results.append(json.loads(line))

    # Remove output file if it exists
    output_path = Path(output_file)
    if output_path.exists():
        # nosemgrep: semgrep.rules.path-traversal-risk, reason: Interactive local grading intentionally replaces its caller-selected output file
        output_path.unlink()

    human_graded_results = []
    correct_count = 0

    if interactive:
        logger.info(f"Human evaluation: {len(results)} examples to grade")
        print(f"Human evaluation: {len(results)} examples to grade")
        print(
            "For each example, you'll see the question, correct answer, and model's response."
        )
        print("You'll be asked to judge if the model's answer is correct.")

    for idx, result in enumerate(results):
        question = result.get("problem", "")
        correct_answer = result.get("correct_answer", "")
        response = result.get("response", "")
        extracted_answer = result.get("extracted_answer", "")

        if interactive:
            print(f"\n\n===== Example {idx + 1}/{len(results)} =====")
            print(f"Question: {question}")
            print(f"\nCorrect Answer: {correct_answer}")
            print(f"\nModel Response: {response}")
            print(f"\nExtracted Answer: {extracted_answer}")

            # Get human judgment
            while True:
                judgment = (
                    input("\nIs the model's answer correct? (y/n): ")
                    .strip()
                    .lower()
                )
                if judgment in ["y", "n"]:
                    break
                print("Please enter 'y' or 'n'")

            is_correct = judgment == "y"

            # Get reasoning
            reasoning = input(
                "Please provide reasoning for your judgment: "
            ).strip()
        else:
            # Non-interactive mode - placeholder for API/UI implementation
            # In a real implementation, this would be filled by UI actions
            is_correct = False
            reasoning = "Non-interactive evaluation"

        if is_correct:
            correct_count += 1

        # Update result with human judgment
        human_result = result.copy()
        human_result.update(
            {
                "is_correct": is_correct,
                "reasoning": reasoning,
                "human_evaluation": True,
            }
        )

        human_graded_results.append(human_result)

        # Write to output file
        with open(output_file, "a", encoding="utf-8") as f:
            f.write(json.dumps(human_result) + "\n")

    accuracy = correct_count / len(results) if results else 0
    logger.info(f"Human evaluation complete. Accuracy: {accuracy:.3f}")
    if interactive:
        print(f"\nHuman evaluation complete. Accuracy: {accuracy:.3f}")
        print(f"Correct: {correct_count}/{len(results)}")

    return human_graded_results