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

The CodeChameleon Converter uses a combination of personal encryption and decryption functions, code nesting, as well as a set of instructions for the response to bypass LLM safeguards.

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.

Code Chameleon Converter based on https://arxiv.org/abs/2402.16717 by Lv, Huijie, et al.

Parameters:
  • encrypt_mode ({"custom", "reverse", "binary_tree", "odd_even", "length"}) – Select a built-in encryption method or provide custom encryption and decryption functions. 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}]”

  • encrypt_function (Callable, default=None) – User provided encryption function. Only used if encrypt_mode is “custom”. Used to encode user prompt.

  • decrypt_function (Callable or list, default=None) – 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.

__init__(*, encrypt_type: str, encrypt_function: Callable | None = None, decrypt_function: Callable | list[Callable | str] | None = None) None[source]#

Methods

__init__(*, encrypt_type[, ...])

convert_async(*, prompt[, input_type])

Converter that encrypts user prompt, adds stringified decrypt function in markdown and instructions.

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

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

Converter that encrypts user prompt, adds stringified decrypt function in markdown and instructions.

input_supported(input_type: Literal['text', 'image_path', 'audio_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