[docs]defpreprocess_json_input(input_str:str)->str:"""Preprocesses a string to be parsed as json. Replace single backslashes with double backslashes, while leaving already escaped ones intact. Args: input_str: String to be preprocessed Returns: Preprocessed string """corrected_str=re.sub(r'(?<!\\)\\(?!["\\/bfnrt]|u[0-9a-fA-F]{4})',r"\\\\",input_str)returncorrected_str
[docs]classAutoGPTOutputParser(BaseAutoGPTOutputParser):"""Output parser for AutoGPT."""
[docs]defparse(self,text:str)->AutoGPTAction:try:parsed=json.loads(text,strict=False)exceptjson.JSONDecodeError:preprocessed_text=preprocess_json_input(text)try:parsed=json.loads(preprocessed_text,strict=False)exceptException:returnAutoGPTAction(name="ERROR",args={"error":f"Could not parse invalid json: {text}"},)try:returnAutoGPTAction(name=parsed["command"]["name"],args=parsed["command"]["args"],)except(KeyError,TypeError):# If the command is null or incomplete, return an erroneous toolreturnAutoGPTAction(name="ERROR",args={"error":f"Incomplete command args: {parsed}"})