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.
| Parameter | Type | Description |
|---|---|---|
converters | list[Converter] | List of Converters |
Returns:
list[ConverterConfiguration]— list[ConverterConfiguration]: List of configurations, one per converter
NormalizerRequest¶
Represents a single request sent to normalizer.
Constructor Parameters:
| Parameter | Type | Description |
|---|---|---|
message | Message | The message to be normalized. |
request_converter_configurations | list[ConverterConfiguration] | Configurations for converting the request. Defaults to an empty list. Defaults to None. |
response_converter_configurations | list[ConverterConfiguration] | Configurations for converting the response. Defaults to an empty list. Defaults to None. |
conversation_id | `str | None` |
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] | NoneProcess the prepended conversation by converting it if needed and adding it to memory.
| Parameter | Type | Description |
|---|---|---|
conversation_id | str | The conversation ID to use for the message pieces |
should_convert | bool | Whether to convert the prepended conversation Defaults to True. |
converter_configurations | `list[ConverterConfiguration] | None` |
prepended_conversation | `list[Message] | None` |
target_identifier | `ComponentIdentifier | None` |
Returns:
list[Message] | None— list[Message] | None: The processed prepended conversation
convert_audio_async¶
convert_audio_async(raw_pcm: bytes, converter_configurations: list[ConverterConfiguration], sample_rate_hz: int, num_channels: int, sample_width_bytes: int) → bytesApply 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.
| Parameter | Type | Description |
|---|---|---|
raw_pcm | bytes | Raw PCM audio samples (no WAV header). |
converter_configurations | list[ConverterConfiguration] | Converters to apply. If empty, raw_pcm is returned unchanged and no temp file is written. |
sample_rate_hz | int | Sample rate of the PCM in Hz. |
num_channels | int | Channel count (1 for mono, 2 for stereo). |
sample_width_bytes | int | Bytes per sample (2 for PCM16). |
Returns:
bytes— The converted raw PCM, matching the input format.
Raises:
ValueError— If the converted audio has a different sample rate, channel count, or sample width than the input.
convert_values_async¶
convert_values_async(converter_configurations: list[ConverterConfiguration], message: Message) → NoneApply converter configurations to message pieces.
| Parameter | Type | Description |
|---|---|---|
converter_configurations | list[ConverterConfiguration] | List of configurations specifying which converters to apply and to which message pieces. |
message | Message | The message containing pieces to be converted. |
Raises:
Exception— Any exception from converters propagates with execution context for error tracing.
hash_and_persist_message_async¶
hash_and_persist_message_async(message: Message) → NoneHash 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.
| Parameter | Type | Description |
|---|---|---|
message | Message | The 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) → MessageSend a single request to a target.
| Parameter | Type | Description |
|---|---|---|
message | Message | The message to be sent. |
target | PromptTarget | The target to which the prompt is sent. |
conversation_id | str | The ID of the conversation. Defaults to None. Defaults to None. |
request_converter_configurations | list[ConverterConfiguration] | Configurations for converting the request. Defaults to an empty list. Defaults to None. |
response_converter_configurations | list[ConverterConfiguration] | Configurations for converting the response. Defaults to an empty list. Defaults to None. |
Returns:
Message— The response received from the target.
Raises:
Exception— If an error occurs during the request processing.ValueError— If the message pieces are not part of the same sequence.
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.
| Parameter | Type | Description |
|---|---|---|
requests | list[NormalizerRequest] | A list of NormalizerRequest objects to be sent. |
target | PromptTarget | The target to which the prompts are sent. |
labels | `dict[str, str] | None` |
batch_size | int | The number of prompts to include in each batch. Defaults to 10. Defaults to 10. |
Returns:
list[Message]— list[Message]: A list of Message objects representing the responses received for each prompt.