mirror of
https://github.com/hwchase17/langchain
synced 2024-11-06 03:20:49 +00:00
Async RetryOutputParser, RetryWithErrorOutputParser and OutputFixingParser (#8776)
Added async parsing functions for RetryOutputParser, RetryWithErrorOutputParser and OutputFixingParser. The async parse functions call the arun methods of the used LLMChains. Fix for #7989 --------- Co-authored-by: Benjamin May <benjamin.may94@gmail.com>
This commit is contained in:
parent
cc908d49a3
commit
33cdb06b5c
@ -53,6 +53,19 @@ class OutputFixingParser(BaseOutputParser[T]):
|
|||||||
|
|
||||||
return parsed_completion
|
return parsed_completion
|
||||||
|
|
||||||
|
async def aparse(self, completion: str) -> T:
|
||||||
|
try:
|
||||||
|
parsed_completion = self.parser.parse(completion)
|
||||||
|
except OutputParserException as e:
|
||||||
|
new_completion = await self.retry_chain.arun(
|
||||||
|
instructions=self.parser.get_format_instructions(),
|
||||||
|
completion=completion,
|
||||||
|
error=repr(e),
|
||||||
|
)
|
||||||
|
parsed_completion = self.parser.parse(new_completion)
|
||||||
|
|
||||||
|
return parsed_completion
|
||||||
|
|
||||||
def get_format_instructions(self) -> str:
|
def get_format_instructions(self) -> str:
|
||||||
return self.parser.get_format_instructions()
|
return self.parser.get_format_instructions()
|
||||||
|
|
||||||
|
@ -79,6 +79,26 @@ class RetryOutputParser(BaseOutputParser[T]):
|
|||||||
|
|
||||||
return parsed_completion
|
return parsed_completion
|
||||||
|
|
||||||
|
async def aparse_with_prompt(self, completion: str, prompt_value: PromptValue) -> T:
|
||||||
|
"""Parse the output of an LLM call using a wrapped parser.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
completion: The chain completion to parse.
|
||||||
|
prompt_value: The prompt to use to parse the completion.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
The parsed completion.
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
parsed_completion = self.parser.parse(completion)
|
||||||
|
except OutputParserException:
|
||||||
|
new_completion = await self.retry_chain.arun(
|
||||||
|
prompt=prompt_value.to_string(), completion=completion
|
||||||
|
)
|
||||||
|
parsed_completion = self.parser.parse(new_completion)
|
||||||
|
|
||||||
|
return parsed_completion
|
||||||
|
|
||||||
def parse(self, completion: str) -> T:
|
def parse(self, completion: str) -> T:
|
||||||
raise NotImplementedError(
|
raise NotImplementedError(
|
||||||
"This OutputParser can only be called by the `parse_with_prompt` method."
|
"This OutputParser can only be called by the `parse_with_prompt` method."
|
||||||
@ -136,6 +156,17 @@ class RetryWithErrorOutputParser(BaseOutputParser[T]):
|
|||||||
|
|
||||||
return parsed_completion
|
return parsed_completion
|
||||||
|
|
||||||
|
async def aparse_with_prompt(self, completion: str, prompt_value: PromptValue) -> T:
|
||||||
|
try:
|
||||||
|
parsed_completion = self.parser.parse(completion)
|
||||||
|
except OutputParserException as e:
|
||||||
|
new_completion = await self.retry_chain.arun(
|
||||||
|
prompt=prompt_value.to_string(), completion=completion, error=repr(e)
|
||||||
|
)
|
||||||
|
parsed_completion = self.parser.parse(new_completion)
|
||||||
|
|
||||||
|
return parsed_completion
|
||||||
|
|
||||||
def parse(self, completion: str) -> T:
|
def parse(self, completion: str) -> T:
|
||||||
raise NotImplementedError(
|
raise NotImplementedError(
|
||||||
"This OutputParser can only be called by the `parse_with_prompt` method."
|
"This OutputParser can only be called by the `parse_with_prompt` method."
|
||||||
|
Loading…
Reference in New Issue
Block a user