-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathrunner.go
More file actions
159 lines (136 loc) · 3.57 KB
/
runner.go
File metadata and controls
159 lines (136 loc) · 3.57 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
package matchspec
import (
"context"
"fmt"
"sync"
"time"
"github.com/greynewell/mist-go/protocol"
"github.com/greynewell/mist-go/tokentrace"
"github.com/greynewell/mist-go/trace"
)
// InferFunc is a function that performs inference for evaluation.
// It takes a prompt and returns the model's response.
type InferFunc func(ctx context.Context, prompt string) (string, error)
// Runner executes evaluation suites and collects results.
type Runner struct {
registry *SuiteRegistry
infer InferFunc
reporter *tokentrace.Reporter
mu sync.Mutex
results []protocol.EvalResult
}
// NewRunner creates a runner with the given suite registry and inference function.
func NewRunner(registry *SuiteRegistry, infer InferFunc, reporter *tokentrace.Reporter) *Runner {
return &Runner{
registry: registry,
infer: infer,
reporter: reporter,
}
}
// Run executes all tasks in the named suite and returns the results.
func (r *Runner) Run(ctx context.Context, run protocol.EvalRun) ([]protocol.EvalResult, error) {
suite, ok := r.registry.Get(run.Suite)
if !ok {
return nil, fmt.Errorf("matchspec: unknown suite %q", run.Suite)
}
ctx, span := trace.Start(ctx, "matchspec.eval")
span.SetAttr("suite", run.Suite)
var results []protocol.EvalResult
var passed, failed int
tasks := suite.Tasks
if len(run.Tasks) > 0 {
tasks = filterTasks(suite.Tasks, run.Tasks)
}
for _, task := range tasks {
result := r.runTask(ctx, suite.Name, task)
results = append(results, result)
if result.Passed {
passed++
} else {
failed++
}
}
span.SetAttr("passed", passed)
span.SetAttr("failed", failed)
span.SetAttr("total", len(results))
if failed > 0 {
span.End("error")
} else {
span.End("ok")
}
r.reporter.Report(ctx, span)
r.mu.Lock()
r.results = append(r.results, results...)
r.mu.Unlock()
return results, nil
}
func (r *Runner) runTask(ctx context.Context, suite string, task Task) protocol.EvalResult {
ctx, span := trace.Start(ctx, "matchspec.task")
span.SetAttr("suite", suite)
span.SetAttr("task", task.Name)
start := time.Now()
response, err := r.infer(ctx, task.Prompt)
duration := time.Since(start)
if err != nil {
span.SetAttr("error", err.Error())
span.End("error")
r.reporter.Report(ctx, span)
return protocol.EvalResult{
Suite: suite,
Task: task.Name,
Passed: false,
Score: 0,
DurationMS: duration.Milliseconds(),
Error: err.Error(),
}
}
passed, score := task.Match(response)
status := "ok"
if !passed {
status = "error"
}
span.SetAttr("passed", passed)
span.SetAttr("score", score)
span.End(status)
r.reporter.Report(ctx, span)
return protocol.EvalResult{
Suite: suite,
Task: task.Name,
Passed: passed,
Score: score,
DurationMS: duration.Milliseconds(),
}
}
// Results returns all collected evaluation results.
func (r *Runner) Results() []protocol.EvalResult {
r.mu.Lock()
defer r.mu.Unlock()
cp := make([]protocol.EvalResult, len(r.results))
copy(cp, r.results)
return cp
}
// ResultsBySuite returns results filtered by suite name.
func (r *Runner) ResultsBySuite(suite string) []protocol.EvalResult {
r.mu.Lock()
defer r.mu.Unlock()
var filtered []protocol.EvalResult
for _, res := range r.results {
if res.Suite == suite {
filtered = append(filtered, res)
}
}
return filtered
}
func filterTasks(all []Task, names []string) []Task {
nameSet := make(map[string]bool, len(names))
for _, n := range names {
nameSet[n] = true
}
var filtered []Task
for _, t := range all {
if nameSet[t.Name] {
filtered = append(filtered, t)
}
}
return filtered
}