pyrit.prompt_converter.CharSwapGenerator#

class CharSwapGenerator(*, max_iterations: int = 10, word_swap_ratio: float = 0.2)[source]#

Bases: PromptConverter

A PromptConverter that applies character swapping to words in the prompt to test adversarial textual robustness.

__init__(*, max_iterations: int = 10, word_swap_ratio: float = 0.2)[source]#

Initializes the CharSwapConverter.

Parameters:
  • max_iterations (int) – Number of times to generate perturbed prompts. The higher the number the higher the chance that words are different from the original prompt.

  • word_swap_ratio (float) – Percentage of words to perturb in the prompt per iteration.

Methods

__init__(*[, max_iterations, word_swap_ratio])

Initializes the CharSwapConverter.

convert_async(*, prompt[, input_type])

Converts the given prompt by applying character swaps.

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

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

get_identifier()

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='text') ConverterResult[source]#

Converts the given prompt by applying character swaps.

Parameters:

prompt (str) – The prompt to be converted.

Returns:

The result containing the perturbed prompts.

Return type:

ConverterResult

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 – 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 – The output type to check

Returns:

True if the output type is supported, False otherwise

Return type:

bool