Skip to content

Commit e75ba4c

Browse files
authored
gguf-py : add support for chat template jinja files (ggml-org#14508)
* add support for chat template jinja files * remove gemma3n hack
1 parent 5d46bab commit e75ba4c

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

convert_hf_to_gguf.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4408,9 +4408,6 @@ def __init__(self, *args, **kwargs):
44084408
]
44094409

44104410
def set_vocab(self):
4411-
with open(self.dir_model / "chat_template.jinja") as f:
4412-
# quick hack to make sure chat template is added
4413-
self.gguf_writer.add_chat_template(f.read())
44144411
super().set_vocab()
44154412

44164413
def set_gguf_parameters(self):

gguf-py/gguf/vocab.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,18 @@ def _try_load_from_tokenizer_json(self, path: Path) -> bool:
245245
if not tokenizer_config:
246246
return True
247247
chat_template_alt = None
248-
chat_template_file = path / 'chat_template.json'
249-
if chat_template_file.is_file():
250-
with open(chat_template_file, encoding = 'utf-8') as f:
248+
chat_template_json = path / 'chat_template.json'
249+
chat_template_jinja = path / 'chat_template.jinja'
250+
if chat_template_jinja.is_file():
251+
with open(chat_template_jinja, encoding = 'utf-8') as f:
252+
chat_template_alt = f.read()
253+
if additional_templates := list((path / 'additional_chat_templates').glob('*.jinja')):
254+
chat_template_alt = [{'name': 'default', 'template': chat_template_alt}]
255+
for template_path in additional_templates:
256+
with open(template_path, encoding = 'utf-8') as fp:
257+
chat_template_alt.append({'name': template_path.stem, 'template': fp.read()})
258+
elif chat_template_json.is_file():
259+
with open(chat_template_json, encoding = 'utf-8') as f:
251260
chat_template_alt = json.load(f).get('chat_template')
252261
chat_template = tokenizer_config.get('chat_template', chat_template_alt)
253262
if chat_template is None or isinstance(chat_template, (str, list)):

0 commit comments

Comments
 (0)