From d8eed6018fedb54ce9e347178cda3a9638c10f27 Mon Sep 17 00:00:00 2001 From: Alexander Dibrov <108030031+dibrale@users.noreply.github.com> Date: Wed, 24 May 2023 12:39:09 -0500 Subject: [PATCH] Output parsing variation allowance (#5178) # Output parsing variation allowance for self-ask with search This change makes self-ask with search easier for Llama models to follow, as they tend toward returning 'Followup:' instead of 'Follow up:' despite an otherwise valid remaining output. Co-authored-by: Dev 2049 --- .../self_ask_with_search/output_parser.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/langchain/agents/self_ask_with_search/output_parser.py b/langchain/agents/self_ask_with_search/output_parser.py index a091adee..1f140221 100644 --- a/langchain/agents/self_ask_with_search/output_parser.py +++ b/langchain/agents/self_ask_with_search/output_parser.py @@ -1,24 +1,21 @@ -from typing import Union +from typing import Sequence, Union from langchain.agents.agent import AgentOutputParser from langchain.schema import AgentAction, AgentFinish, OutputParserException 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]: - followup = "Follow up:" last_line = text.split("\n")[-1] - - if followup not in last_line: - finish_string = "So the final answer is: " - if finish_string not in last_line: + if not any([follow in last_line for follow in self.followups]): + if self.finish_string not in last_line: raise OutputParserException(f"Could not parse output: {text}") - return AgentFinish({"output": last_line[len(finish_string) :]}, text) - - after_colon = text.split(":")[-1] + return AgentFinish({"output": last_line[len(self.finish_string) :]}, text) - if " " == after_colon[0]: - after_colon = after_colon[1:] + after_colon = text.split(":")[-1].strip() return AgentAction("Intermediate Answer", after_colon, text) @property