janus.parsers.parser#

Attributes#

log

Exceptions#

JanusParserException

Exception that output parsers should raise to signify a parsing error.

Classes#

JanusParser

Base class to parse the output of an LLM call.

GenericParser

Base class to parse the output of an LLM call.

Module Contents#

janus.parsers.parser.log#
class janus.parsers.parser.JanusParser#

Bases: langchain.schema.output_parser.BaseOutputParser[str]

Base class to parse the output of an LLM call.

Output parsers help structure language model responses.

Example

class BooleanOutputParser(BaseOutputParser[bool]):
    true_val: str = "YES"
    false_val: str = "NO"

    def parse(self, text: str) -> bool:
        cleaned_text = text.strip().upper()
        if cleaned_text not in (self.true_val.upper(), self.false_val.upper()):
            raise OutputParserException(
                f"BooleanOutputParser expected output value to either be "
                f"{self.true_val} or {self.false_val} (case-insensitive). "
                f"Received {cleaned_text}."
            )
        return cleaned_text == self.true_val.upper()

    @property
    def _type(self) -> str:
        return "boolean_output_parser"
parse_input(block)#

Parse the input block into raw string input ready to be passed to an LLM. Also perform any processing or saving of metadata.

Parameters:

block (janus.language.block.CodeBlock) – The CodeBlock to be processed

Returns:

A parsed version of the input text

Return type:

str

parse_combined_output(text)#

Parse the output text from the LLM when multiple inputs are combined

Parameters:

text (str) – The output text from the LLM

Returns:

A parsed version of the text

Return type:

str

parse_into_block(text, block)#
Parameters:
class janus.parsers.parser.GenericParser#

Bases: JanusParser, langchain_core.output_parsers.StrOutputParser

Base class to parse the output of an LLM call.

Output parsers help structure language model responses.

Example

class BooleanOutputParser(BaseOutputParser[bool]):
    true_val: str = "YES"
    false_val: str = "NO"

    def parse(self, text: str) -> bool:
        cleaned_text = text.strip().upper()
        if cleaned_text not in (self.true_val.upper(), self.false_val.upper()):
            raise OutputParserException(
                f"BooleanOutputParser expected output value to either be "
                f"{self.true_val} or {self.false_val} (case-insensitive). "
                f"Received {cleaned_text}."
            )
        return cleaned_text == self.true_val.upper()

    @property
    def _type(self) -> str:
        return "boolean_output_parser"
parse(text)#

Parse a single string model output into some structure.

Parameters:

text (str | langchain_core.messages.BaseMessage) – String output of a language model.

Returns:

Structured output.

Return type:

str

get_format_instructions()#

Instructions on how the LLM output should be formatted.

Return type:

str

exception janus.parsers.parser.JanusParserException(unparsed_output, *args, **kwargs)#

Bases: langchain_core.exceptions.OutputParserException

Exception that output parsers should raise to signify a parsing error.

This exists to differentiate parsing errors from other code or execution errors that also may arise inside the output parser. OutputParserExceptions will be available to catch and handle in ways to fix the parsing error, while other errors will be raised.

Parameters:
  • error – The error that’s being re-raised or an error message.

  • observation – String explanation of error which can be passed to a model to try and remediate the issue. Defaults to None.

  • llm_output – String model output which is error-ing. Defaults to None.

  • send_to_llm – Whether to send the observation and llm_output back to an Agent after an OutputParserException has been raised. This gives the underlying model driving the agent the context that the previous output was improperly structured, in the hopes that it will update the output to the correct format. Defaults to False.

Initialize self. See help(type(self)) for accurate signature.