From 3659aa28e963ef3f782cd27258e97ddef678c776 Mon Sep 17 00:00:00 2001 From: SmartestWashingMachine Date: Thu, 4 Dec 2025 22:12:45 +1100 Subject: [PATCH] convert: use existing local chat_template if mistral-format model has one. (#17749) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * conversion: use existing local chat_template.jinja file if mistral-format model has one. * fix --mistral-format mistakenly assuming some <=v7 chat template names are file paths and reading them. * Update convert_hf_to_gguf.py - change from exists() to is_file() Co-authored-by: Sigbjørn Skjæret --------- Co-authored-by: Sigbjørn Skjæret --- convert_hf_to_gguf.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/convert_hf_to_gguf.py b/convert_hf_to_gguf.py index 8ddb6d04cd..4590b23921 100755 --- a/convert_hf_to_gguf.py +++ b/convert_hf_to_gguf.py @@ -2341,9 +2341,18 @@ class LlamaModel(TextModel): self.gguf_writer.add_add_bos_token(True) self.gguf_writer.add_add_eos_token(False) - template_dir = Path(__file__).parent / "models/templates/" + local_template_file_path = self.dir_model / "chat_template.jinja" + + if self.is_mistral_format and local_template_file_path.is_file(): + # Ministral-3 and other new Mistral models come with chat templates. + # ref: https://huggingface.co/mistralai/Ministral-3-14B-Instruct-2512/tree/main + logger.info("Using an existing Mistral local chat template.") + + with open(local_template_file_path, "r", encoding="utf-8") as f: + template = f.read() + elif not self.is_mistral_format or not self.disable_mistral_community_chat_template: + template_dir = Path(__file__).parent / "models/templates/" - if not self.is_mistral_format or not self.disable_mistral_community_chat_template: # Log only for Mistral format that the official tokenization and detokenization is via `mistral-common`. if self.is_mistral_format: logger.info( @@ -2351,9 +2360,12 @@ class LlamaModel(TextModel): "Mistral recommends to use `mistral-common` to perform tokenization and detokenization." ) template = MistralModel.get_community_chat_template(vocab, template_dir, self.is_mistral_format) - self.gguf_writer.add_chat_template(template) else: - logger.info("Not using a Mistral community chat template. Ensure to perform the tokenization and detokenization via `mistral-common`.") + logger.info("Not using a Mistral local or community chat template. Ensure to perform the tokenization and detokenization via `mistral-common`.") + template = None + + if template is not None: + self.gguf_writer.add_chat_template(template) def set_vocab(self): if self.is_mistral_format: