-
Notifications
You must be signed in to change notification settings - Fork 2k
/
base.ts
359 lines (325 loc) Β· 11.3 KB
/
base.ts
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
import type { BaseLanguageModelInterface } from "@langchain/core/language_models/base";
import { AgentStep } from "@langchain/core/agents";
import { ChainValues } from "@langchain/core/utils/types";
import {
BaseCallbackConfig,
Callbacks,
} from "@langchain/core/callbacks/manager";
import { BaseChain, LLMChain, LLMChainInput } from "../chains/index.js";
/**
* Base input for evaluators.
*/
export interface LLMEvalChainInput<
T extends EvalOutputType = EvalOutputType,
L extends BaseLanguageModelInterface = BaseLanguageModelInterface
> extends LLMChainInput<T, L> {}
export type ExtractLLMCallOptions<LanguageModelInterface> =
LanguageModelInterface extends BaseLanguageModelInterface<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
any,
infer CallOptions
>
? CallOptions
: never;
/**
* Compare two sets for equality
*
* @param xs
* @param ys
*/
export const eqSet = (xs: Set<string>, ys: Set<string>) =>
xs.size === ys.size && [...xs].every((x) => ys.has(x));
/**
* The type of the output of an evaluation evaluator.
*/
export type EvalOutputType = Record<string, string | number | boolean>;
/**
* Base llm chain class for evaluators.
*/
export abstract class LLMEvalChain<
T extends EvalOutputType = EvalOutputType,
L extends BaseLanguageModelInterface = BaseLanguageModelInterface
> extends LLMChain<T, L> {
requiresInput?: boolean = false;
requiresReference?: boolean = false;
skipInputWarning?: string = `Ignoring input in ${this.constructor.name}, as it is not expected.`;
skipReferenceWarning?: string = `Ignoring reference in ${this.constructor.name}, as it is not expected.`;
/**
* Check if the evaluation arguments are valid.
* @param reference The reference label.
* @param input The input string.
* @throws {Error} If the evaluator requires an input string but none is provided, or if the evaluator requires a reference label but none is provided.
*/
checkEvaluationArgs(reference?: string, input?: string): void {
if (this.requiresInput && input == null) {
throw new Error(`${this.constructor.name} requires an input string.`);
} else if (input != null && !this.requiresInput) {
console.warn(this.skipInputWarning);
}
if (this.requiresReference && reference == null) {
throw new Error(`${this.constructor.name} requires a reference string.`);
} else if (reference != null && !this.requiresReference) {
console.warn(this.skipReferenceWarning);
}
}
}
/**
* Base chain class for evaluators.
*/
export abstract class EvalChain<
RunInput extends ChainValues = ChainValues,
RunOutput extends ChainValues = ChainValues
> extends BaseChain<RunInput, RunOutput> {
requiresInput?: boolean = false;
requiresReference?: boolean = false;
skipInputWarning?: string = `Ignoring input in ${this.constructor.name}, as it is not expected.`;
skipReferenceWarning?: string = `Ignoring reference in ${this.constructor.name}, as it is not expected.`;
/**
* Check if the evaluation arguments are valid.
* @param reference The reference label.
* @param input The input string.
* @throws {Error} If the evaluator requires an input string but none is provided, or if the evaluator requires a reference label but none is provided.
*/
checkEvaluationArgs(reference?: string, input?: string): void {
if (this.requiresInput && input == null) {
throw new Error(`${this.constructor.name} requires an input string.`);
} else if (input != null && !this.requiresInput) {
console.warn(this.skipInputWarning);
}
if (this.requiresReference && reference == null) {
throw new Error(`${this.constructor.name} requires a reference string.`);
} else if (reference != null && !this.requiresReference) {
console.warn(this.skipReferenceWarning);
}
}
}
/**
* @field prediction The output string from the model.
* @field reference The expected output / reference string.
* @field input The input string.
*/
export interface StringEvaluatorArgs {
prediction: string;
reference?: string;
input?: string;
}
/**
* @field prediction The output string from the first model.
* @field predictionB The output string from the second model.
*/
export interface PairwiseStringEvaluatorArgs {
prediction: string;
predictionB: string;
}
/**
* @field The input string.
* @field prediction The output string from the first model.
* @field predictionB The output string from the second model.
* @field reference The expected output / reference string.
*/
export interface LLMPairwiseStringEvaluatorArgs {
input: string;
prediction: string;
predictionB: string;
reference?: string;
}
/**
* Args for AgentTrajectoryEvaluator
* @field input The input to the agent.
* @field prediction The final predicted response.
* @field reference The reference answer.
* @field agentTrajectory The intermediate steps forming the agent trajectory.
*/
export interface LLMTrajectoryEvaluatorArgs {
input: string;
prediction: string;
reference?: string;
agentTrajectory: AgentStep[];
}
/**
* Grade, tag, or otherwise evaluate predictions relative to their inputs
* and/or reference labels
*/
export abstract class LLMStringEvaluator<
T extends EvalOutputType = EvalOutputType,
L extends BaseLanguageModelInterface = BaseLanguageModelInterface
> extends LLMEvalChain<T, L> {
/**
* The name of the evaluation.
*/
evaluationName?: string = this.constructor.name;
/**
* Evaluate Chain or LLM output, based on optional input and label.
* @returns The evaluation results containing the score or value. It is recommended that the dictionary contain the following keys:
* - score: the score of the evaluation, if applicable.
* - value: the string value of the evaluation, if applicable.
* - reasoning: the reasoning for the evaluation, if applicable.
* @param args
* @param callOptions
* @param config
*/
abstract _evaluateStrings(
args: StringEvaluatorArgs & ExtractLLMCallOptions<this["llm"]>,
config?: Callbacks | BaseCallbackConfig
): Promise<ChainValues>;
/**
* Evaluate Chain or LLM output, based on optional input and label.
* @returns The evaluation results containing the score or value. It is recommended that the dictionary contain the following keys:
* - score: the score of the evaluation, if applicable.
* - value: the string value of the evaluation, if applicable.
* - reasoning: the reasoning for the evaluation, if applicable.
* @param args
* @param callOptions
* @param config
*/
evaluateStrings(
args: StringEvaluatorArgs & ExtractLLMCallOptions<this["llm"]>,
config?: Callbacks | BaseCallbackConfig
): Promise<ChainValues> {
this.checkEvaluationArgs(args.reference, args.input);
return this._evaluateStrings(args, config);
}
}
/**
* Grade, tag, or otherwise evaluate predictions relative to their inputs
* and/or reference labels
*/
export abstract class StringEvaluator extends EvalChain {
/**
* The name of the evaluation.
*/
evaluationName?: string = this.constructor.name;
/**
* Evaluate Chain or LLM output, based on optional input and label.
* @returns The evaluation results containing the score or value. It is recommended that the dictionary contain the following keys:
* - score: the score of the evaluation, if applicable.
* - value: the string value of the evaluation, if applicable.
* - reasoning: the reasoning for the evaluation, if applicable.
* @param args
* @param config
*/
abstract _evaluateStrings(
args: StringEvaluatorArgs,
config?: Callbacks | BaseCallbackConfig
): Promise<ChainValues>;
/**
* Evaluate Chain or LLM output, based on optional input and label.
* @returns The evaluation results containing the score or value. It is recommended that the dictionary contain the following keys:
* - score: the score of the evaluation, if applicable.
* - value: the string value of the evaluation, if applicable.
* - reasoning: the reasoning for the evaluation, if applicable.
* @param args
* @param config
*/
evaluateStrings(
args: StringEvaluatorArgs,
config?: Callbacks | BaseCallbackConfig
): Promise<ChainValues> {
this.checkEvaluationArgs(args.reference, args.input);
return this._evaluateStrings(args, config);
}
}
/**
* Compare the output of two models (or two outputs of the same model).
*/
export abstract class PairwiseStringEvaluator extends EvalChain {
/**
* The name of the evaluation.
*/
evaluationName?: string = this.constructor.name;
/**
* Evaluate the output string pairs.
* @param args
* @param config
* @return A dictionary containing the preference, scores, and/or other information.
*/
abstract _evaluateStringPairs(
args: PairwiseStringEvaluatorArgs,
config?: Callbacks | BaseCallbackConfig
): Promise<ChainValues>;
/**
* Evaluate the output string pairs.
* @param args
* @param config
* @return A dictionary containing the preference, scores, and/or other information.
*/
evaluateStringPairs(
args: PairwiseStringEvaluatorArgs,
config?: Callbacks | BaseCallbackConfig
): Promise<ChainValues> {
return this._evaluateStringPairs(args, config);
}
}
/**
* Compare the output of two models (or two outputs of the same model).
*/
export abstract class LLMPairwiseStringEvaluator extends LLMEvalChain {
/**
* The name of the evaluation.
*/
evaluationName?: string = this.constructor.name;
/**
* Evaluate the output string pairs.
* @param args
* @param callOptions
* @param config
* @return A dictionary containing the preference, scores, and/or other information.
*/
abstract _evaluateStringPairs(
args: LLMPairwiseStringEvaluatorArgs,
callOptions?: ExtractLLMCallOptions<this["llm"]>,
config?: Callbacks | BaseCallbackConfig
): Promise<ChainValues>;
/**
* Evaluate the output string pairs.
* @param args
* @param callOptions
* @param config
* @return A dictionary containing the preference, scores, and/or other information.
*/
evaluateStringPairs(
args: LLMPairwiseStringEvaluatorArgs,
callOptions?: ExtractLLMCallOptions<this["llm"]>,
config?: Callbacks | BaseCallbackConfig
): Promise<ChainValues> {
this.checkEvaluationArgs(args.reference, args.input);
return this._evaluateStringPairs(args, callOptions, config);
}
}
/**
* Interface for evaluating agent trajectories.
*/
export abstract class AgentTrajectoryEvaluator extends LLMEvalChain {
requiresInput = true;
/**
* The name of the evaluation.
*/
evaluationName?: string = this.constructor.name;
/**
* Evaluate a trajectory.
* @return The evaluation result.
* @param args
* @param callOptions
* @param config
*/
abstract _evaluateAgentTrajectory(
args: LLMTrajectoryEvaluatorArgs,
callOptions?: ExtractLLMCallOptions<this["llm"]>,
config?: Callbacks | BaseCallbackConfig
): Promise<ChainValues>;
/**
* Evaluate a trajectory.
* @return The evaluation result.
* @param args
* @param callOptions
* @param config
*/
evaluateAgentTrajectory(
args: LLMTrajectoryEvaluatorArgs,
callOptions?: ExtractLLMCallOptions<this["llm"]>,
config?: Callbacks | BaseCallbackConfig
): Promise<ChainValues> {
this.checkEvaluationArgs(args.reference, args.input);
return this._evaluateAgentTrajectory(args, callOptions, config);
}
}