langchain icon indicating copy to clipboard operation
langchain copied to clipboard

Added ChainedOutputParser

Open JacobFV opened this issue 1 year ago • 2 comments

feature: #4253

Note: Still need to add tests

JacobFV avatar May 07 '23 06:05 JacobFV

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()

JacobFV avatar May 09 '23 02:05 JacobFV

BTW, added tests and ready to merge

JacobFV avatar May 09 '23 02:05 JacobFV

See #4272

efriis avatar Nov 07 '23 04:11 efriis

https://github.com/langchain-ai/langchain/pull/4272#issuecomment-1798056650

JacobFV avatar Nov 07 '23 08:11 JacobFV