PyRIT icon indicating copy to clipboard operation
PyRIT copied to clipboard

[DRAFT] FEAT: add superscript converter

Open paulinek13 opened this issue 9 months ago • 5 comments

Description

This PR aims to resolve #528 by adding a new converter: SuperscriptConverter which supports various modes for conversion.

class SuperscriptConverter(PromptConverter):
    """
    Converts the input text to superscript text. Supports various modes for conversion.

    Supported modes:
    - 'all': Converts all words. The default mode.
    - 'alternate': Converts every other word. Configurable.
    - 'random': Converts a random selection of words based on a percentage.

    Note:
        This converter leaves characters that do not have a superscript equivalent unchanged.
    """

Tests and Documentation

I've added unit tests for the converter

paulinek13 avatar Mar 21 '25 12:03 paulinek13

While working on this, I came up with a few additional options/modes for conversion:

  • pattern-based: convert only words matching a specific regex pattern
  • keyword-based: convert only words from a list of keywords
  • toggle mode: conversion for marked sections only, e.g., "This is normal but [[this will be superscript]] and this is normal again"

What do you think about these? Would any of them be valuable to add?

As for subscripts, I believe we could implement this by adding an optional boolean parameter to the SuperscriptConverter class. What do you think?

paulinek13 avatar Mar 22 '25 09:03 paulinek13

You can see I tagged you and this PR in #822 because I think a lot of these patterns can be generalized so that we don't have to reinvent the wheel over and over again for various converters. That said, if you want to leave that for a future issue/PR that's totally fine!

romanlutz avatar Mar 24 '25 17:03 romanlutz

a lot of these patterns can be generalized so that we don't have to reinvent the wheel over and over again for various converters

I totally agree! It 100% makes sense to generalize the common patterns first, so I think I’ll wait with this PR until that’s done

paulinek13 avatar Mar 24 '25 18:03 paulinek13

@romanlutz

I have an idea: maybe we could add a base class for all the simple word-level converters, like WordLevelPromptConverter? I did some prototyping here: paulinek13/PyRIT/tree/idea/word_level_converter

So the usage would look like this:

    keyword_converter = SuperscriptConverter(
        mode="keywords",
        keywords=['test', 'conversion']
    )

And in place of SuperscriptConverter we could place any similar converter (word-level based), for example ROT13Converter, which I actually refactored so that it's based on the WordLevelPromptConverter class and now it's as simple as:

class ROT13Converter(WordLevelPromptConverter):
    async def convert_word_async(self, word: str) -> str:
        return codecs.encode(word, "rot13")

The concept's commit: /commit/be8a5d972fb4e7568e11954d9c531d063766d07d Is this idea actually any good?

paulinek13 avatar Mar 24 '25 21:03 paulinek13

Is this idea actually any good?

I love it! What do others think? @rlundeen2 @nina-msft @eugeniavkim @jbolor21 @jsong468 @bashirpartovi

romanlutz avatar Mar 25 '25 03:03 romanlutz

@paulinek13 I suppose we can continue this one now that #847 is in! It might be much simpler now 🙂

romanlutz avatar Jun 09 '25 21:06 romanlutz

Sure thing! I'm on it 😃

paulinek13 avatar Jun 10 '25 17:06 paulinek13

The SuperscriptConverter and its tests are now updated, I just have a few questions:

  • Should I also list this converter in the following files: https://github.com/Azure/PyRIT/blob/main/tests/integration/cli/test_cli_integration.py and https://github.com/Azure/PyRIT/blob/main/tests/unit/cli/prompt_send_success_converters_default.yaml ?

  • What about subscript? Should I add a converter for it as well? In this PR or a new one?

paulinek13 avatar Jun 10 '25 20:06 paulinek13