pyrit.prompt_normalizer.PromptNormalizer#
- class PromptNormalizer(start_token: str = '⟪', end_token: str = '⟫')[source]#
Bases:
object- __init__(start_token: str = '⟪', end_token: str = '⟫') None[source]#
Initializes the PromptNormalizer.
start_token and end_token are used to delineate which part of a prompt is converted.
Methods
__init__([start_token, end_token])Initializes the PromptNormalizer.
add_prepended_conversation_to_memory(...[, ...])Processes the prepended conversation by converting it if needed and adding it to memory.
convert_values(converter_configurations, message)send_prompt_async(*, seed_group, target[, ...])Sends a single request to a target.
send_prompt_batch_to_target_async(*, ...[, ...])Sends a batch of prompts to the target asynchronously.
set_skip_criteria(skip_criteria, skip_value_type)Sets the skip criteria for the attack.
- async add_prepended_conversation_to_memory(conversation_id: str, should_convert: bool = True, converter_configurations: list[PromptConverterConfiguration] | None = None, attack_identifier: dict[str, str] | None = None, prepended_conversation: list[Message] | None = None) list[Message] | None[source]#
Processes the prepended conversation by converting it if needed and adding it to memory.
- Parameters:
conversation_id (str) – The conversation ID to use for the message pieces
should_convert (bool) – Whether to convert the prepended conversation
converter_configurations (Optional[list[PromptConverterConfiguration]]) – Configurations for converting the request
attack_identifier (Optional[dict[str, str]]) – Identifier for the attack
prepended_conversation (Optional[list[Message]]) – The conversation to prepend
- Returns:
The processed prepended conversation
- Return type:
- async convert_values(converter_configurations: list[PromptConverterConfiguration], message: Message)[source]#
- async send_prompt_async(*, seed_group: SeedGroup, target: PromptTarget, conversation_id: str | None = None, request_converter_configurations: list[PromptConverterConfiguration] = [], response_converter_configurations: list[PromptConverterConfiguration] = [], labels: dict[str, str] | None = None, attack_identifier: dict[str, str] | None = None) Message[source]#
Sends a single request to a target.
- Parameters:
seed_group (SeedGroup) – The seed group to be sent.
target (PromptTarget) – The target to which the prompt is sent.
conversation_id (str, optional) – The ID of the conversation. Defaults to None.
request_converter_configurations (list[PromptConverterConfiguration], optional) – Configurations for converting the request. Defaults to an empty list.
response_converter_configurations (list[PromptConverterConfiguration], optional) – Configurations for converting the response. Defaults to an empty list.
labels (Optional[dict[str, str]], optional) – Labels associated with the request. Defaults to None.
attack_identifier (Optional[dict[str, str]], optional) – Identifier for the attack. Defaults to None.
- Raises:
Exception – If an error occurs during the request processing.
ValueError – If the prompts in the SeedGroup are not part of the same sequence.
- Returns:
The response received from the target.
- Return type:
- async send_prompt_batch_to_target_async(*, requests: list[NormalizerRequest], target: PromptTarget, labels: dict[str, str] | None = None, attack_identifier: dict[str, str] | None = None, batch_size: int = 10) list[Message][source]#
Sends a batch of prompts to the target asynchronously.
- Parameters:
requests (list[NormalizerRequest]) – A list of NormalizerRequest objects to be sent.
target (PromptTarget) – The target to which the prompts are sent.
labels (Optional[dict[str, str]], optional) – A dictionary of labels to be included with the request. Defaults to None.
attack_identifier (Optional[dict[str, str]], optional) – A dictionary identifying the attack. Defaults to None.
batch_size (int, optional) – The number of prompts to include in each batch. Defaults to 10.
- Returns:
- A list of Message objects representing the responses
received for each prompt.
- Return type:
- set_skip_criteria(skip_criteria: PromptFilterCriteria, skip_value_type: Literal['original', 'converted'], ensure_response=True) None[source]#
Sets the skip criteria for the attack.
If prompts match this in memory and are the same as one being sent, then they won’t be sent to a target.
Prompts are the same if either the original prompt or the converted prompt, determined by skip_value_type flag.