[docs]classReActOutputParser(AgentOutputParser):"""Output parser for the ReAct agent."""
[docs]defparse(self,text:str)->Union[AgentAction,AgentFinish]:action_prefix="Action: "ifnottext.strip().split("\n")[-1].startswith(action_prefix):msg=f"Could not parse LLM Output: {text}"raiseOutputParserException(msg)action_block=text.strip().split("\n")[-1]action_str=action_block[len(action_prefix):]# Parse out the action and the directive.re_matches=re.search(r"(.*?)\[(.*?)\]",action_str)ifre_matchesisNone:msg=f"Could not parse action directive: {action_str}"raiseOutputParserException(msg)action,action_input=re_matches.group(1),re_matches.group(2)ifaction=="Finish":returnAgentFinish({"output":action_input},text)returnAgentAction(action,action_input,text)