- Notifications
You must be signed in to change notification settings - Fork 240
/
Copy pathevaluators.go
230 lines (211 loc) · 6.38 KB
/
evaluators.go
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
// Copyright 2024 Google LLC
// SPDX-License-Identifier: Apache-2.0
// Package evaluators defines a set of Genkit Evaluators for popular use-cases
package evaluators
import (
"context"
"errors"
"fmt"
"reflect"
"regexp"
"sync"
jsonata "github.com/blues/jsonata-go"
"github.com/firebase/genkit/go/ai"
"github.com/firebase/genkit/go/core/logger"
"github.com/firebase/genkit/go/genkit"
)
constprovider="genkitEval"
// EvaluatorType is an enum used to indicate the type of evaluator being
// configured for use
typeEvaluatorTypeint
const (
EvaluatorDeepEqualEvaluatorType=iota
EvaluatorRegex
EvaluatorJsonata
)
varevaluatorTypeName=map[EvaluatorType]string{
EvaluatorDeepEqual: "DEEP_EQUAL",
EvaluatorRegex: "REGEX",
EvaluatorJsonata: "JSONATA",
}
func (ssEvaluatorType) String() string {
returnevaluatorTypeName[ss]
}
// MetricConfig provides configuration options for a specific metric. More
// Params (judge LLMs, etc.) could be configured by extending this struct
typeMetricConfigstruct {
MetricTypeEvaluatorType
}
// GenkitEval is a Genkit plugin that provides evaluators
typeGenkitEvalstruct {
Metrics []MetricConfig// Configs for individual metrics
inittedbool// Whether the plugin has been initialized
mu sync.Mutex// Mutex to manage locks
}
func (ge*GenkitEval) Name() string {
returnprovider
}
// Init initializes the plugin.
func (ge*GenkitEval) Init(ctx context.Context, g*genkit.Genkit) (errerror) {
ifge==nil {
ge=&GenkitEval{}
}
ge.mu.Lock()
deferge.mu.Unlock()
ifge.initted {
panic("genkitEval.Init already called")
}
ifge==nil||len(ge.Metrics) ==0 {
returnerrors.New("genkitEval: need to configure at least one metric")
}
ge.initted=true
for_, metric:=rangege.Metrics {
ConfigureMetric(g, metric)
}
returnnil
}
funcConfigureMetric(g*genkit.Genkit, metricMetricConfig) (ai.Evaluator, error) {
switchmetric.MetricType {
caseEvaluatorDeepEqual:
returnconfigureDeepEqualEvaluator(g)
caseEvaluatorJsonata:
returnconfigureJsonataEvaluator(g)
caseEvaluatorRegex:
returnconfigureRegexEvaluator(g)
default:
panic(fmt.Sprintf("Unsupported genkitEval metric type: %s", metric.MetricType.String()))
}
}
funcconfigureRegexEvaluator(g*genkit.Genkit) (ai.Evaluator, error) {
evalOptions:= ai.EvaluatorOptions{
DisplayName: "RegExp",
Definition: "Tests output against the regexp provided as reference",
IsBilled: false,
}
evaluator, err:=genkit.DefineEvaluator(g, provider, "regex", &evalOptions, func(ctx context.Context, req*ai.EvaluatorCallbackRequest) (*ai.EvaluatorCallbackResponse, error) {
dataPoint:=req.Input
varscore ai.Score
ifdataPoint.Output==nil {
returnnil, errors.New("output was not provided")
}
ifdataPoint.Reference==nil {
returnnil, errors.New("reference was not provided")
}
ifreflect.TypeOf(dataPoint.Reference).String() !="string" {
returnnil, errors.New("reference must be a string (regex)")
}
ifreflect.TypeOf(dataPoint.Output).String() =="string" {
// Test against provided regexp
match, _:=regexp.MatchString((dataPoint.Reference).(string), (dataPoint.Output).(string))
status:=ai.ScoreStatusUnknown
ifmatch {
status=ai.ScoreStatusPass
} else {
status=ai.ScoreStatusFail
}
score= ai.Score{
Score: match,
Status: status.String(),
}
} else {
// Mark as failed if output is not string type
logger.FromContext(ctx).Debug("genkitEval",
"regex", fmt.Sprintf("Failed regex evaluation, as output is not string type. TestCaseId: %s", dataPoint.TestCaseId))
score= ai.Score{
Score: false,
Status: ai.ScoreStatusFail.String(),
}
}
callbackResponse:= ai.EvaluatorCallbackResponse{
TestCaseId: req.Input.TestCaseId,
Evaluation: []ai.Score{score},
}
return&callbackResponse, nil
})
iferr!=nil {
returnnil, err
}
returnevaluator, nil
}
funcconfigureDeepEqualEvaluator(g*genkit.Genkit) (ai.Evaluator, error) {
evalOptions:= ai.EvaluatorOptions{
DisplayName: "Deep Equal",
Definition: "Tests equality of output against the provided reference",
IsBilled: false,
}
evaluator, err:=genkit.DefineEvaluator(g, provider, "deep_equal", &evalOptions, func(ctx context.Context, req*ai.EvaluatorCallbackRequest) (*ai.EvaluatorCallbackResponse, error) {
dataPoint:=req.Input
varscore ai.Score
ifdataPoint.Output==nil {
returnnil, errors.New("output was not provided")
}
ifdataPoint.Reference==nil {
returnnil, errors.New("reference was not provided")
}
deepEqual:=reflect.DeepEqual(dataPoint.Reference, dataPoint.Output)
status:=ai.ScoreStatusUnknown
ifdeepEqual {
status=ai.ScoreStatusPass
} else {
status=ai.ScoreStatusFail
}
score= ai.Score{
Score: deepEqual,
Status: status.String(),
}
callbackResponse:= ai.EvaluatorCallbackResponse{
TestCaseId: req.Input.TestCaseId,
Evaluation: []ai.Score{score},
}
return&callbackResponse, nil
})
iferr!=nil {
returnnil, err
}
returnevaluator, nil
}
funcconfigureJsonataEvaluator(g*genkit.Genkit) (ai.Evaluator, error) {
evalOptions:= ai.EvaluatorOptions{
DisplayName: "JSONata",
Definition: "Tests JSONata expression (provided in reference) against output",
IsBilled: false,
}
evaluator, err:=genkit.DefineEvaluator(g, provider, "jsonata", &evalOptions, func(ctx context.Context, req*ai.EvaluatorCallbackRequest) (*ai.EvaluatorCallbackResponse, error) {
dataPoint:=req.Input
varscore ai.Score
ifdataPoint.Output==nil {
returnnil, errors.New("output was not provided")
}
ifdataPoint.Reference==nil {
returnnil, errors.New("reference was not provided")
}
ifreflect.TypeOf(dataPoint.Reference).String() !="string" {
returnnil, errors.New("reference must be a string (jsonata)")
}
// Test against provided jsonata
exp:=jsonata.MustCompile((dataPoint.Reference).(string))
res, err:=exp.Eval(dataPoint.Output)
iferr!=nil {
returnnil, err
}
status:=ai.ScoreStatusUnknown
ifres==false||res==""||res==nil {
status=ai.ScoreStatusFail
} else {
status=ai.ScoreStatusPass
}
score= ai.Score{
Score: res,
Status: status.String(),
}
callbackResponse:= ai.EvaluatorCallbackResponse{
TestCaseId: req.Input.TestCaseId,
Evaluation: []ai.Score{score},
}
return&callbackResponse, nil
})
iferr!=nil {
returnnil, err
}
returnevaluator, nil
}