forked from Archives/langchain
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 ?
This commit is contained in:
parent
a9bb3147d7
commit
5525b704cc
@ -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,6 +13,7 @@ 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]:
|
||||||
|
try:
|
||||||
cleaned_output = text.strip()
|
cleaned_output = text.strip()
|
||||||
if "```json" in cleaned_output:
|
if "```json" in cleaned_output:
|
||||||
_, cleaned_output = cleaned_output.split("```json")
|
_, cleaned_output = cleaned_output.split("```json")
|
||||||
@ -31,6 +32,8 @@ class ConvoOutputParser(AgentOutputParser):
|
|||||||
return AgentFinish({"output": action_input}, text)
|
return AgentFinish({"output": action_input}, text)
|
||||||
else:
|
else:
|
||||||
return AgentAction(action, action_input, text)
|
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:
|
||||||
|
@ -360,7 +360,7 @@ class BaseOutputParser(BaseModel, ABC, Generic[T]):
|
|||||||
return output_parser_dict
|
return output_parser_dict
|
||||||
|
|
||||||
|
|
||||||
class OutputParserException(Exception):
|
class OutputParserException(ValueError):
|
||||||
"""Exception that output parsers should raise to signify a parsing error.
|
"""Exception that output parsers should raise to signify a parsing error.
|
||||||
|
|
||||||
This exists to differentiate parsing errors from other code or execution errors
|
This exists to differentiate parsing errors from other code or execution errors
|
||||||
|
Loading…
Reference in New Issue
Block a user