@ -5,7 +5,7 @@ from typing import Union
from langchain . agents import AgentOutputParser
from langchain . agents import AgentOutputParser
from langchain . agents . conversational_chat . prompt import FORMAT_INSTRUCTIONS
from langchain . agents . conversational_chat . prompt import FORMAT_INSTRUCTIONS
from langchain . schema import AgentAction , AgentFinish
from langchain . schema import AgentAction , AgentFinish , OutputParserException
class ConvoOutputParser ( AgentOutputParser ) :
class ConvoOutputParser ( AgentOutputParser ) :
@ -13,24 +13,27 @@ class ConvoOutputParser(AgentOutputParser):
return FORMAT_INSTRUCTIONS
return FORMAT_INSTRUCTIONS
def parse ( self , text : str ) - > Union [ AgentAction , AgentFinish ] :
def parse ( self , text : str ) - > Union [ AgentAction , AgentFinish ] :
cleaned_output = text . strip ( )
try :
if " ```json " in cleaned_output :
cleaned_output = text . strip ( )
_ , cleaned_output = cleaned_output . split ( " ```json " )
if " ```json " in cleaned_output :
if " ``` " in cleaned_output :
_ , cleaned_output = cleaned_output . split ( " ```json " )
cleaned_output , _ = cleaned_output . split ( " ``` " )
if " ``` " in cleaned_output :
if cleaned_output . startswith ( " ```json " ) :
cleaned_output , _ = cleaned_output . split ( " ``` " )
cleaned_output = cleaned_output [ len ( " ```json " ) : ]
if cleaned_output . startswith ( " ```json " ) :
if cleaned_output . startswith ( " ``` " ) :
cleaned_output = cleaned_output [ len ( " ```json " ) : ]
cleaned_output = cleaned_output [ len ( " ``` " ) : ]
if cleaned_output . startswith ( " ``` " ) :
if cleaned_output . endswith ( " ``` " ) :
cleaned_output = cleaned_output [ len ( " ``` " ) : ]
cleaned_output = cleaned_output [ : - len ( " ``` " ) ]
if cleaned_output . endswith ( " ``` " ) :
cleaned_output = cleaned_output . strip ( )
cleaned_output = cleaned_output [ : - len ( " ``` " ) ]
response = json . loads ( cleaned_output )
cleaned_output = cleaned_output . strip ( )
action , action_input = response [ " action " ] , response [ " action_input " ]
response = json . loads ( cleaned_output )
if action == " Final Answer " :
action , action_input = response [ " action " ] , response [ " action_input " ]
return AgentFinish ( { " output " : action_input } , text )
if action == " Final Answer " :
else :
return AgentFinish ( { " output " : action_input } , text )
return AgentAction ( action , action_input , text )
else :
return AgentAction ( action , action_input , text )
except Exception as e :
raise OutputParserException ( f " Could not parse LLM output: { text } " ) from e
@property
@property
def _type ( self ) - > str :
def _type ( self ) - > str :