Parsers

This document provides an overview of the various parser classes available in the rupsycho.parsers module. The parsers are organized into three categories: cleaners, judges, and validators.

Cleaners

This section defines specialized parser classes called Cleaners, designed to process and clean textual answers by removing artifacts that may have been introduced during the text generation process.

class rupsycho.parsers.cleaners.BasicCleaner(*args, name=None)[source]

Bases: BaseOutputParser[str]

A custom parser that processes and cleans text by removing line breaks and non-ASCII Unicode characters.

This parser is designed to work with LangChain and can be integrated into various chains or agents that require cleaned text output.

Parameters:
  • args (Any)

  • name (str | None)

parse(text)[source]

Parses the input text to remove line breaks and non-ASCII Unicode characters.

Parameters:

text (str) – The input string to be cleaned.

Returns:

The cleaned string with line breaks and non-ASCII Unicode characters removed.

Return type:

str

Raises:

OutputParserException – If an error occurs during parsing, an OutputParserException is raised with a descriptive error message.

class rupsycho.parsers.cleaners.PromptRemovalCleaner(prompt, similarity_threshold=0.7, fast=True)[source]

Bases: BaseOutputParser[str]

A custom parser that removes prompt-related text from a completion. It uses the prompt_cleaner function to process and clean the input text.

Parameters:
  • prompt (str)

  • similarity_threshold (float)

  • fast (bool)

prompt: str
similarity_threshold: float
fast: bool
parse(completion)[source]

Cleans the input completion text by removing prompt-related content.

Parameters:

completion (str) – The input text (completion) that needs to be cleaned.

Returns:

The cleaned completion text.

Return type:

str

Raises:

OutputParserException – If an error occurs during parsing, an OutputParserException is raised with a descriptive error message.

Judges

This section defines specialized parser classes called Judges, designed to evaluate and process input answers. The Judges parser takes in a textual answer and applies a series of checks and transformations to determine a final prediction or verdict.

class rupsycho.parsers.judges.MultipleChoiceParser(possible_answers)[source]

Bases: BaseOutputParser[str]

A custom parser that processes a text input and returns the most likely answer from a set of possible multiple-choice answers using the check_multiple_choice_answers function.

This parser is designed to work with LangChain and can be integrated into various chains or agents that require decision-making based on textual analysis.

Parameters:

possible_answers (list[str])

possible_answers: list[str]
parse(text)[source]

Parses the input text to determine the most likely answer from the possible answers.

Parameters:

text (str)

Return type:

str

Validators

This section defines specialized parser classes called Validators, designed to assess and validate textual answers. The Validator parser takes an answer as input and returns the original answer along with a validation status that indicates whether the input meets specified criteria or rules.

class rupsycho.parsers.validators.ApologiesValidatorParser(*args, name=None)[source]

Bases: BaseOutputParser[dict]

A parser that validates the input text for apologies-related content. Returns the original text along with a validation status.

Parameters:
  • args (Any)

  • name (str | None)

parse(text)[source]

Parse a single string model output into some structure.

Parameters:

text (str) – String output of a language model.

Returns:

Structured output.

Return type:

dict

class rupsycho.parsers.validators.BeingAiValidatorParser(*args, name=None)[source]

Bases: BaseOutputParser[dict]

A parser that validates the input text for being_ai-related content. Returns the original text along with a validation status.

Parameters:
  • args (Any)

  • name (str | None)

parse(text)[source]

Parse a single string model output into some structure.

Parameters:

text (str) – String output of a language model.

Returns:

Structured output.

Return type:

dict

class rupsycho.parsers.validators.RefusalValidatorParser(*args, name=None)[source]

Bases: BaseOutputParser[dict]

A parser that validates the input text for refusal-related content. Returns the original text along with a validation status.

Parameters:
  • args (Any)

  • name (str | None)

parse(text)[source]

Parse a single string model output into some structure.

Parameters:

text (str) – String output of a language model.

Returns:

Structured output.

Return type:

dict

class rupsycho.parsers.validators.ValidatorParser[source]

Bases: BaseOutputParser[dict]

A custom parser that combines the results of ApologiesValidatorParser, BeingAiValidatorParser, and RefusalValidatorParser to validate the input text against these checks and return the original text along with a combined validation status.

apologies_parser: ApologiesValidatorParser
being_ai_parser: BeingAiValidatorParser
refusal_parser: RefusalValidatorParser
parse(text)[source]

Parses the input text by running it through all three validators and combining their results.

Parameters:

text (str) – The input string to be validated.

Returns:

A dictionary containing the original text, a combined validation status, and detailed results from each individual validator.

Return type:

dict