pyrit.prompt_converter.InsertPunctuationConverter#

class InsertPunctuationConverter(word_swap_ratio: float = 0.2, between_words: bool = True)[source]#

Bases: PromptConverter

Inserts punctuation into a prompt to test robustness.

Punctuation insertion: inserting single punctuations in string.punctuation. Words in a prompt: a word does not contain any punctuation and space. “a1b2c3” is a word; “a1 2” are 2 words; “a1,b,3” are 3 words.

__init__(word_swap_ratio: float = 0.2, between_words: bool = True) None[source]#

Initializes the converter with a word swap ratio and punctuation insertion mode.

Parameters:
  • word_swap_ratio (float) – Percentage of words to perturb. Defaults to 0.2.

  • between_words (bool) – If True, insert punctuation only between words. If False, insert punctuation within words. Defaults to True.

Raises:

ValueError – If word_swap_ratio is not between 0 and 1.

Methods

__init__([word_swap_ratio, between_words])

Initializes the converter with a word swap ratio and punctuation insertion mode.

convert_async(*, prompt[, input_type, ...])

Converts the given prompt by inserting punctuation.

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

default_punctuation_list

Common punctuation characters.

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', punctuation_list: List[str] | None = None) ConverterResult[source]#

Converts the given prompt by inserting punctuation.

Parameters:
  • prompt (str) – The text to convert.

  • input_type (PromptDataType) – The type of input data.

  • punctuation_list (Optional[List[str]]) – List of punctuations to use for insertion.

Returns:

The result containing a interation of modified prompts.

Return type:

ConverterResult

Raises:

ValueError – If the input type is not supported.

default_punctuation_list = [',', '.', '!', '?', ':', ';', '-']#

Common punctuation characters. Used if no punctuation list is provided.

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