[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):raiseOutputParserException(f"Could not parse LLM Output: {text}")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:raiseOutputParserException(f"Could not parse action directive: {action_str}")action,action_input=re_matches.group(1),re_matches.group(2)ifaction=="Finish":returnAgentFinish({"output":action_input},text)else:returnAgentAction(action,action_input,text)