(self, model_path: str = '', trust_remote_code: bool = False, **kwargs)
| 685 | """ |
| 686 | |
| 687 | def __init__(self, model_path: str = '', trust_remote_code: bool = False, **kwargs): |
| 688 | self.model_path = model_path |
| 689 | try: |
| 690 | from transformers import AutoProcessor, AutoTokenizer |
| 691 | self.tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=trust_remote_code) |
| 692 | |
| 693 | # Some tokenizers do not have chat_template, in this case try to get chat_template from processor |
| 694 | # If this still does not work, fallback to BaseChatTemplate. |
| 695 | if getattr(self.tokenizer, 'chat_template', None) is None: |
| 696 | try: |
| 697 | processor = AutoProcessor.from_pretrained(model_path, trust_remote_code=trust_remote_code) |
| 698 | self.tokenizer.chat_template = getattr(processor, 'chat_template', None) |
| 699 | except Exception as e: |
| 700 | logger.warning(f'Failed to load processor from {model_path} for chat template. ' |
| 701 | f'Fallback to tokenizer only. Error: {e}') |
| 702 | |
| 703 | # Verify if the model can perform apply_chat_template with different roles. |
| 704 | self.user_start, self.user_end, _, _ = self._user_instruction() |
| 705 | self.assistant_start, self.assistant_end, _, _ = self._assistant_instruction() |
| 706 | _, _, self.sentinel_system_messages, self.sentinel_system_prompt = self._system_instruction() |
| 707 | self.stop_words = [] |
| 708 | if hasattr(self.tokenizer, 'eos_token') and self.tokenizer.eos_token is not None: |
| 709 | self.stop_words.append(self.tokenizer.eos_token) |
| 710 | if hasattr(self.tokenizer, 'eot_token') and self.tokenizer.eot_token is not None: |
| 711 | self.stop_words.append(self.tokenizer.eot_token) |
| 712 | arch, _ = get_model_arch(model_path) |
| 713 | self.is_gpt_oss = arch == 'GptOssForCausalLM' |
| 714 | if self.is_gpt_oss: |
| 715 | self.stop_words.append('<|call|>') |
| 716 | except Exception as e: |
| 717 | raise ValueError(f'Try apply_chat_template failed: {e}') |
| 718 | |
| 719 | def get_prompt(self, prompt, sequence_start=True, **kwargs): |
| 720 | messages = [{'role': 'user', 'content': prompt}] |
nothing calls this directly
no test coverage detected