Spaces:
Running
Running
Sibam commited on
Commit ·
f3f7bc4
1
Parent(s): be1a57b
fix: clamp grader rewards to strictly (0, 1) to pass OpenEnv validation bounds
Browse filesFiles changed (3)
- README.md+5-5
- server/environment.py+4-4
- tests/test_environment.py+12-12
204 unmodified lines2052062072082092102111 unmodified line21321421521621721821922022122222322422522622722822935 unmodified lines265266267268269270271204 unmodified linesEnv-->>API: PairwiseObservation reward=0.0 done=falseAPI-->>Agent: 200 OK Observation JSONNote over Agent,DB: Step Loop — max 5 steps per episodeloop For each annotation stepAgent->>Agent: call_llm(system_prompt, observation)1 unmodified lineAPI->>Env: env.step(PairwiseAction)Env->>Grader: grade_pairwise(action, example)Grader->>Grader: compare choice vs gold_labelGrader-->>Env: reward=1.0 verdict=correctEnv->>DB: _sample_example next exampleDB-->>Env: next exampleEnv-->>API: Observation reward=1.0 done=false step=NAPI-->>Agent: 200 OK StepResult JSONAgent->>Agent: log_step accumulate rewardendNote over Agent,DB: Episode EndEnv-->>API: Observation done=true step_count=5API-->>Agent: 200 OK Final ObservationAgent->>Agent: log_end score rewardsAgent->>API: POST /reset start new episode35 unmodified linessubgraph Episode["Episode Loop"]E1["POST /reset<br/>choose task_type and seed"]E2{"Task Type?"}E3["Pairwise<br/>PairwiseAction: choice A or B<br/>reward 0.0 to 1.0"]E4["Likert<br/>LikertAction: score 4 axes 1 to 5<br/>reward = 1 minus MAE/4"]E5["Consistency<br/>ConsistencyAction: rank A B C D<br/>reward = tau + transitivity"]E6["POST /step<br/>submit action"]204 unmodified lines2052062072082092102111 unmodified line21321421521621721821922022122222322422522622722822935 unmodified lines265266267268269270271204 unmodified linesEnv-->>API: PairwiseObservation reward=0.0 done=falseAPI-->>Agent: 200 OK Observation JSONNote over Agent,DB: Step Loop — max 10 steps per episodeloop For each annotation stepAgent->>Agent: call_llm(system_prompt, observation)1 unmodified lineAPI->>Env: env.step(PairwiseAction)Env->>Grader: grade_pairwise(action, example)Grader->>Grader: compare choice vs gold_labelGrader-->>Env: reward=0.99 verdict=correctEnv->>DB: _sample_example next exampleDB-->>Env: next exampleEnv-->>API: Observation reward=0.99 done=false step=NAPI-->>Agent: 200 OK StepResult JSONAgent->>Agent: log_step accumulate rewardendNote over Agent,DB: Episode EndEnv-->>API: Observation done=true step_count=10API-->>Agent: 200 OK Final ObservationAgent->>Agent: log_end score rewardsAgent->>API: POST /reset start new episode35 unmodified linessubgraph Episode["Episode Loop"]E1["POST /reset<br/>choose task_type and seed"]E2{"Task Type?"}E3["Pairwise<br/>PairwiseAction: choice A or B<br/>reward 0.01 to 0.99"]E4["Likert<br/>LikertAction: score 4 axes 1 to 5<br/>reward = 1 minus MAE/4"]E5["Consistency<br/>ConsistencyAction: rank A B C D<br/>reward = tau + transitivity"]E6["POST /step<br/>submit action"]
65 unmodified lines6667686970717273747537 unmodified lines11311411511611711811951 unmodified lines17117217317417517617765 unmodified linesreward = 0.1verdict = "tie_when_clear"elif choice == gold:reward = 1.0verdict = "correct"else:reward = 0.0verdict = "incorrect"return reward, {37 unmodified linesmae = sum(errors) / len(errors)max_error = 4.0 # max abs difference on 1-5 scalereward = round(1.0 - (mae / max_error), 4)reward = max(0.0, min(1.0, reward))return reward, {"mae": round(mae, 4),51 unmodified linesquality_score = round((tau + 1.0) / 2.0 * 0.5, 4)reward = round(transitivity_score + quality_score, 4)reward = max(0.0, min(1.0, reward))return reward, {"transitivity_score": transitivity_score,65 unmodified lines6667686970717273747537 unmodified lines11311411511611711811951 unmodified lines17117217317417517617765 unmodified linesreward = 0.1verdict = "tie_when_clear"elif choice == gold:reward = 0.99verdict = "correct"else:reward = 0.01verdict = "incorrect"return reward, {37 unmodified linesmae = sum(errors) / len(errors)max_error = 4.0 # max abs difference on 1-5 scalereward = round(1.0 - (mae / max_error), 4)reward = max(0.01, min(0.99, reward))return reward, {"mae": round(mae, 4),51 unmodified linesquality_score = round((tau + 1.0) / 2.0 * 0.5, 4)reward = round(transitivity_score + quality_score, 4)reward = max(0.01, min(0.99, reward))return reward, {"transitivity_score": transitivity_score,
24 unmodified lines252627282930313233343536373812 unmodified lines515253545556574 unmodified lines626364656667682 unmodified lines7172737475767722 unmodified lines1001011021031041051061 unmodified line1081091101111121131149 unmodified lines1241251261271281291305 unmodified lines13613713813914014114241 unmodified lines1841851861871881891901 unmodified line19219319419519619719819920020120220320420524 unmodified linesaction = PairwiseAction(choice="A")example = {"gold_label": "A", "source": "test"}reward, info = grade_pairwise(action, example)assert reward == 1.0assert info["verdict"] == "correct"def test_wrong_choice_scores_0(self):action = PairwiseAction(choice="B")example = {"gold_label": "A", "source": "test"}reward, info = grade_pairwise(action, example)assert reward == 0.0assert info["verdict"] == "incorrect"def test_skip_scores_partial(self):12 unmodified linesfor choice in ["A", "B", "tie", "skip"]:action = PairwiseAction(choice=choice)reward, _ = grade_pairwise(action, {"gold_label": "A", "source": "test"})assert 0.0 <= reward <= 1.0class TestLikertGrader:4 unmodified lines"source": "test",}reward, info = grade_likert(action, example)assert reward == 1.0def test_worst_scores_reward_0(self):action = LikertAction(helpfulness=1, honesty=1, harmlessness=1, instruction_following=1)2 unmodified lines"source": "test",}reward, info = grade_likert(action, example)assert reward == 0.0def test_partial_error_gives_partial_reward(self):action = LikertAction(helpfulness=4, honesty=4, harmlessness=4, instruction_following=4)22 unmodified lines}}reward, _ = grade_likert(action, example)assert 0.0 <= reward <= 1.0, f"Reward out of range: {reward}"class TestConsistencyGrader:1 unmodified lineaction = ConsistencyAction(ranking=["A", "B", "C", "D"])example = {"gold_ranking": ["A", "B", "C", "D"], "source": "test"}reward, info = grade_consistency(action, example)assert reward == 1.0def test_reversed_ranking_scores_low(self):action = ConsistencyAction(ranking=["D", "C", "B", "A"])9 unmodified linesaction = ConsistencyAction(ranking=["A", "B", "C", "X"])example = {"gold_ranking": ["A", "B", "C", "D"], "source": "test"}reward, info = grade_consistency(action, example)assert reward == 0.0assert info["has_invalid_ids"] is Truedef test_reward_always_in_range(self):5 unmodified linesaction = ConsistencyAction(ranking=list(perm))example = {"gold_ranking": gold, "source": "test"}reward, _ = grade_consistency(action, example)assert 0.0 <= reward <= 1.0, f"Reward out of range: {reward} for {perm}"def test_graders_not_always_same_score(self):"""Critical: graders must NOT always return the same score."""41 unmodified linesaction = PairwiseAction(choice="A")obs = self.env.step(action)assert isinstance(obs, PairwiseObservation)assert 0.0 <= obs.reward <= 1.0assert isinstance(obs.done, bool)def test_step_likert(self):1 unmodified lineaction = LikertAction(helpfulness=4, honesty=4, harmlessness=5, instruction_following=4)obs = self.env.step(action)assert isinstance(obs, LikertObservation)assert 0.0 <= obs.reward <= 1.0def test_step_consistency(self):self.env.reset(task_type="consistency")action = ConsistencyAction(ranking=["A", "B", "C", "D"])obs = self.env.step(action)assert isinstance(obs, ConsistencyObservation)assert 0.0 <= obs.reward <= 1.0def test_episode_terminates_after_max_steps(self):self.env.reset(task_type="pairwise")24 unmodified lines252627282930313233343536373812 unmodified lines515253545556574 unmodified lines626364656667682 unmodified lines7172737475767722 unmodified lines1001011021031041051061 unmodified line1081091101111121131149 unmodified lines1241251261271281291305 unmodified lines13613713813914014114241 unmodified lines1841851861871881891901 unmodified line19219319419519619719819920020120220320420524 unmodified linesaction = PairwiseAction(choice="A")example = {"gold_label": "A", "source": "test"}reward, info = grade_pairwise(action, example)assert reward == 0.99assert info["verdict"] == "correct"def test_wrong_choice_scores_0(self):action = PairwiseAction(choice="B")example = {"gold_label": "A", "source": "test"}reward, info = grade_pairwise(action, example)assert reward == 0.01assert info["verdict"] == "incorrect"def test_skip_scores_partial(self):12 unmodified linesfor choice in ["A", "B", "tie", "skip"]:action = PairwiseAction(choice=choice)reward, _ = grade_pairwise(action, {"gold_label": "A", "source": "test"})assert 0.0 < reward < 1.0class TestLikertGrader:4 unmodified lines"source": "test",}reward, info = grade_likert(action, example)assert reward == 0.99def test_worst_scores_reward_0(self):action = LikertAction(helpfulness=1, honesty=1, harmlessness=1, instruction_following=1)2 unmodified lines"source": "test",}reward, info = grade_likert(action, example)assert reward == 0.01def test_partial_error_gives_partial_reward(self):action = LikertAction(helpfulness=4, honesty=4, harmlessness=4, instruction_following=4)22 unmodified lines}}reward, _ = grade_likert(action, example)assert 0.0 < reward < 1.0, f"Reward out of range: {reward}"class TestConsistencyGrader:1 unmodified lineaction = ConsistencyAction(ranking=["A", "B", "C", "D"])example = {"gold_ranking": ["A", "B", "C", "D"], "source": "test"}reward, info = grade_consistency(action, example)assert reward == 0.99def test_reversed_ranking_scores_low(self):action = ConsistencyAction(ranking=["D", "C", "B", "A"])9 unmodified linesaction = ConsistencyAction(ranking=["A", "B", "C", "X"])example = {"gold_ranking": ["A", "B", "C", "D"], "source": "test"}reward, info = grade_consistency(action, example)assert reward == 0.01assert info["has_invalid_ids"] is Truedef test_reward_always_in_range(self):5 unmodified linesaction = ConsistencyAction(ranking=list(perm))example = {"gold_ranking": gold, "source": "test"}reward, _ = grade_consistency(action, example)assert 0.0 < reward < 1.0, f"Reward out of range: {reward} for {perm}"def test_graders_not_always_same_score(self):"""Critical: graders must NOT always return the same score."""41 unmodified linesaction = PairwiseAction(choice="A")obs = self.env.step(action)assert isinstance(obs, PairwiseObservation)assert 0.0 < obs.reward < 1.0assert isinstance(obs.done, bool)def test_step_likert(self):1 unmodified lineaction = LikertAction(helpfulness=4, honesty=4, harmlessness=5, instruction_following=4)obs = self.env.step(action)assert isinstance(obs, LikertObservation)assert 0.0 < obs.reward < 1.0def test_step_consistency(self):self.env.reset(task_type="consistency")action = ConsistencyAction(ranking=["A", "B", "C", "D"])obs = self.env.step(action)assert isinstance(obs, ConsistencyObservation)assert 0.0 < obs.reward < 1.0def test_episode_terminates_after_max_steps(self):self.env.reset(task_type="pairwise")
README.md+5-5
server/environment.py+4-4
tests/test_environment.py+12-12