-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathtest_format.py
More file actions
227 lines (194 loc) · 8.14 KB
/
Copy pathtest_format.py
File metadata and controls
227 lines (194 loc) · 8.14 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
"""
Tests for format reward function.
"""
import os
import re
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.format import format_reward
class TestFormatReward(unittest.TestCase):
"""Test the format reward function."""
def test_think_answer_format_match(self):
"""Test that the format reward correctly identifies matching format."""
# Create a message with the correct format
correct_format = """<think>
This is my reasoning process.
I think the answer is 42.
</think>
<answer>
42
</answer>"""
messages = [
{
"role": "user",
"content": "What is the answer to life, the universe, and everything?",
},
{"role": "assistant", "content": correct_format},
]
result = format_reward(messages=messages, ground_truth=None)
self.assertIsInstance(result, EvaluateResult)
# Check the score is 1.0 for correct format
# Attribute access
self.assertEqual(result.score, 1.0)
self.assertEqual(result.metrics["format_check"].score, 1.0)
self.assertTrue(result.metrics["format_check"].is_score_valid)
# Dictionary access
self.assertEqual(result["score"], 1.0)
self.assertEqual(result["metrics"]["format_check"]["score"], 1.0)
self.assertTrue(result["metrics"]["format_check"]["is_score_valid"])
def test_think_answer_format_mismatch(self):
"""Test that the format reward correctly identifies mismatched format."""
# Create a message with incorrect format
incorrect_format = """I think the answer is 42.
The answer is 42."""
messages = [
{
"role": "user",
"content": "What is the answer to life, the universe, and everything?",
},
{"role": "assistant", "content": incorrect_format},
]
result = format_reward(messages=messages, ground_truth=None)
self.assertIsInstance(result, EvaluateResult)
# Check the score is 0.0 for incorrect format
# Attribute access
self.assertEqual(result.score, 0.0)
self.assertEqual(result.metrics["format_check"].score, 0.0)
self.assertFalse(result.metrics["format_check"].is_score_valid)
# Dictionary access
self.assertEqual(result["score"], 0.0)
self.assertEqual(result["metrics"]["format_check"]["score"], 0.0)
self.assertFalse(result["metrics"]["format_check"]["is_score_valid"])
def test_think_answer_format_wrong_order(self):
"""Test that the format reward fails when tags are in wrong order."""
# Create a message with tags in wrong order
wrong_order = """<answer>
42
</answer>
<think>
This is my reasoning process.
I think the answer is 42.
</think>"""
messages = [
{
"role": "user",
"content": "What is the answer to life, the universe, and everything?",
},
{"role": "assistant", "content": wrong_order},
]
result = format_reward(messages=messages, ground_truth=None)
self.assertIsInstance(result, EvaluateResult)
# Check the score is 0.0 for incorrect order
# Attribute access
self.assertEqual(result.score, 0.0)
self.assertEqual(result.metrics["format_check"].score, 0.0)
self.assertFalse(result.metrics["format_check"].is_score_valid)
# Dictionary access
self.assertEqual(result["score"], 0.0)
self.assertEqual(result["metrics"]["format_check"]["score"], 0.0)
self.assertFalse(result["metrics"]["format_check"]["is_score_valid"])
def test_custom_format_regex(self):
"""Test that the format reward works with custom regex patterns."""
# Create a message with a custom format
custom_format = """[REASONING]
This is my reasoning process.
[/REASONING]
[RESULT]
42
[/RESULT]"""
messages = [
{
"role": "user",
"content": "What is the answer to life, the universe, and everything?",
},
{"role": "assistant", "content": custom_format},
]
# Use a custom regex pattern
custom_regex = r"^\[REASONING\].*?\[/REASONING\].*?\[RESULT\].*?\[/RESULT\]$"
result = format_reward(messages=messages, ground_truth=None, format_regex=custom_regex)
self.assertIsInstance(result, EvaluateResult)
# Check the score is 1.0 for correct custom format
# Attribute access
self.assertEqual(result.score, 1.0)
self.assertEqual(result.metrics["format_check"].score, 1.0)
self.assertTrue(result.metrics["format_check"].is_score_valid)
# Dictionary access
self.assertEqual(result["score"], 1.0)
self.assertEqual(result["metrics"]["format_check"]["score"], 1.0)
self.assertTrue(result["metrics"]["format_check"]["is_score_valid"])
def test_no_messages(self):
"""Test that the format reward handles empty message list."""
result = format_reward(messages=[], ground_truth=None)
self.assertIsInstance(result, EvaluateResult)
# Check the score is 0.0 for no messages
# Attribute access
self.assertEqual(result.score, 0.0)
self.assertEqual(result.metrics["format_check"].score, 0.0)
self.assertFalse(result.metrics["format_check"].is_score_valid)
# Dictionary access
self.assertEqual(result["score"], 0.0)
self.assertEqual(result["metrics"]["format_check"]["score"], 0.0)
self.assertFalse(result["metrics"]["format_check"]["is_score_valid"])
def test_non_assistant_message(self):
"""Test that the format reward handles non-assistant messages."""
messages = [
{
"role": "user",
"content": "What is the answer to life, the universe, and everything?",
}
]
result = format_reward(messages=messages, ground_truth=None)
self.assertIsInstance(result, EvaluateResult)
# Check the score is 0.0 for no assistant message
# Attribute access
self.assertEqual(result.score, 0.0)
self.assertEqual(result.metrics["format_check"].score, 0.0)
self.assertFalse(result.metrics["format_check"].is_score_valid)
# Dictionary access
self.assertEqual(result["score"], 0.0)
self.assertEqual(result["metrics"]["format_check"]["score"], 0.0)
self.assertFalse(result["metrics"]["format_check"]["is_score_valid"])
def test_partial_match_mode(self):
"""Test that the format reward works in partial match mode."""
# Create a message with the format embedded in other text
partial_format = """Here's my response:
<think>
This is my reasoning process.
I think the answer is 42.
</think>
<answer>
42
</answer>
Thanks for asking!"""
messages = [
{
"role": "user",
"content": "What is the answer to life, the universe, and everything?",
},
{"role": "assistant", "content": partial_format},
]
# Exact match should fail
result_exact = format_reward(messages=messages, ground_truth=None, require_exact_match=True)
self.assertIsInstance(result_exact, EvaluateResult)
# Attribute access
self.assertEqual(result_exact.score, 0.0)
# Dictionary access
self.assertEqual(result_exact["score"], 0.0)
# Partial match should succeed with a pattern without anchors
pattern_without_anchors = r"<think>\n.*?</think>\n<answer>\n.*?</answer>"
result_partial = format_reward(
messages=messages,
ground_truth=None,
format_regex=pattern_without_anchors,
require_exact_match=False,
)
self.assertIsInstance(result_partial, EvaluateResult)
# Attribute access
self.assertEqual(result_partial.score, 1.0)
# Dictionary access
self.assertEqual(result_partial["score"], 1.0)
if __name__ == "__main__":
unittest.main()