Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

pyrit.prompt_normalizer

Prompt normalization components for standardizing and converting prompts.

This module provides tools for normalizing prompts before sending them to targets, including converter configurations and request handling.

NormalizerRequest

Represents a single request sent to normalizer.

Constructor Parameters:

ParameterTypeDescription
messageMessageThe message to be normalized.
request_converter_configurationslist[PromptConverterConfiguration]Configurations for converting the request. Defaults to an empty list. Defaults to None.
response_converter_configurationslist[PromptConverterConfiguration]Configurations for converting the response. Defaults to an empty list. Defaults to None.
conversation_idOptional[str]The ID of the conversation. Defaults to None. Defaults to None.

PromptConverterConfiguration

Represents the configuration for a prompt response converter.

The list of converters are applied to a response, which can have multiple response pieces. indexes_to_apply are which pieces to apply to. By default, all indexes are applied. prompt_data_types_to_apply are the types of the responses to apply the converters.

Methods:

from_converters

from_converters(converters: list[PromptConverter]) → list[PromptConverterConfiguration]

Convert a list of converters into a list of PromptConverterConfiguration objects. Each converter gets its own configuration with default settings.

ParameterTypeDescription
converterslist[PromptConverter]List of PromptConverters

Returns:

PromptNormalizer

Handles normalization and processing of prompts before they are sent to targets.

Methods:

add_prepended_conversation_to_memory

add_prepended_conversation_to_memory(conversation_id: str, should_convert: bool = True, converter_configurations: Optional[list[PromptConverterConfiguration]] = None, attack_identifier: Optional[ComponentIdentifier] = None, prepended_conversation: Optional[list[Message]] = None) → Optional[list[Message]]

Process the prepended conversation by converting it if needed and adding it to memory.

ParameterTypeDescription
conversation_idstrThe conversation ID to use for the message pieces
should_convertboolWhether to convert the prepended conversation Defaults to True.
converter_configurationsOptional[list[PromptConverterConfiguration]]Configurations for converting the request Defaults to None.
attack_identifierOptional[ComponentIdentifier]Identifier for the attack Defaults to None.
prepended_conversationOptional[list[Message]]The conversation to prepend Defaults to None.

Returns:

convert_values

convert_values(converter_configurations: list[PromptConverterConfiguration], message: Message) → None

Apply converter configurations to message pieces.

ParameterTypeDescription
converter_configurationslist[PromptConverterConfiguration]List of configurations specifying which converters to apply and to which message pieces.
messageMessageThe message containing pieces to be converted.

Raises:

send_prompt_async

send_prompt_async(message: Message, target: PromptTarget, conversation_id: Optional[str] = None, request_converter_configurations: list[PromptConverterConfiguration] | None = None, response_converter_configurations: list[PromptConverterConfiguration] | None = None, labels: Optional[dict[str, str]] = None, attack_identifier: Optional[ComponentIdentifier] = None) → Message

Send a single request to a target.

ParameterTypeDescription
messageMessageThe message to be sent.
targetPromptTargetThe target to which the prompt is sent.
conversation_idstrThe ID of the conversation. Defaults to None. Defaults to None.
request_converter_configurationslist[PromptConverterConfiguration]Configurations for converting the request. Defaults to an empty list. Defaults to None.
response_converter_configurationslist[PromptConverterConfiguration]Configurations for converting the response. Defaults to an empty list. Defaults to None.
labelsOptional[dict[str, str]]Labels associated with the request. Defaults to None. Defaults to None.
attack_identifierOptional[ComponentIdentifier]Identifier for the attack. Defaults to None. Defaults to None.

Returns:

Raises:

send_prompt_batch_to_target_async

send_prompt_batch_to_target_async(requests: list[NormalizerRequest], target: PromptTarget, labels: Optional[dict[str, str]] = None, attack_identifier: Optional[ComponentIdentifier] = None, batch_size: int = 10) → list[Message]

Send a batch of prompts to the target asynchronously.

ParameterTypeDescription
requestslist[NormalizerRequest]A list of NormalizerRequest objects to be sent.
targetPromptTargetThe target to which the prompts are sent.
labelsOptional[dict[str, str]]A dictionary of labels to be included with the request. Defaults to None. Defaults to None.
attack_identifierOptional[ComponentIdentifier]The attack identifier. Defaults to None. Defaults to None.
batch_sizeintThe number of prompts to include in each batch. Defaults to 10. Defaults to 10.

Returns: