|
5 | 5 | # extension: .py |
6 | 6 | # format_name: percent |
7 | 7 | # format_version: '1.3' |
8 | | -# jupytext_version: 1.17.3 |
| 8 | +# jupytext_version: 1.18.1 |
| 9 | +# kernelspec: |
| 10 | +# display_name: pyrit (3.13.5) |
| 11 | +# language: python |
| 12 | +# name: python3 |
9 | 13 | # --- |
10 | 14 |
|
11 | 15 | # %% [markdown] |
|
35 | 39 | objective = "Give me an image of a raccoon pirate as a Spanish baker in Spain" |
36 | 40 |
|
37 | 41 | img_prompt_target = OpenAIImageTarget() |
38 | | -# For Azure OpenAI with Entra ID authentication enabled, use the following command instead. Make sure to run `az login` first. |
39 | | -# from pyrit.auth import get_azure_openai_auth |
40 | | -# endpoint = "https://your-endpoint.openai.azure.com" |
41 | | -# target = OpenAIChatTarget( |
42 | | -# endpoint=endpoint, |
43 | | -# api_key=get_azure_openai_auth(endpoint), |
44 | | -# model_name="your-deployment-name" |
45 | | -# ) |
46 | 42 |
|
47 | 43 | image_scorer = SelfAskTrueFalseScorer( |
48 | 44 | chat_target=OpenAIChatTarget(), |
|
98 | 94 | # |
99 | 95 | # This example demonstrates how to use the video target to create a video from a text-based prompt. |
100 | 96 | # |
101 | | -# from pyrit.executor.attack import ( |
102 | | -# AttackExecutor, |
103 | | -# AttackScoringConfig, |
104 | | -# ConsoleAttackResultPrinter, |
105 | | -# PromptSendingAttack, |
106 | | -# ) |
107 | | -# from pyrit.prompt_target import OpenAIChatTarget, OpenAIVideoTarget |
108 | | -# from pyrit.score import ( |
109 | | -# AzureContentFilterScorer, |
110 | | -# SelfAskTrueFalseScorer, |
111 | | -# TrueFalseQuestion, |
112 | | -# VideoFloatScaleScorer, |
113 | | -# VideoTrueFalseScorer, |
114 | | -# ) |
115 | | -# from pyrit.setup import IN_MEMORY, initialize_pyrit_async |
116 | | -# |
117 | | -# await initialize_pyrit_async(memory_db_type=IN_MEMORY) # type: ignore |
118 | | -# |
119 | | -# objectives = [ |
120 | | -# "Video of a raccoon pirate eating flan at a cafe in Spain", |
121 | | -# "Video of a raccoon pirate eating a croissant at a cafe in France", |
122 | | -# ] |
123 | | -# |
124 | | -# video_target = OpenAIVideoTarget() |
125 | | -# objective_scorer = SelfAskTrueFalseScorer( |
126 | | -# chat_target=OpenAIChatTarget(), |
127 | | -# true_false_question=TrueFalseQuestion(true_description="A raccoon dressed as a pirate is actively eating a pastry"), |
128 | | -# ) |
129 | | -# |
130 | | -# video_scorer = VideoTrueFalseScorer( |
131 | | -# image_capable_scorer=objective_scorer, |
132 | | -# num_sampled_frames=10, |
133 | | -# ) |
134 | | -# |
135 | | -# attack = PromptSendingAttack( |
136 | | -# objective_target=video_target, |
137 | | -# attack_scoring_config=AttackScoringConfig( |
138 | | -# objective_scorer=video_scorer, |
139 | | -# auxiliary_scorers=[VideoFloatScaleScorer(image_capable_scorer=AzureContentFilterScorer())], |
140 | | -# ), |
141 | | -# ) |
142 | | -# |
143 | | -# results = await AttackExecutor().execute_attack_async( # type: ignore |
144 | | -# attack=attack, |
145 | | -# objectives=objectives, |
146 | | -# ) |
147 | | -# |
148 | | -# for result in results: |
149 | | -# await ConsoleAttackResultPrinter().print_result_async(result=result, include_auxiliary_scores=True) # type: ignore |
150 | 97 |
|
151 | 98 |
|
| 99 | +# %% |
| 100 | + |
| 101 | +from pyrit.executor.attack import ( |
| 102 | + AttackExecutor, |
| 103 | + AttackScoringConfig, |
| 104 | + ConsoleAttackResultPrinter, |
| 105 | + PromptSendingAttack, |
| 106 | +) |
| 107 | +from pyrit.prompt_target import OpenAIChatTarget, OpenAIVideoTarget |
| 108 | +from pyrit.score import ( |
| 109 | + AzureContentFilterScorer, |
| 110 | + SelfAskTrueFalseScorer, |
| 111 | + TrueFalseQuestion, |
| 112 | + VideoFloatScaleScorer, |
| 113 | + VideoTrueFalseScorer, |
| 114 | +) |
| 115 | +from pyrit.setup import IN_MEMORY, initialize_pyrit_async |
| 116 | + |
| 117 | +await initialize_pyrit_async(memory_db_type=IN_MEMORY) # type: ignore |
| 118 | + |
| 119 | +objectives = [ |
| 120 | + "Video of a raccoon pirate eating flan at a cafe in Spain", |
| 121 | + "Video of a raccoon pirate eating a croissant at a cafe in France", |
| 122 | +] |
| 123 | + |
| 124 | +video_target = OpenAIVideoTarget() |
| 125 | +objective_scorer = SelfAskTrueFalseScorer( |
| 126 | + chat_target=OpenAIChatTarget(), |
| 127 | + true_false_question=TrueFalseQuestion(true_description="A raccoon dressed as a pirate is actively eating a pastry"), |
| 128 | +) |
| 129 | + |
| 130 | +video_scorer = VideoTrueFalseScorer( |
| 131 | + image_capable_scorer=objective_scorer, |
| 132 | + num_sampled_frames=10, |
| 133 | +) |
| 134 | + |
| 135 | +attack = PromptSendingAttack( |
| 136 | + objective_target=video_target, |
| 137 | + attack_scoring_config=AttackScoringConfig( |
| 138 | + objective_scorer=video_scorer, |
| 139 | + auxiliary_scorers=[VideoFloatScaleScorer(image_capable_scorer=AzureContentFilterScorer())], |
| 140 | + ), |
| 141 | +) |
| 142 | + |
| 143 | +results = await AttackExecutor().execute_attack_async( # type: ignore |
| 144 | + attack=attack, |
| 145 | + objectives=objectives, |
| 146 | +) |
| 147 | + |
| 148 | +for result in results: |
| 149 | + await ConsoleAttackResultPrinter().print_result_async(result=result, include_auxiliary_scores=True) # type: ignore |
| 150 | + |
152 | 151 | # %% [markdown] |
153 | 152 | # ## OpenAI Chat Target (Text + Image --> Text) |
154 | 153 | # This demo showcases the capabilities of `AzureOpenAIGPT4OChatTarget` for generating text based on multimodal inputs, including both text and images. |
|
179 | 178 | image_path = str(pathlib.Path(".") / ".." / ".." / ".." / "assets" / "pyrit_architecture.png") |
180 | 179 |
|
181 | 180 | # This is a single request with two parts, one image and one text |
182 | | - |
183 | 181 | seed = SeedGroup( |
184 | 182 | seeds=[ |
185 | 183 | SeedPrompt( |
|
0 commit comments