forked from promptfoo/promptfoo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbleu.test.ts
188 lines (160 loc) · 6.22 KB
/
bleu.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
import { calculateBleuScore, handleBleuScore } from '../../src/assertions/bleu';
import type { AssertionParams } from '../../src/types';
describe('BLEU score calculation', () => {
it('identical sentences should have BLEU score close to, but not equal to one due to smoothing', () => {
const references = ['The cat sat on the mat.'];
const candidate = 'The cat sat on the mat.';
const score = calculateBleuScore(candidate, references);
expect(score).toBeGreaterThan(0.999);
});
it('completely different sentences should have very low but non-zero BLEU score due to smoothing', () => {
const references = ['The cat sat on the mat.'];
const candidate = 'Dogs run in the park.';
const score = calculateBleuScore(candidate, references);
expect(score).toBeGreaterThan(0.0);
expect(score).toBeLessThan(0.001);
});
it('partially matching sentences should have score between 0 and 1', () => {
const references = ['The cat sat on the mat.'];
const candidate = 'The dog sat on the mat.';
const score = calculateBleuScore(candidate, references);
expect(score).toBeGreaterThan(0.5);
expect(score).toBeLessThan(1.0);
});
it('should handle custom weights', () => {
const references = ['The cat sat on the mat.'];
const candidate = 'The cat sat on the mat.';
const weights = [0.25, 0.25, 0.25, 0.25];
const score = calculateBleuScore(candidate, references, weights);
expect(score).toBeGreaterThan(0.999);
});
it('should handle empty or single word sentences', () => {
const references = ['cat'];
const candidate = 'cat';
const score = calculateBleuScore(candidate, references);
expect(score).toBeGreaterThan(0.0);
});
it('should handle sentences with different lengths', () => {
const references = ['The cat sat on the mat.'];
const candidate = 'The cat sat.';
const score = calculateBleuScore(candidate, references);
expect(score).toBeGreaterThan(0.0);
expect(score).toBeLessThan(1.0);
});
it('should handle multiple references and take best matching score', () => {
const references = [
'The cat sat on the mat.',
'There is a cat on the mat.',
'A cat is sitting on the mat.',
];
const candidate = 'The cat was sitting on the mat.';
const score = calculateBleuScore(candidate, references);
expect(score).toBeGreaterThan(0.25);
});
it('should use closest reference length for brevity penalty', () => {
const references = ['The cat sat on mat.', 'Cat mat.', 'A cat is sitting on a mat.'];
const candidate = 'The cat sat on mat.';
const score = calculateBleuScore(candidate, references);
expect(score).toBeGreaterThan(0.999);
});
it('should throw error for empty reference array', () => {
expect(() => {
calculateBleuScore('test', []);
}).toThrow('Invalid inputs');
});
it('should throw error for invalid weights', () => {
const references = ['The cat sat on the mat.'];
const invalidWeights = [0.5, 0.5, 0.5, 0.5];
expect(() => {
calculateBleuScore('test', references, invalidWeights);
}).toThrow('Weights must sum to 1');
});
it('should throw error for wrong number of weights', () => {
const references = ['The cat sat on the mat.'];
const invalidWeights = [0.5, 0.5];
expect(() => {
calculateBleuScore('test', references, invalidWeights);
}).toThrow('Invalid inputs');
});
it('should handle multiple references with varying lengths', () => {
const references = ['The small cat sat.', 'A cat was sitting.', 'The cat is on the mat.'];
const candidate = 'The small cat sat.';
const score = calculateBleuScore(candidate, references);
expect(score).toBeGreaterThan(0.999);
});
});
describe('handleBleuScore', () => {
it('should handle string reference with passing score', () => {
const params = {
assertion: { type: 'bleu', value: 'The cat sat on the mat.' },
renderedValue: 'The cat sat on the mat.',
outputString: 'The cat sat on the mat.',
inverse: false,
} as AssertionParams;
expect(handleBleuScore(params)).toEqual({
pass: true,
score: expect.any(Number),
reason: 'Assertion passed',
assertion: expect.any(Object),
});
});
it('should handle array of references', () => {
const params = {
assertion: {
type: 'bleu',
value: ['The cat sat on mat.', 'The cat is sitting on mat.'],
},
renderedValue: ['The cat sat on mat.', 'The cat is sitting on mat.'],
outputString: 'The cat sat on mat.',
inverse: false,
} as AssertionParams;
expect(handleBleuScore(params)).toEqual({
pass: true,
score: expect.any(Number),
reason: 'Assertion passed',
assertion: expect.any(Object),
});
});
it('should handle custom threshold', () => {
const params = {
assertion: { type: 'bleu', value: 'The cat sat on the mat.', threshold: 0.8 },
renderedValue: 'The cat sat on the mat.',
outputString: 'The dog sat on the mat.',
inverse: false,
} as AssertionParams;
expect(handleBleuScore(params)).toEqual({
pass: false,
score: expect.any(Number),
reason: expect.stringMatching(/BLEU score \d+\.\d+ is less than threshold 0\.8/),
assertion: expect.any(Object),
});
});
it('should handle inverse assertion', () => {
const params = {
assertion: { type: 'bleu', value: 'The cat sat on the mat.', threshold: 0.8 },
renderedValue: 'The cat sat on the mat.',
outputString: 'The dog ran in the park.',
inverse: true,
} as AssertionParams;
expect(handleBleuScore(params)).toEqual({
pass: true,
score: expect.any(Number),
reason: 'Assertion passed',
assertion: expect.any(Object),
});
});
it('should use default threshold of 0.5', () => {
const params = {
assertion: { type: 'bleu', value: 'The cat sat on the mat.' },
renderedValue: 'The cat sat on the mat.',
outputString: 'The dog ran in the park.',
inverse: false,
} as AssertionParams;
expect(handleBleuScore(params)).toEqual({
pass: false,
score: expect.any(Number),
reason: expect.stringMatching(/BLEU score \d+\.\d+ is less than threshold 0\.5/),
assertion: expect.any(Object),
});
});
});