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: ...... Returns a dict with keys: - llm_raw_response: the original response - llm_response: the response with and tags - think_content: the content inside tag - action_content: the content inside tag - actions: a list of actions extracted from action_content - format_correct: whether the response strictly follows the expected format """ response = response.replace("","") #Pattern to check for content strictly in the format ...... strict_pattern = r'^\s*(.*?)\s*(.*?)\s*$' strict_match = re.match(strict_pattern, response.strip(), re.DOTALL) # Pattern to extract content from think and answer tags extraction_pattern = r'(.*?)\s*(.*?)' 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_content.strip() + "" + "" + action_content.strip() + "" 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: ... Returns a dict with keys: - llm_raw_response: the original response - llm_response: the response with tag - think_content: empty string (no think content in this format) - action_content: the content inside tag - actions: a list of actions extracted from action_content - format_correct: whether the response strictly follows the expected format """ response = response.replace("","") # Pattern to check for content strictly in the format ... strict_pattern = r'^\s*(.*?)\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'(.*?)' 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 = "" + action_content.strip() + "" 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: ......... Returns a dict with keys: - llm_raw_response: the original response - llm_response: the response with all tags - observation_content: the content inside tag - think_content: the entire content inside tag - reasoning_content: the content inside tag - action_content: the content inside tag - actions: a list of actions extracted from action_content - format_correct: whether the response strictly follows the expected format """ response = response.replace("","") # Pattern to check for content strictly in the expected format strict_pattern = r'^\s*\s*(.*?)\s*(.*?)\s*\s*(.*?)\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'\s*(.*?)\s*(.*?)\s*\s*(.*?)' 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_content + "" + reasoning_content + "" 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_content.strip() + "" + "" + action_content.strip() + "" 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: ......... Returns a dict with keys: - llm_raw_response: the original response - llm_response: the response with all tags - think_content: the entire content inside tag - reasoning_content: the content inside tag - prediction_content: the content inside tag - action_content: the content inside tag - actions: a list of actions extracted from action_content - format_correct: whether the response strictly follows the expected format """ response = response.replace("","") # Pattern to check for content strictly in the expected format strict_pattern = r'^\s*\s*(.*?)\s*(.*?)\s*\s*(.*?)\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'\s*(.*?)\s*(.*?)\s*\s*(.*?)' 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_content + "" + prediction_content + "" 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_content.strip() + "" + "" + action_content.strip() + "" 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: ............ Returns a dict with keys: - llm_raw_response: the original response - llm_response: the response with all tags - observation_content: the content inside tag - reasoning_content: the content inside tag - prediction_content: the content inside tag - think_content: the entire content inside tag - action_content: the content inside tag - actions: a list of actions extracted from action_content - format_correct: whether the response strictly follows the expected format """ response = response.replace("","") # Pattern to check for content strictly in the expected format strict_pattern = r'^\s*\s*(.*?)\s*(.*?)\s*(.*?)\s*\s*(.*?)\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'\s*(.*?)\s*(.*?)\s*(.*?)\s*\s*(.*?)' 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_content + "" + reasoning_content + "" + prediction_content + "" 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_content.strip() + "" + "" + action_content.strip() + "" 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, }