File size: 14,400 Bytes
fcc38f9 | 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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | import re
from typing import Dict, List
import json
def parse_freethink(response: str, special_token_list=None, action_sep=',', max_actions=3) -> Dict:
"""
Parse response in format: <think>...</think><answer>...</answer>
Returns a dict with keys:
- llm_raw_response: the original response
- llm_response: the response with <think> and <answer> tags
- think_content: the content inside <think> tag
- action_content: the content inside <answer> tag
- actions: a list of actions extracted from action_content
- format_correct: whether the response strictly follows the expected format
"""
response = response.replace("<image>","")
#Pattern to check for content strictly in the format <think>...</think><answer>...</answer>
strict_pattern = r'^\s*<think>(.*?)</think>\s*<answer>(.*?)</answer>\s*$'
strict_match = re.match(strict_pattern, response.strip(), re.DOTALL)
# Pattern to extract content from think and answer tags
extraction_pattern = r'<think>(.*?)</think>\s*<answer>(.*?)</answer>'
match = re.search(extraction_pattern, response, re.DOTALL)
format_correct = strict_match is not None
if not strict_match:
think_content, action_content, actions = "", "", []
else:
think_content, action_content = match.group(1), match.group(2)
if special_token_list is not None:
for special_token in special_token_list: # remove all special tokens in responses to forbid confusion in training
action_content = action_content.replace(special_token, "").strip()
think_content = think_content.replace(special_token, "").strip()
actions = [action.strip() for action in action_content.split(action_sep) if action.strip()]
if len(actions) > max_actions:
actions = actions[:max_actions] #Only the first MAX_ACTIONS actions are kept in the rollout.
action_content = (" " + action_sep + " ").join(actions)
llm_response = "<think>" + think_content.strip() + "</think>" + "<answer>" + action_content.strip() + "</answer>"
return {
"llm_raw_response": response,
"llm_response": llm_response,
"think_content": think_content,
"action_content": action_content,
"actions": actions,
"format_correct": format_correct
}
def parse_no_think(response: str, special_token_list=None, action_sep=',', max_actions=3) -> Dict:
"""
Parse response in format: <answer>...</answer>
Returns a dict with keys:
- llm_raw_response: the original response
- llm_response: the response with <answer> tag
- think_content: empty string (no think content in this format)
- action_content: the content inside <answer> tag
- actions: a list of actions extracted from action_content
- format_correct: whether the response strictly follows the expected format
"""
response = response.replace("<image>","")
# Pattern to check for content strictly in the format <answer>...</answer>
strict_pattern = r'^\s*<answer>(.*?)</answer>\s*$'
strict_match = re.match(strict_pattern, response.strip(), re.DOTALL)
format_correct = strict_match is not None
# Pattern to extract content from answer tag
extraction_pattern = r'<answer>(.*?)</answer>'
match = re.search(extraction_pattern, response, re.DOTALL)
#format_correct = match is not None
if not strict_match:
think_content, action_content, actions = "", "", []
else:
action_content = match.group(1)
think_content = "" # No think content in this format
if special_token_list is not None:
for special_token in special_token_list:
action_content = action_content.replace(special_token, "").strip()
actions = [action.strip() for action in action_content.split(action_sep) if action.strip()]
if len(actions) > max_actions:
actions = actions[:max_actions]
action_content = (" " + action_sep + " ").join(actions)
llm_response = "<answer>" + action_content.strip() + "</answer>"
return {
"llm_raw_response": response,
"llm_response": llm_response,
"think_content": think_content,
"action_content": action_content,
"actions": actions,
"format_correct": format_correct
}
def parse_grounding(response: str, special_token_list=None, action_sep=',', max_actions=3) -> Dict:
"""
Parse response in format: <think><observation>...</observation><reasoning>...</reasoning></think><answer>...</answer>
Returns a dict with keys:
- llm_raw_response: the original response
- llm_response: the response with all tags
- observation_content: the content inside <observation> tag
- think_content: the entire content inside <think> tag
- reasoning_content: the content inside <reasoning> tag
- action_content: the content inside <answer> tag
- actions: a list of actions extracted from action_content
- format_correct: whether the response strictly follows the expected format
"""
response = response.replace("<image>","")
# Pattern to check for content strictly in the expected format
strict_pattern = r'^\s*<think>\s*<observation>(.*?)</observation>\s*<reasoning>(.*?)</reasoning>\s*</think>\s*<answer>(.*?)</answer>\s*$'
strict_match = re.match(strict_pattern, response.strip(), re.DOTALL)
format_correct = strict_match is not None
# Pattern to extract content from tags
extraction_pattern = r'<think>\s*<observation>(.*?)</observation>\s*<reasoning>(.*?)</reasoning>\s*</think>\s*<answer>(.*?)</answer>'
match = re.search(extraction_pattern, response, re.DOTALL)
if not match:
observation_content, reasoning_content, action_content, actions = "", "", "", []
think_content = ""
else:
observation_content = match.group(1)
reasoning_content = match.group(2)
action_content = match.group(3)
think_content = "<observation>" + observation_content + "</observation><reasoning>" + reasoning_content + "</reasoning>"
if special_token_list is not None:
for special_token in special_token_list:
observation_content = observation_content.replace(special_token, "").strip()
reasoning_content = reasoning_content.replace(special_token, "").strip()
action_content = action_content.replace(special_token, "").strip()
think_content = think_content.replace(special_token, "").strip()
actions = [action.strip() for action in action_content.split(action_sep) if action.strip()]
if len(actions) > max_actions:
actions = actions[:max_actions]
action_content = (" " + action_sep + " ").join(actions)
# Reconstruct the cleaned llm_response
llm_response = "<think>" + think_content.strip() + "</think>" + "<answer>" + action_content.strip() + "</answer>"
return {
"llm_raw_response": response,
"llm_response": llm_response,
"observation_content": observation_content,
"think_content": think_content,
"reasoning_content": reasoning_content,
"action_content": action_content,
"actions": actions,
"format_correct": format_correct
}
def parse_worldmodeling(response: str, special_token_list=None, action_sep=',', max_actions=3) -> Dict:
"""
Parse response in format: <think><reasoning>...</reasoning><prediction>...</prediction></think><answer>...</answer>
Returns a dict with keys:
- llm_raw_response: the original response
- llm_response: the response with all tags
- think_content: the entire content inside <think> tag
- reasoning_content: the content inside <reasoning> tag
- prediction_content: the content inside <prediction> tag
- action_content: the content inside <answer> tag
- actions: a list of actions extracted from action_content
- format_correct: whether the response strictly follows the expected format
"""
response = response.replace("<image>","")
# Pattern to check for content strictly in the expected format
strict_pattern = r'^\s*<think>\s*<reasoning>(.*?)</reasoning>\s*<prediction>(.*?)</prediction>\s*</think>\s*<answer>(.*?)</answer>\s*$'
strict_match = re.match(strict_pattern, response.strip(), re.DOTALL)
format_correct = strict_match is not None
# Pattern to extract content from tags
extraction_pattern = r'<think>\s*<reasoning>(.*?)</reasoning>\s*<prediction>(.*?)</prediction>\s*</think>\s*<answer>(.*?)</answer>'
match = re.search(extraction_pattern, response, re.DOTALL)
if not match:
reasoning_content, prediction_content, action_content, actions = "", "", "", []
think_content = ""
else:
reasoning_content = match.group(1)
prediction_content = match.group(2)
action_content = match.group(3)
think_content = "<reasoning>" + reasoning_content + "</reasoning><prediction>" + prediction_content + "</prediction>"
if special_token_list is not None:
for special_token in special_token_list:
reasoning_content = reasoning_content.replace(special_token, "").strip()
prediction_content = prediction_content.replace(special_token, "").strip()
action_content = action_content.replace(special_token, "").strip()
think_content = think_content.replace(special_token, "").strip()
actions = [action.strip() for action in action_content.split(action_sep) if action.strip()]
if len(actions) > max_actions:
actions = actions[:max_actions]
action_content = (" " + action_sep + " ").join(actions)
# Reconstruct the cleaned llm_response
llm_response = "<think>" + think_content.strip() + "</think>" + "<answer>" + action_content.strip() + "</answer>"
return {
"llm_raw_response": response,
"llm_response": llm_response,
"think_content": think_content,
"reasoning_content": reasoning_content,
"prediction_content": prediction_content,
"action_content": action_content,
"actions": actions,
"format_correct": format_correct
}
def parse_grounding_worldmodeling(response: str, special_token_list=None, action_sep=',', max_actions=3) -> Dict:
"""
Parse response in format: <think><observation>...</observation><reasoning>...</reasoning><prediction>...</prediction></think><answer>...</answer>
Returns a dict with keys:
- llm_raw_response: the original response
- llm_response: the response with all tags
- observation_content: the content inside <observation> tag
- reasoning_content: the content inside <reasoning> tag
- prediction_content: the content inside <prediction> tag
- think_content: the entire content inside <think> tag
- action_content: the content inside <answer> tag
- actions: a list of actions extracted from action_content
- format_correct: whether the response strictly follows the expected format
"""
response = response.replace("<image>","")
# Pattern to check for content strictly in the expected format
strict_pattern = r'^\s*<think>\s*<observation>(.*?)</observation>\s*<reasoning>(.*?)</reasoning>\s*<prediction>(.*?)</prediction>\s*</think>\s*<answer>(.*?)</answer>\s*$'
strict_match = re.match(strict_pattern, response.strip(), re.DOTALL)
format_correct = strict_match is not None
# Pattern to extract content from tags
extraction_pattern = r'<think>\s*<observation>(.*?)</observation>\s*<reasoning>(.*?)</reasoning>\s*<prediction>(.*?)</prediction>\s*</think>\s*<answer>(.*?)</answer>'
match = re.search(extraction_pattern, response, re.DOTALL)
if not match:
observation_content, reasoning_content, prediction_content, action_content, actions = "", "", "", "", []
think_content = ""
else:
observation_content = match.group(1)
reasoning_content = match.group(2)
prediction_content = match.group(3)
action_content = match.group(4)
think_content = "<observation>" + observation_content + "</observation><reasoning>" + reasoning_content + "</reasoning><prediction>" + prediction_content + "</prediction>"
if special_token_list is not None:
for special_token in special_token_list:
observation_content = observation_content.replace(special_token, "").strip()
reasoning_content = reasoning_content.replace(special_token, "").strip()
prediction_content = prediction_content.replace(special_token, "").strip()
action_content = action_content.replace(special_token, "").strip()
think_content = think_content.replace(special_token, "").strip()
actions = [action.strip() for action in action_content.split(action_sep) if action.strip()]
if len(actions) > max_actions:
actions = actions[:max_actions]
action_content = (" " + action_sep + " ").join(actions)
# Reconstruct the cleaned llm_response
llm_response = "<think>" + think_content.strip() + "</think>" + "<answer>" + action_content.strip() + "</answer>"
return {
"llm_raw_response": response,
"llm_response": llm_response,
"observation_content": observation_content,
"reasoning_content": reasoning_content,
"prediction_content": prediction_content,
"think_content": think_content,
"action_content": action_content,
"actions": actions,
"format_correct": format_correct
}
PARSE_FUNC_MAP = {
"free_think": parse_freethink,
"no_think": parse_no_think,
"grounding": parse_grounding,
"worldmodeling": parse_worldmodeling,
"grounding_worldmodeling": parse_grounding_worldmodeling,
"grounding_structured": parse_grounding,
"worldmodeling_structured": parse_worldmodeling,
"grounding_worldmodeling_structured": parse_grounding_worldmodeling,
"grounding_symbolic": parse_grounding,
"worldmodeling_symbolic": parse_worldmodeling,
"grounding_worldmodeling_symbolic": parse_grounding_worldmodeling,
}
|