pyrit.prompt_converter.CodeChameleonConverter#
- class CodeChameleonConverter(*, encrypt_type: str, encrypt_function: Callable | None = None, decrypt_function: Callable | list[Callable | str] | None = None)[source]#
Bases:
PromptConverter
Encrypts user prompt, adds stringified decrypt function in markdown and instructions.
The user prompt is encrypted, and the target is asked to solve the encrypted problem by completing a ProblemSolver class utilizing the decryption function while following the instructions.
- Supports the following encryption types:
- custom:
User provided encryption and decryption functions. Encryption function used to encode prompt. Markdown formatting and plaintext instructions appended to decryption function, used as text only. Should include imports.
- reverse:
Reverse the prompt. “How to cut down a tree?” becomes “tree? a down cut to How”.
- binary_tree:
Encode prompt using binary tree. “How to cut down a tree”?” becomes
{'value': 'cut', 'left': {'value': 'How', 'left': None, 'right': {'value': 'to', 'left': None, 'right': None}}, 'right': {'value': 'a', 'left': {'value': 'down', 'left': None, 'right': None}, 'right': {'value': 'tree?', 'left': None, 'right': None}}}
- odd_even:
All words in odd indices of prompt followed by all words in even indices. “How to cut down a tree?” becomes “How cut a to down tree?”.
- length:
List of words in prompt sorted by length, use word as key, original index as value. “How to cut down a tree?” becomes
[{'a': 4}, {'to': 1}, {'How': 0}, {'cut': 2}, {'down': 3}, {'tree?': 5}]
Code Chameleon Converter based on https://arxiv.org/abs/2402.16717 by Lv, Huijie, et al.
- __init__(*, encrypt_type: str, encrypt_function: Callable | None = None, decrypt_function: Callable | list[Callable | str] | None = None) None [source]#
Initializes the converter with the specified encryption type and optional functions.
- Parameters:
encrypt_type (str) – Must be one of “custom”, “reverse”, “binary_tree”, “odd_even” or “length”.
encrypt_function (Callable, optional) – User provided encryption function. Only used if encrypt_mode is “custom”. Used to encode user prompt.
decrypt_function (Callable or list, optional) – User provided encryption function. Only used if encrypt_mode is “custom”. Used as part of markdown code block instructions in system prompt. If list is provided, strings will be treated as single statements for imports or comments. Functions will take the source code of the function.
- Raises:
ValueError – If
encrypt_type
is not valid or ifencrypt_function
ordecrypt_function
are not provided whenencrypt_type
is “custom”.
Methods
__init__
(*, encrypt_type[, ...])Initializes the converter with the specified encryption type and optional functions.
convert_async
(*, prompt[, input_type])Converts the given prompt by applying the specified encryption function.
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 applying the specified encryption function.
- 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:
- 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: