|
| 1 | +import unittest |
| 2 | + |
| 3 | +from transformers import AutoTokenizer |
| 4 | + |
| 5 | +from tests.tools import get_model_path |
| 6 | +from trinity.buffer.schema.formatter import ( |
| 7 | + DPOMessagesFormatter, |
| 8 | + DPOPlaintextFormatter, |
| 9 | + SFTMessagesFormatter, |
| 10 | + SFTPlaintextFormatter, |
| 11 | +) |
| 12 | +from trinity.common.config import FormatConfig |
| 13 | +from trinity.common.constants import PromptType |
| 14 | +from trinity.common.experience import Experience |
| 15 | + |
| 16 | + |
| 17 | +class TestFormatter(unittest.TestCase): |
| 18 | + def setUp(self): |
| 19 | + self.tokenizer = AutoTokenizer.from_pretrained(get_model_path()) |
| 20 | + |
| 21 | + def test_sft_messages_formatter(self): |
| 22 | + config = FormatConfig( |
| 23 | + prompt_type=PromptType.MESSAGES, |
| 24 | + messages_key="message_list", |
| 25 | + ) |
| 26 | + formatter = SFTMessagesFormatter(tokenizer=self.tokenizer, format_config=config) |
| 27 | + sample = { |
| 28 | + "message_list": [ |
| 29 | + {"role": "user", "content": "Hi"}, |
| 30 | + {"role": "assistant", "content": "Hello"}, |
| 31 | + ] |
| 32 | + } |
| 33 | + |
| 34 | + exp = formatter.format(sample) |
| 35 | + self.assertIsInstance(exp, Experience) |
| 36 | + self.assertIsNotNone(exp.tokens) |
| 37 | + self.assertIsNotNone(exp.prompt_length) |
| 38 | + self.assertTrue(exp.prompt_length < len(exp.tokens)) |
| 39 | + sequence = self.tokenizer.decode(exp.tokens) |
| 40 | + |
| 41 | + self.assertIn("Hi", sequence) |
| 42 | + self.assertIn("Hello", sequence) |
| 43 | + |
| 44 | + # test tool |
| 45 | + config = FormatConfig( |
| 46 | + prompt_type=PromptType.MESSAGES, |
| 47 | + messages_key="messages", |
| 48 | + tools_key="tools", |
| 49 | + ) |
| 50 | + formatter = SFTMessagesFormatter(tokenizer=self.tokenizer, format_config=config) |
| 51 | + sample = { |
| 52 | + "messages": [ |
| 53 | + { |
| 54 | + "role": "system", |
| 55 | + "content": "You are a helpful assistant with access to various tools. Use them when needed to help users.", |
| 56 | + }, |
| 57 | + {"role": "user", "content": "What's the weather like in Beijing today?"}, |
| 58 | + { |
| 59 | + "role": "assistant", |
| 60 | + "content": "Let me get the weather for you.", |
| 61 | + "tool_calls": [ |
| 62 | + { |
| 63 | + "id": "call_abc123", |
| 64 | + "type": "function", |
| 65 | + "function": { |
| 66 | + "name": "get_weather", |
| 67 | + "arguments": '{"location": "Beijing", "unit": "celsius"}', |
| 68 | + }, |
| 69 | + } |
| 70 | + ], |
| 71 | + }, |
| 72 | + { |
| 73 | + "role": "tool", |
| 74 | + "content": '{"temperature": 22, "condition": "sunny", "humidity": 45}', |
| 75 | + "tool_call_id": "call_abc123", |
| 76 | + }, |
| 77 | + { |
| 78 | + "role": "assistant", |
| 79 | + "content": "The weather in Beijing today is sunny with a temperature of 22°C and humidity at 45%. It's a pleasant day!", |
| 80 | + }, |
| 81 | + ], |
| 82 | + "tools": [ |
| 83 | + { |
| 84 | + "type": "function", |
| 85 | + "function": { |
| 86 | + "name": "get_weather", |
| 87 | + "description": "Get the current weather in a given location", |
| 88 | + "parameters": { |
| 89 | + "type": "object", |
| 90 | + "properties": { |
| 91 | + "location": { |
| 92 | + "type": "string", |
| 93 | + "description": "The city and state, e.g. San Francisco, CA", |
| 94 | + }, |
| 95 | + "unit": { |
| 96 | + "type": "string", |
| 97 | + "enum": ["celsius", "fahrenheit"], |
| 98 | + "description": "The temperature unit", |
| 99 | + }, |
| 100 | + }, |
| 101 | + "required": ["location"], |
| 102 | + }, |
| 103 | + }, |
| 104 | + } |
| 105 | + ], |
| 106 | + } |
| 107 | + exp = formatter.format(sample) |
| 108 | + self.assertIsInstance(exp, Experience) |
| 109 | + self.assertIsNotNone(exp.tokens) |
| 110 | + self.assertIsNotNone(exp.prompt_length) |
| 111 | + self.assertTrue(exp.prompt_length < len(exp.tokens)) |
| 112 | + sequence = self.tokenizer.decode(exp.tokens) |
| 113 | + self.assertIn("What's the weather like in Beijing today?", sequence) |
| 114 | + self.assertIn( |
| 115 | + "The weather in Beijing today is sunny with a temperature of 22°C and humidity at 45%. It's a pleasant day!", |
| 116 | + sequence, |
| 117 | + ) |
| 118 | + self.assertIn("get_weather", sequence) |
| 119 | + |
| 120 | + def test_sft_plaintext_formatter(self): |
| 121 | + # with system prompt key |
| 122 | + config = FormatConfig( |
| 123 | + prompt_type=PromptType.PLAINTEXT, |
| 124 | + system_prompt_key="system", |
| 125 | + system_prompt="You are a programmer.", # has lower priority than system_prompt_key |
| 126 | + prompt_key="prompt", |
| 127 | + response_key="response", |
| 128 | + ) |
| 129 | + formatter = SFTPlaintextFormatter(tokenizer=self.tokenizer, format_config=config) |
| 130 | + sample = { |
| 131 | + "system": "You are a helpful assistant.", |
| 132 | + "prompt": "What is 2+2?", |
| 133 | + "response": "2+2=4", |
| 134 | + } |
| 135 | + exp = formatter.format(sample) |
| 136 | + self.assertIsInstance(exp, Experience) |
| 137 | + self.assertIsNotNone(exp.tokens) |
| 138 | + self.assertIsNotNone(exp.prompt_length) |
| 139 | + self.assertTrue(exp.prompt_length < len(exp.tokens)) |
| 140 | + # detokenize exp.tokens into text |
| 141 | + sequence = self.tokenizer.decode(exp.tokens) |
| 142 | + self.assertIn("You are a helpful assistant.", sequence) |
| 143 | + self.assertIn("What is 2+2?", sequence) |
| 144 | + self.assertIn("2+2=4", sequence) |
| 145 | + |
| 146 | + # with system prompt |
| 147 | + config = FormatConfig( |
| 148 | + prompt_type=PromptType.PLAINTEXT, |
| 149 | + system_prompt="You are a programmer.", |
| 150 | + prompt_key="prompt", |
| 151 | + response_key="response", |
| 152 | + ) |
| 153 | + formatter = SFTPlaintextFormatter(tokenizer=self.tokenizer, format_config=config) |
| 154 | + |
| 155 | + exp = formatter.format(sample) |
| 156 | + self.assertIsInstance(exp, Experience) |
| 157 | + self.assertIsNotNone(exp.tokens) |
| 158 | + self.assertIsNotNone(exp.prompt_length) |
| 159 | + self.assertTrue(exp.prompt_length < len(exp.tokens)) |
| 160 | + # detokenize exp.tokens into text |
| 161 | + sequence = self.tokenizer.decode(exp.tokens) |
| 162 | + self.assertIn("You are a programmer.", sequence) |
| 163 | + self.assertIn("What is 2+2?", sequence) |
| 164 | + self.assertIn("2+2=4", sequence) |
| 165 | + |
| 166 | + def test_dpo_plaintext_formatter(self): |
| 167 | + config = FormatConfig( |
| 168 | + prompt_type=PromptType.PLAINTEXT, |
| 169 | + prompt_key="prompt", |
| 170 | + chosen_key="chosen", |
| 171 | + rejected_key="rejected", |
| 172 | + ) |
| 173 | + formatter = DPOPlaintextFormatter(tokenizer=self.tokenizer, format_config=config) |
| 174 | + sample = {"prompt": "What is 2+2?", "chosen": "2+2=4", "rejected": "2+2=5"} |
| 175 | + exp = formatter.format(sample) |
| 176 | + self.assertIsInstance(exp, Experience) |
| 177 | + self.assertIsNotNone(exp.tokens) |
| 178 | + self.assertIsNotNone(exp.chosen) |
| 179 | + self.assertIsNotNone(exp.rejected) |
| 180 | + self.assertIsNotNone(exp.prompt_length) |
| 181 | + prompt = self.tokenizer.decode(exp.tokens) |
| 182 | + chosen = self.tokenizer.decode(exp.chosen) |
| 183 | + rejected = self.tokenizer.decode(exp.rejected) |
| 184 | + self.assertIn("What is 2+2?", prompt) |
| 185 | + self.assertIn("2+2=4", chosen) |
| 186 | + self.assertIn("2+2=5", rejected) |
| 187 | + self.assertNotIn("What is 2+2?", chosen) |
| 188 | + self.assertNotIn("What is 2+2?", rejected) |
| 189 | + self.assertNotIn("2+2=4", prompt) |
| 190 | + self.assertNotIn("2+2=5", prompt) |
| 191 | + |
| 192 | + def test_dpo_messages_formatter(self): |
| 193 | + config = FormatConfig( |
| 194 | + prompt_type=PromptType.MESSAGES, |
| 195 | + messages_key="messages", |
| 196 | + chosen_key="chosen", |
| 197 | + rejected_key="rejected", |
| 198 | + ) |
| 199 | + formatter = DPOMessagesFormatter(tokenizer=self.tokenizer, format_config=config) |
| 200 | + sample = { |
| 201 | + "messages": [ |
| 202 | + {"role": "user", "content": "What is your name?"}, |
| 203 | + ], |
| 204 | + "chosen": [ |
| 205 | + {"role": "assistant", "content": "My name is Assistant."}, |
| 206 | + ], |
| 207 | + "rejected": [{"role": "assistant", "content": "I don't have a favorite color."}], |
| 208 | + } |
| 209 | + exp = formatter.format(sample) |
| 210 | + self.assertIsInstance(exp, Experience) |
| 211 | + self.assertIsNotNone(exp.tokens) |
| 212 | + self.assertIsNotNone(exp.prompt_length) |
| 213 | + # detokenize exp.tokens into text |
| 214 | + prompt = self.tokenizer.decode(exp.tokens) |
| 215 | + chosen = self.tokenizer.decode(exp.chosen) |
| 216 | + rejected = self.tokenizer.decode(exp.rejected) |
| 217 | + self.assertIn("What is your name?", prompt) |
| 218 | + self.assertIn("My name is Assistant.", chosen) |
| 219 | + self.assertIn("I don't have a favorite color.", rejected) |
0 commit comments