(
self,
conversation,
max_length,
)
| 66 | ) |
| 67 | |
| 68 | def parse( |
| 69 | self, |
| 70 | conversation, |
| 71 | max_length, |
| 72 | ): |
| 73 | messages = [] |
| 74 | if conversation[0]["role"] == "system": |
| 75 | warnings.warn( |
| 76 | "System prompt from the sample overrides the registered template.", |
| 77 | stacklevel=2, |
| 78 | ) |
| 79 | messages.append({"role": "system", "content": conversation[0]["content"]}) |
| 80 | conversation = conversation[1:] |
| 81 | elif self.system_prompt: |
| 82 | messages.append({"role": "system", "content": self.system_prompt}) |
| 83 | |
| 84 | for idx, sentence in enumerate(conversation): |
| 85 | role = sentence["role"] |
| 86 | assert idx != 0 or role == "user", ( |
| 87 | f"Conversation must start with user, got {role}." |
| 88 | ) |
| 89 | tool_calls = sentence.get("tool_calls") |
| 90 | if isinstance(tool_calls, str): |
| 91 | try: |
| 92 | sentence["tool_calls"] = json.loads(tool_calls) |
| 93 | except json.JSONDecodeError: |
| 94 | assert False, f"Failed to parse tool_calls JSON: {tool_calls}" |
| 95 | messages.append(sentence) |
| 96 | render_messages = self._prepare_render_messages(messages) |
| 97 | conversation_text = render_chat_messages( |
| 98 | self.tokenizer, |
| 99 | render_messages, |
| 100 | add_generation_prompt=False, |
| 101 | ) |
| 102 | |
| 103 | encoding = self.tokenizer( |
| 104 | conversation_text, |
| 105 | max_length=max_length, |
| 106 | truncation=True, |
| 107 | return_tensors="pt", |
| 108 | add_special_tokens=False, |
| 109 | ) |
| 110 | input_ids = encoding.input_ids[0] |
| 111 | attention_mask = encoding.attention_mask[0] |
| 112 | loss_mask = torch.zeros(len(input_ids), dtype=torch.long) |
| 113 | |
| 114 | matches = list(re.finditer(self.assistant_pattern, conversation_text, re.DOTALL)) |
| 115 | for match in matches: |
| 116 | content_start_char = match.start(1) |
| 117 | if self.assistant_loss_prefix and conversation_text.startswith( |
| 118 | self.assistant_loss_prefix, |
| 119 | content_start_char, |
| 120 | ): |
| 121 | content_start_char += len(self.assistant_loss_prefix) |
| 122 | content_end_char = match.end(1) |
| 123 | prefix_ids = self.tokenizer.encode( |
| 124 | conversation_text[:content_start_char], |
| 125 | add_special_tokens=False, |
no test coverage detected