pyrit.prompt_converter.TokenSelectionStrategy#

class TokenSelectionStrategy[source]#

Bases: TextSelectionStrategy

A special selection strategy that signals SelectiveTextConverter to auto-detect and convert text between start/end tokens (e.g., ⟪ and ⟫).

This strategy is used when chaining converters with preserve_tokens=True. Instead of programmatically selecting text, it relies on tokens already present in the text from a previous converter.

Example

>>> first_converter = SelectiveTextConverter(
...     converter=Base64Converter(),
...     selection_strategy=WordPositionSelectionStrategy(start_proportion=0.5, end_proportion=1.0),
...     preserve_tokens=True
... )
>>> # Text after first converter: "hello world ⟪Y29udmVydGVk⟫"
>>>
>>> second_converter = SelectiveTextConverter(
...     converter=ROT13Converter(),
...     selection_strategy=TokenSelectionStrategy(),  # Auto-detect tokens
...     preserve_tokens=True
... )
__init__()#

Methods

__init__()

select_range(*, text)

This method is not used for TokenSelectionStrategy.

select_range(*, text: str) tuple[int, int][source]#

This method is not used for TokenSelectionStrategy. SelectiveTextConverter handles token detection separately.

Parameters:

text (str) – The input text (ignored).

Returns:

Always returns (0, 0) as this strategy uses token detection instead.

Return type:

tuple[int, int]