Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 49 additions & 48 deletions doc/code/converters/0_converters.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -96,54 +96,55 @@
"52 text text IPAConverter\n",
"53 text text ImagePromptStyleConverter\n",
"54 text text InsertPunctuationConverter\n",
"55 text text JsonStringConverter\n",
"56 text text LLMGenericTextConverter\n",
"57 text text LeetspeakConverter\n",
"58 text text LetterBijectionConverter\n",
"59 text text LogicJailbreakConverter\n",
"60 text text MaliciousQuestionGeneratorConverter\n",
"61 text text MathObfuscationConverter\n",
"62 text text MathPromptConverter\n",
"63 text text MorseConverter\n",
"64 text text NatoConverter\n",
"65 text text NegationTrapConverter\n",
"66 text text NoiseConverter\n",
"67 text text PersuasionConverter\n",
"68 text text PinyinConverter\n",
"69 text text PolicyPuppetryConverter\n",
"70 text text PromptTemplateConverter\n",
"71 text text PuzzledConverter\n",
"72 text text ROT13Converter\n",
"73 text text RandomCapitalLettersConverter\n",
"74 text text RandomTranslationConverter\n",
"75 text text RepeatTokenConverter\n",
"76 text text RolePlayToolCallConverter\n",
"77 text text SATAMaskingConverter\n",
"78 text text ScientificTranslationConverter\n",
"79 text text SearchReplaceConverter\n",
"80 text text SelectiveTextConverter\n",
"81 text text SneakyBitsSmugglerConverter\n",
"82 text text StringJoinConverter\n",
"83 text text SuffixAppendConverter\n",
"84 text text SuperscriptConverter\n",
"85 text text TaskFramingConverter\n",
"86 text text TatweelConverter\n",
"87 text text TemplateSegmentConverter\n",
"88 text text TenseConverter\n",
"89 text text TextJailbreakConverter\n",
"90 text text TokenBijectionConverter\n",
"91 text text ToneConverter\n",
"92 text text ToxicSentenceGeneratorConverter\n",
"93 text text TranslationConverter\n",
"94 text text UnicodeConfusableConverter\n",
"95 text text UnicodeReplacementConverter\n",
"96 text text UnicodeSubstitutionConverter\n",
"97 text text UrlConverter\n",
"98 text text VariationConverter\n",
"99 text text VariationSelectorSmugglerConverter\n",
"100 text text VigenereConverter\n",
"101 text text ZalgoConverter\n",
"102 text text ZeroWidthConverter\n"
"55 text text JsonFieldConverter\n",
"56 text text JsonStringConverter\n",
"57 text text LLMGenericTextConverter\n",
"58 text text LeetspeakConverter\n",
"59 text text LetterBijectionConverter\n",
"60 text text LogicJailbreakConverter\n",
"61 text text MaliciousQuestionGeneratorConverter\n",
"62 text text MathObfuscationConverter\n",
"63 text text MathPromptConverter\n",
"64 text text MorseConverter\n",
"65 text text NatoConverter\n",
"66 text text NegationTrapConverter\n",
"67 text text NoiseConverter\n",
"68 text text PersuasionConverter\n",
"69 text text PinyinConverter\n",
"70 text text PolicyPuppetryConverter\n",
"71 text text PromptTemplateConverter\n",
"72 text text PuzzledConverter\n",
"73 text text ROT13Converter\n",
"74 text text RandomCapitalLettersConverter\n",
"75 text text RandomTranslationConverter\n",
"76 text text RepeatTokenConverter\n",
"77 text text RolePlayToolCallConverter\n",
"78 text text SATAMaskingConverter\n",
"79 text text ScientificTranslationConverter\n",
"80 text text SearchReplaceConverter\n",
"81 text text SelectiveTextConverter\n",
"82 text text SneakyBitsSmugglerConverter\n",
"83 text text StringJoinConverter\n",
"84 text text SuffixAppendConverter\n",
"85 text text SuperscriptConverter\n",
"86 text text TaskFramingConverter\n",
"87 text text TatweelConverter\n",
"88 text text TemplateSegmentConverter\n",
"89 text text TenseConverter\n",
"90 text text TextJailbreakConverter\n",
"91 text text TokenBijectionConverter\n",
"92 text text ToneConverter\n",
"93 text text ToxicSentenceGeneratorConverter\n",
"94 text text TranslationConverter\n",
"95 text text UnicodeConfusableConverter\n",
"96 text text UnicodeReplacementConverter\n",
"97 text text UnicodeSubstitutionConverter\n",
"98 text text UrlConverter\n",
"99 text text VariationConverter\n",
"100 text text VariationSelectorSmugglerConverter\n",
"101 text text VigenereConverter\n",
"102 text text ZalgoConverter\n",
"103 text text ZeroWidthConverter\n"
]
}
],
Expand Down
45 changes: 45 additions & 0 deletions doc/code/converters/6_selectively_converting.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,51 @@
"assert mixed.output_text == \"prefix [⟪jbeq⟫] suffix\""
]
},
{
"cell_type": "markdown",
"id": "9aaf21b2",
"metadata": {},
"source": [
"## 8. Convert One Field of a JSON Request\n",
"\n",
"Some targets take a JSON request, such as a decision model request with `state` and `questions`.\n",
"`JsonFieldConverter` applies a text converter to the string at one RFC 6901 JSON Pointer\n",
"(`/state`, `/messages/0/content`; `~1` and `~0` escape `/` and `~` in a key) and leaves the rest\n",
"of the request as it was, byte for byte. Markers are resolved inside that field, so they cannot\n",
"split the JSON, and markers in other fields stay in place for a later converter."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6d001bdc",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{\"state\": \"UmVmdW5kIG15IOKfqm9yZGVy4p+r\", \"questions\": {\"refund\": {\"type\": \"noul\", \"instructions\": \"Is this a refund?\"}}}\n",
"{\"state\": \"Refund my b3JkZXI=\", \"questions\": {\"refund\": {\"type\": \"noul\", \"instructions\": \"Is this a refund?\"}}}\n"
]
}
],
"source": [
"from pyrit.converter import JsonFieldConverter\n",
"\n",
"request = (\n",
" '{\"state\": \"Refund my ⟪order⟫\", \"questions\": {\"refund\": {\"type\": \"noul\", \"instructions\": \"Is this a refund?\"}}}'\n",
")\n",
"json_field = JsonFieldConverter(sub_converter=Base64Converter(), json_pointer=\"/state\")\n",
"\n",
"whole_field = await json_field.convert_async(prompt=request) # type: ignore\n",
"marked_only = await json_field.convert_tokens_async(prompt=request) # type: ignore\n",
"print(whole_field.output_text)\n",
"print(marked_only.output_text)\n",
"assert whole_field.output_text == request.replace('\"Refund my ⟪order⟫\"', '\"UmVmdW5kIG15IOKfqm9yZGVy4p+r\"')\n",
"assert marked_only.output_text == request.replace('\"Refund my ⟪order⟫\"', '\"Refund my b3JkZXI=\"')"
]
},
{
"cell_type": "markdown",
"id": "25",
Expand Down
24 changes: 24 additions & 0 deletions doc/code/converters/6_selectively_converting.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,30 @@
print(mixed.output_text)
assert mixed.output_text == "prefix [⟪jbeq⟫] suffix"

# %% [markdown]
# ## 8. Convert One Field of a JSON Request
#
# Some targets take a JSON request, such as a decision model request with `state` and `questions`.
# `JsonFieldConverter` applies a text converter to the string at one RFC 6901 JSON Pointer
# (`/state`, `/messages/0/content`; `~1` and `~0` escape `/` and `~` in a key) and leaves the rest
# of the request as it was, byte for byte. Markers are resolved inside that field, so they cannot
# split the JSON, and markers in other fields stay in place for a later converter.

# %%
from pyrit.converter import JsonFieldConverter

request = (
'{"state": "Refund my ⟪order⟫", "questions": {"refund": {"type": "noul", "instructions": "Is this a refund?"}}}'
)
json_field = JsonFieldConverter(sub_converter=Base64Converter(), json_pointer="/state")

whole_field = await json_field.convert_async(prompt=request) # type: ignore
marked_only = await json_field.convert_tokens_async(prompt=request) # type: ignore
print(whole_field.output_text)
print(marked_only.output_text)
assert whole_field.output_text == request.replace('"Refund my ⟪order⟫"', '"UmVmdW5kIG15IOKfqm9yZGVy4p+r"')
assert marked_only.output_text == request.replace('"Refund my ⟪order⟫"', '"Refund my b3JkZXI="')

# %% [markdown]
# ## Limits and Custom Converters
#
Expand Down
2 changes: 2 additions & 0 deletions pyrit/converter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
from pyrit.converter.image_rotation_converter import ImageRotationConverter
from pyrit.converter.insert_punctuation_converter import InsertPunctuationConverter
from pyrit.converter.ipa_converter import IPAConverter
from pyrit.converter.json_field_converter import JsonFieldConverter
from pyrit.converter.json_string_converter import JsonStringConverter
from pyrit.converter.leetspeak_converter import LeetspeakConverter
from pyrit.converter.llm_generic_text_converter import LLMGenericTextConverter
Expand Down Expand Up @@ -202,6 +203,7 @@
"IndexSelectionStrategy": "pyrit.converter.text_selection_strategy",
"InsertPunctuationConverter": "pyrit.converter.insert_punctuation_converter",
"IPAConverter": "pyrit.converter.ipa_converter",
"JsonFieldConverter": "pyrit.converter.json_field_converter",
"JsonStringConverter": "pyrit.converter.json_string_converter",
"KeywordSelectionStrategy": "pyrit.converter.text_selection_strategy",
"LeetspeakConverter": "pyrit.converter.leetspeak_converter",
Expand Down
Loading
Loading