[docs]classConvoOutputParser(AgentOutputParser):"""Output parser for the conversational agent."""ai_prefix:str="AI""""Prefix to use before AI output."""format_instructions:str=FORMAT_INSTRUCTIONS"""Default formatting instructions"""
[docs]defget_format_instructions(self)->str:"""Returns formatting instructions for the given output parser."""returnself.format_instructions
[docs]defparse(self,text:str)->Union[AgentAction,AgentFinish]:"""Parse the output from the agent into an AgentAction or AgentFinish object. Args: text: The text to parse. Returns: An AgentAction or AgentFinish object. """iff"{self.ai_prefix}:"intext:returnAgentFinish({"output":text.split(f"{self.ai_prefix}:")[-1].strip()},text)regex=r"Action: (.*?)[\n]*Action Input: ([\s\S]*)"match=re.search(regex,text,re.DOTALL)ifnotmatch:raiseOutputParserException(f"Could not parse LLM output: `{text}`")action=match.group(1)action_input=match.group(2)returnAgentAction(action.strip(),action_input.strip(" ").strip('"'),text)