"""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()