Skip to content

Commit

Permalink
FEAT New URL Converter (#399)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbolor21 authored Oct 1, 2024
1 parent 3f0189b commit 1f4912a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pyrit/prompt_converter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
from pyrit.prompt_converter.translation_converter import TranslationConverter
from pyrit.prompt_converter.unicode_confusable_converter import UnicodeConfusableConverter
from pyrit.prompt_converter.unicode_sub_converter import UnicodeSubstitutionConverter
from pyrit.prompt_converter.url_converter import UrlConverter
from pyrit.prompt_converter.variation_converter import VariationConverter


Expand Down Expand Up @@ -79,5 +80,6 @@
"TranslationConverter",
"UnicodeConfusableConverter",
"UnicodeSubstitutionConverter",
"UrlConverter",
"VariationConverter",
]
22 changes: 22 additions & 0 deletions pyrit/prompt_converter/url_converter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.

import urllib.parse

from pyrit.models import PromptDataType
from pyrit.prompt_converter import PromptConverter, ConverterResult


class UrlConverter(PromptConverter):

async def convert_async(self, *, prompt: str, input_type: PromptDataType = "text") -> ConverterResult:
"""
Simple converter that just URL encodes the prompt
"""
if not self.input_supported(input_type):
raise ValueError("Input type not supported")

return ConverterResult(output_text=urllib.parse.quote(prompt), output_type="text")

def input_supported(self, input_type: PromptDataType) -> bool:
return input_type == "text"
9 changes: 9 additions & 0 deletions tests/converter/test_prompt_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
SuffixAppendConverter,
UnicodeSubstitutionConverter,
UnicodeConfusableConverter,
UrlConverter,
)


Expand Down Expand Up @@ -348,3 +349,11 @@ async def test_character_space_converter_punctuation() -> None:
output = await converter.convert_async(prompt="Hello, world! How's everything?", input_type="text")
assert output.output_type == "text"
assert output.output_text == "H e l l o w o r l d H o w s e v e r y t h i n g "


@pytest.mark.asyncio
async def test_url_converter() -> None:
converter = UrlConverter()
output = await converter.convert_async(prompt="Test Prompt")
assert output.output_type == "text"
assert output.output_text == "Test%20Prompt"

0 comments on commit 1f4912a

Please sign in to comment.