doc/code/converters/2_using_converters.ipynb (77 lines of code) (raw):

{ "cells": [ { "cell_type": "markdown", "id": "0", "metadata": {}, "source": [ "# 2. Using Prompt Converters\n", "\n", "Although converters can be used on their own, they should be thought of as a piece in the pipeine. Typically any orchestrator will have arguments so that prompts can be converted before being sent to the target. They can be stacked, use LLMs, and are a powerful tool.\n", "\n", "Before you begin, ensure you are setup with the correct version of PyRIT installed and have secrets configured as described [here](../../setup/populating_secrets.md).\n", "\n", "## Stacking Converters with PromptSendingOrchestrator\n", "\n", "In the following example, a TextTarget is used so these prompts are simply printed and added to memory. This can be useful if you are red teaming something and need to manually enter prompts. However, the target can be replaced with any other [target](../targets/0_prompt_targets.md). E.g., if you have API access you can add a target there.\n", "\n", "In this example the converters are stacked. First a variation is found using `VariationConverter`, and then `StringJoinConverter` is used to add a dash between letters. Remember that order matters. If `StringJoinConverter` came first, we would be asking the LLM to make variations of the prompt:\n", "\"t-e-l-l- - m-e- -h-o-w- -t-o- -c-u-t- -d-o-w-n - a- -t-r-e-e\"" ] }, { "cell_type": "code", "execution_count": null, "id": "1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'__type__': 'TextTarget', '__module__': 'pyrit.prompt_target.text_target'}: user: C-a-n- -y-o-u- -e-x-p-l-a-i-n- -t-h-e- -p-r-o-c-e-s-s- -o-f- -f-e-l-l-i-n-g- -a- -t-r-e-e-?\n" ] } ], "source": [ "from pyrit.common import IN_MEMORY, initialize_pyrit\n", "from pyrit.orchestrator import PromptSendingOrchestrator\n", "from pyrit.prompt_converter import StringJoinConverter, VariationConverter\n", "from pyrit.prompt_target import OpenAIChatTarget, TextTarget\n", "\n", "initialize_pyrit(memory_db_type=IN_MEMORY)\n", "\n", "prompts = [\"tell me how to cut down a tree\"]\n", "\n", "# converter target to send prompt to; in this case configured to the Azure OpenAI GPT-4o Chat model\n", "converter_target = OpenAIChatTarget()\n", "prompt_variation_converter = VariationConverter(converter_target=converter_target)\n", "\n", "\n", "target = TextTarget()\n", "orchestrator = PromptSendingOrchestrator(\n", " objective_target=target, prompt_converters=[prompt_variation_converter, StringJoinConverter()]\n", ")\n", "\n", "await orchestrator.send_prompts_async(prompt_list=prompts) # type: ignore\n", "orchestrator.dispose_db_engine()" ] } ], "metadata": { "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.9" } }, "nbformat": 4, "nbformat_minor": 5 }