update for ragas

This commit is contained in:
wangwei
2026-07-01 17:53:00 +08:00
parent 2bb804b059
commit 4e74e1b247
15 changed files with 507 additions and 53 deletions
+20 -1
View File
@@ -40,7 +40,14 @@ const result = {{
desc: p.describeMetric("faithfulness"),
noiseDesc: p.describeMetric("noise_sensitivity"),
noiseBin: p.binColor("noise_sensitivity", 0.0),
faithBin: p.binColor("faithfulness", 0.8)
faithBin: p.binColor("faithfulness", 0.8),
lowerBetterNoise: p.isLowerBetter("noise_sensitivity"),
lowerBetterFaith: p.isLowerBetter("faithfulness"),
upHigher: p.deltaInfo("faithfulness", 0.80, 0.60),
downHigher: p.deltaInfo("faithfulness", 0.60, 0.80),
noiseImproved: p.deltaInfo("noise_sensitivity", 0.10, 0.30),
noiseWorse: p.deltaInfo("noise_sensitivity", 0.30, 0.10),
noBaseline: p.deltaInfo("faithfulness", 0.80, null)
}};
console.log(JSON.stringify(result));
"""
@@ -55,6 +62,15 @@ console.log(JSON.stringify(result));
assert '"noiseDesc":"' in output
assert '"noiseBin":"#16a34a"' in output
assert '"faithBin":"#16a34a"' in output
assert '"lowerBetterNoise":true' in output
assert '"lowerBetterFaith":false' in output
# higher-better: rising value is an improvement (green ▲); falling is a regression (red ▼)
assert '"upHigher":{"hasData":true,"delta":0.2,"improved":true,"arrow":"","magnitude":"0.20","cls":"delta-good"}' in output
assert '"downHigher":{"hasData":true,"delta":-0.2,"improved":false,"arrow":"","magnitude":"0.20","cls":"delta-bad"}' in output
# noise_sensitivity (lower-better): falling value is an improvement (green ▼)
assert '"noiseImproved":{"hasData":true,"delta":-0.2,"improved":true,"arrow":"","magnitude":"0.20","cls":"delta-good"}' in output
assert '"noiseWorse":{"hasData":true,"delta":0.2,"improved":false,"arrow":"","magnitude":"0.20","cls":"delta-bad"}' in output
assert '"noBaseline":{"hasData":false' in output
def test_report_and_index_load_metric_presenter_helper() -> None:
@@ -66,3 +82,6 @@ def test_report_and_index_load_metric_presenter_helper() -> None:
assert "js/metric_presenter.js" in index_html
assert "MetricPresenter.describeMetric" in report_js
assert "MetricPresenter.scoreClass" in app_js
# history comparison table uses the direction-aware delta helper
assert "MetricPresenter.deltaInfo" in report_js
assert "history-table" in report_js
+71
View File
@@ -4,8 +4,11 @@ from __future__ import annotations
from pathlib import Path
import pandas as pd
import pytest
from webapp.services import question_history
from webapp.services import report_builder
from webapp.services.report_builder import build_report
from webapp.services.run_reader import _infer_metrics_from_scores, _read_weights_from_snapshot
@@ -115,3 +118,71 @@ def test_build_report_ranks_noise_sensitivity_with_lower_values_as_better(tmp_pa
"s-warn",
"s-good",
]
def test_lowest_samples_attaches_history_from_index() -> None:
"""Surfaced samples are annotated with the same question's prior-run scores."""
frame = pd.DataFrame(
[
{"sample_id": "s1", "question": " How LONG is the tube? ", "faithfulness": 0.40},
{"sample_id": "s2", "question": "unrelated question", "faithfulness": 0.30},
]
)
history_index = {
question_history.normalize_question("How long is the tube?"): [
{
"run_id": "prev",
"scenario_name": "scn",
"finished_at": "2026-01-01T00:00:00",
"metrics": {"faithfulness": 0.90},
}
]
}
samples = report_builder._lowest_samples(frame, ["faithfulness"], history_index)
by_id = {s.sample_id: s for s in samples}
assert len(by_id["s1"].history) == 1
assert by_id["s1"].history[0].run_id == "prev"
assert by_id["s1"].history[0].metrics["faithfulness"] == 0.90
assert by_id["s2"].history == [] # no match → no history
def test_build_report_attaches_question_history(tmp_path: Path, monkeypatch) -> None:
"""build_report wires the question-history index into surfaced samples."""
run_dir = tmp_path / "run"
run_dir.mkdir(parents=True, exist_ok=True)
(run_dir / "scores.csv").write_text(
"\n".join(
[
"sample_id,question,faithfulness",
"s1,How long is the tube?,0.40",
]
),
encoding="utf-8",
)
(run_dir / "summary.md").write_text("summary", encoding="utf-8")
(run_dir / "optimization_advice.md").write_text("", encoding="utf-8")
captured: dict = {}
def _fake_index(exclude_run_id=None, extra_roots=None):
captured["exclude_run_id"] = exclude_run_id
return {
question_history.normalize_question("How long is the tube?"): [
{
"run_id": "older",
"scenario_name": "scn",
"finished_at": "2026-01-01T00:00:00",
"metrics": {"faithfulness": 0.95},
}
]
}
monkeypatch.setattr(question_history, "build_question_history_index", _fake_index)
report = build_report(run_dir, ["faithfulness"])
assert captured["exclude_run_id"] == "run" # current run excluded from history
assert report.lowest_samples[0].history[0].run_id == "older"
assert report.lowest_samples[0].history[0].metrics["faithfulness"] == 0.95