langchain
langchain copied to clipboard
Added ChainedOutputParser
feature: #4253
Note: Still need to add tests
how is this different than the "CombiningOutputParser"?
The CombiningOutputParser
parses in parallel:
class CombiningOutputParser(BaseOutputParser):
"""Class to combine multiple output parsers into one."""
parsers: List[BaseOutputParser]
[...]
def get_format_instructions(self) -> str:
"""Instructions on how the LLM output should be formatted."""
initial = f"For your first output: {self.parsers[0].get_format_instructions()}"
subsequent = "\n".join(
[
f"Complete that output fully. Then produce another output, separated by two newline characters: {p.get_format_instructions()}" # noqa: E501
for p in self.parsers[1:]
]
)
return f"{initial}\n{subsequent}"
def parse(self, text: str) -> Dict[str, Any]:
"""Parse the output of an LLM call."""
texts = text.split("\n\n")
output = dict()
for i, parser in enumerate(self.parsers):
output.update(parser.parse(texts[i].strip()))
return output
whereas the ChainedOutputParser
parses in serial:
class ChainedOutputParser(BaseOutputParser[T]):
"""Chains multiple output parsers together."""
parsers: list[BaseOutputParser]
def parse(self, text: str) -> T:
"""Parse the output of an LLM call by chaining multiple parsers together."""
value = text
for parser in self.parsers:
value = parser.parse(value)
return value
def parse_with_prompt(self, completion: str, prompt: PromptValue) -> Any:
"""Parse the output of an LLM call with a given prompt by chaining multiple parsers together."""
value = completion
for parser in self.parsers:
value = parser.parse_with_prompt(value, prompt)
return value
def get_format_instructions(self) -> str:
"""Get the formatting instructions for the first parser in the chain."""
return self.parsers[0].get_format_instructions()
BTW, added tests and ready to merge
See #4272
https://github.com/langchain-ai/langchain/pull/4272#issuecomment-1798056650