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.

ConverterConfiguration

Represents the configuration for a 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[Converter]) → list[ConverterConfiguration]

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

ParameterTypeDescription
converterslist[Converter]List of Converters

Returns:

NormalizerRequest

Represents a single request sent to normalizer.

Constructor Parameters:

ParameterTypeDescription
messageMessageThe message to be normalized.
request_converter_configurationslist[ConverterConfiguration]Configurations for converting the request. Defaults to an empty list. Defaults to None.
response_converter_configurationslist[ConverterConfiguration]Configurations for converting the response. Defaults to an empty list. Defaults to None.
conversation_id`strNone`

PromptNormalizer

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

Methods:

add_prepended_conversation_to_memory_async

add_prepended_conversation_to_memory_async(conversation_id: str, should_convert: bool = True, converter_configurations: list[ConverterConfiguration] | None = None, prepended_conversation: list[Message] | None = None, target_identifier: ComponentIdentifier | None = None) → list[Message] | None

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_configurations`list[ConverterConfiguration]None`
prepended_conversation`list[Message]None`
target_identifier`ComponentIdentifierNone`

Returns:

convert_audio_async

convert_audio_async(raw_pcm: bytes, converter_configurations: list[ConverterConfiguration], sample_rate_hz: int, num_channels: int, sample_width_bytes: int) → bytes

Apply converters to raw PCM audio and return the converted PCM.

Wraps the input PCM in a temporary WAV file, builds a single-piece audio_path Message, runs convert_values, then reads the converted file back as raw PCM. The caller’s PCM format is preserved end-to-end; converters that change the format trigger a ValueError on read-back.

ParameterTypeDescription
raw_pcmbytesRaw PCM audio samples (no WAV header).
converter_configurationslist[ConverterConfiguration]Converters to apply. If empty, raw_pcm is returned unchanged and no temp file is written.
sample_rate_hzintSample rate of the PCM in Hz.
num_channelsintChannel count (1 for mono, 2 for stereo).
sample_width_bytesintBytes per sample (2 for PCM16).

Returns:

Raises:

convert_values_async

convert_values_async(converter_configurations: list[ConverterConfiguration], message: Message) → None

Apply converter configurations to message pieces.

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

Raises:

hash_and_persist_message_async

hash_and_persist_message_async(message: Message) → None

Hash and persist a Message to memory.

Use when a target assembles a Message outside the send_prompt_async flow (e.g. streaming sessions that yield per-turn Messages directly). Register the conversation once via MemoryInterface.add_conversation_to_memory before persisting its messages.

ParameterTypeDescription
messageMessageThe message to hash and persist.

send_prompt_async

send_prompt_async(message: Message, target: PromptTarget, conversation_id: str | None = None, request_converter_configurations: list[ConverterConfiguration] | None = None, response_converter_configurations: list[ConverterConfiguration] | None = 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[ConverterConfiguration]Configurations for converting the request. Defaults to an empty list. Defaults to None.
response_converter_configurationslist[ConverterConfiguration]Configurations for converting the response. Defaults to an empty list. Defaults to None.

Returns:

Raises:

send_prompt_batch_to_target_async

send_prompt_batch_to_target_async(requests: list[NormalizerRequest], target: PromptTarget, labels: dict[str, str] | None = 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.
labels`dict[str, str]None`
batch_sizeintThe number of prompts to include in each batch. Defaults to 10. Defaults to 10.

Returns: