File size: 3,301 Bytes
d360720
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Run with: python -m unittest discover -s openjudgment -p test_v4_helpsteer.py"""
from copy import deepcopy
import json
import unittest

from v4_helpsteer import adapt


def example(votes):
    return dict(context=[dict(role="user", content="Compare these answers.")],
                response1="First response", response2="Second response",
                overall_preference=2,
                individual_preference=[dict(score=v, reasoning="SECRET_RATIONALE",
                    feedback1="SECRET_FEEDBACK", feedback2="SECRET_FEEDBACK") for v in votes])


class HelpSteerTests(unittest.TestCase):
    def test_feedback_rubric_and_strict_ambiguity(self):
        source = example([1])
        source["feedback1"] = ["The response is not helpful. SECRET", "The response is perfectly helpful. SECRET"]
        source["feedback2"] = ["The response is mostly helpful. SECRET", "I think it is mostly helpful."]
        task, = adapt(source, "feedback")
        self.assertEqual(task["target"], [0.5, 0, 0, 0, 0.5])
        self.assertNotIn("SECRET", task["state"])
        self.assertEqual(set(json.loads(task["state"])), {"context", "response"})
        source["feedback1"] = ["The response is partially helpful."]
        self.assertEqual(adapt(source, "feedback"), [])

    def test_boundaries_and_sign(self):
        for score in range(-3, 4):
            task, = adapt(example([score]))
            self.assertEqual(task["target"], [float(i == score) for i in range(-3, 4)])
        self.assertIn("Response 1 is much better", adapt(example([-3]))[0]["candidates"][0])
        self.assertIn("Response 2 is much better", adapt(example([3]))[0]["candidates"][-1])

    def test_disagreement_and_swap(self):
        source = example([-3, 0, 1])
        original = deepcopy(source)
        task, = adapt(source)
        self.assertEqual(task["target"], [1/3, 0, 0, 1/3, 1/3, 0, 0])
        self.assertEqual(source, original)
        swapped = deepcopy(source)
        swapped["response1"], swapped["response2"] = source["response2"], source["response1"]
        for vote in swapped["individual_preference"]:
            vote["score"] *= -1
        other, = adapt(swapped)
        self.assertEqual(other["target"], task["target"][::-1])
        self.assertEqual(other["group_key"], task["group_key"])

    def test_no_label_leakage_and_no_aggregate_fallback(self):
        task, = adapt(example([-1, -1, 3]))
        self.assertEqual(set(json.loads(task["state"])), {"context", "response1", "response2"})
        self.assertNotIn("SECRET", task["state"] + task["instructions"])
        self.assertNotIn("overall_preference", task["state"])
        self.assertEqual(adapt(example([])), [])
        for bad in [4, -4, True, 1.0, "1", None]:
            self.assertEqual(adapt(example([-1, bad])), [])

    def test_context_normalization_preserves_semantics(self):
        source = example([0])
        source["context"][0]["content"] = "Code:\r\n  Pass"
        a, = adapt(source)
        source["context"][0]["content"] = "Code:\n  Pass"
        b, = adapt(source)
        self.assertEqual(a["group_key"], b["group_key"])
        source["context"][0]["content"] = "code:\n pass"
        self.assertNotEqual(a["group_key"], adapt(source)[0]["group_key"])


if __name__ == "__main__":
    unittest.main()