@ -1,24 +1,21 @@
from typing import Union
from typing import Sequence, Union
from langchain . agents . agent import AgentOutputParser
from langchain . agents . agent import AgentOutputParser
from langchain . schema import AgentAction , AgentFinish , OutputParserException
from langchain . schema import AgentAction , AgentFinish , OutputParserException
class SelfAskOutputParser ( AgentOutputParser ) :
class SelfAskOutputParser ( AgentOutputParser ) :
followups : Sequence [ str ] = ( " Follow up: " , " Followup: " )
finish_string : str = " So the final answer is: "
def parse ( self , text : str ) - > Union [ AgentAction , AgentFinish ] :
def parse ( self , text : str ) - > Union [ AgentAction , AgentFinish ] :
followup = " Follow up: "
last_line = text . split ( " \n " ) [ - 1 ]
last_line = text . split ( " \n " ) [ - 1 ]
if not any ( [ follow in last_line for follow in self . followups ] ) :
if followup not in last_line :
if self . finish_string not in last_line :
finish_string = " So the final answer is: "
if finish_string not in last_line :
raise OutputParserException ( f " Could not parse output: { text } " )
raise OutputParserException ( f " Could not parse output: { text } " )
return AgentFinish ( { " output " : last_line [ len ( finish_string ) : ] } , text )
return AgentFinish ( { " output " : last_line [ len ( self . finish_string ) : ] } , text )
after_colon = text . split ( " : " ) [ - 1 ]
if " " == after_colon [ 0 ] :
after_colon = text . split ( " : " ) [ - 1 ] . strip ( )
after_colon = after_colon [ 1 : ]
return AgentAction ( " Intermediate Answer " , after_colon , text )
return AgentAction ( " Intermediate Answer " , after_colon , text )
@property
@property