From 5525b704cc5e561638b79fb64960d7aff778667c Mon Sep 17 00:00:00 2001 From: Chakib Ben Ziane Date: Fri, 19 May 2023 01:20:35 +0200 Subject: [PATCH] Chatconv agent: output parser exception (#4923) the output parser form chat conversational agent now raises `OutputParserException` like the rest. The `raise OutputParserExeption(...) from e` form also carries through the original error details on what went wrong. I added the `ValueError` as a base class to `OutputParserException` to avoid breaking code that was relying on `ValueError` as a way to catch exceptions from the agent. So catching ValuError still works. Not sure if this is a good idea though ? --- .../conversational_chat/output_parser.py | 41 ++++++++++--------- langchain/schema.py | 2 +- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/langchain/agents/conversational_chat/output_parser.py b/langchain/agents/conversational_chat/output_parser.py index 99880fac..d8a2c593 100644 --- a/langchain/agents/conversational_chat/output_parser.py +++ b/langchain/agents/conversational_chat/output_parser.py @@ -5,7 +5,7 @@ from typing import Union from langchain.agents import AgentOutputParser 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): @@ -13,24 +13,27 @@ class ConvoOutputParser(AgentOutputParser): return FORMAT_INSTRUCTIONS def parse(self, text: str) -> Union[AgentAction, AgentFinish]: - cleaned_output = text.strip() - if "```json" in cleaned_output: - _, cleaned_output = cleaned_output.split("```json") - if "```" in cleaned_output: - cleaned_output, _ = cleaned_output.split("```") - if cleaned_output.startswith("```json"): - cleaned_output = cleaned_output[len("```json") :] - if cleaned_output.startswith("```"): - cleaned_output = cleaned_output[len("```") :] - if cleaned_output.endswith("```"): - cleaned_output = cleaned_output[: -len("```")] - cleaned_output = cleaned_output.strip() - response = json.loads(cleaned_output) - action, action_input = response["action"], response["action_input"] - if action == "Final Answer": - return AgentFinish({"output": action_input}, text) - else: - return AgentAction(action, action_input, text) + try: + cleaned_output = text.strip() + if "```json" in cleaned_output: + _, cleaned_output = cleaned_output.split("```json") + if "```" in cleaned_output: + cleaned_output, _ = cleaned_output.split("```") + if cleaned_output.startswith("```json"): + cleaned_output = cleaned_output[len("```json") :] + if cleaned_output.startswith("```"): + cleaned_output = cleaned_output[len("```") :] + if cleaned_output.endswith("```"): + cleaned_output = cleaned_output[: -len("```")] + cleaned_output = cleaned_output.strip() + response = json.loads(cleaned_output) + action, action_input = response["action"], response["action_input"] + if action == "Final Answer": + return AgentFinish({"output": 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 def _type(self) -> str: diff --git a/langchain/schema.py b/langchain/schema.py index 61c536dd..40fd659d 100644 --- a/langchain/schema.py +++ b/langchain/schema.py @@ -360,7 +360,7 @@ class BaseOutputParser(BaseModel, ABC, Generic[T]): return output_parser_dict -class OutputParserException(Exception): +class OutputParserException(ValueError): """Exception that output parsers should raise to signify a parsing error. This exists to differentiate parsing errors from other code or execution errors