Skip to content

Commit 3659aa2

Browse files
convert: use existing local chat_template if mistral-format model has one. (#17749)
* 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 <[email protected]> --------- Co-authored-by: Sigbjørn Skjæret <[email protected]>
1 parent 2a73f81 commit 3659aa2

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

convert_hf_to_gguf.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2341,19 +2341,31 @@ def _set_vocab_mistral(self):
23412341
self.gguf_writer.add_add_bos_token(True)
23422342
self.gguf_writer.add_add_eos_token(False)
23432343

2344-
template_dir = Path(__file__).parent / "models/templates/"
2344+
local_template_file_path = self.dir_model / "chat_template.jinja"
2345+
2346+
if self.is_mistral_format and local_template_file_path.is_file():
2347+
# Ministral-3 and other new Mistral models come with chat templates.
2348+
# ref: https://huggingface.co/mistralai/Ministral-3-14B-Instruct-2512/tree/main
2349+
logger.info("Using an existing Mistral local chat template.")
2350+
2351+
with open(local_template_file_path, "r", encoding="utf-8") as f:
2352+
template = f.read()
2353+
elif not self.is_mistral_format or not self.disable_mistral_community_chat_template:
2354+
template_dir = Path(__file__).parent / "models/templates/"
23452355

2346-
if not self.is_mistral_format or not self.disable_mistral_community_chat_template:
23472356
# Log only for Mistral format that the official tokenization and detokenization is via `mistral-common`.
23482357
if self.is_mistral_format:
23492358
logger.info(
23502359
"Using a Mistral community chat template. These templates can be subject to errors in early days or weeks after a release. "
23512360
"Mistral recommends to use `mistral-common` to perform tokenization and detokenization."
23522361
)
23532362
template = MistralModel.get_community_chat_template(vocab, template_dir, self.is_mistral_format)
2354-
self.gguf_writer.add_chat_template(template)
23552363
else:
2356-
logger.info("Not using a Mistral community chat template. Ensure to perform the tokenization and detokenization via `mistral-common`.")
2364+
logger.info("Not using a Mistral local or community chat template. Ensure to perform the tokenization and detokenization via `mistral-common`.")
2365+
template = None
2366+
2367+
if template is not None:
2368+
self.gguf_writer.add_chat_template(template)
23572369

23582370
def set_vocab(self):
23592371
if self.is_mistral_format:

0 commit comments

Comments
 (0)