pyrit.prompt_converter.RepeatTokenConverter#

class RepeatTokenConverter(*, token_to_repeat: str, times_to_repeat: int, token_insert_mode: Literal['split', 'prepend', 'append', 'repeat'] | None = None)[source]#

Bases: PromptConverter

Repeats a specified token a specified number of times in addition to a given prompt.

Based on: https://dropbox.tech/machine-learning/bye-bye-bye-evolution-of-repeated-token-attacks-on-chatgpt-models

Supported insertion modes:
  • “split”:

    The prompt text will be split on the first occurrence of (.?!) punctuation, and repeated tokens will be inserted at the location of the split.

  • “prepend”:

    Repeated tokens will be inserted before the prompt text.

  • “append”:

    Repeated tokens will be inserted after the prompt text.

  • “repeat”:

    The prompt text will be ignored, and the result will only contain repeated tokens.

__init__(*, token_to_repeat: str, times_to_repeat: int, token_insert_mode: Literal['split', 'prepend', 'append', 'repeat'] | None = None) None[source]#

Initializes the converter with the specified token, number of repetitions, and insertion mode.

Parameters:
  • token_to_repeat (str) – The string to be repeated.

  • times_to_repeat (int) – The number of times the string will be repeated.

  • token_insert_mode (str, optional) – The mode of insertion for the repeated token. Can be “split”, “prepend”, “append”, or “repeat”.

Methods

__init__(*, token_to_repeat, times_to_repeat)

Initializes the converter with the specified token, number of repetitions, and insertion mode.

convert_async(*, prompt[, input_type])

Converts the given prompt by repeating the specified token a specified number of times.

convert_tokens_async(*, prompt[, ...])

Converts substrings within a prompt that are enclosed by specified start and end tokens.

get_identifier()

Returns an identifier dictionary for the converter.

input_supported(input_type)

Checks if the input type is supported by the converter.

output_supported(output_type)

Checks if the output type is supported by the converter.

Attributes

supported_input_types

Returns a list of supported input types for the converter.

supported_output_types

Returns a list of supported output types for the converter.

async convert_async(*, prompt: str, input_type: Literal['text', 'image_path', 'audio_path', 'video_path', 'url', 'error'] = 'text') ConverterResult[source]#

Converts the given prompt by repeating the specified token a specified number of times.

input_supported(input_type: Literal['text', 'image_path', 'audio_path', 'video_path', 'url', 'error']) bool[source]#

Checks if the input type is supported by the converter.

Parameters:

input_type (PromptDataType) – The input type to check.

Returns:

True if the input type is supported, False otherwise.

Return type:

bool

output_supported(output_type: Literal['text', 'image_path', 'audio_path', 'video_path', 'url', 'error']) bool[source]#

Checks if the output type is supported by the converter.

Parameters:

output_type (PromptDataType) – The output type to check.

Returns:

True if the output type is supported, False otherwise.

Return type:

bool