From ee940b8d5a0c7057428d684dc1b1c813c5cdb860 Mon Sep 17 00:00:00 2001 From: hdnh2006 Date: Wed, 7 Feb 2024 10:38:25 +0100 Subject: [PATCH] Standardize output_parser across all agent types --- libs/langchain/langchain/agents/chat/output_parser.py | 6 +++++- .../langchain/agents/conversational/output_parser.py | 6 +++++- .../langchain/agents/conversational_chat/output_parser.py | 1 + libs/langchain/langchain/agents/mrkl/output_parser.py | 6 +++++- .../langchain/agents/structured_chat/output_parser.py | 7 ++++++- 5 files changed, 22 insertions(+), 4 deletions(-) diff --git a/libs/langchain/langchain/agents/chat/output_parser.py b/libs/langchain/langchain/agents/chat/output_parser.py index 069a7fdbb5cd7..cdb8c0d0dae1d 100644 --- a/libs/langchain/langchain/agents/chat/output_parser.py +++ b/libs/langchain/langchain/agents/chat/output_parser.py @@ -14,11 +14,15 @@ class ChatOutputParser(AgentOutputParser): """Output parser for the chat agent.""" + format_instructions: str = FORMAT_INSTRUCTIONS + """Default formatting instructions""" + pattern = re.compile(r"^.*?`{3}(?:json)?\n(.*?)`{3}.*?$", re.DOTALL) """Regex pattern to parse the output.""" def get_format_instructions(self) -> str: - return FORMAT_INSTRUCTIONS + """Returns formatting instructions for the given output parser.""" + return self.format_instructions def parse(self, text: str) -> Union[AgentAction, AgentFinish]: includes_answer = FINAL_ANSWER_ACTION in text diff --git a/libs/langchain/langchain/agents/conversational/output_parser.py b/libs/langchain/langchain/agents/conversational/output_parser.py index 6d0446b81bf22..e74e626faa99e 100644 --- a/libs/langchain/langchain/agents/conversational/output_parser.py +++ b/libs/langchain/langchain/agents/conversational/output_parser.py @@ -14,8 +14,12 @@ class ConvoOutputParser(AgentOutputParser): ai_prefix: str = "AI" """Prefix to use before AI output.""" + format_instructions: str = FORMAT_INSTRUCTIONS + """Default formatting instructions""" + def get_format_instructions(self) -> str: - return FORMAT_INSTRUCTIONS + """Returns formatting instructions for the given output parser.""" + return self.format_instructions def parse(self, text: str) -> Union[AgentAction, AgentFinish]: if f"{self.ai_prefix}:" in text: diff --git a/libs/langchain/langchain/agents/conversational_chat/output_parser.py b/libs/langchain/langchain/agents/conversational_chat/output_parser.py index ea3a380f4bc92..6fd330ccf8d18 100644 --- a/libs/langchain/langchain/agents/conversational_chat/output_parser.py +++ b/libs/langchain/langchain/agents/conversational_chat/output_parser.py @@ -15,6 +15,7 @@ class ConvoOutputParser(AgentOutputParser): """Output parser for the conversational agent.""" format_instructions: str = FORMAT_INSTRUCTIONS + """Default formatting instructions""" def get_format_instructions(self) -> str: """Returns formatting instructions for the given output parser.""" diff --git a/libs/langchain/langchain/agents/mrkl/output_parser.py b/libs/langchain/langchain/agents/mrkl/output_parser.py index b716c49db4808..f6489c2a79289 100644 --- a/libs/langchain/langchain/agents/mrkl/output_parser.py +++ b/libs/langchain/langchain/agents/mrkl/output_parser.py @@ -22,8 +22,12 @@ class MRKLOutputParser(AgentOutputParser): """MRKL Output parser for the chat agent.""" + format_instructions: str = FORMAT_INSTRUCTIONS + """Default formatting instructions""" + def get_format_instructions(self) -> str: - return FORMAT_INSTRUCTIONS + """Returns formatting instructions for the given output parser.""" + return self.format_instructions def parse(self, text: str) -> Union[AgentAction, AgentFinish]: includes_answer = FINAL_ANSWER_ACTION in text diff --git a/libs/langchain/langchain/agents/structured_chat/output_parser.py b/libs/langchain/langchain/agents/structured_chat/output_parser.py index eb85d6684b6e8..2f91a8352a0e6 100644 --- a/libs/langchain/langchain/agents/structured_chat/output_parser.py +++ b/libs/langchain/langchain/agents/structured_chat/output_parser.py @@ -20,10 +20,15 @@ class StructuredChatOutputParser(AgentOutputParser): """Output parser for the structured chat agent.""" + format_instructions: str = FORMAT_INSTRUCTIONS + """Default formatting instructions""" + pattern = re.compile(r"```(?:json\s+)?(\W.*?)```", re.DOTALL) + """Regex pattern to parse the output.""" def get_format_instructions(self) -> str: - return FORMAT_INSTRUCTIONS + """Returns formatting instructions for the given output parser.""" + return self.format_instructions def parse(self, text: str) -> Union[AgentAction, AgentFinish]: try: