Skip to content

Commit 8ebe58e

Browse files
committed
Clamp step scores with SAFE_SCORE
1 parent 2ce7b08 commit 8ebe58e

5 files changed

Lines changed: 19 additions & 13 deletions

File tree

app.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from fastapi import FastAPI, HTTPException
1515
from pydantic import BaseModel, Field
1616
from openai import OpenAI
17+
from src.score_utils import SAFE_SCORE
1718

1819

1920
# ============================================================================
@@ -144,7 +145,7 @@ def step(self, action: str) -> dict:
144145
if self.done:
145146
return {
146147
"state": "environment already done",
147-
"reward": 0.0,
148+
"reward": SAFE_SCORE(0.0),
148149
"done": True
149150
}
150151

@@ -157,7 +158,7 @@ def step(self, action: str) -> dict:
157158
else:
158159
return {
159160
"state": f"invalid action: {action}",
160-
"reward": 0.0,
161+
"reward": SAFE_SCORE(0.0),
161162
"done": self.done
162163
}
163164

@@ -170,7 +171,7 @@ def _classify_email(self) -> dict:
170171

171172
return {
172173
"state": f"classified as: {self.category}",
173-
"reward": 0.33,
174+
"reward": SAFE_SCORE(0.33),
174175
"done": False,
175176
"info": {
176177
"action": "classify_email",
@@ -197,7 +198,7 @@ def _extract_entities(self) -> dict:
197198

198199
return {
199200
"state": f"extracted: {json.dumps(self.extracted_data)}",
200-
"reward": 0.33,
201+
"reward": SAFE_SCORE(0.33),
201202
"done": False,
202203
"info": {
203204
"action": "extract_entities",
@@ -229,7 +230,7 @@ def _generate_reply(self) -> dict:
229230

230231
return {
231232
"state": f"reply generated: {self.response[:100]}...",
232-
"reward": 0.34,
233+
"reward": SAFE_SCORE(0.34),
233234
"done": True,
234235
"info": {
235236
"action": "generate_reply",

server/app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
from src.env import EmailTriageEnv, OpenEnvEmailTriageEnv
2323
from src.models import Action
24-
from src.score_utils import SAFE_SCORE
24+
from src.score_utils import SAFE_SCORE
2525

2626

2727
ALLOWED_CATEGORIES = {"billing", "technical", "sales", "account", "complaint", "shipping", "other"}
@@ -77,7 +77,7 @@
7777
def _new_scoreboard() -> Dict[str, Dict[str, Any]]:
7878
return {
7979
task_id: {
80-
"score": 0.0,
80+
"score": SAFE_SCORE(0.01),
8181
"steps": 0,
8282
"cumulative_reward": 0.0,
8383
"done": False,

src/env.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ def step(self, action: Action | Dict[str, Any]) -> Tuple[Observation, float, boo
6363

6464
current_email = self.dataset[self.index]
6565
reward_obj = compute_step_reward(action=action, truth=current_email, task=self.task)
66+
reward = SAFE_SCORE(reward_obj.total)
6667
self.grader.update(action=action, truth=current_email)
6768

6869
# Update tracking statistics
@@ -77,8 +78,8 @@ def step(self, action: Action | Dict[str, Any]) -> Tuple[Observation, float, boo
7778
if action.priority and action.priority.value == "urgent":
7879
self.urgent_handled += 1
7980

80-
self.last_reward = reward_obj.total
81-
self.cumulative_reward += reward_obj.total
81+
self.last_reward = reward
82+
self.cumulative_reward += reward
8283

8384
self.index += 1
8485
self.done = self.index >= len(self.dataset)
@@ -106,7 +107,7 @@ def step(self, action: Action | Dict[str, Any]) -> Tuple[Observation, float, boo
106107
"urgent_handled": self.urgent_handled,
107108
},
108109
}
109-
return next_observation, reward_obj.total, self.done, info
110+
return next_observation, reward, self.done, info
110111

111112
def final_score(self) -> float:
112113
return SAFE_SCORE(self.grader.score())
@@ -142,7 +143,7 @@ def step(self, action: Dict[str, Any]) -> Tuple[Dict[str, Any], float, bool, Dic
142143
predicted_action = str(action.get("action", "")).strip()
143144
expected_action = self._expected_action_for_email(current_email)
144145
correct = predicted_action == expected_action
145-
reward = 1.0 if correct else -1.0
146+
reward = SAFE_SCORE(1.0 if correct else -1.0)
146147

147148
self.current_index += 1
148149
self.done = self.current_index >= len(self.emails)

src/graders.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77

88
def safe_score(correct: int, total: int) -> float:
9-
return SAFE_SCORE(safe_ratio_score(correct=correct, total=total))
9+
score = safe_ratio_score(correct=correct, total=total)
10+
return SAFE_SCORE(score)
1011

1112

1213
class DeterministicTriageGrader:
@@ -37,4 +38,5 @@ def update(self, action: Action, truth: EmailRecord) -> None:
3738
self.correct += 1
3839

3940
def score(self) -> float:
40-
return SAFE_SCORE(safe_ratio_score(correct=self.correct, total=self.total))
41+
score = safe_ratio_score(correct=self.correct, total=self.total)
42+
return SAFE_SCORE(score)

src/rewards.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
from src.models import Action, EmailRecord, Reward, TriageAction
4+
from src.score_utils import SAFE_SCORE
45
from src.tasks import TaskConfig
56

67

@@ -39,6 +40,7 @@ def compute_step_reward(action: Action, truth: EmailRecord, task: TaskConfig) ->
3940
penalties["unnecessary_escalation"] = UNNECESSARY_ESCALATION_PENALTY
4041

4142
total = category_component + priority_component + action_component + reply_component + sum(penalties.values())
43+
total = SAFE_SCORE(total)
4244

4345
return Reward(
4446
total=total,

0 commit comments

Comments
 (0)