Skip to content

Commit 7a7f8b6

Browse files
authored
MAINT: fix docstrings for /prompt_converter (microsoft#1314)
1 parent af011a0 commit 7a7f8b6

72 files changed

Lines changed: 919 additions & 208 deletions

File tree

Some content is hidden

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

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ notice-rgx = "Copyright \\(c\\) Microsoft Corporation\\.\\s*\\n.*Licensed under
279279
# Temporary ignores for pyrit/ subdirectories until issue #1176
280280
# https://github.com/Azure/PyRIT/issues/1176 is fully resolved
281281
# TODO: Remove these ignores once the issues are fixed
282-
"pyrit/{auxiliary_attacks,exceptions,models,prompt_converter,ui}/**/*.py" = ["D101", "D102", "D103", "D104", "D105", "D106", "D107", "D401", "D404", "D417", "D418", "DOC102", "DOC201", "DOC202", "DOC402", "DOC501"]
282+
"pyrit/{auxiliary_attacks,exceptions,models,ui}/**/*.py" = ["D101", "D102", "D103", "D104", "D105", "D106", "D107", "D401", "D404", "D417", "D418", "DOC102", "DOC201", "DOC202", "DOC402", "DOC501"]
283283
"pyrit/__init__.py" = ["D104"]
284284

285285
[tool.ruff.lint.pydocstyle]

pyrit/prompt_converter/__init__.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
# Copyright (c) Microsoft Corporation.
22
# Licensed under the MIT license.
33

4+
"""
5+
Prompt converters for transforming prompts before sending them to targets in red teaming workflows.
6+
7+
Converters are organized into categories: Text-to-Text (encoding, obfuscation, translation, variation),
8+
Audio (text-to-audio, audio-to-text, audio-to-audio), Image (text-to-image, image-to-image),
9+
Video (image-to-video), File (text-to-PDF/URL), Selective Converting (partial prompt transformation),
10+
and Human-in-the-Loop (interactive review). Converters can be stacked together to create complex
11+
transformation pipelines for testing AI system robustness.
12+
"""
13+
414
from pyrit.prompt_converter.add_image_text_converter import AddImageTextConverter
515
from pyrit.prompt_converter.add_image_to_video_converter import AddImageVideoConverter
616
from pyrit.prompt_converter.add_text_image_converter import AddTextImageConverter

pyrit/prompt_converter/add_image_text_converter.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def __init__(
3838
y_pos: int = 10,
3939
):
4040
"""
41-
Initializes the converter with the image file path and text properties.
41+
Initialize the converter with the image file path and text properties.
4242
4343
Args:
4444
img_to_add (str): File path of image to add text to.
@@ -65,7 +65,7 @@ def __init__(
6565

6666
def _load_font(self) -> FreeTypeFont:
6767
"""
68-
Loads the font for a given font name and font size.
68+
Load the font for a given font name and font size.
6969
7070
Returns:
7171
ImageFont.FreeTypeFont or ImageFont.ImageFont: The loaded font object. If the specified font
@@ -84,13 +84,16 @@ def _load_font(self) -> FreeTypeFont:
8484

8585
def _add_text_to_image(self, text: str) -> Image.Image:
8686
"""
87-
Adds wrapped text to the image at `self._img_to_add`.
87+
Add wrapped text to the image at `self._img_to_add`.
8888
8989
Args:
9090
text (str): The text to add to the image.
9191
9292
Returns:
9393
Image.Image: The image with added text.
94+
95+
Raises:
96+
ValueError: If ``text`` is empty.
9497
"""
9598
if not text:
9699
raise ValueError("Please provide valid text value")
@@ -123,7 +126,7 @@ def _add_text_to_image(self, text: str) -> Image.Image:
123126

124127
async def convert_async(self, *, prompt: str, input_type: PromptDataType = "text") -> ConverterResult:
125128
"""
126-
Converts the given prompt by adding it as text to the image.
129+
Convert the given prompt by adding it as text to the image.
127130
128131
Args:
129132
prompt (str): The text to be added to the image.

pyrit/prompt_converter/add_image_to_video_converter.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def __init__(
4242
img_resize_size: tuple[int, int] = (500, 500),
4343
):
4444
"""
45-
Initializes the converter with the video path and image properties.
45+
Initialize the converter with the video path and image properties.
4646
4747
Args:
4848
video_path (str): File path of video to add image to.
@@ -63,14 +63,18 @@ def __init__(
6363

6464
async def _add_image_to_video(self, image_path: str, output_path: str) -> str:
6565
"""
66-
Adds an image to video.
66+
Add an image to video.
6767
6868
Args:
6969
image_path (str): The image path to add to video.
7070
output_path (str): The output video path.
7171
7272
Returns:
7373
str: The output video path.
74+
75+
Raises:
76+
ModuleNotFoundError: If OpenCV is not installed.
77+
ValueError: If the image path is invalid or unsupported video format.
7478
"""
7579
try:
7680
import cv2 # noqa: F401
@@ -167,7 +171,7 @@ async def _add_image_to_video(self, image_path: str, output_path: str) -> str:
167171

168172
async def convert_async(self, *, prompt: str, input_type: PromptDataType = "image_path") -> ConverterResult:
169173
"""
170-
Converts the given prompt (image) by adding it to a video.
174+
Convert the given prompt (image) by adding it to a video.
171175
172176
Args:
173177
prompt (str): The image path to be added to the video.

pyrit/prompt_converter/add_text_image_converter.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def __init__(
3838
y_pos: int = 10,
3939
):
4040
"""
41-
Initializes the converter with the text and text properties.
41+
Initialize the converter with the text and text properties.
4242
4343
Args:
4444
text_to_add (str): Text to add to an image. Defaults to empty string.
@@ -65,7 +65,7 @@ def __init__(
6565

6666
def _load_font(self) -> FreeTypeFont:
6767
"""
68-
Loads the font for a given font name and font size.
68+
Load the font for a given font name and font size.
6969
7070
Returns:
7171
ImageFont.FreeTypeFont or ImageFont.ImageFont: The loaded font object. If the specified font
@@ -84,7 +84,7 @@ def _load_font(self) -> FreeTypeFont:
8484

8585
def _add_text_to_image(self, image: Image.Image) -> Image.Image:
8686
"""
87-
Adds wrapped text to the image.
87+
Add wrapped text to the image.
8888
8989
Args:
9090
image (Image.Image): The image to which text will be added.
@@ -119,7 +119,7 @@ def _add_text_to_image(self, image: Image.Image) -> Image.Image:
119119

120120
async def convert_async(self, *, prompt: str, input_type: PromptDataType = "image_path") -> ConverterResult:
121121
"""
122-
Converts the given prompt (image) by adding text to it.
122+
Convert the given prompt (image) by adding text to it.
123123
124124
Args:
125125
prompt (str): The image file path to which text will be added.

pyrit/prompt_converter/ansi_escape/ansi_attack_converter.py

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def __init__(
4141
incorporate_user_prompt: bool = True,
4242
):
4343
"""
44-
Initializes the converter with various options to control the scenarios generated.
44+
Initialize the converter with various options to control the scenarios generated.
4545
4646
Args:
4747
include_raw (bool): Include scenarios with raw ANSI codes.
@@ -59,13 +59,43 @@ def __init__(
5959
self.incorporate_user_prompt = incorporate_user_prompt
6060

6161
def input_supported(self, input_type: PromptDataType) -> bool:
62+
"""
63+
Check if the input type is supported.
64+
65+
Args:
66+
input_type (PromptDataType): The type of input data.
67+
68+
Returns:
69+
bool: True if the input type is supported, False otherwise.
70+
"""
6271
return input_type == "text"
6372

6473
def output_supported(self, output_type: PromptDataType) -> bool:
74+
"""
75+
Check if the output type is supported.
76+
77+
Args:
78+
output_type (PromptDataType): The type of output data.
79+
80+
Returns:
81+
bool: True if the output type is supported, False otherwise.
82+
"""
6583
return output_type == "text"
6684

6785
async def convert_async(self, *, prompt: str, input_type: PromptDataType = "text") -> ConverterResult:
68-
"""Converts the given prompt into an ANSI attack scenario."""
86+
"""
87+
Convert the given prompt into an ANSI attack scenario.
88+
89+
Args:
90+
prompt (str): The original user prompt.
91+
input_type (PromptDataType): The type of input data.
92+
93+
Returns:
94+
ConverterResult: The result containing the generated ANSI scenario prompt.
95+
96+
Raises:
97+
ValueError: If the input type is not supported.
98+
"""
6999
if not self.input_supported(input_type):
70100
raise ValueError("Input type not supported")
71101

pyrit/prompt_converter/ascii_art_converter.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,27 @@ class AsciiArtConverter(PromptConverter):
1717

1818
def __init__(self, font: str = "rand") -> None:
1919
"""
20-
Initializes the converter with a specified font.
20+
Initialize the converter with a specified font.
2121
2222
Args:
2323
font (str): The font to use for ASCII art. Defaults to "rand" which selects a random font.
2424
"""
2525
self.font_value = font
2626

2727
async def convert_async(self, *, prompt: str, input_type: PromptDataType = "text") -> ConverterResult:
28-
"""Converts the given prompt into ASCII art."""
28+
"""
29+
Convert the given prompt into ASCII art.
30+
31+
Args:
32+
prompt (str): The prompt to be converted.
33+
input_type (PromptDataType): The type of input data.
34+
35+
Returns:
36+
ConverterResult: The result containing the ASCII art representation of the prompt.
37+
38+
Raises:
39+
ValueError: If the input type is not supported.
40+
"""
2941
if not self.input_supported(input_type):
3042
raise ValueError("Input type not supported")
3143

pyrit/prompt_converter/ask_to_decode_converter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class AskToDecodeConverter(PromptConverter):
4141

4242
def __init__(self, template: Optional[str] = None, encoding_name: str = "cipher") -> None:
4343
"""
44-
Initializes the converter with a specified encoding name and template.
44+
Initialize the converter with a specified encoding name and template.
4545
4646
By default, if no template is provided, a random template from basic_templates
4747
will be used. If an encoding_name is provided, both basic_templates and
@@ -60,14 +60,14 @@ def __init__(self, template: Optional[str] = None, encoding_name: str = "cipher"
6060

6161
async def convert_async(self, *, prompt: str, input_type: PromptDataType = "text") -> ConverterResult:
6262
"""
63-
Converts the given encoded text by wrapping it with a decoding request prompt.
63+
Convert the given encoded text by wrapping it with a decoding request prompt.
6464
6565
Args:
6666
prompt (str): The encoded text to be wrapped with a decoding request.
6767
input_type (PromptDataType, optional): Type of input data. Defaults to "text".
6868
6969
Returns:
70-
ConverterResult: The encoded text wrapped in a decoding prompt.
70+
ConverterResult: The result containing the converted prompt.
7171
7272
Raises:
7373
ValueError: If the input type is not supported (only "text" is supported).

pyrit/prompt_converter/atbash_converter.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class AtbashConverter(PromptConverter):
2525

2626
def __init__(self, *, append_description: bool = False) -> None:
2727
"""
28-
Initializes the converter with an option to append a description.
28+
Initialize the converter with an option to append a description.
2929
3030
Args:
3131
append_description (bool): If True, appends plaintext "expert" text to the prompt.
@@ -40,7 +40,19 @@ def __init__(self, *, append_description: bool = False) -> None:
4040
)
4141

4242
async def convert_async(self, *, prompt: str, input_type: PromptDataType = "text") -> ConverterResult:
43-
"""Converts the given prompt using the Atbash cipher."""
43+
"""
44+
Convert the given prompt using the Atbash cipher.
45+
46+
Args:
47+
prompt (str): The prompt to be converted.
48+
input_type (PromptDataType): The type of input data.
49+
50+
Returns:
51+
ConverterResult: The result containing the encoded prompt.
52+
53+
Raises:
54+
ValueError: If the input type is not supported.
55+
"""
4456
if not self.input_supported(input_type):
4557
raise ValueError("Input type not supported")
4658

pyrit/prompt_converter/audio_frequency_converter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def __init__(
3333
shift_value: int = 20000,
3434
) -> None:
3535
"""
36-
Initializes the converter with the specified output format and shift value.
36+
Initialize the converter with the specified output format and shift value.
3737
3838
Args:
3939
output_format (str): The format of the audio file, defaults to "wav".
@@ -44,7 +44,7 @@ def __init__(
4444

4545
async def convert_async(self, *, prompt: str, input_type: PromptDataType = "audio_path") -> ConverterResult:
4646
"""
47-
Converts the given audio file by shifting its frequency.
47+
Convert the given audio file by shifting its frequency.
4848
4949
Args:
5050
prompt (str): File path to the audio file to be converted.

0 commit comments

Comments
 (0)