Skip to content

Commit 067afe2

Browse files
CISCMinh141120
authored andcommitted
gguf-py : add support for chat template jinja files (ggml-org#14508)
* add support for chat template jinja files * remove gemma3n hack
1 parent c07421d commit 067afe2

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
@@ -246,9 +246,18 @@ def _try_load_from_tokenizer_json(self, path: Path) -> bool:
246246
if not tokenizer_config:
247247
return True
248248
chat_template_alt = None
249-
chat_template_file = path / 'chat_template.json'
250-
if chat_template_file.is_file():
251-
with open(chat_template_file, encoding = 'utf-8') as f:
249+
chat_template_json = path / 'chat_template.json'
250+
chat_template_jinja = path / 'chat_template.jinja'
251+
if chat_template_jinja.is_file():
252+
with open(chat_template_jinja, encoding = 'utf-8') as f:
253+
chat_template_alt = f.read()
254+
if additional_templates := list((path / 'additional_chat_templates').glob('*.jinja')):
255+
chat_template_alt = [{'name': 'default', 'template': chat_template_alt}]
256+
for template_path in additional_templates:
257+
with open(template_path, encoding = 'utf-8') as fp:
258+
chat_template_alt.append({'name': template_path.stem, 'template': fp.read()})
259+
elif chat_template_json.is_file():
260+
with open(chat_template_json, encoding = 'utf-8') as f:
252261
chat_template_alt = json.load(f).get('chat_template')
253262
chat_template = tokenizer_config.get('chat_template', chat_template_alt)
254263
if chat_template is None or isinstance(chat_template, (str, list)):

0 commit comments

Comments
 (0)