forked from promptfoo/promptfoo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv.test.ts
427 lines (343 loc) · 13.1 KB
/
csv.test.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
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
import { assertionFromString, testCaseFromCsvRow } from '../src/csv';
import logger from '../src/logger';
import type { Assertion, CsvRow, TestCase } from '../src/types';
jest.mock('../src/logger', () => ({
warn: jest.fn(),
}));
describe('testCaseFromCsvRow', () => {
it('should create a TestCase with assertions and options from a CSV row', () => {
const row: CsvRow = {
__expected1: 'equals:Expected output',
__expected2: 'contains:part of output',
__prefix: 'Prefix',
__suffix: 'Suffix',
__description: 'Test description',
__providerOutput: 'Provider output',
__metric: 'metric-name',
__threshold: '0.8',
var1: 'value1',
var2: 'value2',
};
const expectedTestCase: TestCase = {
vars: {
var1: 'value1',
var2: 'value2',
},
assert: [
{ type: 'equals', value: 'Expected output', metric: 'metric-name' },
{ type: 'contains', value: 'part of output', metric: 'metric-name' },
],
options: {
prefix: 'Prefix',
suffix: 'Suffix',
},
description: 'Test description',
providerOutput: 'Provider output',
threshold: 0.8,
};
const result = testCaseFromCsvRow(row);
expect(result).toEqual(expectedTestCase);
});
it('should handle CSV row with only variables', () => {
const row: CsvRow = {
var1: 'value1',
var2: 'value2',
};
const expectedTestCase: TestCase = {
vars: {
var1: 'value1',
var2: 'value2',
},
assert: [],
options: {},
};
const result = testCaseFromCsvRow(row);
expect(result).toEqual(expectedTestCase);
});
it('should log a warning for single underscore usage with reserved keys', () => {
const row: CsvRow = {
_expected: 'equals:Expected output',
var1: 'value1',
};
testCaseFromCsvRow(row);
testCaseFromCsvRow(row);
expect(logger.warn).toHaveBeenCalledWith(
'You used a single underscore for the key "_expected". Did you mean to use "__expected" instead?',
);
expect(logger.warn).toHaveBeenCalledTimes(1);
});
});
describe('assertionFromString', () => {
it('should create an equality assertion', () => {
const expected = 'Expected output';
const result: Assertion = assertionFromString(expected);
expect(result.type).toBe('equals');
expect(result.value).toBe(expected);
});
it('should create an is-json assertion', () => {
const expected = 'is-json';
const result: Assertion = assertionFromString(expected);
expect(result.type).toBe('is-json');
});
it('should create an is-json assertion with value', () => {
const expected = `is-json:
required: ['color']
type:object
properties:
color:
type:string
`;
const result: Assertion = assertionFromString(expected);
expect(result.type).toBe('is-json');
expect(result.value).toBe(
`
required: ['color']
type:object
properties:
color:
type:string
`,
);
});
it('should create an contains-json assertion', () => {
const expected = 'contains-json';
const result: Assertion = assertionFromString(expected);
expect(result.type).toBe('contains-json');
});
it('should create a function assertion', () => {
const expected = 'fn:output === "Expected output"';
const result: Assertion = assertionFromString(expected);
expect(result.type).toBe('javascript');
expect(result.value).toBe('output === "Expected output"');
});
it('should create a similarity assertion', () => {
const expected = 'similar(0.9):Expected output';
const result: Assertion = assertionFromString(expected);
expect(result.type).toBe('similar');
expect(result.value).toBe('Expected output');
expect(result.threshold).toBe(0.9);
});
it('should create a contains assertion', () => {
const expected = 'contains:substring';
const result: Assertion = assertionFromString(expected);
expect(result.type).toBe('contains');
expect(result.value).toBe('substring');
});
it('should create a not-contains assertion', () => {
const expected = 'not-contains:substring';
const result: Assertion = assertionFromString(expected);
expect(result.type).toBe('not-contains');
expect(result.value).toBe('substring');
});
it('should create a contains-any assertion', () => {
const expected = 'contains-any:substring1,substring2';
const result: Assertion = assertionFromString(expected);
expect(result.type).toBe('contains-any');
expect(result.value).toEqual(['substring1', 'substring2']);
});
it('should create a contains-all assertion', () => {
const expected = 'contains-all:substring1,substring2';
const result: Assertion = assertionFromString(expected);
expect(result.type).toBe('contains-all');
expect(result.value).toEqual(['substring1', 'substring2']);
});
it('should create a regex assertion', () => {
const expected = 'regex:\\d+';
const result: Assertion = assertionFromString(expected);
expect(result.type).toBe('regex');
expect(result.value).toBe('\\d+');
});
it('should create a not-regex assertion', () => {
const expected = 'not-regex:\\d+';
const result: Assertion = assertionFromString(expected);
expect(result.type).toBe('not-regex');
expect(result.value).toBe('\\d+');
});
it('should create an icontains assertion', () => {
const expected = 'icontains:substring';
const result: Assertion = assertionFromString(expected);
expect(result.type).toBe('icontains');
expect(result.value).toBe('substring');
});
it('should create a not-icontains assertion', () => {
const expected = 'not-icontains:substring';
const result: Assertion = assertionFromString(expected);
expect(result.type).toBe('not-icontains');
expect(result.value).toBe('substring');
});
it('should create a webhook assertion', () => {
const expected = 'webhook:https://example.com';
const result: Assertion = assertionFromString(expected);
expect(result.type).toBe('webhook');
expect(result.value).toBe('https://example.com');
});
it('should create a not-webhook assertion', () => {
const expected = 'not-webhook:https://example.com';
const result: Assertion = assertionFromString(expected);
expect(result.type).toBe('not-webhook');
expect(result.value).toBe('https://example.com');
});
it('should create a rouge-n assertion', () => {
const expected = 'rouge-n(0.225):foo';
const result: Assertion = assertionFromString(expected);
expect(result.type).toBe('rouge-n');
expect(result.value).toBe('foo');
expect(result.threshold).toBeCloseTo(0.225);
});
it('should create a not-rouge-n assertion', () => {
const expected = 'not-rouge-n(0.225):foo';
const result: Assertion = assertionFromString(expected);
expect(result.type).toBe('not-rouge-n');
expect(result.value).toBe('foo');
expect(result.threshold).toBeCloseTo(0.225);
});
it('should create a starts-with assertion', () => {
const expected = 'starts-with:Expected';
const result: Assertion = assertionFromString(expected);
expect(result.type).toBe('starts-with');
expect(result.value).toBe('Expected');
});
it('should create a levenshtein assertion', () => {
const expected = 'levenshtein(5):Expected output';
const result: Assertion = assertionFromString(expected);
expect(result.type).toBe('levenshtein');
expect(result.value).toBe('Expected output');
expect(result.threshold).toBe(5);
});
it('should create a classifier assertion', () => {
const expected = 'classifier(0.5):classA';
const result: Assertion = assertionFromString(expected);
expect(result.type).toBe('classifier');
expect(result.value).toBe('classA');
expect(result.threshold).toBe(0.5);
});
it('should create a latency assertion', () => {
const expected = 'latency(1000)';
const result: Assertion = assertionFromString(expected);
expect(result.type).toBe('latency');
expect(result.threshold).toBe(1000);
});
it('should create a perplexity assertion', () => {
const expected = 'perplexity(1.5)';
const result: Assertion = assertionFromString(expected);
expect(result.type).toBe('perplexity');
expect(result.threshold).toBe(1.5);
});
it('should create a perplexity-score assertion', () => {
const expected = 'perplexity-score(0.5)';
const result: Assertion = assertionFromString(expected);
expect(result.type).toBe('perplexity-score');
expect(result.threshold).toBe(0.5);
});
it('should create a cost assertion', () => {
const expected = 'cost(0.001)';
const result: Assertion = assertionFromString(expected);
expect(result.type).toBe('cost');
expect(result.threshold).toBe(0.001);
});
it('should create an openai function call assertion', () => {
const expected = 'is-valid-openai-function-call';
const result: Assertion = assertionFromString(expected);
expect(result.type).toBe('is-valid-openai-function-call');
});
it('should create a python assertion', () => {
const expected = 'python: file://file.py ';
const result: Assertion = assertionFromString(expected);
expect(result.type).toBe('python');
expect(result.value).toBe('file://file.py');
});
it('should parse python: assertion', () => {
const assertion = assertionFromString('python:some_python_code');
expect(assertion).toEqual({
type: 'python',
value: 'some_python_code',
});
});
it(`should parse file:// prefix assertion`, () => {
const assertion = assertionFromString('file://script.py');
expect(assertion).toEqual({
type: 'python',
value: 'script.py',
});
});
it(`should parse file:// prefix assertion with function name`, () => {
const assertion = assertionFromString('file://script.py:function_name');
expect(assertion).toEqual({
type: 'python',
value: 'script.py:function_name',
});
});
it('should create a javascript assertion', () => {
const expected = 'javascript: x > 10';
const result: Assertion = assertionFromString(expected);
expect(result.type).toBe('javascript');
expect(result.value).toBe('x > 10');
});
it('should create an llm-rubric assertion with "grade:" prefix', () => {
const expected = 'grade:Evaluate the response based on clarity and accuracy';
const result: Assertion = assertionFromString(expected);
expect(result.type).toBe('llm-rubric');
expect(result.value).toBe('Evaluate the response based on clarity and accuracy');
});
it('should create an llm-rubric assertion with "llm-rubric:" prefix', () => {
const expected = 'llm-rubric:Rate the answer on a scale of 1-10 for completeness';
const result: Assertion = assertionFromString(expected);
expect(result.type).toBe('llm-rubric');
expect(result.value).toBe('Rate the answer on a scale of 1-10 for completeness');
});
it('should handle legacy javascript option', () => {
const expected = 'javascript:output === "Expected output"';
const result: Assertion = assertionFromString(expected);
expect(result.type).toBe('javascript');
expect(result.value).toBe('output === "Expected output"');
});
it('should handle legacy eval option', () => {
const expected = 'eval:output === "Expected output"';
const result: Assertion = assertionFromString(expected);
expect(result.type).toBe('javascript');
expect(result.value).toBe('output === "Expected output"');
});
it('should handle legacy fn option', () => {
const expected = 'fn:output === "Expected output"';
const result: Assertion = assertionFromString(expected);
expect(result.type).toBe('javascript');
expect(result.value).toBe('output === "Expected output"');
});
it('should use DEFAULT_SEMANTIC_SIMILARITY_THRESHOLD for similar assertion without threshold', () => {
const expected = 'similar:Expected output';
const result: Assertion = assertionFromString(expected);
expect(result.type).toBe('similar');
expect(result.value).toBe('Expected output');
expect(result.threshold).toBe(0.8); // DEFAULT_SEMANTIC_SIMILARITY_THRESHOLD
});
it('should use 0.75 as default threshold for other assertions requiring threshold', () => {
const assertionTypes = [
'answer-relevance',
'classifier',
'context-faithfulness',
'context-recall',
'context-relevance',
'cost',
'latency',
'levenshtein',
'perplexity-score',
'perplexity',
'rouge-n',
'starts-with',
];
for (const type of assertionTypes) {
const expected = `${type}:Expected output`;
const result: Assertion = assertionFromString(expected);
expect(result.type).toBe(type);
expect(result.value).toBe('Expected output');
expect(result.threshold).toBe(0.75);
}
});
it('should use provided threshold when specified', () => {
const expected = 'similar(0.9):Expected output';
const result: Assertion = assertionFromString(expected);
expect(result.type).toBe('similar');
expect(result.value).toBe('Expected output');
expect(result.threshold).toBe(0.9);
});
});