-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscorer_calibration_test.go
More file actions
325 lines (279 loc) · 8.86 KB
/
scorer_calibration_test.go
File metadata and controls
325 lines (279 loc) · 8.86 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
//go:build calibration
package llmtest_test
import (
"context"
"encoding/json"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"testing"
"github.com/adamwoolhether/llmtest"
"github.com/adamwoolhether/llmtest/provider"
)
func TestMain(m *testing.M) {
code := m.Run()
llmtest.Flush() // writes JSON summary if LLMTEST_OUTPUT is set
os.Exit(code)
}
type calibrationCase struct {
ID string `json:"id"`
Description string `json:"description"`
Criterion string `json:"criterion"`
Input string `json:"input"`
ActualOutput string `json:"actual_output"`
ExpectedOutput string `json:"expected_output"`
Context []string `json:"context"`
RetrievalContext []string `json:"retrieval_context"`
ExpectedVerdict string `json:"expected_verdict"`
Tags []string `json:"tags"`
}
type providerConfig struct {
name string
model string
provider provider.Provider
}
var (
ollamaModels = []string{"llama3.2", "qwen2.5"}
openaiModels = []string{"gpt-4.1-mini", "o4-mini"}
anthropicModels = []string{"claude-sonnet-4-5-20250929"}
)
func modelsForProvider(providerName string) []string {
envKey := "LLMTEST_CALIBRATION_MODELS_" + strings.ToUpper(providerName)
if v := os.Getenv(envKey); v != "" {
return strings.Split(v, ",")
}
switch providerName {
case "ollama":
return ollamaModels
case "openai":
return openaiModels
case "anthropic":
return anthropicModels
default:
return nil
}
}
func calibrationProviders(t *testing.T) []providerConfig {
t.Helper()
var configs []providerConfig
if os.Getenv("OPENAI_API_KEY") != "" {
p, err := provider.OpenAI()
if err != nil {
t.Logf("skipping openai: %v", err)
} else {
for _, m := range modelsForProvider("openai") {
configs = append(configs, providerConfig{name: "openai", model: m, provider: p})
}
}
}
if os.Getenv("ANTHROPIC_API_KEY") != "" {
p, err := provider.Anthropic()
if err != nil {
t.Logf("skipping anthropic: %v", err)
} else {
for _, m := range modelsForProvider("anthropic") {
configs = append(configs, providerConfig{name: "anthropic", model: m, provider: p})
}
}
}
if calibrationOllamaReachable() {
p, err := provider.Ollama()
if err != nil {
t.Logf("skipping ollama: %v", err)
} else {
for _, m := range modelsForProvider("ollama") {
configs = append(configs, providerConfig{name: "ollama", model: m, provider: p})
}
}
}
if len(configs) == 0 {
t.Fatal("no providers available: set OPENAI_API_KEY, ANTHROPIC_API_KEY, or run Ollama")
}
return configs
}
func calibrationOllamaReachable() bool {
u := os.Getenv("LLMTEST_OLLAMA_URL")
if u == "" {
u = "http://localhost:11434"
}
resp, err := http.Get(u)
if err != nil {
return false
}
defer func() {
io.Copy(io.Discard, resp.Body)
resp.Body.Close()
}()
return resp.StatusCode == 200
}
func loadCases(t *testing.T, filename string) []calibrationCase {
t.Helper()
data, err := os.ReadFile(filepath.Join("testdata", "calibration", filename))
if err != nil {
t.Fatalf("loading %s: %v", filename, err)
}
var cases []calibrationCase
if err := json.Unmarshal(data, &cases); err != nil {
t.Fatalf("parsing %s: %v", filename, err)
}
return cases
}
func toTestCase(cc calibrationCase) llmtest.TestCase {
return llmtest.TestCase{
Input: cc.Input,
ActualOutput: cc.ActualOutput,
ExpectedOutput: cc.ExpectedOutput,
Context: cc.Context,
RetrievalContext: cc.RetrievalContext,
}
}
// groupedProviders returns provider configs grouped by name with stable ordering.
func groupedProviders(t *testing.T) ([]string, map[string][]providerConfig) {
t.Helper()
configs := calibrationProviders(t)
grouped := make(map[string][]providerConfig)
var order []string
for _, pc := range configs {
if _, ok := grouped[pc.name]; !ok {
order = append(order, pc.name)
}
grouped[pc.name] = append(grouped[pc.name], pc)
}
return order, grouped
}
// runCalibration is a shared helper for calibration tests. It loads the
// given fixture file, iterates over all provider/model/case combinations,
// creates a Rubric scorer with the given options, and calls assertFn with
// the result for each case.
func runCalibration(
t *testing.T,
filename string,
scorerOpts func(cc calibrationCase, pc providerConfig) []llmtest.ScorerOption,
assertFn func(t *testing.T, cc calibrationCase, result llmtest.Result),
) {
t.Helper()
order, grouped := groupedProviders(t)
cases := loadCases(t, filename)
for _, name := range order {
t.Run(name, func(t *testing.T) {
t.Parallel()
for _, pc := range grouped[name] {
pc := pc
t.Run(pc.model, func(t *testing.T) {
t.Parallel()
for _, cc := range cases {
cc := cc
t.Run(cc.ID, func(t *testing.T) {
t.Parallel()
opts := scorerOpts(cc, pc)
scorer := llmtest.Rubric(cc.Criterion, opts...)
result, err := scorer.Score(context.Background(), toTestCase(cc))
if err != nil {
t.Fatalf("score error: %v", err)
}
t.Logf("verdict=%s score=%.1f reason=%s tokens=%d latency=%dms",
result.Verdict, result.Score, result.Reason, result.Tokens, result.LatencyMS)
assertFn(t, cc, result)
})
}
})
}
})
}
}
func defaultScorerOpts(cc calibrationCase, pc providerConfig) []llmtest.ScorerOption {
return []llmtest.ScorerOption{llmtest.Provider(pc.provider), llmtest.Model(pc.model)}
}
// TestCalibrationPass verifies that obvious-pass cases return PASS.
func TestCalibrationPass(t *testing.T) {
runCalibration(t, "obvious_pass.json", defaultScorerOpts, func(t *testing.T, cc calibrationCase, result llmtest.Result) {
if result.Verdict != llmtest.Pass {
t.Errorf("[%s] expected PASS, got %s: %s", cc.ID, result.Verdict, result.Reason)
}
})
}
// TestCalibrationFail verifies that obvious-fail cases return FAIL.
func TestCalibrationFail(t *testing.T) {
runCalibration(t, "obvious_fail.json", defaultScorerOpts, func(t *testing.T, cc calibrationCase, result llmtest.Result) {
if result.Verdict != llmtest.Fail {
t.Errorf("[%s] expected FAIL, got %s: %s", cc.ID, result.Verdict, result.Reason)
}
})
}
// TestCalibrationAmbiguous runs each case 3 times and expects at least 2/3 agreement.
func TestCalibrationAmbiguous(t *testing.T) {
order, grouped := groupedProviders(t)
cases := loadCases(t, "ambiguous.json")
for _, name := range order {
t.Run(name, func(t *testing.T) {
t.Parallel()
for _, pc := range grouped[name] {
pc := pc
t.Run(pc.model, func(t *testing.T) {
t.Parallel()
for _, cc := range cases {
cc := cc
t.Run(cc.ID, func(t *testing.T) {
t.Parallel()
scorer := llmtest.Rubric(cc.Criterion, llmtest.Provider(pc.provider), llmtest.Model(pc.model))
counts := make(map[llmtest.Verdict]int)
for i := 0; i < 3; i++ {
result, err := scorer.Score(context.Background(), toTestCase(cc))
if err != nil {
t.Fatalf("score error (run %d): %v", i, err)
}
counts[result.Verdict]++
t.Logf("run %d: verdict=%s reason=%s", i, result.Verdict, result.Reason)
}
var maxCount int
var maxVerdict llmtest.Verdict
for v, c := range counts {
if c > maxCount {
maxCount = c
maxVerdict = v
}
}
if maxCount < 2 {
t.Errorf("[%s] no 2/3 agreement: %v", cc.ID, counts)
} else {
t.Logf("[%s] agreement: %s (%d/3)", cc.ID, maxVerdict, maxCount)
}
})
}
})
}
})
}
}
// TestCalibrationThreshold verifies that the Threshold option correctly
// promotes verdicts: Threshold(0.5) should promote PARTIAL to PASS
// while leaving FAIL unchanged.
func TestCalibrationThreshold(t *testing.T) {
thresholdOpts := func(cc calibrationCase, pc providerConfig) []llmtest.ScorerOption {
return []llmtest.ScorerOption{llmtest.Provider(pc.provider), llmtest.Model(pc.model), llmtest.Threshold(0.5)}
}
t.Run("pass_unaffected", func(t *testing.T) {
runCalibration(t, "obvious_pass.json", thresholdOpts, func(t *testing.T, cc calibrationCase, result llmtest.Result) {
if result.Verdict != llmtest.Pass {
t.Errorf("[%s] expected PASS with Threshold(0.5), got %s: %s", cc.ID, result.Verdict, result.Reason)
}
})
})
t.Run("fail_unaffected", func(t *testing.T) {
runCalibration(t, "obvious_fail.json", thresholdOpts, func(t *testing.T, cc calibrationCase, result llmtest.Result) {
if result.Verdict != llmtest.Fail {
t.Errorf("[%s] expected FAIL with Threshold(0.5), got %s: %s", cc.ID, result.Verdict, result.Reason)
}
})
})
}
// TestCalibrationAdversarial verifies that adversarial cases do NOT return PASS.
func TestCalibrationAdversarial(t *testing.T) {
runCalibration(t, "adversarial.json", defaultScorerOpts, func(t *testing.T, cc calibrationCase, result llmtest.Result) {
if result.Verdict == llmtest.Pass {
t.Errorf("[%s] expected NOT PASS, got PASS: %s", cc.ID, result.Reason)
}
})
}