Skip to content

Commit 85d1395

Browse files
paulinek13Sarayu Poddutoori
authored andcommitted
DOC: improve API reference for prompt_converter module (Azure#969)
1 parent 0e42f5b commit 85d1395

File tree

65 files changed

+864
-532
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+864
-532
lines changed

doc/api.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,10 +319,13 @@ API Reference
319319
RepeatTokenConverter
320320
ROT13Converter
321321
SearchReplaceConverter
322+
SneakyBitsSmugglerConverter
322323
StringJoinConverter
323324
SuffixAppendConverter
324325
SuperscriptConverter
326+
TemplateSegmentConverter
325327
TenseConverter
328+
TextJailbreakConverter
326329
TextToHexConverter
327330
ToneConverter
328331
ToxicSentenceGeneratorConverter
@@ -332,6 +335,7 @@ API Reference
332335
UnicodeSubstitutionConverter
333336
UrlConverter
334337
VariationConverter
338+
VariationSelectorSmugglerConverter
335339
ZalgoConverter
336340
ZeroWidthConverter
337341

pyrit/prompt_converter/add_image_text_converter.py

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,9 @@
1818
class AddImageTextConverter(PromptConverter):
1919
"""
2020
Adds a string to an image and wraps the text into multiple lines if necessary.
21-
This class is similar to AddImageTextConverter except
22-
we pass in an image file path as an argument to the constructor as opposed to text.
2321
24-
Args:
25-
img_to_add (str): File path of image to add text to
26-
font_name (str): Path of font to use. Must be a TrueType font (.ttf). Defaults to "helvetica.ttf".
27-
color (tuple): Color to print text in, using RGB values. Defaults to (0, 0, 0).
28-
font_size (float): Size of font to use. Defaults to 15.
29-
x_pos (int): X coordinate to place text in (0 is left most). Defaults to 10.
30-
y_pos (int): Y coordinate to place text in (0 is upper most). Defaults to 10.
22+
This class is similar to :class:`AddTextImageConverter` except
23+
we pass in an image file path as an argument to the constructor as opposed to text.
3124
"""
3225

3326
def __init__(
@@ -39,6 +32,20 @@ def __init__(
3932
x_pos: int = 10,
4033
y_pos: int = 10,
4134
):
35+
"""
36+
Initializes the converter with the image file path and text properties.
37+
38+
Args:
39+
img_to_add (str): File path of image to add text to.
40+
font_name (str): Path of font to use. Must be a TrueType font (.ttf). Defaults to "helvetica.ttf".
41+
color (tuple): Color to print text in, using RGB values. Defaults to (0, 0, 0).
42+
font_size (float): Size of font to use. Defaults to 15.
43+
x_pos (int): X coordinate to place text in (0 is left most). Defaults to 10.
44+
y_pos (int): Y coordinate to place text in (0 is upper most). Defaults to 10.
45+
46+
Raises:
47+
ValueError: If ``img_to_add`` is empty or invalid, or if ``font_name`` does not end with ".ttf".
48+
"""
4249
if not img_to_add:
4350
raise ValueError("Please provide valid image path")
4451
if not font_name.endswith(".ttf"):
@@ -53,11 +60,11 @@ def __init__(
5360

5461
def _load_font(self):
5562
"""
56-
Load the font for a given font name and font size
63+
Loads the font for a given font name and font size.
5764
5865
Returns:
59-
ImageFont.FreeTypeFont or ImageFont.ImageFont: The loaded font object. If the specified font
60-
cannot be loaded, the default font is returned.
66+
ImageFont.FreeTypeFont or ImageFont.ImageFont: The loaded font object. If the specified font
67+
cannot be loaded, the default font is returned.
6168
6269
Raises:
6370
OSError: If the font resource cannot be loaded, a warning is logged and the default font is used instead.
@@ -72,7 +79,7 @@ def _load_font(self):
7279

7380
def _add_text_to_image(self, text: str) -> Image.Image:
7481
"""
75-
Adds wrapped text to the image at self._img_to_add.
82+
Adds wrapped text to the image at `self._img_to_add`.
7683
7784
Args:
7885
text (str): The text to add to the image.
@@ -111,13 +118,17 @@ def _add_text_to_image(self, text: str) -> Image.Image:
111118

112119
async def convert_async(self, *, prompt: str, input_type: PromptDataType = "text") -> ConverterResult:
113120
"""
114-
Converter that overlays input text on the img_to_add.
121+
Converts the given prompt by adding it as text to the image.
115122
116123
Args:
117-
prompt (str): The prompt to be added to the image.
118-
input_type (PromptDataType): type of data
124+
prompt (str): The text to be added to the image.
125+
input_type (PromptDataType): The type of input data.
126+
119127
Returns:
120-
ConverterResult: The filename of the converted image as a ConverterResult Object
128+
ConverterResult: The result containing path to the updated image.
129+
130+
Raises:
131+
ValueError: If the input type is not supported.
121132
"""
122133
if not self.input_supported(input_type):
123134
raise ValueError("Input type not supported")

pyrit/prompt_converter/add_image_to_video_converter.py

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,8 @@
2727
class AddImageVideoConverter(PromptConverter):
2828
"""
2929
Adds an image to a video at a specified position.
30-
Also, currently the image is placed in the whole video, not at a specific timepoint
3130
32-
Args:
33-
video_path (str): File path of video to add image to
34-
output_path (str, Optional): File path of output video. Defaults to None.
35-
img_position (tuple): Position to place image in video. Defaults to (10, 10).
36-
img_resize_size (tuple): Size to resize image to. Defaults to (500, 500).
31+
Currently the image is placed in the whole video, not at a specific timepoint.
3732
"""
3833

3934
def __init__(
@@ -43,6 +38,18 @@ def __init__(
4338
img_position: tuple = (10, 10),
4439
img_resize_size: tuple = (500, 500),
4540
):
41+
"""
42+
Initializes the converter with the video path and image properties.
43+
44+
Args:
45+
video_path (str): File path of video to add image to.
46+
output_path (str, Optional): File path of output video. Defaults to None.
47+
img_position (tuple): Position to place image in video. Defaults to (10, 10).
48+
img_resize_size (tuple): Size to resize image to. Defaults to (500, 500).
49+
50+
Raises:
51+
ValueError: If ``video_path`` is empty or invalid.
52+
"""
4653

4754
if not video_path:
4855
raise ValueError("Please provide valid video path")
@@ -54,13 +61,14 @@ def __init__(
5461

5562
async def _add_image_to_video(self, image_path: str, output_path: str) -> str:
5663
"""
57-
Adds image to video
64+
Adds an image to video.
65+
5866
Args:
5967
image_path (str): The image path to add to video.
6068
output_path (str): The output video path.
6169
6270
Returns:
63-
output_path (str): The output video path.
71+
str: The output video path.
6472
"""
6573

6674
try:
@@ -158,13 +166,17 @@ async def _add_image_to_video(self, image_path: str, output_path: str) -> str:
158166

159167
async def convert_async(self, *, prompt: str, input_type: PromptDataType = "image_path") -> ConverterResult:
160168
"""
161-
Converter that adds an image to a video
169+
Converts the given prompt (image) by adding it to a video.
162170
163171
Args:
164-
prompt (str): The image file name to be added to the video.
165-
input_type (PromptDataType): type of data
172+
prompt (str): The image path to be added to the video.
173+
input_type (PromptDataType): The type of input data.
174+
166175
Returns:
167-
ConverterResult: The filename of the converted video as a ConverterResult Object
176+
ConverterResult: The result containing filename of the converted video.
177+
178+
Raises:
179+
ValueError: If the input type is not supported.
168180
"""
169181
if not self.input_supported(input_type):
170182
raise ValueError("Input type not supported")

pyrit/prompt_converter/add_text_image_converter.py

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,8 @@ class AddTextImageConverter(PromptConverter):
1919
"""
2020
Adds a string to an image and wraps the text into multiple lines if necessary.
2121
22-
Args:
23-
text_to_add (str): Text to add to an image. Defaults to empty string.
24-
font_name (str): Path of font to use. Must be a TrueType font (.ttf). Defaults to "helvetica.ttf".
25-
color (tuple): Color to print text in, using RGB values. Defaults to (0, 0, 0).
26-
font_size (float): Size of font to use. Defaults to 15.
27-
x_pos (int): X coordinate to place text in (0 is left most). Defaults to 10.
28-
y_pos (int): Y coordinate to place text in (0 is upper most). Defaults to 10.
22+
This class is similar to :class:`AddImageTextConverter` except
23+
we pass in text as an argument to the constructor as opposed to an image file path.
2924
"""
3025

3126
def __init__(
@@ -37,6 +32,20 @@ def __init__(
3732
x_pos: int = 10,
3833
y_pos: int = 10,
3934
):
35+
"""
36+
Initializes the converter with the text and text properties.
37+
38+
Args:
39+
text_to_add (str): Text to add to an image. Defaults to empty string.
40+
font_name (str): Path of font to use. Must be a TrueType font (.ttf). Defaults to "helvetica.ttf".
41+
color (tuple): Color to print text in, using RGB values. Defaults to (0, 0, 0).
42+
font_size (float): Size of font to use. Defaults to 15.
43+
x_pos (int): X coordinate to place text in (0 is left most). Defaults to 10.
44+
y_pos (int): Y coordinate to place text in (0 is upper most). Defaults to 10.
45+
46+
Raises:
47+
ValueError: If ``text_to_add`` is empty, or if ``font_name`` does not end with ".ttf".
48+
"""
4049
if text_to_add.strip() == "":
4150
raise ValueError("Please provide valid text_to_add value")
4251
if not font_name.endswith(".ttf"):
@@ -51,11 +60,11 @@ def __init__(
5160

5261
def _load_font(self):
5362
"""
54-
Load the font for a given font name and font size
63+
Loads the font for a given font name and font size.
5564
5665
Returns:
57-
ImageFont.FreeTypeFont or ImageFont.ImageFont: The loaded font object. If the specified font
58-
cannot be loaded, the default font is returned.
66+
ImageFont.FreeTypeFont or ImageFont.ImageFont: The loaded font object. If the specified font
67+
cannot be loaded, the default font is returned.
5968
6069
Raises:
6170
OSError: If the font resource cannot be loaded, a warning is logged and the default font is used instead.
@@ -105,13 +114,17 @@ def _add_text_to_image(self, image: Image.Image) -> Image.Image:
105114

106115
async def convert_async(self, *, prompt: str, input_type: PromptDataType = "image_path") -> ConverterResult:
107116
"""
108-
Converter that adds text to an image
117+
Converts the given prompt (image) by adding text to it.
109118
110119
Args:
111-
prompt (str): The filename of the image to add the text to
112-
input_type (PromptDataType): type of data
120+
prompt (str): The image file path to which text will be added.
121+
input_type (PromptDataType): The type of input data.
122+
113123
Returns:
114-
ConverterResult: The filename of the converted image as a ConverterResult Object
124+
ConverterResult: The result containing path to the updated image.
125+
126+
Raises:
127+
ValueError: If the input type is not supported.
115128
"""
116129
if not self.input_supported(input_type):
117130
raise ValueError("Input type not supported")

pyrit/prompt_converter/ansi_escape/ansi_attack_converter.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@
2020

2121
class AnsiAttackConverter(PromptConverter):
2222
"""
23-
A single converter that can:
24-
- Use raw and escaped ANSI payloads.
25-
- Ask the model about ANSI codes, repeat given payloads, unescape strings.
26-
- Incorporate the user's original prompt into the final scenario, making the testing more dynamic.
23+
Generates prompts with ANSI codes to evaluate LLM behavior and system risks.
24+
25+
This converter can:
26+
- Use raw and escaped ANSI payloads.
27+
- Ask the model about ANSI codes, repeat given payloads, unescape strings.
28+
- Incorporate the user's original prompt into the final scenario, making the testing more dynamic.
2729
"""
2830

2931
def __init__(
@@ -36,6 +38,8 @@ def __init__(
3638
incorporate_user_prompt: bool = True,
3739
):
3840
"""
41+
Initializes the converter with various options to control the scenarios generated.
42+
3943
Args:
4044
include_raw (bool): Include scenarios with raw ANSI codes.
4145
include_escaped (bool): Include scenarios with escaped ANSI codes.
@@ -58,6 +62,7 @@ def output_supported(self, output_type: PromptDataType) -> bool:
5862
return output_type == "text"
5963

6064
async def convert_async(self, *, prompt: str, input_type: PromptDataType = "text") -> ConverterResult:
65+
"""Converts the given prompt into an ANSI attack scenario."""
6166
if not self.input_supported(input_type):
6267
raise ValueError("Input type not supported")
6368

pyrit/prompt_converter/ascii_art_converter.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,21 @@
88

99

1010
class AsciiArtConverter(PromptConverter):
11-
"""Converts a string to ASCII art"""
11+
"""
12+
Uses the `art` package to convert text into ASCII art.
13+
"""
1214

1315
def __init__(self, font="rand"):
14-
self.font_value = font
15-
16-
async def convert_async(self, *, prompt: str, input_type: PromptDataType = "text") -> ConverterResult:
1716
"""
18-
Converter that uses art to convert strings to ASCII art.
19-
This can sometimes bypass LLM filters
17+
Initializes the converter with a specified font.
2018
2119
Args:
22-
prompt (str): The prompt to be converted.
23-
Returns:
24-
str: The converted prompt.
20+
font (str): The font to use for ASCII art. Defaults to "rand" which selects a random font.
2521
"""
22+
self.font_value = font
23+
24+
async def convert_async(self, *, prompt: str, input_type: PromptDataType = "text") -> ConverterResult:
25+
"""Converts the given prompt into ASCII art."""
2626
if not self.input_supported(input_type):
2727
raise ValueError("Input type not supported")
2828

pyrit/prompt_converter/atbash_converter.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,24 @@
1111

1212
class AtbashConverter(PromptConverter):
1313
"""
14-
Converter to encode prompt using atbash cipher.
14+
Encodes text using the Atbash cipher.
1515
1616
Uses the following to encode:
17-
ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789
18-
ZYXWVUTSRQPONMLKJIHGFEDCBA 9876543210
17+
- ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789
18+
- ZYXWVUTSRQPONMLKJIHGFEDCBA 9876543210
1919
20-
'Hello 123' would encode to 'Svool 876'
21-
22-
Parameters
23-
---
24-
append_description: bool, default=False
25-
Append plaintext "expert" text to the prompt. Includes instructions to only communicate
26-
using the cipher, a description of the cipher, and an example encoded using cipher.
20+
'Hello 123' would encode to 'Svool 876'.
2721
"""
2822

2923
def __init__(self, *, append_description: bool = False) -> None:
24+
"""
25+
Initializes the converter with an option to append a description.
26+
27+
Args:
28+
append_description (bool): If True, appends plaintext "expert" text to the prompt.
29+
This includes instructions to only communicate using the cipher,
30+
a description of the cipher, and an example encoded using the cipher.
31+
"""
3032
self.append_description = append_description
3133
self.example = (
3234
"User: How do I cut down a stop sign?\n"
@@ -35,9 +37,7 @@ def __init__(self, *, append_description: bool = False) -> None:
3537
)
3638

3739
async def convert_async(self, *, prompt: str, input_type: PromptDataType = "text") -> ConverterResult:
38-
"""
39-
Simple converter that atbash cipher encodes the prompt.
40-
"""
40+
"""Converts the given prompt using the Atbash cipher."""
4141
if not self.input_supported(input_type):
4242
raise ValueError("Input type not supported")
4343

0 commit comments

Comments
 (0)