-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent_eval.go
More file actions
401 lines (335 loc) · 11.9 KB
/
agent_eval.go
File metadata and controls
401 lines (335 loc) · 11.9 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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package ml
import (
"bufio"
"context"
"encoding/json"
"fmt"
"io"
"log"
"os"
"path/filepath"
"strings"
coreio "dappco.re/go/core/io"
coreerr "dappco.re/go/core/log"
)
// ProbeResult holds the result of running all probes against a checkpoint.
type ProbeResult struct {
Accuracy float64 `json:"accuracy"`
Correct int `json:"correct"`
Total int `json:"total"`
ByCategory map[string]CategoryResult `json:"by_category"`
Probes map[string]SingleProbeResult `json:"probes"`
}
// CategoryResult holds pass/fail counts for a probe category.
type CategoryResult struct {
Correct int `json:"correct"`
Total int `json:"total"`
}
// SingleProbeResult holds the result of a single probe.
type SingleProbeResult struct {
Passed bool `json:"passed"`
Response string `json:"response"`
}
// ProbeCallback is called after each probe completes for real-time streaming.
type ProbeCallback func(probeID, category string, passed bool, response string, correct, total int)
// CapResponseEntry holds a capability probe response with its metadata for judge scoring.
type CapResponseEntry struct {
ProbeID string
Category string
Prompt string
Answer string
Response string
Passed bool
}
// ContentResponse holds a content probe response for later judging.
type ContentResponse struct {
Probe ContentProbe
Response string
}
// probeRunnerResponse is the JSON response from the Python probe runner.
type probeRunnerResponse struct {
Response string `json:"response"`
Error string `json:"error"`
Elapsed float64 `json:"elapsed"`
}
// processMLXNative scores a checkpoint using Ollama on M3.
func processMLXNative(cfg *AgentConfig, influx *InfluxClient, cp Checkpoint) error {
ollamaBase, ok := OllamaBaseModelMap[cp.ModelTag]
if !ok {
return coreerr.E("ml.processMLXNative", fmt.Sprintf("unknown Ollama model for tag %s", cp.ModelTag), nil)
}
hfBase := HFBaseModelMap[cp.ModelTag]
if hfBase == "" {
hfBase = ollamaBase
}
tempModel := fmt.Sprintf("lem-%s-%d", cp.ModelTag, cp.Iteration)
localAdapterDir := filepath.Join(cfg.WorkDir, "adapter-"+cp.Dirname)
peftDir := filepath.Join(cfg.WorkDir, "peft-"+cp.Dirname)
coreio.Local.EnsureDir(localAdapterDir)
defer func() {
os.RemoveAll(localAdapterDir)
os.RemoveAll(peftDir)
OllamaDeleteModel(cfg.JudgeURL, tempModel)
}()
log.Printf("Fetching adapter from M3 (%s)...", cp.Filename)
remoteSF := fmt.Sprintf("%s/%s", cp.RemoteDir, cp.Filename)
remoteCfg := fmt.Sprintf("%s/adapter_config.json", cp.RemoteDir)
localSF := filepath.Join(localAdapterDir, cp.Filename)
localCfg := filepath.Join(localAdapterDir, "adapter_config.json")
ctx := context.Background()
t := cfg.transport()
if err := t.CopyFrom(ctx, remoteSF, localSF); err != nil {
return coreerr.E("ml.processMLXNative", "scp safetensors", err)
}
if err := t.CopyFrom(ctx, remoteCfg, localCfg); err != nil {
return coreerr.E("ml.processMLXNative", "scp config", err)
}
log.Println("Converting MLX → PEFT format...")
if err := ConvertMLXtoPEFT(localSF, localCfg, peftDir, hfBase); err != nil {
return coreerr.E("ml.processMLXNative", "convert adapter", err)
}
log.Printf("Creating Ollama model %s (base: %s)...", tempModel, ollamaBase)
if err := OllamaCreateModel(cfg.JudgeURL, tempModel, ollamaBase, peftDir); err != nil {
return coreerr.E("ml.processMLXNative", "ollama create", err)
}
log.Printf("Ollama model %s ready", tempModel)
probeBackend := NewHTTPBackend(cfg.JudgeURL, tempModel)
results, fullResponses := RunCapabilityProbesFull(ctx, probeBackend, func(probeID, category string, passed bool, response string, correct, total int) {
passedInt := 0
if passed {
passedInt = 1
}
ts := (EpochBase + int64(cp.Iteration)*1000 + int64(total+100)) * 1_000_000_000
line := fmt.Sprintf(
MeasurementProbeScore+",model=%s,run_id=%s,label=%s,probe_id=%s passed=%di,iteration=%di %d",
EscapeLp(cp.ModelTag), EscapeLp(cp.RunID), EscapeLp(cp.Label), EscapeLp(probeID),
passedInt, cp.Iteration, ts,
)
if err := influx.WriteLp([]string{line}); err != nil {
log.Printf(" [%s] InfluxDB stream failed: %v", probeID, err)
}
})
log.Printf("Capability: %s -- %.1f%% (%d/%d)",
cp.Label, results.Accuracy, results.Correct, results.Total)
if err := PushCapabilitySummary(influx, cp, results); err != nil {
log.Printf("InfluxDB summary push failed, buffering: %v", err)
BufferInfluxResult(cfg.WorkDir, cp, results)
}
PushCapabilityResultsDB(cfg.DBPath, cp, results)
judgeBackend := NewHTTPBackend(cfg.JudgeURL, cfg.JudgeModel)
judge := NewJudge(judgeBackend)
log.Printf("Judging %d capability responses (0-10 quality scoring)...", len(fullResponses))
ScoreCapabilityAndPush(ctx, judge, influx, cp, fullResponses)
log.Printf("Running %d content probes (0-10 judge scoring)...", len(ContentProbes))
contentResponses := RunContentProbesViaAPI(ctx, probeBackend)
if len(contentResponses) > 0 {
contentRunID := strings.Replace(cp.RunID, "-capability-", "-content-", 1)
ScoreContentAndPush(ctx, judge, influx, cp, contentRunID, contentResponses)
}
return nil
}
// processWithConversion fetches adapter locally, converts MLX→PEFT, and scores.
func processWithConversion(cfg *AgentConfig, influx *InfluxClient, cp Checkpoint) error {
localAdapterDir := filepath.Join(cfg.WorkDir, cp.Dirname)
coreio.Local.EnsureDir(localAdapterDir)
localSF := filepath.Join(localAdapterDir, cp.Filename)
localCfg := filepath.Join(localAdapterDir, "adapter_config.json")
defer func() {
coreio.Local.Delete(localSF)
coreio.Local.Delete(localCfg)
peftDir := filepath.Join(cfg.WorkDir, fmt.Sprintf("peft_%07d", cp.Iteration))
os.RemoveAll(peftDir)
}()
log.Println("Fetching adapter from M3...")
remoteSF := fmt.Sprintf("%s/%s", cp.RemoteDir, cp.Filename)
remoteCfg := fmt.Sprintf("%s/adapter_config.json", cp.RemoteDir)
ctx := context.Background()
t := cfg.transport()
if err := t.CopyFrom(ctx, remoteSF, localSF); err != nil {
return coreerr.E("ml.processWithConversion", "scp safetensors", err)
}
if err := t.CopyFrom(ctx, remoteCfg, localCfg); err != nil {
return coreerr.E("ml.processWithConversion", "scp config", err)
}
log.Println("Converting MLX to PEFT format...")
peftDir := filepath.Join(cfg.WorkDir, fmt.Sprintf("peft_%07d", cp.Iteration))
if err := ConvertMLXtoPEFT(localSF, localCfg, peftDir, cfg.BaseModel); err != nil {
return coreerr.E("ml.processWithConversion", "convert adapter", err)
}
log.Printf("Running %d capability probes...", len(CapabilityProbes))
modelName := cfg.Model
if modelName == "" {
modelName = cp.ModelTag
}
backend := NewHTTPBackend(cfg.APIURL, modelName)
results := RunCapabilityProbes(ctx, backend)
log.Printf("Result: %s -- %.1f%% (%d/%d)",
cp.Label, results.Accuracy, results.Correct, results.Total)
if err := PushCapabilityResults(influx, cp, results); err != nil {
log.Printf("InfluxDB push failed, buffering: %v", err)
BufferInfluxResult(cfg.WorkDir, cp, results)
}
PushCapabilityResultsDB(cfg.DBPath, cp, results)
return nil
}
// RunCapabilityProbes runs all capability probes against a backend.
func RunCapabilityProbes(ctx context.Context, backend Backend) ProbeResult {
results := ProbeResult{
ByCategory: make(map[string]CategoryResult),
Probes: make(map[string]SingleProbeResult),
}
correct := 0
total := 0
for _, probe := range CapabilityProbes {
res, err := backend.Generate(ctx, probe.Prompt, GenOpts{Temperature: CapabilityTemperature, MaxTokens: CapabilityMaxTokens})
if err != nil {
log.Printf(" [%s] ERROR: %v", probe.ID, err)
results.Probes[probe.ID] = SingleProbeResult{Passed: false, Response: err.Error()}
total++
cat := results.ByCategory[probe.Category]
cat.Total++
results.ByCategory[probe.Category] = cat
continue
}
clean := StripThinkBlocks(res.Text)
passed := probe.Check(clean)
total++
if passed {
correct++
}
cat := results.ByCategory[probe.Category]
cat.Total++
if passed {
cat.Correct++
}
results.ByCategory[probe.Category] = cat
stored := clean
if len(stored) > MaxStoredResponseLen {
stored = stored[:MaxStoredResponseLen]
}
results.Probes[probe.ID] = SingleProbeResult{Passed: passed, Response: stored}
status := "FAIL"
if passed {
status = "PASS"
}
log.Printf(" [%s] %s (expected: %s)", probe.ID, status, probe.Answer)
}
if total > 0 {
results.Accuracy = float64(correct) / float64(total) * 100
}
results.Correct = correct
results.Total = total
return results
}
// RunCapabilityProbesFull runs all probes via a backend and returns both
// aggregate results and full responses for judge scoring.
func RunCapabilityProbesFull(ctx context.Context, backend Backend, onProbe ProbeCallback) (ProbeResult, []CapResponseEntry) {
results := ProbeResult{
ByCategory: make(map[string]CategoryResult),
Probes: make(map[string]SingleProbeResult),
}
var fullResponses []CapResponseEntry
correct := 0
total := 0
for _, probe := range CapabilityProbes {
res, err := backend.Generate(ctx, probe.Prompt, GenOpts{Temperature: CapabilityTemperature, MaxTokens: CapabilityMaxTokens})
response := res.Text
if err != nil {
log.Printf(" [%s] ERROR: %v", probe.ID, err)
response = fmt.Sprintf("ERROR: %v", err)
}
clean := StripThinkBlocks(response)
passed := probe.Check(clean)
total++
if passed {
correct++
}
cat := results.ByCategory[probe.Category]
cat.Total++
if passed {
cat.Correct++
}
results.ByCategory[probe.Category] = cat
stored := clean
if len(stored) > MaxStoredResponseLen {
stored = stored[:MaxStoredResponseLen]
}
results.Probes[probe.ID] = SingleProbeResult{Passed: passed, Response: stored}
fullResponses = append(fullResponses, CapResponseEntry{
ProbeID: probe.ID,
Category: probe.Category,
Prompt: probe.Prompt,
Answer: probe.Answer,
Response: clean,
Passed: passed,
})
status := "FAIL"
if passed {
status = "PASS"
}
log.Printf(" [%s] %s (expected: %s)", probe.ID, status, probe.Answer)
if onProbe != nil {
onProbe(probe.ID, probe.Category, passed, stored, correct, total)
}
}
if total > 0 {
results.Accuracy = float64(correct) / float64(total) * 100
}
results.Correct = correct
results.Total = total
return results, fullResponses
}
// RunContentProbesViaAPI runs content probes via a backend.
func RunContentProbesViaAPI(ctx context.Context, backend Backend) []ContentResponse {
var responses []ContentResponse
for _, probe := range ContentProbes {
res, err := backend.Generate(ctx, probe.Prompt, GenOpts{Temperature: ContentTemperature, MaxTokens: ContentMaxTokens})
if err != nil {
log.Printf(" [content:%s] ERROR: %v", probe.ID, err)
continue
}
reply := StripThinkBlocks(res.Text)
log.Printf(" [content:%s] got %d chars", probe.ID, len(reply))
responses = append(responses, ContentResponse{
Probe: probe,
Response: reply,
})
}
return responses
}
// RunContentProbesViaRunner sends content probes through an SSH probe runner.
func RunContentProbesViaRunner(stdin io.WriteCloser, scanner *bufio.Scanner) []ContentResponse {
var responses []ContentResponse
for _, probe := range ContentProbes {
req := map[string]any{
"prompt": probe.Prompt,
"max_tokens": ContentMaxTokens,
"temp": ContentTemperature,
}
reqJSON, _ := json.Marshal(req)
fmt.Fprintf(stdin, "%s\n", reqJSON)
var response string
if scanner.Scan() {
var resp probeRunnerResponse
if err := json.Unmarshal(scanner.Bytes(), &resp); err != nil {
log.Printf(" [content:%s] parse error: %v", probe.ID, err)
continue
} else if resp.Error != "" {
log.Printf(" [content:%s] ERROR: %s", probe.ID, resp.Error)
continue
} else {
response = resp.Response
}
} else {
log.Printf(" [content:%s] no response from runner", probe.ID)
continue
}
response = StripThinkBlocks(response)
log.Printf(" [content:%s] got %d chars", probe.ID, len(response))
responses = append(responses, ContentResponse{
Probe: probe,
Response: response,
})
}
return responses
}