feat: Oberfläche neu, Modell-Adapter wählbar, Bewertung je Bereich
Die erste Fassung der Oberfläche war inhaltlich und optisch schwach. Neu: drei Reiter (Benchmark, Ergebnisse, Einstellungen). Modelle werden in den Einstellungen mit Adapter (Ollama oder OpenAI-kompatibel), URL, Modell-ID und Schlüssel-Variable angelegt, mit Verbindungstest. config.toml speichert die Liste. Bewertung absolut in Prozent: ein Wert je Bereich A bis D und eine Gesamtnote, gewichtet nach weight. Batch-Verlauf, Schritt-Ansicht, Erklärung der Bewertung. 55 Unit-Tests grün, svelte-check ohne Fehler. Im Browser mit Beispieldaten geprüft, nicht in der echten App. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@ -1,21 +1,45 @@
|
||||
//! Rangliste und Matrix aus den Läufen eines Batches (F-09, SPEC Abschnitt 9).
|
||||
//! Bewertung je Modell und Bereich (F-09, SPEC Abschnitt 9).
|
||||
//!
|
||||
//! Gesamtscore je Modell: gewichteter Mittelwert der Aufgaben-Scores (`weight` aus
|
||||
//! `task.toml`). Erfolgsquote je (Modell, Aufgabe): Anteil der Läufe mit Klasse `ok`.
|
||||
//! Tie-Breaker: Median der Dauer, dann mittlere Completion-Tokens. Wilson-Intervalle
|
||||
//! und Kategorie-Ranking folgen mit F-12 (Phase 2).
|
||||
//! Basis ist eine absolute Bewertung, kein Vergleich gegen ein Referenzmodell:
|
||||
//! - Wert je Bereich (Gruppe A bis D): gewichteter Mittelwert der Aufgaben im Bereich in Prozent.
|
||||
//! Je Aufgabe zählt der mittlere Score über die Wiederholungen.
|
||||
//! - Gesamtnote: gewichteter Mittelwert über alle Aufgaben des Batches in Prozent.
|
||||
//! - Gewichte kommen aus `weight` in `task.toml`.
|
||||
//! - Bereiche ohne Läufe eines Modells bleiben leer und zählen nicht mit.
|
||||
//! - Reihenfolge: Gesamtnote absteigend, dann Median der Dauer, dann mittlere Completion-Tokens.
|
||||
|
||||
use crate::agent::ErrorClass;
|
||||
use crate::store::RunRecord;
|
||||
use serde::Serialize;
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
/// Gruppe und Gewicht einer Aufgabe.
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct TaskInfo {
|
||||
pub group: String,
|
||||
pub weight: f64,
|
||||
}
|
||||
|
||||
/// Wert eines Modells in einem Bereich.
|
||||
#[derive(Debug, Clone, PartialEq, Serialize)]
|
||||
pub struct GroupScore {
|
||||
pub group: String,
|
||||
/// Prozent von 0 bis 100. `None`, wenn das Modell im Bereich keine Läufe hat.
|
||||
pub percent: Option<f64>,
|
||||
/// Anzahl Aufgaben, die in den Wert eingehen.
|
||||
pub tasks: usize,
|
||||
pub runs: usize,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize)]
|
||||
pub struct ModelRank {
|
||||
pub rank: usize,
|
||||
pub model: String,
|
||||
/// Gewichteter Mittelwert der Aufgaben-Scores, 0.0 bis 1.0.
|
||||
pub total_score: f64,
|
||||
/// Gesamtnote in Prozent von 0 bis 100.
|
||||
pub total_percent: f64,
|
||||
pub groups: Vec<GroupScore>,
|
||||
/// Bereich mit dem niedrigsten Wert, nur wenn das Modell mindestens zwei Bereiche hat.
|
||||
pub weakest_group: Option<String>,
|
||||
pub runs: usize,
|
||||
pub successes: usize,
|
||||
pub median_duration_ms: u128,
|
||||
@ -28,24 +52,53 @@ pub struct MatrixCell {
|
||||
pub task_id: String,
|
||||
pub runs: usize,
|
||||
pub successes: usize,
|
||||
/// Mittlerer Score über die Wiederholungen.
|
||||
/// Mittlerer Score über die Wiederholungen, 0.0 bis 1.0.
|
||||
pub mean_score: f64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize)]
|
||||
pub struct TaskColumn {
|
||||
pub id: String,
|
||||
pub group: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize)]
|
||||
pub struct Ranking {
|
||||
pub batch_id: i64,
|
||||
/// Bereiche, die im Batch vorkommen, sortiert.
|
||||
pub groups: Vec<String>,
|
||||
pub models: Vec<ModelRank>,
|
||||
pub cells: Vec<MatrixCell>,
|
||||
pub tasks: Vec<String>,
|
||||
/// Aufgaben des Batches, sortiert nach Bereich und ID.
|
||||
pub tasks: Vec<TaskColumn>,
|
||||
}
|
||||
|
||||
/// Berechnet Rangliste und Matrix. `weights` ordnet jeder Aufgaben-ID ihr Gewicht zu;
|
||||
/// fehlt eine ID, zählt das Gewicht 1.0.
|
||||
pub fn compute(batch_id: i64, runs: &[RunRecord], weights: &BTreeMap<String, f64>) -> Ranking {
|
||||
let weight_of = |task: &str| weights.get(task).copied().unwrap_or(1.0);
|
||||
const UNKNOWN_GROUP: &str = "?";
|
||||
|
||||
/// Berechnet Bewertung und Matrix. Unbekannte Aufgaben zählen mit Gewicht 1.0 im Bereich `?`.
|
||||
pub fn compute(batch_id: i64, runs: &[RunRecord], tasks: &BTreeMap<String, TaskInfo>) -> Ranking {
|
||||
let info_of = |task: &str| -> TaskInfo {
|
||||
tasks.get(task).cloned().unwrap_or(TaskInfo {
|
||||
group: UNKNOWN_GROUP.into(),
|
||||
weight: 1.0,
|
||||
})
|
||||
};
|
||||
|
||||
// Aufgaben des Batches mit Bereich.
|
||||
let mut task_cols: BTreeMap<String, String> = BTreeMap::new();
|
||||
for r in runs {
|
||||
task_cols
|
||||
.entry(r.task_id.clone())
|
||||
.or_insert_with(|| info_of(&r.task_id).group);
|
||||
}
|
||||
let mut task_list: Vec<TaskColumn> = task_cols
|
||||
.iter()
|
||||
.map(|(id, group)| TaskColumn { id: id.clone(), group: group.clone() })
|
||||
.collect();
|
||||
task_list.sort_by(|a, b| a.group.cmp(&b.group).then(a.id.cmp(&b.id)));
|
||||
let mut groups: Vec<String> = task_list.iter().map(|t| t.group.clone()).collect();
|
||||
groups.dedup();
|
||||
|
||||
// Pro Modell: Liste der Läufe.
|
||||
let mut by_model: BTreeMap<&str, Vec<&RunRecord>> = BTreeMap::new();
|
||||
for r in runs {
|
||||
by_model.entry(r.model.as_str()).or_default().push(r);
|
||||
@ -54,44 +107,81 @@ pub fn compute(batch_id: i64, runs: &[RunRecord], weights: &BTreeMap<String, f64
|
||||
let mut models: Vec<ModelRank> = by_model
|
||||
.iter()
|
||||
.map(|(model, rs)| {
|
||||
// Mittelwert je Aufgabe, dann gewichtet über die Aufgaben.
|
||||
// Mittel je Aufgabe über die Wiederholungen.
|
||||
let mut per_task: BTreeMap<&str, (f64, usize)> = BTreeMap::new();
|
||||
for r in rs {
|
||||
let e = per_task.entry(r.task_id.as_str()).or_insert((0.0, 0));
|
||||
e.0 += r.score;
|
||||
e.1 += 1;
|
||||
}
|
||||
let (mut wsum, mut wtotal) = (0.0, 0.0);
|
||||
// Je Bereich: gewichtete Summe, Gewicht, Aufgaben, Läufe.
|
||||
let mut per_group: BTreeMap<String, (f64, f64, usize, usize)> = BTreeMap::new();
|
||||
let (mut total_sum, mut total_w) = (0.0, 0.0);
|
||||
for (task, (sum, n)) in &per_task {
|
||||
let w = weight_of(task);
|
||||
wsum += w * (sum / *n as f64);
|
||||
wtotal += w;
|
||||
let info = info_of(task);
|
||||
let mean = sum / *n as f64;
|
||||
let g = per_group.entry(info.group.clone()).or_insert((0.0, 0.0, 0, 0));
|
||||
g.0 += info.weight * mean;
|
||||
g.1 += info.weight;
|
||||
g.2 += 1;
|
||||
g.3 += *n;
|
||||
total_sum += info.weight * mean;
|
||||
total_w += info.weight;
|
||||
}
|
||||
let total_score = if wtotal > 0.0 { wsum / wtotal } else { 0.0 };
|
||||
let total_percent = if total_w > 0.0 { 100.0 * total_sum / total_w } else { 0.0 };
|
||||
|
||||
let group_scores: Vec<GroupScore> = groups
|
||||
.iter()
|
||||
.map(|g| match per_group.get(g) {
|
||||
Some((sum, w, tasks_n, runs_n)) if *w > 0.0 => GroupScore {
|
||||
group: g.clone(),
|
||||
percent: Some(100.0 * sum / w),
|
||||
tasks: *tasks_n,
|
||||
runs: *runs_n,
|
||||
},
|
||||
_ => GroupScore { group: g.clone(), percent: None, tasks: 0, runs: 0 },
|
||||
})
|
||||
.collect();
|
||||
|
||||
let weakest_group = {
|
||||
let filled: Vec<&GroupScore> =
|
||||
group_scores.iter().filter(|g| g.percent.is_some()).collect();
|
||||
if filled.len() >= 2 {
|
||||
filled
|
||||
.iter()
|
||||
.min_by(|a, b| {
|
||||
a.percent
|
||||
.partial_cmp(&b.percent)
|
||||
.unwrap_or(std::cmp::Ordering::Equal)
|
||||
})
|
||||
.map(|g| g.group.clone())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
};
|
||||
|
||||
let mut durations: Vec<u128> = rs.iter().map(|r| r.duration_ms).collect();
|
||||
durations.sort_unstable();
|
||||
let median = median_u128(&durations);
|
||||
|
||||
let mean_tokens = rs.iter().map(|r| r.completion_tokens as f64).sum::<f64>() / rs.len() as f64;
|
||||
let successes = rs.iter().filter(|r| r.error_class == ErrorClass::Ok).count();
|
||||
let mean_tokens =
|
||||
rs.iter().map(|r| r.completion_tokens as f64).sum::<f64>() / rs.len() as f64;
|
||||
|
||||
ModelRank {
|
||||
rank: 0,
|
||||
model: model.to_string(),
|
||||
total_score,
|
||||
total_percent,
|
||||
groups: group_scores,
|
||||
weakest_group,
|
||||
runs: rs.len(),
|
||||
successes,
|
||||
median_duration_ms: median,
|
||||
successes: rs.iter().filter(|r| r.error_class == ErrorClass::Ok).count(),
|
||||
median_duration_ms: median_u128(&durations),
|
||||
mean_completion_tokens: mean_tokens,
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
// Absteigend nach Score, dann Median-Dauer, dann mittlere Completion-Tokens.
|
||||
models.sort_by(|a, b| {
|
||||
b.total_score
|
||||
.partial_cmp(&a.total_score)
|
||||
b.total_percent
|
||||
.partial_cmp(&a.total_percent)
|
||||
.unwrap_or(std::cmp::Ordering::Equal)
|
||||
.then(a.median_duration_ms.cmp(&b.median_duration_ms))
|
||||
.then(
|
||||
@ -104,15 +194,12 @@ pub fn compute(batch_id: i64, runs: &[RunRecord], weights: &BTreeMap<String, f64
|
||||
m.rank = i + 1;
|
||||
}
|
||||
|
||||
// Matrix Modell × Aufgabe.
|
||||
let mut cell_map: BTreeMap<(&str, &str), Vec<&RunRecord>> = BTreeMap::new();
|
||||
let mut task_set: BTreeMap<&str, ()> = BTreeMap::new();
|
||||
for r in runs {
|
||||
cell_map
|
||||
.entry((r.model.as_str(), r.task_id.as_str()))
|
||||
.or_default()
|
||||
.push(r);
|
||||
task_set.insert(r.task_id.as_str(), ());
|
||||
}
|
||||
let cells = cell_map
|
||||
.iter()
|
||||
@ -125,12 +212,7 @@ pub fn compute(batch_id: i64, runs: &[RunRecord], weights: &BTreeMap<String, f64
|
||||
})
|
||||
.collect();
|
||||
|
||||
Ranking {
|
||||
batch_id,
|
||||
models,
|
||||
cells,
|
||||
tasks: task_set.keys().map(|t| t.to_string()).collect(),
|
||||
}
|
||||
Ranking { batch_id, groups, models, cells, tasks: task_list }
|
||||
}
|
||||
|
||||
fn median_u128(sorted: &[u128]) -> u128 {
|
||||
@ -165,48 +247,101 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gewichteter_score_und_rang() {
|
||||
let runs = vec![
|
||||
run("A", "t1", 1.0, ErrorClass::Ok, 1000, 10),
|
||||
run("A", "t2", 0.0, ErrorClass::CheckFailed, 1000, 10),
|
||||
run("B", "t1", 1.0, ErrorClass::Ok, 1000, 10),
|
||||
run("B", "t2", 1.0, ErrorClass::Ok, 1000, 10),
|
||||
];
|
||||
let mut w = BTreeMap::new();
|
||||
w.insert("t1".to_string(), 1.0);
|
||||
w.insert("t2".to_string(), 3.0);
|
||||
let r = compute(1, &runs, &w);
|
||||
assert_eq!(r.models[0].model, "B");
|
||||
assert_eq!(r.models[0].rank, 1);
|
||||
assert!((r.models[0].total_score - 1.0).abs() < 1e-9);
|
||||
// A: (1*1 + 3*0) / 4 = 0.25
|
||||
assert!((r.models[1].total_score - 0.25).abs() < 1e-9);
|
||||
fn infos(list: &[(&str, &str, f64)]) -> BTreeMap<String, TaskInfo> {
|
||||
list.iter()
|
||||
.map(|(id, g, w)| (id.to_string(), TaskInfo { group: g.to_string(), weight: *w }))
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gleichstand_nach_median_dauer() {
|
||||
fn prozent_je_bereich_und_gesamt() {
|
||||
// A: a1 (Gewicht 1), B: b1 (Gewicht 3).
|
||||
let tasks = infos(&[("a1", "A", 1.0), ("b1", "B", 3.0)]);
|
||||
let runs = vec![
|
||||
run("slow", "t1", 1.0, ErrorClass::Ok, 9000, 10),
|
||||
run("fast", "t1", 1.0, ErrorClass::Ok, 2000, 10),
|
||||
run("m", "a1", 1.0, ErrorClass::Ok, 1000, 10),
|
||||
run("m", "b1", 0.0, ErrorClass::CheckFailed, 1000, 10),
|
||||
];
|
||||
let r = compute(1, &runs, &BTreeMap::new());
|
||||
assert_eq!(r.models[0].model, "fast");
|
||||
let r = compute(1, &runs, &tasks);
|
||||
let m = &r.models[0];
|
||||
assert_eq!(r.groups, vec!["A".to_string(), "B".to_string()]);
|
||||
assert_eq!(m.groups[0].percent, Some(100.0));
|
||||
assert_eq!(m.groups[1].percent, Some(0.0));
|
||||
// Gesamt: (1*1 + 3*0) / 4 = 25 %
|
||||
assert!((m.total_percent - 25.0).abs() < 1e-9);
|
||||
assert_eq!(m.weakest_group.as_deref(), Some("B"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn matrix_zaehlt_erfolge_und_mittelwert() {
|
||||
fn wiederholungen_werden_je_aufgabe_gemittelt() {
|
||||
let tasks = infos(&[("a1", "A", 1.0), ("a2", "A", 1.0)]);
|
||||
let runs = vec![
|
||||
run("A", "t1", 1.0, ErrorClass::Ok, 1, 1),
|
||||
run("A", "t1", 0.5, ErrorClass::CheckFailed, 1, 1),
|
||||
run("A", "t1", 0.0, ErrorClass::MaxSteps, 1, 1),
|
||||
run("m", "a1", 1.0, ErrorClass::Ok, 1, 1),
|
||||
run("m", "a1", 0.0, ErrorClass::CheckFailed, 1, 1),
|
||||
run("m", "a2", 1.0, ErrorClass::Ok, 1, 1),
|
||||
];
|
||||
let r = compute(1, &runs, &BTreeMap::new());
|
||||
assert_eq!(r.cells.len(), 1);
|
||||
assert_eq!(r.cells[0].runs, 3);
|
||||
assert_eq!(r.cells[0].successes, 1);
|
||||
assert!((r.cells[0].mean_score - 0.5).abs() < 1e-9);
|
||||
assert_eq!(r.tasks, vec!["t1".to_string()]);
|
||||
let r = compute(1, &runs, &tasks);
|
||||
// a1: 50 %, a2: 100 % -> Bereich A 75 %. Jede Aufgabe zählt gleich viel, egal wie oft sie lief.
|
||||
assert!((r.models[0].groups[0].percent.unwrap() - 75.0).abs() < 1e-9);
|
||||
assert_eq!(r.models[0].groups[0].tasks, 2);
|
||||
assert_eq!(r.models[0].groups[0].runs, 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fehlender_bereich_bleibt_leer_und_zaehlt_nicht() {
|
||||
let tasks = infos(&[("a1", "A", 1.0), ("b1", "B", 1.0)]);
|
||||
let runs = vec![
|
||||
run("x", "a1", 1.0, ErrorClass::Ok, 1, 1),
|
||||
run("y", "a1", 1.0, ErrorClass::Ok, 1, 1),
|
||||
run("y", "b1", 1.0, ErrorClass::Ok, 1, 1),
|
||||
];
|
||||
let r = compute(1, &runs, &tasks);
|
||||
let x = r.models.iter().find(|m| m.model == "x").unwrap();
|
||||
assert_eq!(x.groups[1].percent, None);
|
||||
assert_eq!(x.weakest_group, None);
|
||||
assert!((x.total_percent - 100.0).abs() < 1e-9);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rang_nach_gesamt_dann_dauer() {
|
||||
let tasks = infos(&[("a1", "A", 1.0)]);
|
||||
let runs = vec![
|
||||
run("slow", "a1", 1.0, ErrorClass::Ok, 9000, 10),
|
||||
run("fast", "a1", 1.0, ErrorClass::Ok, 2000, 10),
|
||||
run("schlecht", "a1", 0.0, ErrorClass::CheckFailed, 100, 10),
|
||||
];
|
||||
let r = compute(1, &runs, &tasks);
|
||||
let order: Vec<&str> = r.models.iter().map(|m| m.model.as_str()).collect();
|
||||
assert_eq!(order, vec!["fast", "slow", "schlecht"]);
|
||||
assert_eq!(r.models[2].rank, 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn matrix_und_aufgabenliste_nach_bereich_sortiert() {
|
||||
let tasks = infos(&[("b1", "B", 1.0), ("a1", "A", 1.0)]);
|
||||
let runs = vec![
|
||||
run("m", "b1", 1.0, ErrorClass::Ok, 1, 1),
|
||||
run("m", "a1", 0.5, ErrorClass::CheckFailed, 1, 1),
|
||||
];
|
||||
let r = compute(1, &runs, &tasks);
|
||||
let ids: Vec<&str> = r.tasks.iter().map(|t| t.id.as_str()).collect();
|
||||
assert_eq!(ids, vec!["a1", "b1"]);
|
||||
assert_eq!(r.cells.len(), 2);
|
||||
let a1 = r.cells.iter().find(|c| c.task_id == "a1").unwrap();
|
||||
assert!((a1.mean_score - 0.5).abs() < 1e-9);
|
||||
assert_eq!(a1.successes, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unbekannte_aufgabe_landet_im_bereich_fragezeichen() {
|
||||
let r = compute(1, &[run("m", "alt", 1.0, ErrorClass::Ok, 1, 1)], &BTreeMap::new());
|
||||
assert_eq!(r.groups, vec!["?".to_string()]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn leerer_batch() {
|
||||
let r = compute(7, &[], &BTreeMap::new());
|
||||
assert!(r.models.is_empty() && r.cells.is_empty() && r.tasks.is_empty() && r.groups.is_empty());
|
||||
assert_eq!(r.batch_id, 7);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -215,11 +350,4 @@ mod tests {
|
||||
assert_eq!(median_u128(&[1, 2, 3, 4]), 2);
|
||||
assert_eq!(median_u128(&[]), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn leerer_batch_ergibt_leere_rangliste() {
|
||||
let r = compute(7, &[], &BTreeMap::new());
|
||||
assert!(r.models.is_empty() && r.cells.is_empty() && r.tasks.is_empty());
|
||||
assert_eq!(r.batch_id, 7);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user