110 lines
3.9 KiB
Go
110 lines
3.9 KiB
Go
package eval
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// Threshold is the floor a judge must clear to be trusted with a ceiling.
|
|
type Threshold struct {
|
|
MinNefariousRecall float64 `json:"min_nefarious_recall"`
|
|
MinNefariousPrecision float64 `json:"min_nefarious_precision"`
|
|
MaxFalseBlockRate float64 `json:"max_false_block_rate"`
|
|
// Why says in one line where the numbers come from.
|
|
Why string `json:"why"`
|
|
}
|
|
|
|
// Score is one configuration's measured quality.
|
|
type Score struct {
|
|
Accuracy float64 `json:"accuracy"`
|
|
NefariousRecall float64 `json:"nefarious_recall"`
|
|
NefariousPrecision float64 `json:"nefarious_precision"`
|
|
FalseBlockRate float64 `json:"false_block_rate"`
|
|
}
|
|
|
|
// Record is the recorded outcome of measuring a judge model against the
|
|
// labelled cases. The guard reads it at start and trusts the judge only while
|
|
// it passes.
|
|
type Record struct {
|
|
Model string `json:"model"`
|
|
// Date is the day the measurement was taken, as YYYY-MM-DD.
|
|
Date string `json:"date"`
|
|
Cases int `json:"cases"`
|
|
// JudgeErrors counts judge calls that failed during the run.
|
|
JudgeErrors int `json:"judge_errors"`
|
|
// Lens is the deterministic lens alone, Judged the lens with the judge.
|
|
Lens Score `json:"lens"`
|
|
Judged Score `json:"judged"`
|
|
Fixed int `json:"fixed"`
|
|
Broke int `json:"broke"`
|
|
Threshold Threshold `json:"threshold"`
|
|
}
|
|
|
|
// NewRecord records a run against a threshold.
|
|
func NewRecord(model string, at time.Time, s Summary, t Threshold) Record {
|
|
score := func(x Scores) Score {
|
|
return Score{x.Accuracy, x.NefariousRecall, x.NefariousPrecision, x.FalseBlockRate}
|
|
}
|
|
return Record{
|
|
Model: model, Date: at.UTC().Format(time.DateOnly), Cases: s.Judged.Total, JudgeErrors: s.JudgeErrors,
|
|
Lens: score(s.Baseline), Judged: score(s.Judged), Fixed: s.Fixed, Broke: s.Broke, Threshold: t,
|
|
}
|
|
}
|
|
|
|
// ParseRecord decodes a recorded result.
|
|
func ParseRecord(data []byte) (Record, error) {
|
|
var r Record
|
|
if err := json.Unmarshal(data, &r); err != nil {
|
|
return Record{}, fmt.Errorf("decode eval result: %w", err)
|
|
}
|
|
if _, err := time.Parse(time.DateOnly, r.Date); err != nil {
|
|
return Record{}, fmt.Errorf("eval result date: %w", err)
|
|
}
|
|
if r.Model == "" || r.Cases == 0 {
|
|
return Record{}, errors.New("eval result names no model or no cases")
|
|
}
|
|
return r, nil
|
|
}
|
|
|
|
// Shortfalls lists every way the judged scores miss the threshold, or a failed
|
|
// judge call. Empty means the judge passed.
|
|
func (r Record) Shortfalls() []string {
|
|
var out []string
|
|
if r.JudgeErrors > 0 {
|
|
out = append(out, fmt.Sprintf("%d judge calls failed", r.JudgeErrors))
|
|
}
|
|
if r.Judged.NefariousRecall < r.Threshold.MinNefariousRecall {
|
|
out = append(out, fmt.Sprintf("nefarious recall %.2f is below %.2f", r.Judged.NefariousRecall, r.Threshold.MinNefariousRecall))
|
|
}
|
|
if r.Judged.NefariousPrecision < r.Threshold.MinNefariousPrecision {
|
|
out = append(out, fmt.Sprintf("nefarious precision %.2f is below %.2f", r.Judged.NefariousPrecision, r.Threshold.MinNefariousPrecision))
|
|
}
|
|
if r.Judged.FalseBlockRate > r.Threshold.MaxFalseBlockRate {
|
|
out = append(out, fmt.Sprintf("false block rate %.2f is above %.2f", r.Judged.FalseBlockRate, r.Threshold.MaxFalseBlockRate))
|
|
}
|
|
return out
|
|
}
|
|
|
|
// AdvisoryReason says why the judge named by model must stay advisory at now, or
|
|
// is empty when it may count. A result earns trust only for the model it
|
|
// measured, and only for maxAge after the measurement.
|
|
func (r Record) AdvisoryReason(model string, now time.Time, maxAge time.Duration) string {
|
|
if r.Model != model {
|
|
return fmt.Sprintf("the recorded eval measured %q, not %q", r.Model, model)
|
|
}
|
|
taken, err := time.Parse(time.DateOnly, r.Date)
|
|
if err != nil {
|
|
return "the recorded eval has no valid date"
|
|
}
|
|
if now.Sub(taken) > maxAge {
|
|
return "the recorded eval from " + r.Date + " is older than the allowed age"
|
|
}
|
|
if short := r.Shortfalls(); len(short) > 0 {
|
|
return "the recorded eval did not pass: " + strings.Join(short, "; ")
|
|
}
|
|
return ""
|
|
}
|