Spaces:
Sleeping
Sleeping
Commit Β·
6f3d9d6
1
Parent(s): 6ceec85
fix: parse chat completions in rewards
Browse filesNormalize TRL chat completion outputs before applying JSON reward parsing so GRPO can score conversational generations.
Made-with: Cursor
- training/train_grpo.py +18 -0
training/train_grpo.py
CHANGED
|
@@ -112,8 +112,26 @@ def build_dataset(n_episodes: int, seed: int = SEED) -> "datasets.Dataset":
|
|
| 112 |
|
| 113 |
# ββ Reward functions βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 114 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
def _parse_completion(text: str) -> dict | None:
|
| 116 |
"""Extract first JSON object from completion text."""
|
|
|
|
| 117 |
# Strip any <think>...</think> blocks (chain-of-thought models)
|
| 118 |
text = re.sub(r"<think>.*?</think>", "", text, flags=re.DOTALL).strip()
|
| 119 |
# Try direct parse first
|
|
|
|
| 112 |
|
| 113 |
# ββ Reward functions βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 114 |
|
| 115 |
+
def _completion_to_text(completion) -> str:
|
| 116 |
+
"""Normalize TRL string or chat-message completions into assistant text."""
|
| 117 |
+
if isinstance(completion, str):
|
| 118 |
+
return completion
|
| 119 |
+
if isinstance(completion, dict):
|
| 120 |
+
return str(completion.get("content", completion))
|
| 121 |
+
if isinstance(completion, list):
|
| 122 |
+
parts = []
|
| 123 |
+
for item in completion:
|
| 124 |
+
if isinstance(item, dict):
|
| 125 |
+
parts.append(str(item.get("content", "")))
|
| 126 |
+
else:
|
| 127 |
+
parts.append(str(item))
|
| 128 |
+
return "\n".join(part for part in parts if part)
|
| 129 |
+
return str(completion)
|
| 130 |
+
|
| 131 |
+
|
| 132 |
def _parse_completion(text: str) -> dict | None:
|
| 133 |
"""Extract first JSON object from completion text."""
|
| 134 |
+
text = _completion_to_text(text)
|
| 135 |
# Strip any <think>...</think> blocks (chain-of-thought models)
|
| 136 |
text = re.sub(r"<think>.*?</think>", "", text, flags=re.DOTALL).strip()
|
| 137 |
# Try direct parse first
|