EvalPage.tsx
10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { FlaskConical, Play, Plus, Target, Trash2 } from "lucide-react";
import { useCallback, useEffect, useState } from "react";
import { Badge, Card, CardHeader, Empty, Field, Modal, Progress, Segmented, Spinner, Stat } from "@/components/ui";
import { errText, fmtTime, onto, type EvalCase, type EvalRun, type Me } from "@/lib/onto";
import { useJobPoll } from "@/lib/useJobPoll";
import { cn } from "@/lib/utils";
import { toast } from "@/store/useAppStore";
const KIND_LABEL: Record<string, string> = { knowledge: "知识", data: "数据", mixed: "混合", process: "流程", action: "行动" };
/**
* 评测中心:问答评测(五类用例),以及「待发布版本」评测 ——
* 打开发布闸门后,本体发布前必须对待发布版本跑评测且通过率达标。
*/
export function EvalPage({ me }: { me: Me | null }) {
const canEdit = !!me?.permissions.includes("build");
return <div className="space-y-4"><QaEval canEdit={canEdit} /></div>;
}
function QaEval({ canEdit }: { canEdit: boolean }) {
const [suites, setSuites] = useState<{ suite: string; cases: number; by_kind: Record<string, number> }[]>([]);
const [gold, setGold] = useState<string[]>([]);
const [suite, setSuite] = useState("");
const [cases, setCases] = useState<EvalCase[]>([]);
const [runs, setRuns] = useState<EvalRun[]>([]);
const [runSel, setRunSel] = useState<EvalRun | null>(null);
const [edit, setEdit] = useState<EvalCase | "new" | null>(null);
const [jobId, setJobId] = useState<number | null>(null);
const loadRuns = useCallback(() => { void onto.evalRuns().then(setRuns); }, []);
const job = useJobPoll(jobId, () => loadRuns());
const load = useCallback(() => {
void onto.evalSuites().then((r) => { setSuites(r.suites); setGold(r.gold_sets); if (!suite && r.suites[0]) setSuite(r.suites[0].suite); });
loadRuns();
}, [suite, loadRuns]);
useEffect(load, [load]);
useEffect(() => { if (suite) void onto.evalCases(suite).then(setCases); }, [suite]);
const start = async (target: string) => {
try { const r = await onto.startEval(suite, target); setJobId(r.job_id); toast("info", "评测已开始", "每条用例都会完整走一遍统一提问"); }
catch (e) { toast("error", errText(e)); }
};
return (
<div className="space-y-4">
<Card className="flex flex-wrap items-center gap-2 p-3">
<select className="input h-8 w-48" value={suite} onChange={(e) => setSuite(e.target.value)}>
{!suites.length && <option value="">(还没有评测集)</option>}
{suites.map((s) => <option key={s.suite} value={s.suite}>{s.suite}({s.cases})</option>)}
</select>
{canEdit && gold.map((g) => (
<button key={g} className="btn btn-sm" onClick={async () => {
const r = await onto.importGold(g); toast("ok", `导入 ${r.imported} 条金标用例`); setSuite(g); load();
}}><Target className="h-3.5 w-3.5" />导入金标集 {g}</button>
))}
{canEdit && <button className="btn btn-sm" onClick={() => setEdit("new")}><Plus className="h-3.5 w-3.5" />加用例</button>}
<div className="ml-auto flex gap-2">
{canEdit && suite && <>
<button className="btn btn-sm" onClick={() => void start("published")}><Play className="h-3.5 w-3.5" />评测线上版本</button>
<button className="btn btn-primary btn-sm" onClick={() => void start("draft")} title="用包含未发布修改的本体来评测,发布闸门看的是它">
<FlaskConical className="h-3.5 w-3.5" />评测待发布版本</button>
</>}
</div>
{job && job.status !== "done" && job.status !== "failed" && (
<div className="w-full"><Progress value={job.progress} /><p className="mt-1 text-label text-muted">{job.message}</p></div>
)}
</Card>
<div className="grid gap-4 xl:grid-cols-2">
<Card>
<CardHeader title="评测记录" />
<div className="divide-y divide-line">
{!runs.length && <Empty title="还没跑过评测" />}
{runs.map((r) => (
<button key={r.id} className="flex w-full items-center gap-3 px-4 py-2.5 text-left hover:bg-ink/[0.03]"
onClick={() => void onto.evalRun(r.id).then(setRunSel)}>
<span className={cn("w-14 text-section font-semibold tabular-nums", r.pass_rate >= 0.7 ? "text-ok" : "text-warn")}>
{r.status === "done" ? `${Math.round(r.pass_rate * 100)}%` : "…"}</span>
<div className="min-w-0 flex-1">
<p className="text-body">{r.suite} · {r.target === "draft" ? "待发布版本" : "线上版本"}</p>
<p className="text-caption text-muted">{r.passed}/{r.total} 通过 · {fmtTime(r.created_at)}</p>
</div>
<div className="flex flex-wrap gap-1">
{Object.entries(r.summary?.by_kind || {}).map(([k, v]: any) => (
<Badge key={k}>{KIND_LABEL[k] || k} {Math.round(v.pass_rate * 100)}%</Badge>
))}
</div>
</button>
))}
</div>
</Card>
<Card>
<CardHeader title={`用例(${cases.length})`} />
<div className="max-h-[520px] divide-y divide-line overflow-auto">
{cases.map((c) => (
<button key={c.id} className="block w-full px-4 py-2 text-left hover:bg-ink/[0.03]" onClick={() => canEdit && setEdit(c)}>
<p className="text-body"><Badge>{KIND_LABEL[c.kind]}</Badge> {c.question}</p>
<p className="truncate text-caption text-muted">
{c.must_include.length ? `关键词:${c.must_include.join("、")} ` : ""}{c.expected_calls.length ? `期望调用:${c.expected_calls.join("、")} ` : ""}
{c.expected_action ? `期望动作:${c.expected_action}` : ""}{c.source === "feedback" ? " · 来自用户纠错" : ""}
</p>
</button>
))}
</div>
</Card>
</div>
{runSel && (
<Modal open onClose={() => setRunSel(null)} width="max-w-4xl" title={`评测结果:${runSel.passed}/${runSel.total} 通过`}
desc={`${runSel.suite} · ${runSel.target === "draft" ? "待发布版本" : "线上版本"} · 平均分 ${runSel.summary?.avg_score}`}>
<div className="space-y-2">
{(runSel.results || []).map((r: any) => (
<details key={r.case_id} className={cn("rounded-lg border p-2.5 text-label", r.passed ? "border-line" : "border-bad/40 bg-bad/[0.03]")}>
<summary className="cursor-pointer">
<Badge tone={r.passed ? "bg-ok/10 text-ok" : "bg-bad/10 text-bad"}>{Math.round(r.score * 100)}</Badge>{" "}
<Badge>{KIND_LABEL[r.kind]}</Badge> {r.question}
</summary>
<div className="mt-2 space-y-1">
<p className="text-muted">调用:{(r.calls || []).join(" → ") || "无"}</p>
<pre className="max-h-48 overflow-auto whitespace-pre-wrap rounded bg-ink/5 p-2 font-sans">{r.answer || r.error}</pre>
<pre className="text-caption text-muted">{JSON.stringify(r.checks, null, 1)}</pre>
</div>
</details>
))}
</div>
</Modal>
)}
{edit && <CaseModal c={edit === "new" ? null : edit} suite={suite || "default"} onClose={() => setEdit(null)}
onDone={() => { setEdit(null); load(); if (suite) void onto.evalCases(suite).then(setCases); }} />}
</div>
);
}
function CaseModal({ c, suite, onClose, onDone }: { c: EvalCase | null; suite: string; onClose: () => void; onDone: () => void }) {
const [v, setV] = useState<any>(c ? { ...c, must_include: c.must_include.join(","), expected_calls: c.expected_calls.join(",") }
: { suite, kind: "knowledge", question: "", expected: "", must_include: "", expected_calls: "", expected_action: "" });
const split = (s: string) => s.split(/[,,、]/).map((x) => x.trim()).filter(Boolean);
return (
<Modal open onClose={onClose} title={c ? "编辑用例" : "新用例"}
footer={<>
{c && <button className="btn mr-auto" onClick={async () => { await onto.deleteEvalCase(c.id); onDone(); }}><Trash2 className="h-3.5 w-3.5" />删除</button>}
<button className="btn" onClick={onClose}>取消</button>
<button className="btn btn-primary" onClick={async () => {
try { await onto.saveEvalCase({ ...v, must_include: split(v.must_include), expected_calls: split(v.expected_calls) }, c?.id); onDone(); }
catch (e) { toast("error", errText(e)); }
}}>保存</button></>}>
<div className="space-y-3">
<div className="grid grid-cols-2 gap-3">
<Field label="评测集"><input className="input" value={v.suite} onChange={(e) => setV({ ...v, suite: e.target.value })} /></Field>
<Field label="类别"><select className="input" value={v.kind} onChange={(e) => setV({ ...v, kind: e.target.value })}>
{Object.entries(KIND_LABEL).map(([k, l]) => <option key={k} value={k}>{l}</option>)}</select></Field>
</div>
<Field label="问题" required><input className="input" value={v.question} onChange={(e) => setV({ ...v, question: e.target.value })} /></Field>
<Field label="参考答案要点(评分时对照)"><textarea className="input" value={v.expected} onChange={(e) => setV({ ...v, expected: e.target.value })} /></Field>
<Field label="答案必须包含的关键词(逗号分隔)"><input className="input" value={v.must_include} onChange={(e) => setV({ ...v, must_include: e.target.value })} /></Field>
<div className="grid grid-cols-2 gap-3">
<Field label="期望调用的函数"><input className="input" value={v.expected_calls} onChange={(e) => setV({ ...v, expected_calls: e.target.value })} placeholder="metric.query" /></Field>
<Field label="期望建议的动作"><input className="input" value={v.expected_action} onChange={(e) => setV({ ...v, expected_action: e.target.value })} /></Field>
</div>
</div>
</Modal>
);
}