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
36 changes: 36 additions & 0 deletions doc/code/converters/1_text_to_text_converters.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,42 @@
"result = await converter.convert_async(prompt=\"make a raccoon in a pirate ship\")\n",
"print(\"Image Filter Conversion:\", result.output_text) # type: ignore"
]
},
{
"cell_type": "markdown",
"id": "17",
"metadata": {},
"source": [
"### 1.5 Custom LLM converter with `LLMGenericTextConverter`\n",
"\n",
"Use `LLMGenericTextConverter` for a one-off LLM rewrite when no built-in converter fits. Give it a target and a `system_prompt_template` and it becomes a custom converter without writing a class. The incoming prompt is available to the template as `objective`, and extra keyword arguments are passed to the template as parameters. Like the other LLM-based converters here, this cell needs a configured target to run.\n",
"\n",
"If the LLM output needs parsing (for example, extracting JSON), subclass `LLMGenericTextConverter` and override `_process_response` instead.\n",
""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "18",
"metadata": {},
"outputs": [],
"source": [
"from pyrit.converter import LLMGenericTextConverter\n",
"\n",
"system_prompt = SeedPrompt(\n",
" value=\"Rewrite the text in {{ style }} style. Text: {{ objective }}\",\n",
" parameters=[\"objective\", \"style\"],\n",
" data_type=\"text\",\n",
")\n",
"custom_converter = LLMGenericTextConverter(\n",
" converter_target=attack_llm,\n",
" system_prompt_template=system_prompt,\n",
" style=\"pirate\",\n",
")\n",
"result = await custom_converter.convert_async(prompt=prompt)\n",
"print(\"Custom LLM rewrite:\", result.output_text) # type: ignore"
]
}
],
"metadata": {
Expand Down
23 changes: 23 additions & 0 deletions doc/code/converters/1_text_to_text_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,3 +512,26 @@
)
result = await converter.convert_async(prompt="make a raccoon in a pirate ship")
print("Image Filter Conversion:", result.output_text) # type: ignore

# %% [markdown]
# ### 1.5 Custom LLM converter with `LLMGenericTextConverter`
#
# Use `LLMGenericTextConverter` for a one-off LLM rewrite when no built-in converter fits. Give it a target and a `system_prompt_template` and it becomes a custom converter without writing a class. The incoming prompt is available to the template as `objective`, and extra keyword arguments are passed to the template as parameters. Like the other LLM-based converters here, this cell needs a configured target to run.
#
# If the LLM output needs parsing (for example, extracting JSON), subclass `LLMGenericTextConverter` and override `_process_response` instead.

# %%
from pyrit.converter import LLMGenericTextConverter

system_prompt = SeedPrompt(
value="Rewrite the text in {{ style }} style. Text: {{ objective }}",
parameters=["objective", "style"],
data_type="text",
)
custom_converter = LLMGenericTextConverter(
converter_target=attack_llm,
system_prompt_template=system_prompt,
style="pirate",
)
result = await custom_converter.convert_async(prompt=prompt)
print("Custom LLM rewrite:", result.output_text) # type: ignore