~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""Flask routes for benchmark web interface."""
from fastapi import APIRouter, Depends, Request
from fastapi.responses import JSONResponse
from ..dependencies.auth import require_auth
from ..dependencies.threadpool import run_db_sync
from ..dependencies.rate_limit import limiter
from ..template_config import templates
import time
from loguru import logger
from sqlalchemy.exc import SQLAlchemyError
from ...database.session_context import get_user_db_session
from ...llm.providers.base import normalize_provider
from ...benchmarks.graders import normalize_grader_provider
from local_deep_research.settings import SettingsManager
from ...benchmarks.web_api.benchmark_service import benchmark_service
from ..dependencies.json_body import read_json_dict
from typing import Annotated
# Benchmark runs spawn full LLM + search loops (expensive enough that
# rate-limiting them is a DoS defense, not just good manners). Tight
# cap since a single run can cost tens of LLM completions.
_BENCHMARK_START_RATE_LIMIT = "3 per minute"
# SHARED bucket across both start endpoints. Applying `limiter.limit()` to
# each route separately gave /api/start and /api/start-simple their OWN
# 3/min bucket -- 6 starts a minute combined, twice the cap the comment
# above describes, for two routes that kick off the same expensive work.
# This is the same mistake metrics.py documents fixing for the journal
# endpoints (three routes, three 60/min buckets, 180/min combined).
# key_func is deliberately omitted: slowapi does `key_func or self._key_func`,
# so this keeps the limiter's default per-IP keying rather than quietly
# switching these routes to per-user as well.
_benchmark_start_limit = limiter.shared_limit(
_BENCHMARK_START_RATE_LIMIT, scope="benchmark_start"
)
# The grading/evaluation failure prefixes the benchmark producers attach
# to BenchmarkResult.grader_response when they store raw ``str(e)`` of the
# LLM call (benchmark_service.py ``"Evaluation failed: ..."`` / graders.py
# ``_grading_error_result`` ``"Grading failed: ..."``). The raw text can
# carry server-level LLM endpoints / hosts / filesystem paths, so it must
# not cross the HTTP boundary (CWE-209) — the same rationale as the
# run-level ``_GENERIC_BENCHMARK_ERROR`` (#4843). Full detail stays in the
# ``logger.exception`` records at the producer sites.
_GRADER_FAILURE_PREFIXES = ("Evaluation failed:", "Grading failed:")
_GENERIC_GRADER_FAILURE_DETAIL = "internal error (details in server logs)"
# Left-edge decoration in front of the failure prefix on rows written by
# earlier producer eras (a94af439f7..9a8b36c19b, 2025-06..08): the
# missing-API-key wrapper prefixed the failure text with a literal key
# emoji — ``🔑 Evaluation failed: {raw}. Set llm.openai_endpoint...`` —
# and rows can also carry incidental leading whitespace or a zero-width
# space (U+200B, which is *not* whitespace to ``str.strip``).
_GRADER_FAILURE_DECORATION = "\u200b\U0001f511"
def _safe_grader_response(grader_response):
"""Genericize raw grader failure detail at the HTTP boundary.
Failure-prefixed values keep their prefix (the payload still reads as
a grading/evaluation failure) but lose the raw exception detail.
Everything else — ordinary grading rationale, NULL/empty values from
pre-existing rows — passes through unchanged. Producer payloads are
deliberately raw and stay raw (pinned by
tests/benchmarks/web_api/test_benchmark_service_coverage.py); this is
the read-side enforcement point, mirroring how
``get_benchmark_status`` genericizes ``BenchmarkRun.error_message``.
Rows from the decorated producer eras carry the key-emoji / whitespace
decoration in front of the failure prefix, which defeats a plain
``startswith`` match — so the decoration is stripped from the left
before matching (equivalent to
``^[\\s\\u200b\\U0001f511]*(Evaluation failed|Grading failed):``),
and the surfaced payload drops it, keeping the bare failure prefix.
"""
if not isinstance(grader_response, str):
return grader_response
# Scan the left-edge decoration once, without copying each remaining
# suffix. Keep the original value intact for ordinary grading rationale.
start = 0
for char in grader_response:
if not (char.isspace() or char in _GRADER_FAILURE_DECORATION):
break
start += 1
for prefix in _GRADER_FAILURE_PREFIXES:
if grader_response.startswith(prefix, start):
return f"{prefix} {_GENERIC_GRADER_FAILURE_DETAIL}"
return grader_response
# Create the router for benchmark routes
router = APIRouter(prefix="/benchmark", tags=["benchmark"])
# NOTE: Routes use username (not .get()) intentionally.
# Depends(require_auth) guarantees the key exists; direct access fails
# fast if the dependency is ever removed.
def _grader_key_error(evaluation_config, settings_manager):
"""Refuse a benchmark whose grader would have no key for its endpoint.
Grading failures are recorded per example, so without this check every
example would run its research and then fail to grade. Returns a 400
response, or ``None`` when the grader can be built.
"""
from ...benchmarks.graders import (
GRADER_KEY_ERROR_MESSAGE,
check_grader_endpoint_key,
)
from ...config.llm_config import LLMNotConfiguredError
try:
snapshot = settings_manager.get_all_settings()
except (SQLAlchemyError, LookupError, ValueError):
# Same best-effort contract as BenchmarkService.start_benchmark:
# an unreadable snapshot is handled (and logged) by the run itself.
logger.exception("Grader key pre-check skipped")
return None
if not isinstance(snapshot, dict):
return None
try:
check_grader_endpoint_key(evaluation_config or None, snapshot)
except LLMNotConfiguredError:
return JSONResponse(
{"success": False, "error": GRADER_KEY_ERROR_MESSAGE},
status_code=400,
)
except Exception:
# Anything else is the grader's own failure to report at grading
# time, as before this pre-check existed.
logger.exception("Grader key pre-check skipped")
return None
def _validate_datasets_config(datasets_config):
"""Return ``(total_examples, error)`` for a benchmark dataset mapping.
Dataset counts feed both database totals and ``load_dataset``'s integer
``num_examples`` argument. Validate the nested shape once at the HTTP
boundary so a scalar entry, a truthy boolean, a fractional value, or a
negative count cannot turn into a late 500 after settings have already
been loaded or a benchmark row has been created. Counts intentionally
have no upper cap (#4080); zero and a missing count remain valid for a
disabled dataset, provided another dataset has a positive count.
"""
if not isinstance(datasets_config, dict):
return 0, "datasets_config must be an object"
total_examples = 0
for config in datasets_config.values():
if not isinstance(config, dict):
return 0, "Each datasets_config entry must be an object"
count = config.get("count", 0)
if isinstance(count, bool) or not isinstance(count, int) or count < 0:
return 0, "Dataset counts must be non-negative integers"
total_examples += count
return total_examples, None
@router.get("/")
def index(request: Request, username: Annotated[str, Depends(require_auth)]):
"""Benchmark dashboard page."""
with get_user_db_session(username) as db_session:
settings_manager = SettingsManager(db_session)
# Load evaluation settings from database
eval_settings = {
"evaluation_provider": settings_manager.get_setting(
"benchmark.evaluation.provider", "openai_endpoint"
),
"evaluation_model": settings_manager.get_setting(
"benchmark.evaluation.model", ""
),
"evaluation_endpoint_url": settings_manager.get_setting(
"benchmark.evaluation.endpoint_url", ""
),
"evaluation_temperature": settings_manager.get_setting(
"benchmark.evaluation.temperature", 0
),
# Whether a grader key is saved; the key itself never reaches
# the page.
"evaluation_api_key_saved": bool(
str(
settings_manager.get_setting(
"benchmark.evaluation.api_key", ""
)
or ""
).strip()
),
}
return templates.TemplateResponse(
request=request,
name="pages/benchmark.html",
context={"request": request, "eval_settings": eval_settings},
)
@router.get("/results")
def results(request: Request, username: Annotated[str, Depends(require_auth)]):
"""Benchmark results history page."""
return templates.TemplateResponse(
request=request,
name="pages/benchmark_results.html",
context={"request": request},
)
@router.post("/api/start")
@_benchmark_start_limit
async def start_benchmark(
request: Request, username: Annotated[str, Depends(require_auth)]
):
"""Start a new benchmark run."""
data, body_error = await read_json_dict(
request, "simple", "No data provided"
)
if body_error is not None:
return body_error
session_id = request.session.get("session_id")
return await run_db_sync(_start_benchmark_sync, data, username, session_id)
def _start_benchmark_sync(data, username, session_id):
try:
# Extract configuration
run_name = data.get("run_name")
datasets_config = data.get("datasets_config", {})
total_examples, datasets_error = _validate_datasets_config(
datasets_config
)
if datasets_error is not None:
return JSONResponse({"error": datasets_error}, status_code=400)
if total_examples == 0:
return JSONResponse(
{
"error": "At least one dataset with count > 0 must be specified"
},
status_code=400,
)
# Get search config from database instead of request
from ...database.session_context import get_user_db_session
from local_deep_research.settings import SettingsManager
# Try to get password from session store for background thread
from ...database.session_passwords import session_password_store
user_password = None
if session_id:
user_password = session_password_store.get_session_password(
username, session_id
)
search_config = {}
evaluation_config = {}
with get_user_db_session(username) as db_session:
# Use the logged-in user's settings
settings_manager = SettingsManager(db_session)
# Build search config from database settings
search_config = {
"iterations": int(
settings_manager.get_setting("search.iterations", 8)
),
"questions_per_iteration": int(
settings_manager.get_setting(
"search.questions_per_iteration", 5
)
),
"search_tool": settings_manager.get_setting(
"search.tool", "searxng"
),
"search_strategy": settings_manager.get_setting(
"search.search_strategy", "focused_iteration"
),
"model_name": settings_manager.get_setting("llm.model"),
"provider": settings_manager.get_setting("llm.provider"),
"temperature": float(
settings_manager.get_setting("llm.temperature", 0.7)
),
"max_tokens": settings_manager.get_setting(
"llm.max_tokens", 30000
),
"context_window_unrestricted": settings_manager.get_setting(
"llm.context_window_unrestricted", True
),
"context_window_size": settings_manager.get_setting(
"llm.context_window_size", 128000
),
"local_context_window_size": settings_manager.get_setting(
"llm.local_context_window_size", 8192
),
}
# Add provider-specific settings
provider = normalize_provider(search_config.get("provider"))
if provider == "openai_endpoint":
search_config["openai_endpoint_url"] = (
settings_manager.get_setting("llm.openai_endpoint.url")
)
elif provider == "openai":
search_config["openai_api_key"] = settings_manager.get_setting(
"llm.openai.api_key"
)
elif provider == "anthropic":
search_config["anthropic_api_key"] = (
settings_manager.get_setting("llm.anthropic.api_key")
)
# Get evaluation config from database settings or request.
# When caller supplies one, validate structure + whitelist fields
# — the old code passed the raw dict straight into
# `create_benchmark_run`, where it becomes LLM API call
# parameters (provider name, model name, endpoint URL, API key).
# Without validation a user could override the endpoint to
# point at an arbitrary internal service.
if "evaluation_config" in data:
raw = data["evaluation_config"]
if not isinstance(raw, dict):
return JSONResponse(
{
"success": False,
"error": "evaluation_config must be an object",
},
status_code=400,
)
_ALLOWED_EVAL_KEYS = {
"provider",
"model_name",
"temperature",
"max_tokens",
}
unknown = set(raw.keys()) - _ALLOWED_EVAL_KEYS
if unknown:
return JSONResponse(
{
"success": False,
"error": f"Unknown evaluation_config keys: {sorted(unknown)}",
},
status_code=400,
)
# Coerce and constrain field types
evaluation_config = {}
if "provider" in raw and isinstance(raw["provider"], str):
evaluation_config["provider"] = normalize_grader_provider(
raw["provider"][:64]
)
if "model_name" in raw and isinstance(raw["model_name"], str):
evaluation_config["model_name"] = raw["model_name"][:256]
if "temperature" in raw:
try:
evaluation_config["temperature"] = max(
0.0, min(float(raw["temperature"]), 2.0)
)
except (TypeError, ValueError):
pass
if "max_tokens" in raw:
try:
evaluation_config["max_tokens"] = max(
1, min(int(raw["max_tokens"]), 100000)
)
except (TypeError, ValueError):
pass
else:
# Read evaluation config from database settings
evaluation_provider = normalize_grader_provider(
settings_manager.get_setting(
"benchmark.evaluation.provider", "openai_endpoint"
)
)
evaluation_model = settings_manager.get_setting(
"benchmark.evaluation.model", "anthropic/claude-3.7-sonnet"
)
evaluation_temperature = float(
settings_manager.get_setting(
"benchmark.evaluation.temperature", 0
)
)
evaluation_config = {
"provider": evaluation_provider,
"model_name": evaluation_model,
"temperature": evaluation_temperature,
}
# Add provider-specific settings for evaluation
if evaluation_provider == "openai_endpoint":
evaluation_config["openai_endpoint_url"] = (
settings_manager.get_setting(
"benchmark.evaluation.endpoint_url",
"https://openrouter.ai/api/v1",
)
)
elif evaluation_provider == "openai":
evaluation_config["openai_api_key"] = (
settings_manager.get_setting("llm.openai.api_key")
)
elif evaluation_provider == "anthropic":
evaluation_config["anthropic_api_key"] = (
settings_manager.get_setting("llm.anthropic.api_key")
)
grader_key_error = _grader_key_error(
evaluation_config, settings_manager
)
if grader_key_error is not None:
return grader_key_error
# Create benchmark run
benchmark_run_id = benchmark_service.create_benchmark_run(
run_name=run_name,
search_config=search_config,
evaluation_config=evaluation_config,
datasets_config=datasets_config,
username=username,
user_password=user_password,
)
# Start benchmark
success = benchmark_service.start_benchmark(
benchmark_run_id, username, user_password
)
if success:
return {
"success": True,
"benchmark_run_id": benchmark_run_id,
"message": "Benchmark started successfully",
}
return JSONResponse(
{"success": False, "error": "Failed to start benchmark"},
status_code=500,
)
except Exception:
logger.exception("Error starting benchmark")
return JSONResponse(
{"success": False, "error": "An internal error has occurred."},
status_code=500,
)
@router.get("/api/running")
def get_running_benchmark(
request: Request, username: Annotated[str, Depends(require_auth)]
):
"""Check if there's a running benchmark and return its ID."""
try:
from ...database.models.benchmark import BenchmarkRun, BenchmarkStatus
from ...database.session_context import get_user_db_session
with get_user_db_session(username) as session:
# Find any benchmark that's currently running
running_benchmark = (
session.query(BenchmarkRun)
.filter(BenchmarkRun.status == BenchmarkStatus.IN_PROGRESS)
.order_by(BenchmarkRun.created_at.desc())
.first()
)
if running_benchmark:
return {
"success": True,
"benchmark_run_id": running_benchmark.id,
"run_name": running_benchmark.run_name,
"total_examples": running_benchmark.total_examples,
"completed_examples": running_benchmark.completed_examples,
}
return {
"success": False,
"message": "No running benchmark found",
}
except Exception:
logger.exception("Error checking for running benchmark")
return JSONResponse(
{"success": False, "error": "An internal error has occurred."},
status_code=500,
)
@router.get("/api/status/{benchmark_run_id}")
@limiter.exempt
def get_benchmark_status(
request: Request,
benchmark_run_id: int,
username: Annotated[str, Depends(require_auth)],
):
"""Get status of a benchmark run."""
try:
status = benchmark_service.get_benchmark_status(
benchmark_run_id, username
)
if status:
logger.info(
f"Returning status for benchmark {benchmark_run_id}: "
f"completed={status.get('completed_examples')}, "
f"overall_acc={status.get('overall_accuracy')}, "
f"avg_time={status.get('avg_time_per_example')}, "
f"estimated_remaining={status.get('estimated_time_remaining')}"
)
return {"success": True, "status": status}
return JSONResponse(
{"success": False, "error": "Benchmark run not found"},
status_code=404,
)
except Exception:
logger.exception("Error getting benchmark status")
return JSONResponse(
{"success": False, "error": "An internal error has occurred."},
status_code=500,
)
@router.post("/api/cancel/{benchmark_run_id}")
def cancel_benchmark(
request: Request,
benchmark_run_id: int,
username: Annotated[str, Depends(require_auth)],
):
"""Cancel a running benchmark."""
try:
success = benchmark_service.cancel_benchmark(benchmark_run_id, username)
if success:
return {
"success": True,
"message": "Benchmark cancelled successfully",
}
return JSONResponse(
{"success": False, "error": "Failed to cancel benchmark"},
status_code=500,
)
except Exception:
logger.exception("Error cancelling benchmark")
return JSONResponse(
{"success": False, "error": "An internal error has occurred."},
status_code=500,
)
@router.get("/api/history")
def get_benchmark_history(
request: Request, username: Annotated[str, Depends(require_auth)]
):
"""Get list of recent benchmark runs."""
try:
from ...database.models.benchmark import BenchmarkRun
from ...database.session_context import get_user_db_session
with get_user_db_session(username) as session:
# Get all benchmark runs (completed, failed, cancelled, or in-progress)
runs = (
session.query(BenchmarkRun)
.order_by(BenchmarkRun.created_at.desc())
.limit(50)
.all()
)
# Format runs for display
formatted_runs = []
for run in runs:
# Calculate average processing time from results
avg_processing_time = None
avg_search_results = None
try:
from sqlalchemy import func
from ...database.models.benchmark import BenchmarkResult
avg_result = (
session.query(func.avg(BenchmarkResult.processing_time))
.filter(
BenchmarkResult.benchmark_run_id == run.id,
BenchmarkResult.processing_time.isnot(None),
BenchmarkResult.processing_time > 0,
)
.scalar()
)
if avg_result:
avg_processing_time = float(avg_result)
except Exception:
logger.warning(
f"Error calculating avg processing time for run {run.id}"
)
# Calculate average search results and total search requests from metrics
total_search_requests = None
try:
from ...database.models import SearchCall
from ...metrics.search_tracker import (
SEARCH_STATUS_SKIPPED,
)
# Get all results for this run to find research_ids
results = (
session.query(BenchmarkResult)
.filter(BenchmarkResult.benchmark_run_id == run.id)
.all()
)
research_ids = [
r.research_id for r in results if r.research_id
]
if research_ids:
# SearchCall is in the same per-user DB, query directly
search_calls = (
session.query(SearchCall)
.filter(SearchCall.research_id.in_(research_ids))
.all()
)
# Group by research_id and calculate metrics per research session
research_results = {}
research_requests = {}
for call in search_calls:
# A search skipped because its engine was
# unavailable sent no request.
if call.success_status == SEARCH_STATUS_SKIPPED:
continue
if call.research_id:
if call.research_id not in research_results:
research_results[call.research_id] = 0
research_requests[call.research_id] = 0
research_results[call.research_id] += (
call.results_count or 0
)
research_requests[call.research_id] += 1
# Calculate averages across research sessions
if research_results:
total_results = sum(research_results.values())
avg_search_results = total_results / len(
research_results
)
total_requests = sum(research_requests.values())
total_search_requests = total_requests / len(
research_requests
)
except Exception:
logger.warning(
f"Error calculating search metrics for run {run.id}"
)
formatted_runs.append(
{
"id": run.id,
"run_name": run.run_name or f"Benchmark #{run.id}",
"created_at": run.created_at.isoformat(),
# `start_time` is the wall-clock instant work began
# (status flipped to IN_PROGRESS); used by the YAML
# download for `date_tested` instead of the (broken)
# download-time stamp. NULL on rows that never started.
"start_time": run.start_time.isoformat()
if run.start_time
else None,
# `ldr_version` is captured by start_benchmark; NULL
# on rows created before migration 0014. The YAML
# download substitutes "unknown (pre-0014 run)".
# `settings_snapshot` is intentionally NOT included
# here — it's ~184KB per run and would balloon the
# history list response. Loaded on-demand from
# /api/results/<id>/export when YAML is downloaded.
"ldr_version": run.ldr_version,
"total_examples": run.total_examples,
"completed_examples": run.completed_examples,
"overall_accuracy": run.overall_accuracy,
"status": run.status.value,
"search_config": run.search_config,
"evaluation_config": run.evaluation_config,
"datasets_config": run.datasets_config,
"avg_processing_time": avg_processing_time,
"avg_search_results": avg_search_results,
"total_search_requests": total_search_requests,
}
)
return {"success": True, "runs": formatted_runs}
except Exception:
logger.exception("Error getting benchmark history")
return JSONResponse(
{"success": False, "error": "An internal error has occurred."},
status_code=500,
)
@router.get("/api/results/{benchmark_run_id}")
@limiter.exempt
def get_benchmark_results(
request: Request,
benchmark_run_id: int,
username: Annotated[str, Depends(require_auth)],
):
"""Get detailed results for a benchmark run."""
try:
from ...database.models.benchmark import BenchmarkResult
from ...database.session_context import get_user_db_session
logger.info(f"Getting results for benchmark {benchmark_run_id}")
# First sync any pending results from active runs
benchmark_service.sync_pending_results(benchmark_run_id, username)
persistence_error = benchmark_service.get_result_persistence_error(
benchmark_run_id, username
)
with get_user_db_session(username) as session:
# Get recent results (limit to last 10)
try:
limit = int(request.query_params.get("limit", 10))
except (TypeError, ValueError):
limit = 10
limit = max(1, min(limit, 1000))
results = (
session.query(BenchmarkResult)
.filter(BenchmarkResult.benchmark_run_id == benchmark_run_id)
# Results are returned unfiltered, including rows whose
# evaluation is still pending — same as main's equivalent
# route (benchmarks/web_api/benchmark_routes.py), which also
# has no is_correct filter here. The is_correct.isnot(None)
# filter belongs to the aggregate/stats queries in
# benchmark_service.py, not to this listing.
.order_by(BenchmarkResult.id.desc()) # Most recent first
.limit(limit)
.all()
)
logger.info(f"Found {len(results)} results")
# Build a map of research_id to total search results
search_results_by_research_id = {}
try:
from ...database.models import SearchCall
# Get all unique research_ids from our results
research_ids = [r.research_id for r in results if r.research_id]
if research_ids:
# SearchCall is in the same per-user DB, query directly
all_search_calls = (
session.query(SearchCall)
.filter(SearchCall.research_id.in_(research_ids))
.all()
)
# Group search results by research_id
for call in all_search_calls:
if call.research_id:
if (
call.research_id
not in search_results_by_research_id
):
search_results_by_research_id[
call.research_id
] = 0
search_results_by_research_id[call.research_id] += (
call.results_count or 0
)
logger.info(
f"Found search metrics for {len(search_results_by_research_id)} research IDs from {len(all_search_calls)} total search calls"
)
logger.debug(
f"Research IDs from results: {research_ids[:5] if len(research_ids) > 5 else research_ids}"
)
logger.debug(
f"Search results by research_id: {dict(list(search_results_by_research_id.items())[:5])}"
)
except Exception:
logger.exception(
f"Error getting search metrics for benchmark {benchmark_run_id}"
)
# Format results for UI display
formatted_results = []
for result in results:
# Get search result count using research_id
search_result_count = 0
try:
if (
result.research_id
and result.research_id in search_results_by_research_id
):
search_result_count = search_results_by_research_id[
result.research_id
]
logger.debug(
f"Found {search_result_count} search results for research_id {result.research_id}"
)
except Exception:
logger.exception(
f"Error getting search results for result {result.example_id}"
)
formatted_results.append(
{
"example_id": result.example_id,
"dataset_type": result.dataset_type.value,
"question": result.question,
"correct_answer": result.correct_answer,
"model_answer": result.extracted_answer,
"full_response": result.response,
"is_correct": result.is_correct,
"confidence": result.confidence,
"grader_response": _safe_grader_response(
result.grader_response
),
"processing_time": result.processing_time,
"search_result_count": search_result_count,
"sources": result.sources,
"completed_at": result.completed_at.isoformat()
if result.completed_at
else None,
}
)
payload = {"success": True, "results": formatted_results}
if persistence_error:
payload["persistence_error"] = persistence_error
return payload
except Exception:
logger.exception("Error getting benchmark results")
return JSONResponse(
{"success": False, "error": "An internal error has occurred."},
status_code=500,
)
@router.get("/api/results/{benchmark_run_id}/export")
def export_benchmark_results(
request: Request,
benchmark_run_id: int,
username: Annotated[str, Depends(require_auth)],
):
"""Get lightweight results for YAML export plus run-level provenance.
Returns a `metadata` block alongside the per-result rows so the YAML
download path can stamp the version/settings that ran the benchmark
instead of falling back to download-time values. Pre-0014 rows return
NULL for the new metadata fields. The `success: True` envelope is
preserved so the existing JS contract (5 callsites in
benchmark_results.html) keeps working.
"""
try:
from sqlalchemy.orm import load_only
from ...database.models.benchmark import (
BenchmarkResult,
BenchmarkRun,
)
from ...database.session_context import get_user_db_session
# The ~184KB settings snapshot is only loaded/returned when the
# client opts in (Export -> "Include settings snapshot"); the
# default summary download skips it to avoid transferring a blob it
# would discard.
include_settings = request.query_params.get(
"include_settings", ""
).lower() in (
"1",
"true",
)
logger.info(
"Exporting benchmark results for run {} by user {}",
benchmark_run_id,
username,
)
with get_user_db_session(username) as session:
# Fetch only the provenance columns we need from BenchmarkRun.
# `load_only` keeps us off the heavy JSON config blobs (and the
# settings_snapshot blob unless it was requested).
run_columns = [
BenchmarkRun.ldr_version,
BenchmarkRun.start_time,
BenchmarkRun.created_at,
]
if include_settings:
run_columns.append(BenchmarkRun.settings_snapshot)
run = (
session.query(BenchmarkRun)
.options(load_only(*run_columns))
.filter(BenchmarkRun.id == benchmark_run_id)
.one_or_none()
)
results = (
session.query(BenchmarkResult)
.options(
load_only(
BenchmarkResult.example_id,
BenchmarkResult.dataset_type,
BenchmarkResult.question,
BenchmarkResult.correct_answer,
BenchmarkResult.extracted_answer,
BenchmarkResult.is_correct,
BenchmarkResult.confidence,
BenchmarkResult.processing_time,
BenchmarkResult.completed_at,
)
)
.filter(BenchmarkResult.benchmark_run_id == benchmark_run_id)
.order_by(BenchmarkResult.id.asc())
.all()
)
formatted = []
for r in results:
formatted.append(
{
"example_id": r.example_id,
"dataset_type": r.dataset_type.value,
"question": r.question,
"correct_answer": r.correct_answer,
"model_answer": r.extracted_answer,
"is_correct": r.is_correct,
"confidence": r.confidence,
"processing_time": r.processing_time,
"completed_at": r.completed_at.isoformat()
if r.completed_at
else None,
}
)
# Build metadata block. Each field is independently null-checked
# so a pre-0014 row, or an in-flight run with no start_time,
# serializes cleanly as JSON null.
if run is not None:
started_at = (
run.start_time.isoformat()
if run.start_time
else (
run.created_at.isoformat() if run.created_at else None
)
)
metadata = {
"ldr_version": run.ldr_version,
"started_at": started_at,
"settings_snapshot": (
run.settings_snapshot if include_settings else None
),
}
else:
metadata = {
"ldr_version": None,
"started_at": None,
"settings_snapshot": None,
}
logger.info(
"Exported {} results for benchmark run {}",
len(formatted),
benchmark_run_id,
)
return {
"success": True,
"metadata": metadata,
"results": formatted,
}
except Exception:
logger.exception("Error exporting benchmark results")
return JSONResponse(
{"success": False, "error": "An internal error has occurred."},
status_code=500,
)
@router.get("/api/configs")
def get_saved_configs(
request: Request, username: Annotated[str, Depends(require_auth)]
):
"""Get list of saved benchmark configurations."""
try:
# Returns built-in default configs (saved configs not yet in DB)
default_configs = [
{
"id": 1,
"name": "Quick Test",
"description": "Fast benchmark with minimal examples",
"search_config": {
"iterations": 3,
"questions_per_iteration": 3,
"search_tool": "searxng",
"search_strategy": "focused_iteration",
},
"datasets_config": {
"simpleqa": {"count": 10},
"browsecomp": {"count": 5},
},
},
{
"id": 2,
"name": "Standard Evaluation",
"description": "Comprehensive benchmark with standard settings",
"search_config": {
"iterations": 8,
"questions_per_iteration": 5,
"search_tool": "searxng",
"search_strategy": "focused_iteration",
},
"datasets_config": {
"simpleqa": {"count": 50},
"browsecomp": {"count": 25},
},
},
]
return {"success": True, "configs": default_configs}
except Exception:
logger.exception("Error getting saved configs")
return JSONResponse(
{"success": False, "error": "An internal error has occurred."},
status_code=500,
)
@router.post("/api/start-simple")
@_benchmark_start_limit
async def start_benchmark_simple(
request: Request, username: Annotated[str, Depends(require_auth)]
):
"""Start a benchmark using current database settings."""
data, body_error = await read_json_dict(
request, "simple", "Request body must be valid JSON"
)
if body_error is not None:
return body_error
session_id = request.session.get("session_id")
return await run_db_sync(
_start_benchmark_simple_sync, data, username, session_id
)
def _start_benchmark_simple_sync(data, username, session_id):
try:
datasets_config = data.get("datasets_config", {})
total_examples, datasets_error = _validate_datasets_config(
datasets_config
)
if datasets_error is not None:
return JSONResponse({"error": datasets_error}, status_code=400)
# Validate datasets
if total_examples == 0:
return JSONResponse(
{
"error": "At least one dataset with count > 0 must be specified"
},
status_code=400,
)
# Get current settings from database
# Try to get password from session store for background thread
from ...database.session_passwords import session_password_store
user_password = None
if session_id:
user_password = session_password_store.get_session_password(
username, session_id
)
with get_user_db_session(username, user_password) as session:
# For benchmarks, use a default test username
settings_manager = SettingsManager(session)
# Build search config from database settings
search_config = {
"iterations": int(
settings_manager.get_setting("search.iterations", 8)
),
"questions_per_iteration": int(
settings_manager.get_setting(
"search.questions_per_iteration", 5
)
),
"search_tool": settings_manager.get_setting(
"search.tool", "searxng"
),
"search_strategy": settings_manager.get_setting(
"search.search_strategy", "focused_iteration"
),
"model_name": settings_manager.get_setting("llm.model"),
"provider": settings_manager.get_setting("llm.provider"),
"temperature": float(
settings_manager.get_setting("llm.temperature", 0.7)
),
"max_tokens": settings_manager.get_setting(
"llm.max_tokens", 30000
),
"context_window_unrestricted": settings_manager.get_setting(
"llm.context_window_unrestricted", True
),
"context_window_size": settings_manager.get_setting(
"llm.context_window_size", 128000
),
"local_context_window_size": settings_manager.get_setting(
"llm.local_context_window_size", 8192
),
}
# Add provider-specific settings
provider = normalize_provider(search_config.get("provider"))
if provider == "openai_endpoint":
search_config["openai_endpoint_url"] = (
settings_manager.get_setting("llm.openai_endpoint.url")
)
elif provider == "openai":
search_config["openai_api_key"] = settings_manager.get_setting(
"llm.openai.api_key"
)
elif provider == "anthropic":
search_config["anthropic_api_key"] = (
settings_manager.get_setting("llm.anthropic.api_key")
)
# Read evaluation config from database settings
evaluation_provider = normalize_grader_provider(
settings_manager.get_setting(
"benchmark.evaluation.provider", "openai_endpoint"
)
)
evaluation_model = settings_manager.get_setting(
"benchmark.evaluation.model", "anthropic/claude-3.7-sonnet"
)
evaluation_temperature = float(
settings_manager.get_setting(
"benchmark.evaluation.temperature", 0
)
)
evaluation_config = {
"provider": evaluation_provider,
"model_name": evaluation_model,
"temperature": evaluation_temperature,
}
# Add provider-specific settings for evaluation
if evaluation_provider == "openai_endpoint":
evaluation_config["openai_endpoint_url"] = (
settings_manager.get_setting(
"benchmark.evaluation.endpoint_url",
"https://openrouter.ai/api/v1",
)
)
elif evaluation_provider == "openai":
evaluation_config["openai_api_key"] = (
settings_manager.get_setting("llm.openai.api_key")
)
elif evaluation_provider == "anthropic":
evaluation_config["anthropic_api_key"] = (
settings_manager.get_setting("llm.anthropic.api_key")
)
grader_key_error = _grader_key_error(
evaluation_config, settings_manager
)
if grader_key_error is not None:
return grader_key_error
# Create and start benchmark
benchmark_run_id = benchmark_service.create_benchmark_run(
run_name=f"Quick Benchmark - {data.get('run_name', '')}",
search_config=search_config,
evaluation_config=evaluation_config,
datasets_config=datasets_config,
username=username,
user_password=user_password,
)
success = benchmark_service.start_benchmark(
benchmark_run_id, username, user_password
)
if success:
return {
"success": True,
"benchmark_run_id": benchmark_run_id,
"message": "Benchmark started with current settings",
}
return JSONResponse(
{"success": False, "error": "Failed to start benchmark"},
status_code=500,
)
except Exception:
logger.exception("Error starting simple benchmark")
return JSONResponse(
{"success": False, "error": "An internal error has occurred."},
status_code=500,
)
@router.post("/api/validate-config")
async def validate_config(
request: Request, username: Annotated[str, Depends(require_auth)]
):
"""Validate a benchmark configuration.
Note: not using @require_json_body because this endpoint returns
{"valid": False, "errors": [...]} which doesn't match the decorator's
three standard error formats.
"""
# Parsed outside the try/except below: a malformed body must raise
# json.JSONDecodeError uncaught so it reaches the app's registered
# handler (-> 400), not the broad `except Exception` further down
# (which would otherwise turn it into a hardcoded 500).
data = await request.json()
if not isinstance(data, dict):
return {"valid": False, "errors": ["No data provided"]}
try:
errors = []
# Validate search config
search_config = data.get("search_config", {})
if not search_config.get("search_tool"):
errors.append("Search tool is required")
if not search_config.get("search_strategy"):
errors.append("Search strategy is required")
# Validate datasets config
datasets_config = data.get("datasets_config", {})
total_examples, datasets_error = _validate_datasets_config(
datasets_config
)
if datasets_error is not None:
errors.append(datasets_error)
else:
if not datasets_config:
errors.append("At least one dataset must be configured")
if total_examples == 0:
errors.append("Total examples must be greater than 0")
# No upper cap: #4080 deliberately removed the 1000-example limit so
# users can run large benchmarks (e.g. a full SimpleQA sweep).
return {
"valid": len(errors) == 0,
"errors": errors,
"total_examples": total_examples,
}
except Exception:
logger.exception("Error validating config")
return JSONResponse(
{"valid": False, "errors": ["An internal error has occurred."]},
status_code=500,
)
@router.get("/api/search-quality")
@limiter.exempt
def get_search_quality(
request: Request, username: Annotated[str, Depends(require_auth)]
):
"""Get current search quality metrics from rate limiting data."""
try:
from ...database.models import RateLimitEstimate
quality_stats = []
with get_user_db_session(username) as db_session:
estimates = db_session.query(RateLimitEstimate).all()
for est in estimates:
quality_stats.append(
{
"engine_type": est.engine_type,
"total_attempts": est.total_attempts,
"success_rate": round(est.success_rate * 100, 1),
"status": (
"EXCELLENT"
if est.success_rate >= 0.95
else "GOOD"
if est.success_rate >= 0.9
else "CAUTION"
if est.success_rate >= 0.75
else "WARNING"
if est.success_rate >= 0.5
else "CRITICAL"
),
}
)
return {
"success": True,
"search_quality": quality_stats,
"timestamp": time.time(),
}
except Exception:
logger.exception("Error getting search quality")
return JSONResponse(
{"success": False, "error": "An internal error has occurred."},
status_code=500,
)
@router.delete("/api/delete/{benchmark_run_id}")
def delete_benchmark_run(
request: Request,
benchmark_run_id: int,
username: Annotated[str, Depends(require_auth)],
):
"""Delete a benchmark run and all its results."""
try:
from ...database.models.benchmark import (
BenchmarkProgress,
BenchmarkResult,
BenchmarkRun,
)
from ...database.session_context import get_user_db_session
with get_user_db_session(username) as session:
# Check if benchmark run exists
benchmark_run = (
session.query(BenchmarkRun)
.filter(BenchmarkRun.id == benchmark_run_id)
.first()
)
if not benchmark_run:
return JSONResponse(
{"success": False, "error": "Benchmark run not found"},
status_code=404,
)
# Prevent deletion of running benchmarks
if benchmark_run.status.value == "in_progress":
return JSONResponse(
{
"success": False,
"error": "Cannot delete a running benchmark. Cancel it first.",
},
status_code=400,
)
# Delete related records (cascade should handle this, but being explicit)
session.query(BenchmarkResult).filter(
BenchmarkResult.benchmark_run_id == benchmark_run_id
).delete()
session.query(BenchmarkProgress).filter(
BenchmarkProgress.benchmark_run_id == benchmark_run_id
).delete()
# Delete the benchmark run
session.delete(benchmark_run)
session.commit()
logger.info(f"Deleted benchmark run {benchmark_run_id}")
return {
"success": True,
"message": f"Benchmark run {benchmark_run_id} deleted successfully",
}
except Exception:
logger.exception(f"Error deleting benchmark run {benchmark_run_id}")
return JSONResponse(
{"success": False, "error": "An internal error has occurred."},
status_code=500,
)