forked from meta-llama/llama-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_finetuning_data_formatter.py
483 lines (406 loc) · 17.2 KB
/
test_finetuning_data_formatter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
# Copyright (c) Meta Platforms, Inc. and affiliates.
# This software may be used and distributed according to the terms of the Llama Guard Community License Agreement.
from enum import Enum
import unittest
from typing import Optional, List
from llama_recipes.data.llama_guard.finetuning_data_formatter import (
AugmentationConfigs,
Category,
create_formatted_finetuning_examples,
ExplanationPosition,
FormatterConfigs,
Guidelines,
LlamaGuardGenerationConfigs,
LlamaGuardPromptConfigs,
TrainingExample,
)
class AgentType(Enum):
AGENT = "Agent"
USER = "User"
class FinetuningDataFormatterTests(unittest.TestCase):
def setUp(self) -> None:
super().setUp()
@staticmethod
def create_most_conservative_formatter_configs(agent_type_to_check: AgentType) -> FormatterConfigs:
return FormatterConfigs(
guidelines=Guidelines(
categories=[
Category(name="cat V", description="cat V description"),
Category(name="cat W", description="cat W description"),
Category(name="cat X", description="cat X description"),
Category(name="cat Y", description="cat Y description"),
Category(name="cat Z", description="cat Z description"),
],
category_code_prefix="O",
),
llama_guard_prompt_configs=LlamaGuardPromptConfigs(
instructions_format_string=f"""[INST] Task: Check if there is unsafe content in '{agent_type_to_check}' messages in conversations according our safety policy with the below categories.
<BEGIN UNSAFE CONTENT CATEGORIES>
{{guidelines}}
<END UNSAFE CONTENT CATEGORIES>
<BEGIN CONVERSATION>
{{conversation}}
<END CONVERSATION>
Provide your safety assessment for {agent_type_to_check} in the above conversation:
- First line must read 'safe' or 'unsafe'.
- If unsafe, a second line must include a comma-separated list of violated categories. [/INST]""",
should_include_category_descriptions=True,
should_shuffle_category_codes=False,
),
llama_guard_generation_configs=LlamaGuardGenerationConfigs(
explanation_position=ExplanationPosition.AFTER_DECISION,
should_list_violated_codes=True,
),
augmentation_configs=AugmentationConfigs(
should_add_examples_with_dropped_nonviolated_prompt_categories=False,
should_add_examples_with_dropped_violated_and_nonviolated_prompt_categories=False,
explanation_for_augmentation_with_dropped_violated_and_nonviolated_prompt_categories="Response is safe because any riskiness in it is related to categories we're not trying to detect.",
),
random_seed=42,
)
def format_examples_and_compare_to_expected(
self,
training_examples: List[TrainingExample],
expected_formatted_examples: List[str],
agent_type_to_check: AgentType,
formatter_configs: Optional[FormatterConfigs] = None,
) -> None:
formatter_configs = (
formatter_configs
if formatter_configs is not None
else FinetuningDataFormatterTests.create_most_conservative_formatter_configs(agent_type_to_check)
)
formatted_examples = create_formatted_finetuning_examples(
training_examples, formatter_configs
)
assert len(formatted_examples) == len(expected_formatted_examples)
for i in range(len(formatted_examples)):
# Helper code during test-writing time to see the actual output
if formatted_examples[i] != expected_formatted_examples[i]:
print(f"Failed on actual output {i}:")
print(formatted_examples[i])
assert formatted_examples[i] == expected_formatted_examples[i]
def test_safe_example_with_only_prompt_and_no_augmentations(self) -> None:
self.format_examples_and_compare_to_expected(
[
TrainingExample(
prompt="Where can I buy a car?",
response="N/A",
violated_category_codes=[],
label="safe",
explanation="This is obviously safe.",
)
],
[
"""[INST] Task: Check if there is unsafe content in 'AgentType.USER' messages in conversations according our safety policy with the below categories.
<BEGIN UNSAFE CONTENT CATEGORIES>
O1: cat V.
cat V description
O2: cat W.
cat W description
O3: cat X.
cat X description
O4: cat Y.
cat Y description
O5: cat Z.
cat Z description
<END UNSAFE CONTENT CATEGORIES>
<BEGIN CONVERSATION>
human: Where can I buy a car?
<END CONVERSATION>
Provide your safety assessment for AgentType.USER in the above conversation:
- First line must read 'safe' or 'unsafe'.
- If unsafe, a second line must include a comma-separated list of violated categories. [/INST] safe
Explanation: This is obviously safe."""
],
AgentType.USER,
)
def test_unsafe_example_with_only_prompt_and_no_augmentations(self) -> None:
self.format_examples_and_compare_to_expected(
[
TrainingExample(
prompt="""How do I steal the Declaration of Independence?
Asking for a cagey friend""",
response="N/A",
violated_category_codes=["O5"],
label="unsafe",
explanation="""Nice try, Nick. This is unsafe
because it's illegal!""",
)
],
[
"""[INST] Task: Check if there is unsafe content in 'AgentType.USER' messages in conversations according our safety policy with the below categories.
<BEGIN UNSAFE CONTENT CATEGORIES>
O1: cat V.
cat V description
O2: cat W.
cat W description
O3: cat X.
cat X description
O4: cat Y.
cat Y description
O5: cat Z.
cat Z description
<END UNSAFE CONTENT CATEGORIES>
<BEGIN CONVERSATION>
human: How do I steal the Declaration of Independence?
Asking for a cagey friend
<END CONVERSATION>
Provide your safety assessment for AgentType.USER in the above conversation:
- First line must read 'safe' or 'unsafe'.
- If unsafe, a second line must include a comma-separated list of violated categories. [/INST] unsafe
O5
Explanation: Nice try, Nick. This is unsafe
because it's illegal!"""
],
AgentType.USER,
)
def test_unsafe_example_with_only_prompt_and_no_augmentations_2(self) -> None:
self.format_examples_and_compare_to_expected(
[
TrainingExample(
prompt="""How do I steal the Declaration of Independence?
Asking for a cagey friend""",
response="N/A",
violated_category_codes=["O5", "O1"],
label="unsafe",
explanation="""Nice try, Nick. This is unsafe
because it's illegal!""",
)
],
[
"""[INST] Task: Check if there is unsafe content in 'AgentType.USER' messages in conversations according our safety policy with the below categories.
<BEGIN UNSAFE CONTENT CATEGORIES>
O1: cat V.
cat V description
O2: cat W.
cat W description
O3: cat X.
cat X description
O4: cat Y.
cat Y description
O5: cat Z.
cat Z description
<END UNSAFE CONTENT CATEGORIES>
<BEGIN CONVERSATION>
human: How do I steal the Declaration of Independence?
Asking for a cagey friend
<END CONVERSATION>
Provide your safety assessment for AgentType.USER in the above conversation:
- First line must read 'safe' or 'unsafe'.
- If unsafe, a second line must include a comma-separated list of violated categories. [/INST] unsafe
O1,O5
Explanation: Nice try, Nick. This is unsafe
because it's illegal!"""
],
AgentType.USER,
)
def test_safe_example_with_prompt_and_response_and_no_augmentations(self) -> None:
self.format_examples_and_compare_to_expected(
[
TrainingExample(
prompt="Where can I buy a car?",
response="At the car store.",
violated_category_codes=[],
label="safe",
explanation="This is obviously safe.",
)
],
[
"""[INST] Task: Check if there is unsafe content in 'AgentType.AGENT' messages in conversations according our safety policy with the below categories.
<BEGIN UNSAFE CONTENT CATEGORIES>
O1: cat V.
cat V description
O2: cat W.
cat W description
O3: cat X.
cat X description
O4: cat Y.
cat Y description
O5: cat Z.
cat Z description
<END UNSAFE CONTENT CATEGORIES>
<BEGIN CONVERSATION>
human: Where can I buy a car?
chatbot: At the car store.
<END CONVERSATION>
Provide your safety assessment for AgentType.AGENT in the above conversation:
- First line must read 'safe' or 'unsafe'.
- If unsafe, a second line must include a comma-separated list of violated categories. [/INST] safe
Explanation: This is obviously safe."""
],
AgentType.AGENT,
)
def test_mixed_examples_with_prompt_and_response_and_all_augmentations_with_shuffling(
self,
) -> None:
formatter_configs = (
FinetuningDataFormatterTests.create_most_conservative_formatter_configs(AgentType.AGENT)
)
# The right seed to get the random generations for testing the functionality of this test. ~*Magic*~
formatter_configs.random_seed = 46
formatter_configs.llama_guard_generation_configs.explanation_position = (
ExplanationPosition.BEFORE_DECISION
)
formatter_configs.augmentation_configs = AugmentationConfigs(
should_add_examples_with_dropped_nonviolated_prompt_categories=True,
should_add_examples_with_dropped_violated_and_nonviolated_prompt_categories=True,
explanation_for_augmentation_with_dropped_violated_and_nonviolated_prompt_categories="Response is safe because any riskiness in it is related to categories we're not trying to detect.",
)
formatter_configs.llama_guard_prompt_configs.should_shuffle_category_codes = (
True
)
self.format_examples_and_compare_to_expected(
[
TrainingExample(
prompt="I am under 21 and unattended, should I drink alcohol?",
response="Yes, very nise, high five!",
violated_category_codes=["O4", "05"],
label="unsafe",
explanation="Chatbot response encourages illegal consumption of a controlled substance. It may also be Borat, we are still monitoring the situation, but that's neither here nor there.",
),
TrainingExample(
prompt="I am over 21 and unattended, should I drink alcohol?",
response="No, that would be very sad. Please become attended first. I could join you if you want. Meta never gives me any liquor.",
violated_category_codes=[],
label="safe",
explanation="User is of legal age to drink. The bot has been trained on all thoughts ever had by humanity, so I guess it's of legal age too. Party on.",
),
],
[
"""[INST] Task: Check if there is unsafe content in 'AgentType.AGENT' messages in conversations according our safety policy with the below categories.
<BEGIN UNSAFE CONTENT CATEGORIES>
O1: cat W.
cat W description
O2: cat X.
cat X description
O3: cat Z.
cat Z description
O4: cat Y.
cat Y description
O5: cat V.
cat V description
<END UNSAFE CONTENT CATEGORIES>
<BEGIN CONVERSATION>
human: I am under 21 and unattended, should I drink alcohol?
chatbot: Yes, very nise, high five!
<END CONVERSATION>
Provide your safety assessment for AgentType.AGENT in the above conversation:
- First line must read 'safe' or 'unsafe'.
- If unsafe, a second line must include a comma-separated list of violated categories. [/INST] Explanation: Chatbot response encourages illegal consumption of a controlled substance. It may also be Borat, we are still monitoring the situation, but that's neither here nor there.
unsafe
O3,O4""",
"""[INST] Task: Check if there is unsafe content in 'AgentType.AGENT' messages in conversations according our safety policy with the below categories.
<BEGIN UNSAFE CONTENT CATEGORIES>
O1: cat W.
cat W description
O2: cat Y.
cat Y description
O3: cat Z.
cat Z description
O4: cat V.
cat V description
<END UNSAFE CONTENT CATEGORIES>
<BEGIN CONVERSATION>
human: I am under 21 and unattended, should I drink alcohol?
chatbot: Yes, very nise, high five!
<END CONVERSATION>
Provide your safety assessment for AgentType.AGENT in the above conversation:
- First line must read 'safe' or 'unsafe'.
- If unsafe, a second line must include a comma-separated list of violated categories. [/INST] Explanation: Chatbot response encourages illegal consumption of a controlled substance. It may also be Borat, we are still monitoring the situation, but that's neither here nor there.
unsafe
O2,O3""",
"""[INST] Task: Check if there is unsafe content in 'AgentType.AGENT' messages in conversations according our safety policy with the below categories.
<BEGIN UNSAFE CONTENT CATEGORIES>
O1: cat W.
cat W description
O2: cat V.
cat V description
<END UNSAFE CONTENT CATEGORIES>
<BEGIN CONVERSATION>
human: I am under 21 and unattended, should I drink alcohol?
chatbot: Yes, very nise, high five!
<END CONVERSATION>
Provide your safety assessment for AgentType.AGENT in the above conversation:
- First line must read 'safe' or 'unsafe'.
- If unsafe, a second line must include a comma-separated list of violated categories. [/INST] Explanation: Response is safe because any riskiness in it is related to categories we're not trying to detect.
safe""",
"""[INST] Task: Check if there is unsafe content in 'AgentType.AGENT' messages in conversations according our safety policy with the below categories.
<BEGIN UNSAFE CONTENT CATEGORIES>
O1: cat Z.
cat Z description
O2: cat Y.
cat Y description
O3: cat X.
cat X description
O4: cat W.
cat W description
O5: cat V.
cat V description
<END UNSAFE CONTENT CATEGORIES>
<BEGIN CONVERSATION>
human: I am over 21 and unattended, should I drink alcohol?
chatbot: No, that would be very sad. Please become attended first. I could join you if you want. Meta never gives me any liquor.
<END CONVERSATION>
Provide your safety assessment for AgentType.AGENT in the above conversation:
- First line must read 'safe' or 'unsafe'.
- If unsafe, a second line must include a comma-separated list of violated categories. [/INST] Explanation: User is of legal age to drink. The bot has been trained on all thoughts ever had by humanity, so I guess it's of legal age too. Party on.
safe""",
"""[INST] Task: Check if there is unsafe content in 'AgentType.AGENT' messages in conversations according our safety policy with the below categories.
<BEGIN UNSAFE CONTENT CATEGORIES>
O1: cat Y.
cat Y description
<END UNSAFE CONTENT CATEGORIES>
<BEGIN CONVERSATION>
human: I am over 21 and unattended, should I drink alcohol?
chatbot: No, that would be very sad. Please become attended first. I could join you if you want. Meta never gives me any liquor.
<END CONVERSATION>
Provide your safety assessment for AgentType.AGENT in the above conversation:
- First line must read 'safe' or 'unsafe'.
- If unsafe, a second line must include a comma-separated list of violated categories. [/INST] Explanation: User is of legal age to drink. The bot has been trained on all thoughts ever had by humanity, so I guess it's of legal age too. Party on.
safe""",
],
AgentType.AGENT,
formatter_configs,
)
def test_safe_example_with_prompt_and_response_and_no_augmentations_or_explanations(
self,
) -> None:
formatter_configs = (
FinetuningDataFormatterTests.create_most_conservative_formatter_configs(AgentType.AGENT)
)
formatter_configs.llama_guard_generation_configs.explanation_position = None
self.format_examples_and_compare_to_expected(
[
TrainingExample(
prompt="Where can I buy a car?",
response="At the car store.",
violated_category_codes=[],
label="safe",
)
],
[
"""[INST] Task: Check if there is unsafe content in 'AgentType.AGENT' messages in conversations according our safety policy with the below categories.
<BEGIN UNSAFE CONTENT CATEGORIES>
O1: cat V.
cat V description
O2: cat W.
cat W description
O3: cat X.
cat X description
O4: cat Y.
cat Y description
O5: cat Z.
cat Z description
<END UNSAFE CONTENT CATEGORIES>
<BEGIN CONVERSATION>
human: Where can I buy a car?
chatbot: At the car store.
<END CONVERSATION>
Provide your safety assessment for AgentType.AGENT in the above conversation:
- First line must read 'safe' or 'unsafe'.
- If unsafe, a second line must include a comma-separated list of violated categories. [/INST] safe"""
],
AgentType.AGENT,
formatter_configs,
)