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.

18.0 KB
Raw Blame
{% extends "base.html" %}

{% set active_page = 'benchmark' %}

{% block title %}Benchmark - Deep Research System{% endblock %}

{% block extra_head %}
<meta name="csrf-token" content="{{ csrf_token() }}">
<style nonce="{{ csp_nonce }}">
.ldr-benchmark-card {
    max-width: 700px;
    margin: 0 auto;
}

.ldr-current-config {
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 20px;
    margin-bottom: 20px;
}

.ldr-config-item {
    display: flex;
    justify-content: space-between;
    padding: 8px 0;
    border-bottom: 1px solid var(--border-color);
}

.ldr-config-item:last-child {
    border-bottom: none;
}

.ldr-config-label {
    font-weight: 500;
    color: var(--text-muted);
}

.ldr-config-value {
    font-weight: 600;
    color: var(--primary-color);
}

.ldr-dataset-selection {
    margin: 20px 0;
}

.ldr-dataset-option {
    display: flex;
    align-items: center;
    padding: 15px;
    margin: 10px 0;
    background: var(--card-bg);
    border: 2px solid var(--border-color);
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s;
}

.ldr-dataset-option:hover {
    border-color: var(--primary-color);
}

.ldr-dataset-option.ldr-selected {
    border-color: var(--primary-color);
    background: rgba(var(--accent-primary-rgb), 0.1);
}

.ldr-dataset-option input[type="number"] {
    width: 80px;
    margin-left: auto;
}

.ldr-start-button {
    width: 100%;
    padding: 15px;
    font-size: 1.1rem;
}

.ldr-progress-section {
    display: none;
    margin-top: 30px;
}

.ldr-progress-bar {
    width: 100%;
    height: 30px;
    background: var(--bg-secondary);
    border-radius: 15px;
    overflow: hidden;
    margin: 20px 0;
}

.ldr-progress-fill {
    height: 100%;
    background: linear-gradient(90deg, var(--primary-color), var(--accent-secondary));
    width: 0%;
    transition: width 0.3s ease;
}

.ldr-stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 15px;
    margin-top: 20px;
}

.ldr-stat-card {
    padding: 15px;
    background: var(--bg-secondary);
    border-radius: 8px;
    text-align: center;
}

.ldr-stat-value {
    font-size: 1.8rem;
    font-weight: bold;
    color: var(--primary-color);
}

.ldr-stat-label {
    font-size: 0.9rem;
    color: var(--text-muted);
    margin-top: 5px;
}
</style>
{% endblock %}

{% block content %}
<div class="ldr-page active" id="benchmark">
    <div class="ldr-page-header">
        <h1>Benchmark Your Configuration</h1>
        <p class="ldr-page-subtitle">Test your current settings against standard datasets</p>
    </div>

    <div id="benchmark-alert" class="ldr-settings-alert-container" role="alert" style="display:none"></div>

    <div class="ldr-card ldr-benchmark-card">
        <div class="ldr-card-content">
            <!-- Current Configuration Display -->
            <div class="ldr-current-config">
                <h3>Current Configuration</h3>
                <div id="config-display">
                    <div class="ldr-config-item">
                        <span class="ldr-config-label">Loading...</span>
                        <span class="ldr-config-value"></span>
                    </div>
                </div>
            </div>

            <!-- Dataset Selection -->
            <div class="ldr-dataset-selection">
                <h3>Select Datasets to Test</h3>

                <div class="ldr-dataset-option ldr-selected" data-dataset="simpleqa">
                    <div>
                        <h4>SimpleQA</h4>
                        <p>Fact-based questions with clear answers</p>
                    </div>
                    <input type="number" id="simpleqa_count" value="20" min="1">
                </div>

                <div class="ldr-dataset-option ldr-selected" data-dataset="browsecomp">
                    <div>
                        <h4>BrowseComp</h4>
                        <p>Complex browsing and comparison tasks</p>
                    </div>
                    <input type="number" id="browsecomp_count" value="10" min="1">
                </div>
            </div>

            <!-- Start Button -->
            <button type="button" id="start-benchmark" class="btn btn-primary ldr-start-button">
                <i aria-hidden="true" class="fas fa-play"></i> Start Benchmark
            </button>

            <!-- Progress Section -->
            <div class="ldr-progress-section" id="progress-section">
                <h3>Benchmark Progress</h3>

                <div class="ldr-progress-bar">
                    <div class="ldr-progress-fill" id="progress-fill"></div>
                </div>

                <div class="ldr-stats-grid">
                    <div class="ldr-stat-card">
                        <div class="ldr-stat-value" id="stat-accuracy">--%</div>
                        <div class="ldr-stat-label">Accuracy</div>
                    </div>
                    <div class="ldr-stat-card">
                        <div class="ldr-stat-value" id="stat-completed">0</div>
                        <div class="ldr-stat-label">Completed</div>
                    </div>
                    <div class="ldr-stat-card">
                        <div class="ldr-stat-value" id="stat-rate">--</div>
                        <div class="ldr-stat-label">Per Minute</div>
                    </div>
                    <div class="ldr-stat-card">
                        <div class="ldr-stat-value" id="stat-remaining">--</div>
                        <div class="ldr-stat-label">Time Left</div>
                    </div>
                </div>

                <div style="margin-top: 20px; text-align: center;">
                    <button type="button" id="cancel-benchmark" class="btn btn-secondary">
                        <i aria-hidden="true" class="fas fa-stop"></i> Cancel
                    </button>
                </div>
            </div>
        </div>
    </div>
</div>

<script nonce="{{ csp_nonce }}">
let currentBenchmarkId = null;
let progressInterval = null;
let progressRunGeneration = 0;
let progressStatusRequestId = 0;
let latestAppliedProgressStatusRequestId = 0;
let terminalProgressRunGeneration = null;

document.addEventListener('DOMContentLoaded', function() {
    loadCurrentConfig();
    setupEventListeners();
});

async function loadCurrentConfig() {
    try {
        // Get current settings from the database. Canonical bulk-settings
        // endpoint is /settings/api (settings router prefix /settings,
        // route /api). The earlier /research/settings/api was a Flask-era
        // guess and 404s — the config card silently rendered "Not
        // configured" for every value.
        const response = await fetch('/settings/api');
        const data = await response.json();

        if (data.settings) {
            const config = {
                provider: data.settings['llm.provider']?.value || 'Not configured',
                model: data.settings['llm.model']?.value || 'Not configured',
                search_tool: data.settings['search.tool']?.value || 'searxng',
                iterations: data.settings['search.iterations']?.value || '8',
                questions_per_iteration: data.settings['search.questions_per_iteration']?.value || '5',
                search_strategy: data.settings['search.search_strategy']?.value || 'focused_iteration'
            };
            displayConfig(config);
        }
    } catch (error) {
        console.error('Error loading config:', error);
        // Use fallback display
        displayConfig({
            provider: 'Loading...',
            model: 'Loading...',
            search_tool: 'Loading...',
            iterations: '-'
        });
    }
}

function displayConfig(config) {
    const configDisplay = document.getElementById('config-display');
    configDisplay.innerHTML = `
        <div class="ldr-config-item">
            <span class="ldr-config-label">LLM Provider</span>
            <span class="ldr-config-value">${escapeHtml(config.provider || 'Not set')}</span>
        </div>
        <div class="ldr-config-item">
            <span class="ldr-config-label">Model</span>
            <span class="ldr-config-value">${escapeHtml(config.model || 'Not set')}</span>
        </div>
        <div class="ldr-config-item">
            <span class="ldr-config-label">Search Engine</span>
            <span class="ldr-config-value">${escapeHtml(config.search_tool || 'searxng')}</span>
        </div>
        <div class="ldr-config-item">
            <span class="ldr-config-label">Search Iterations</span>
            <span class="ldr-config-value">${escapeHtml(config.iterations || '8')}</span>
        </div>
    `;
}

function setupEventListeners() {
    // Dataset selection
    document.querySelectorAll('.ldr-dataset-option').forEach(option => {
        option.addEventListener('click', function(e) {
            if (!e.target.matches('input')) {
                this.classList.toggle('ldr-selected');
            }
        });
    });

    // Start benchmark
    document.getElementById('start-benchmark').addEventListener('click', startBenchmark);

    // Cancel benchmark
    document.getElementById('cancel-benchmark').addEventListener('click', cancelBenchmark);
}

async function startBenchmark() {
    // Get dataset configuration
    const datasets = {};
    document.querySelectorAll('.ldr-dataset-option.ldr-selected').forEach(option => {
        const dataset = option.getAttribute('data-dataset');
        const count = parseInt(option.querySelector('input').value) || 0;
        if (count > 0) {
            datasets[dataset] = { count: count };
        }
    });

    if (Object.keys(datasets).length === 0) {
        showAlert('Please select at least one dataset', 'error');
        return;
    }

    // Disable start button
    const startButton = document.getElementById('start-benchmark');
    if (startButton.disabled) return;
    startButton.disabled = true;

    try {
        // Get CSRF token
        const csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content');

        const response = await fetch('/benchmark/api/start-simple', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                'X-CSRFToken': csrfToken
            },
            body: JSON.stringify({ datasets_config: datasets })
        });

        let data = {};
        try {
            data = await response.json();
        } catch (error) {
            if (response.ok) throw error;
        }

        if (!response.ok) {
            const payloadError = data.error ?? data.detail ?? data.message;
            let errorMessage = `HTTP ${response.status}`;
            if (typeof payloadError === 'string' && payloadError.trim()) {
                errorMessage = payloadError;
            } else if (payloadError !== undefined && payloadError !== null) {
                errorMessage = JSON.stringify(payloadError);
            }
            throw new Error(errorMessage);
        }

        const runId = data.benchmark_run_id;
        const hasRunId = (
            (typeof runId === 'string' && runId.trim() !== '') ||
            (typeof runId === 'number' && Number.isFinite(runId))
        );
        if (data.success && hasRunId) {
            currentBenchmarkId = runId;
            showProgress();
            startProgressTracking();
            showAlert('Benchmark started!', 'success');
        } else {
            const payloadError = data.error ?? data.detail ?? data.message;
            let errorMessage = 'response did not include a benchmark run ID';
            if (typeof payloadError === 'string' && payloadError.trim()) {
                errorMessage = payloadError;
            } else if (payloadError !== undefined && payloadError !== null) {
                errorMessage = JSON.stringify(payloadError);
            }
            showAlert('Error: ' + errorMessage, 'error');
            startButton.disabled = false;
        }
    } catch (error) {
        console.error('Error starting benchmark:', error);
        showAlert('Failed to start benchmark: ' + error.message, 'error');
        startButton.disabled = false;
    }
}

function showProgress() {
    document.querySelector('.ldr-dataset-selection').style.display = 'none';
    document.getElementById('start-benchmark').style.display = 'none';
    document.getElementById('progress-section').style.display = 'block';
}

function hideProgress() {
    document.querySelector('.ldr-dataset-selection').style.display = 'block';
    document.getElementById('start-benchmark').style.display = 'block';
    document.getElementById('progress-section').style.display = 'none';
    document.getElementById('start-benchmark').disabled = false;
}

function startProgressTracking() {
    progressRunGeneration++;
    progressStatusRequestId++;
    if (progressInterval) {
        clearInterval(progressInterval);
        progressInterval = null;
    }
    const initialUpdate = updateProgress(); // Initial update
    progressInterval = setInterval(updateProgress, 2000);
    return initialUpdate;
}

function formatBenchmarkAccuracy(value) {
    return typeof value === 'number' && Number.isFinite(value)
        ? value.toFixed(1) + '%'
        : 'N/A';
}

async function updateProgress() {
    const trackedBenchmarkId = currentBenchmarkId;
    const trackingGeneration = progressRunGeneration;
    const requestId = ++progressStatusRequestId;
    if (!trackedBenchmarkId) return;

    try {
        const response = await fetch(`/benchmark/api/status/${trackedBenchmarkId}`);
        const data = await response.json();
        const terminalSnapshot = Boolean(
            data?.success &&
            ['completed', 'failed', 'cancelled'].includes(data.status?.status)
        );

        if (
            trackingGeneration !== progressRunGeneration ||
            String(currentBenchmarkId) !== String(trackedBenchmarkId) ||
            terminalProgressRunGeneration === trackingGeneration ||
            (requestId < latestAppliedProgressStatusRequestId && !terminalSnapshot)
        ) return;

        if (data.success) {
            const status = data.status;
            latestAppliedProgressStatusRequestId = Math.max(
                latestAppliedProgressStatusRequestId, requestId
            );

            // Update progress bar
            const percentage = status.total_examples > 0 ?
                (status.completed_examples / status.total_examples * 100) : 0;
            document.getElementById('progress-fill').style.width = percentage + '%';

            // Update stats
            document.getElementById('stat-accuracy').textContent =
                formatBenchmarkAccuracy(status.overall_accuracy);
            document.getElementById('stat-completed').textContent =
                `${status.completed_examples}/${status.total_examples}`;
            document.getElementById('stat-rate').textContent =
                status.processing_rate ? status.processing_rate.toFixed(1) : '--';

            // Estimate remaining time
            if (status.processing_rate > 0 && status.completed_examples < status.total_examples) {
                const remaining = status.total_examples - status.completed_examples;
                const minutes = Math.ceil(remaining / status.processing_rate);
                document.getElementById('stat-remaining').textContent = `${minutes}m`;
            } else {
                document.getElementById('stat-remaining').textContent = '--';
            }

            // Check if completed
            if (status.status === 'completed' || status.status === 'failed' || status.status === 'cancelled') {
                terminalProgressRunGeneration = trackingGeneration;
                progressStatusRequestId++;
                clearInterval(progressInterval);
                progressInterval = null;

                if (status.status === 'completed') {
                    showAlert(
                        `Benchmark completed! Final accuracy: ${formatBenchmarkAccuracy(status.overall_accuracy)}`,
                        'success'
                    );
                } else {
                    showAlert(`Benchmark ${status.status}`, 'error');
                }

                setTimeout(() => {
                    if (
                        trackingGeneration !== progressRunGeneration ||
                        String(currentBenchmarkId) !== String(trackedBenchmarkId)
                    ) return;
                    hideProgress();
                    currentBenchmarkId = null;
                    progressRunGeneration++;
                }, 3000);
            }
        }
    } catch (error) {
        if (
            trackingGeneration !== progressRunGeneration ||
            String(currentBenchmarkId) !== String(trackedBenchmarkId) ||
            requestId !== progressStatusRequestId
        ) return;
        console.error('Error updating progress:', error);
    }
}

async function cancelBenchmark() {
    if (!currentBenchmarkId) return;

    try {
        const csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
        const response = await fetch(`/benchmark/api/cancel/${currentBenchmarkId}`, {
            method: 'POST',
            headers: {
                'X-CSRFToken': csrfToken
            }
        });

        const data = await response.json();
        if (data.success) {
            showAlert('Benchmark cancelled', 'info');
        }
    } catch (error) {
        console.error('Error cancelling benchmark:', error);
    }
}

function showAlert(message, type) {
    const alertContainer = document.getElementById('benchmark-alert');
    alertContainer.innerHTML = `
        <div class="ldr-settings-alert alert-${escapeHtml(type === 'error' ? 'danger' : type)}">
            <span>${escapeHtml(message)}</span>
            <button type="button" class="ldr-close-alert" data-ldr-onclick="ldr-20a43efc0120" data-ldr-template-token="{{ template_event_token }}">
                <i aria-hidden="true" class="fas fa-times"></i>
            </button>
        </div>
    `;
    alertContainer.style.display = 'block';

    if (type === 'success') {
        setTimeout(() => {
            alertContainer.style.display = 'none';
        }, 5000);
    }
}
</script>
{% endblock %}