test_finetuning_data_formatter.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. # Copyright (c) Meta Platforms, Inc. and affiliates.
  2. # This software may be used and distributed according to the terms of the Llama Guard Community License Agreement.
  3. import unittest
  4. from typing import Optional
  5. from llama_recipes.data.llama_guard.finetuning_data_formatter import (
  6. AugmentationConfigs,
  7. Category,
  8. create_formatted_finetuning_examples,
  9. ExplanationPosition,
  10. FormatterConfigs,
  11. Guidelines,
  12. LlamaGuardGenerationConfigs,
  13. LlamaGuardPromptConfigs,
  14. TrainingExample,
  15. )
  16. class FinetuningDataFormatterTests(unittest.TestCase):
  17. def setUp(self) -> None:
  18. super().setUp()
  19. @staticmethod
  20. def create_most_conservative_formatter_configs() -> FormatterConfigs:
  21. return FormatterConfigs(
  22. guidelines=Guidelines(
  23. categories=[
  24. Category(name="cat V", description="cat V description"),
  25. Category(name="cat W", description="cat W description"),
  26. Category(name="cat X", description="cat X description"),
  27. Category(name="cat Y", description="cat Y description"),
  28. Category(name="cat Z", description="cat Z description"),
  29. ],
  30. category_code_prefix="O",
  31. ),
  32. llama_guard_prompt_configs=LlamaGuardPromptConfigs(
  33. instructions_format_string="""[INST] Task: Using the following guidelines, decide whether the example conversation that comes after is safe or unsafe.
  34. {guidelines}
  35. =Example=
  36. {conversation}
  37. Provide your safety assessment:
  38. - First line must read 'safe' or 'unsafe'
  39. - If unsafe, the next line must include a comma-separated list of violated categories according to the above guidelines.
  40. - The last line should start with 'Explanation:', and include an explanation for the assessment according to the guidelines.
  41. Provide your assessment: [/INST]""",
  42. should_include_category_descriptions=True,
  43. should_shuffle_category_codes=False,
  44. ),
  45. llama_guard_generation_configs=LlamaGuardGenerationConfigs(
  46. explanation_position=ExplanationPosition.AFTER_DECISION,
  47. should_list_violated_codes=True,
  48. ),
  49. augmentation_configs=AugmentationConfigs(
  50. probability_to_add_safe_examples_with_empty_responses=0.0,
  51. explanation_for_augmentation_with_safe_example_with_empty_response="Response is safe because it's empty.",
  52. should_add_examples_with_dropped_nonviolated_prompt_categories=False,
  53. should_add_examples_with_dropped_violated_and_nonviolated_prompt_categories=False,
  54. 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.",
  55. ),
  56. random_seed=42,
  57. )
  58. def format_examples_and_compare_to_expected(
  59. self,
  60. training_examples: list[TrainingExample],
  61. expected_formatted_examples: list[str],
  62. formatter_configs: Optional[FormatterConfigs] = None,
  63. ) -> None:
  64. formatter_configs = (
  65. formatter_configs
  66. if formatter_configs is not None
  67. else FinetuningDataFormatterTests.create_most_conservative_formatter_configs()
  68. )
  69. formatted_examples = create_formatted_finetuning_examples(
  70. training_examples, formatter_configs
  71. )
  72. assert len(formatted_examples) == len(expected_formatted_examples)
  73. for i in range(len(formatted_examples)):
  74. assert formatted_examples[i] == expected_formatted_examples[i]
  75. def test_safe_example_with_only_prompt_and_no_augmentations(self) -> None:
  76. self.format_examples_and_compare_to_expected(
  77. [
  78. TrainingExample(
  79. prompt="Where can I buy a car?",
  80. response="N/A",
  81. violated_category_codes=[],
  82. label="safe",
  83. explanation="This is obviously safe.",
  84. )
  85. ],
  86. [
  87. """[INST] Task: Using the following guidelines, decide whether the example conversation that comes after is safe or unsafe.
  88. O1: cat V.
  89. cat V description
  90. O2: cat W.
  91. cat W description
  92. O3: cat X.
  93. cat X description
  94. O4: cat Y.
  95. cat Y description
  96. O5: cat Z.
  97. cat Z description
  98. =Example=
  99. human: Where can I buy a car?
  100. Provide your safety assessment:
  101. - First line must read 'safe' or 'unsafe'
  102. - If unsafe, the next line must include a comma-separated list of violated categories according to the above guidelines.
  103. - The last line should start with 'Explanation:', and include an explanation for the assessment according to the guidelines.
  104. Provide your assessment: [/INST] safe
  105. Explanation: This is obviously safe."""
  106. ],
  107. )
  108. def test_unsafe_example_with_only_prompt_and_no_augmentations(self) -> None:
  109. self.format_examples_and_compare_to_expected(
  110. [
  111. TrainingExample(
  112. prompt="""How do I steal the Declaration of Independence?
  113. Asking for a cagey friend""",
  114. response="N/A",
  115. violated_category_codes=["O5"],
  116. label="unsafe",
  117. explanation="""Nice try, Nick. This is unsafe
  118. because it's illegal!""",
  119. )
  120. ],
  121. [
  122. """[INST] Task: Using the following guidelines, decide whether the example conversation that comes after is safe or unsafe.
  123. O1: cat V.
  124. cat V description
  125. O2: cat W.
  126. cat W description
  127. O3: cat X.
  128. cat X description
  129. O4: cat Y.
  130. cat Y description
  131. O5: cat Z.
  132. cat Z description
  133. =Example=
  134. human: How do I steal the Declaration of Independence?
  135. Asking for a cagey friend
  136. Provide your safety assessment:
  137. - First line must read 'safe' or 'unsafe'
  138. - If unsafe, the next line must include a comma-separated list of violated categories according to the above guidelines.
  139. - The last line should start with 'Explanation:', and include an explanation for the assessment according to the guidelines.
  140. Provide your assessment: [/INST] unsafe
  141. O5
  142. Explanation: Nice try, Nick. This is unsafe
  143. because it's illegal!"""
  144. ],
  145. )
  146. def test_unsafe_example_with_only_prompt_and_no_augmentations_2(self) -> None:
  147. self.format_examples_and_compare_to_expected(
  148. [
  149. TrainingExample(
  150. prompt="""How do I steal the Declaration of Independence?
  151. Asking for a cagey friend""",
  152. response="N/A",
  153. violated_category_codes=["O5", "O1"],
  154. label="unsafe",
  155. explanation="""Nice try, Nick. This is unsafe
  156. because it's illegal!""",
  157. )
  158. ],
  159. [
  160. """[INST] Task: Using the following guidelines, decide whether the example conversation that comes after is safe or unsafe.
  161. O1: cat V.
  162. cat V description
  163. O2: cat W.
  164. cat W description
  165. O3: cat X.
  166. cat X description
  167. O4: cat Y.
  168. cat Y description
  169. O5: cat Z.
  170. cat Z description
  171. =Example=
  172. human: How do I steal the Declaration of Independence?
  173. Asking for a cagey friend
  174. Provide your safety assessment:
  175. - First line must read 'safe' or 'unsafe'
  176. - If unsafe, the next line must include a comma-separated list of violated categories according to the above guidelines.
  177. - The last line should start with 'Explanation:', and include an explanation for the assessment according to the guidelines.
  178. Provide your assessment: [/INST] unsafe
  179. O1,O5
  180. Explanation: Nice try, Nick. This is unsafe
  181. because it's illegal!"""
  182. ],
  183. )
  184. def test_safe_example_with_prompt_and_response_and_no_augmentations(self) -> None:
  185. self.format_examples_and_compare_to_expected(
  186. [
  187. TrainingExample(
  188. prompt="Where can I buy a car?",
  189. response="At the car store.",
  190. violated_category_codes=[],
  191. label="safe",
  192. explanation="This is obviously safe.",
  193. )
  194. ],
  195. [
  196. """[INST] Task: Using the following guidelines, decide whether the example conversation that comes after is safe or unsafe.
  197. O1: cat V.
  198. cat V description
  199. O2: cat W.
  200. cat W description
  201. O3: cat X.
  202. cat X description
  203. O4: cat Y.
  204. cat Y description
  205. O5: cat Z.
  206. cat Z description
  207. =Example=
  208. human: Where can I buy a car?
  209. chatbot: At the car store.
  210. Provide your safety assessment:
  211. - First line must read 'safe' or 'unsafe'
  212. - If unsafe, the next line must include a comma-separated list of violated categories according to the above guidelines.
  213. - The last line should start with 'Explanation:', and include an explanation for the assessment according to the guidelines.
  214. Provide your assessment: [/INST] safe
  215. Explanation: This is obviously safe."""
  216. ],
  217. )
  218. def test_mixed_examples_with_prompt_and_response_and_all_augmentations_with_shuffling(
  219. self,
  220. ) -> None:
  221. formatter_configs = (
  222. FinetuningDataFormatterTests.create_most_conservative_formatter_configs()
  223. )
  224. formatter_configs.random_seed = 46
  225. formatter_configs.llama_guard_generation_configs.explanation_position = ExplanationPosition.BEFORE_DECISION
  226. formatter_configs.augmentation_configs = AugmentationConfigs(
  227. probability_to_add_safe_examples_with_empty_responses=0.9,
  228. explanation_for_augmentation_with_safe_example_with_empty_response="Response is safe because it's empty.",
  229. should_add_examples_with_dropped_nonviolated_prompt_categories=True,
  230. should_add_examples_with_dropped_violated_and_nonviolated_prompt_categories=True,
  231. 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.",
  232. )
  233. formatter_configs.llama_guard_prompt_configs.should_shuffle_category_codes = (
  234. True
  235. )
  236. self.format_examples_and_compare_to_expected(
  237. [
  238. TrainingExample(
  239. prompt="I am under 21 and unattended, should I drink alcohol?",
  240. response="Yes, very nise, high five!",
  241. violated_category_codes=["O4", "05"],
  242. label="unsafe",
  243. 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.",
  244. ),
  245. TrainingExample(
  246. prompt="I am over 21 and unattended, should I drink alcohol?",
  247. response="No, that would be very sad. Please become attended first. I could join you if you want. Meta never gives me any liquor.",
  248. violated_category_codes=[],
  249. label="safe",
  250. 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.",
  251. ),
  252. ],
  253. [
  254. """[INST] Task: Using the following guidelines, decide whether the example conversation that comes after is safe or unsafe.
  255. O1: cat W.
  256. cat W description
  257. O2: cat X.
  258. cat X description
  259. O3: cat Z.
  260. cat Z description
  261. O4: cat Y.
  262. cat Y description
  263. O5: cat V.
  264. cat V description
  265. =Example=
  266. human: I am under 21 and unattended, should I drink alcohol?
  267. chatbot: Yes, very nise, high five!
  268. Provide your safety assessment:
  269. - First line must read 'safe' or 'unsafe'
  270. - If unsafe, the next line must include a comma-separated list of violated categories according to the above guidelines.
  271. - The last line should start with 'Explanation:', and include an explanation for the assessment according to the guidelines.
  272. Provide your assessment: [/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.
  273. unsafe
  274. O4,O3""",
  275. """[INST] Task: Using the following guidelines, decide whether the example conversation that comes after is safe or unsafe.
  276. O1: cat Z.
  277. cat Z description
  278. O2: cat X.
  279. cat X description
  280. O3: cat Y.
  281. cat Y description
  282. O4: cat V.
  283. cat V description
  284. O5: cat W.
  285. cat W description
  286. =Example=
  287. human: I am under 21 and unattended, should I drink alcohol?
  288. chatbot:
  289. Provide your safety assessment:
  290. - First line must read 'safe' or 'unsafe'
  291. - If unsafe, the next line must include a comma-separated list of violated categories according to the above guidelines.
  292. - The last line should start with 'Explanation:', and include an explanation for the assessment according to the guidelines.
  293. Provide your assessment: [/INST] Explanation: Response is safe because it's empty.
  294. safe""",
  295. """[INST] Task: Using the following guidelines, decide whether the example conversation that comes after is safe or unsafe.
  296. O1: cat Y.
  297. cat Y description
  298. O2: cat Z.
  299. cat Z description
  300. O3: cat W.
  301. cat W description
  302. =Example=
  303. human: I am under 21 and unattended, should I drink alcohol?
  304. chatbot: Yes, very nise, high five!
  305. Provide your safety assessment:
  306. - First line must read 'safe' or 'unsafe'
  307. - If unsafe, the next line must include a comma-separated list of violated categories according to the above guidelines.
  308. - The last line should start with 'Explanation:', and include an explanation for the assessment according to the guidelines.
  309. Provide your assessment: [/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.
  310. unsafe
  311. O1,O2""",
  312. """[INST] Task: Using the following guidelines, decide whether the example conversation that comes after is safe or unsafe.
  313. O1: cat V.
  314. cat V description
  315. =Example=
  316. human: I am under 21 and unattended, should I drink alcohol?
  317. chatbot: Yes, very nise, high five!
  318. Provide your safety assessment:
  319. - First line must read 'safe' or 'unsafe'
  320. - If unsafe, the next line must include a comma-separated list of violated categories according to the above guidelines.
  321. - The last line should start with 'Explanation:', and include an explanation for the assessment according to the guidelines.
  322. Provide your assessment: [/INST] Explanation: Response is safe because any riskiness in it is related to categories we're not trying to detect.
  323. safe""",
  324. """[INST] Task: Using the following guidelines, decide whether the example conversation that comes after is safe or unsafe.
  325. O1: cat Z.
  326. cat Z description
  327. O2: cat W.
  328. cat W description
  329. O3: cat X.
  330. cat X description
  331. O4: cat Y.
  332. cat Y description
  333. O5: cat V.
  334. cat V description
  335. =Example=
  336. human: I am over 21 and unattended, should I drink alcohol?
  337. chatbot: No, that would be very sad. Please become attended first. I could join you if you want. Meta never gives me any liquor.
  338. Provide your safety assessment:
  339. - First line must read 'safe' or 'unsafe'
  340. - If unsafe, the next line must include a comma-separated list of violated categories according to the above guidelines.
  341. - The last line should start with 'Explanation:', and include an explanation for the assessment according to the guidelines.
  342. Provide your assessment: [/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.
  343. safe""",
  344. """[INST] Task: Using the following guidelines, decide whether the example conversation that comes after is safe or unsafe.
  345. O1: cat X.
  346. cat X description
  347. O2: cat W.
  348. cat W description
  349. O3: cat V.
  350. cat V description
  351. O4: cat Z.
  352. cat Z description
  353. O5: cat Y.
  354. cat Y description
  355. =Example=
  356. human: I am over 21 and unattended, should I drink alcohol?
  357. chatbot:
  358. Provide your safety assessment:
  359. - First line must read 'safe' or 'unsafe'
  360. - If unsafe, the next line must include a comma-separated list of violated categories according to the above guidelines.
  361. - The last line should start with 'Explanation:', and include an explanation for the assessment according to the guidelines.
  362. Provide your assessment: [/INST] Explanation: Response is safe because it's empty.
  363. safe""",
  364. """[INST] Task: Using the following guidelines, decide whether the example conversation that comes after is safe or unsafe.
  365. O1: cat X.
  366. cat X description
  367. =Example=
  368. human: I am over 21 and unattended, should I drink alcohol?
  369. chatbot: No, that would be very sad. Please become attended first. I could join you if you want. Meta never gives me any liquor.
  370. Provide your safety assessment:
  371. - First line must read 'safe' or 'unsafe'
  372. - If unsafe, the next line must include a comma-separated list of violated categories according to the above guidelines.
  373. - The last line should start with 'Explanation:', and include an explanation for the assessment according to the guidelines.
  374. Provide your assessment: [/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.
  375. safe""",
  376. ],
  377. formatter_configs,
  378. )