-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathtest_accuracy_length.py
More file actions
286 lines (231 loc) · 11.1 KB
/
Copy pathtest_accuracy_length.py
File metadata and controls
286 lines (231 loc) · 11.1 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
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
"""
Tests for cosine-scaled accuracy + length reward function.
"""
import os
import sys
import unittest
# Add the parent directory to sys.path
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))
from eval_protocol.models import EvaluateResult, Message
from eval_protocol.rewards.accuracy_length import cosine_scaled_accuracy_length_reward
class TestCosineScaledAccuracyLengthReward(unittest.TestCase):
"""Test the cosine-scaled accuracy + length reward function."""
def test_correct_short_answer(self):
"""Test with a correct short answer."""
content = "The answer is 4."
messages = [
{"role": "user", "content": "What is 2+2?"},
{"role": "assistant", "content": content},
]
gt_list = [{"role": "assistant", "content": "4"}]
result = cosine_scaled_accuracy_length_reward(messages=messages, ground_truth=gt_list, max_length=50)
self.assertIsInstance(result, EvaluateResult)
# Should get a high score for short correct answer
# Attribute access
self.assertGreaterEqual(result.score, 0.7)
self.assertTrue(result.metrics["combined_reward"].is_score_valid)
self.assertTrue(result.metrics["accuracy"].is_score_valid)
# Dictionary access
self.assertGreaterEqual(result["score"], 0.7)
self.assertTrue(result["metrics"]["combined_reward"]["is_score_valid"])
self.assertTrue(result["metrics"]["accuracy"]["is_score_valid"])
def test_correct_long_answer(self):
"""Test with a correct but verbose answer."""
content = """
To solve this addition problem, I'll work through it step by step.
Starting with 2+2:
- I have 2 units
- I need to add 2 more units
- 2 + 2 = 4
Therefore, the answer is 4.
"""
messages = [
{"role": "user", "content": "What is 2+2?"},
{"role": "assistant", "content": content},
]
gt_list = [{"role": "assistant", "content": "4"}]
result = cosine_scaled_accuracy_length_reward(messages=messages, ground_truth=gt_list, max_length=50)
self.assertIsInstance(result, EvaluateResult)
# Should get a medium score for correct but verbose answer
# Attribute access
self.assertGreaterEqual(result.score, 0.5)
self.assertTrue(result.metrics["combined_reward"].is_score_valid)
self.assertTrue(result.metrics["accuracy"].is_score_valid)
# Dictionary access
self.assertGreaterEqual(result["score"], 0.5)
self.assertTrue(result["metrics"]["combined_reward"]["is_score_valid"])
self.assertTrue(result["metrics"]["accuracy"]["is_score_valid"])
def test_incorrect_short_answer(self):
"""Test with an incorrect short answer."""
content = "The answer is 5."
messages = [
{"role": "user", "content": "What is 2+2?"},
{"role": "assistant", "content": content},
]
gt_list = [{"role": "assistant", "content": "4"}]
result = cosine_scaled_accuracy_length_reward(messages=messages, ground_truth=gt_list, max_length=50)
self.assertIsInstance(result, EvaluateResult)
# Should get a low score for incorrect answer
# Attribute access
self.assertLess(result.score, 0.5)
self.assertFalse(result.metrics["combined_reward"].is_score_valid)
self.assertFalse(result.metrics["accuracy"].is_score_valid)
# Dictionary access
self.assertLess(result["score"], 0.5)
self.assertFalse(result["metrics"]["combined_reward"]["is_score_valid"])
self.assertFalse(result["metrics"]["accuracy"]["is_score_valid"])
def test_incorrect_long_answer(self):
"""Test with an incorrect verbose answer."""
content = """
To solve this addition problem, I'll work through it step by step.
Starting with 2+2:
- I have 2 units
- I need to add 2 more units
- Since 2 is followed by 3, which is followed by 5
- Therefore 2 + 2 = 5
So the answer is 5.
"""
messages = [
{"role": "user", "content": "What is 2+2?"},
{"role": "assistant", "content": content},
]
gt_list = [{"role": "assistant", "content": "4"}]
result = cosine_scaled_accuracy_length_reward(messages=messages, ground_truth=gt_list, max_length=50)
self.assertIsInstance(result, EvaluateResult)
# Should get a low score for incorrect verbose answer
# But slightly higher than incorrect short answer
# Attribute access
self.assertLess(result.score, 0.5)
self.assertFalse(result.metrics["combined_reward"].is_score_valid)
self.assertFalse(result.metrics["accuracy"].is_score_valid)
# Dictionary access
self.assertLess(result["score"], 0.5)
self.assertFalse(result["metrics"]["combined_reward"]["is_score_valid"])
self.assertFalse(result["metrics"]["accuracy"]["is_score_valid"])
def test_correct_short_vs_correct_long(self):
"""Test that correct short answers score higher than correct long answers."""
content_short = "The answer is 4."
content_long = """
To solve this addition problem, I'll work through it step by step.
Starting with 2+2:
- I have 2 units
- I need to add 2 more units
- 2 + 2 = 4
Therefore, the answer is 4.
"""
messages_short = [
{"role": "user", "content": "What is 2+2?"},
{"role": "assistant", "content": content_short},
]
messages_long = [
{"role": "user", "content": "What is 2+2?"},
{"role": "assistant", "content": content_long},
]
gt_list = [{"role": "assistant", "content": "4"}]
result_short = cosine_scaled_accuracy_length_reward(
messages=messages_short, ground_truth=gt_list, max_length=50
)
result_long = cosine_scaled_accuracy_length_reward(messages=messages_long, ground_truth=gt_list, max_length=50)
self.assertIsInstance(result_short, EvaluateResult)
self.assertIsInstance(result_long, EvaluateResult)
# Short correct should score higher than long correct
# Attribute access
self.assertGreater(result_short.score, result_long.score)
# Dictionary access
self.assertGreater(result_short["score"], result_long["score"])
def test_incorrect_short_vs_incorrect_long(self):
"""Test that incorrect long answers score slightly higher than incorrect short."""
content_short = "The answer is 5."
content_long = """
To solve this addition problem, I'll work through it step by step.
Starting with 2+2:
- I have 2 units
- I need to add 2 more units
- Since 2 is followed by 3, which is followed by 5
- Therefore 2 + 2 = 5
So the answer is 5.
"""
messages_short = [
{"role": "user", "content": "What is 2+2?"},
{"role": "assistant", "content": content_short},
]
messages_long = [
{"role": "user", "content": "What is 2+2?"},
{"role": "assistant", "content": content_long},
]
gt_list = [{"role": "assistant", "content": "4"}]
result_short = cosine_scaled_accuracy_length_reward(
messages=messages_short, ground_truth=gt_list, max_length=50
)
result_long = cosine_scaled_accuracy_length_reward(messages=messages_long, ground_truth=gt_list, max_length=50)
self.assertIsInstance(result_short, EvaluateResult)
self.assertIsInstance(result_long, EvaluateResult)
# Long incorrect should score slightly higher than short incorrect
# This rewards showing work even if the answer is wrong
# Attribute access
self.assertGreaterEqual(result_long.score, result_short.score)
# Dictionary access
self.assertGreaterEqual(result_long["score"], result_short["score"])
def test_custom_weights(self):
"""Test with custom accuracy and length weights."""
content = "The answer is 4."
messages = [
{"role": "user", "content": "What is 2+2?"},
{"role": "assistant", "content": content},
]
gt_list = [{"role": "assistant", "content": "4"}]
# Default weights (accuracy_weight=0.7, length_weight=0.3)
result_default = cosine_scaled_accuracy_length_reward(messages=messages, ground_truth=gt_list, max_length=50)
# Custom weights (accuracy_weight=0.9, length_weight=0.1)
result_custom = cosine_scaled_accuracy_length_reward(
messages=messages,
ground_truth=gt_list,
max_length=50,
correctness_weight=0.9,
length_weight=0.1,
)
self.assertIsInstance(result_default, EvaluateResult)
self.assertIsInstance(result_custom, EvaluateResult)
# Scores should be different with different weights
# Attribute access
self.assertNotEqual(result_default.score, result_custom.score)
# Dictionary access
self.assertNotEqual(result_default["score"], result_custom["score"])
def test_correct_beats_incorrect(self):
"""Test that even long correct answers beat short incorrect answers."""
content_long_correct = """
To solve this addition problem, I'll work through it step by step.
I'll start by adding 2 and 2 together.
2 represents a quantity of two units.
When I add another 2 units, I get a total of 4 units.
This is a simple addition problem that demonstrates the basic concept of addition.
In mathematics, addition is one of the four basic operations, alongside subtraction, multiplication, and division.
For this specific problem, I'll now calculate:
2 + 2 = 4
The answer is 4.
"""
content_short_incorrect = "The answer is 5."
messages_long_correct = [
{"role": "user", "content": "What is 2+2?"},
{"role": "assistant", "content": content_long_correct},
]
messages_short_incorrect = [
{"role": "user", "content": "What is 2+2?"},
{"role": "assistant", "content": content_short_incorrect},
]
gt_list = [{"role": "assistant", "content": "4"}]
result_long_correct = cosine_scaled_accuracy_length_reward(
messages=messages_long_correct, ground_truth=gt_list, max_length=50
)
result_short_incorrect = cosine_scaled_accuracy_length_reward(
messages=messages_short_incorrect, ground_truth=gt_list, max_length=50
)
self.assertIsInstance(result_long_correct, EvaluateResult)
self.assertIsInstance(result_short_incorrect, EvaluateResult)
# Long correct should always beat short incorrect
# Attribute access
self.assertGreater(result_long_correct.score, result_short_incorrect.score)
# Dictionary access
self.assertGreater(result_long_correct["score"], result_short_incorrect["score"])
if __name__ == "__main__":
unittest.main()