Skip to content

Commit 690bce5

Browse files
committed
fix(mtmd): register standard Jinja chat template helpers
- Expose raise_exception so unsupported inputs return the template's intended error message instead of an undefined-function error. - Expose strftime_now for model chat templates that use date formatting. - Reuse the existing formatter implementations and add regression tests for both helpers. Signed-off-by: JamePeng <jame_peng@sina.com>
1 parent 1cc541e commit 690bce5

2 files changed

Lines changed: 41 additions & 3 deletions

File tree

‎llama_cpp/llama_multimodal.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
_convert_completion_to_chat,
4545
_convert_completion_to_chat_function,
4646
_grammar_for_response_format,
47-
ImmutableSandboxedEnvironment
47+
ImmutableSandboxedEnvironment,
48+
Jinja2ChatFormatter,
4849
)
4950

5051
class MTMDBaseHandler:
@@ -1017,10 +1018,13 @@ def __init__(
10171018
self._change_chat_template(self.chat_format)
10181019

10191020
def _change_chat_template(self, new_template: str):
1020-
self.chat_template = ImmutableSandboxedEnvironment(
1021+
environment = ImmutableSandboxedEnvironment(
10211022
trim_blocks=True,
10221023
lstrip_blocks=True
1023-
).from_string(new_template)
1024+
)
1025+
environment.globals["raise_exception"] = Jinja2ChatFormatter.raise_exception
1026+
environment.globals["strftime_now"] = Jinja2ChatFormatter.strftime_now
1027+
self.chat_template = environment.from_string(new_template)
10241028

10251029
def _init_mtmd_context(self, llama_model: llama_core.Llama):
10261030
if self.mtmd_ctx is not None:

‎tests/test_media.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import struct
88

99
import pytest
10+
from jinja2.exceptions import TemplateError
1011

1112

1213
@pytest.fixture
@@ -336,6 +337,39 @@ class CustomChatHandler(multimodal.MTMDChatHandler):
336337
handler.close()
337338

338339

340+
def test_mtmd_chat_template_raise_exception_preserves_message(tmp_path):
341+
multimodal = importlib.import_module("llama_cpp.llama_multimodal")
342+
handler = multimodal.MTMDChatHandler(
343+
mmproj_path=str(tmp_path),
344+
verbose=False,
345+
chat_template_override=(
346+
"{% if video %}{{ raise_exception('Video not supported') }}"
347+
"{% else %}text supported{% endif %}"
348+
),
349+
)
350+
try:
351+
assert handler.chat_template.render(video=False) == "text supported"
352+
with pytest.raises(TemplateError, match="Video not supported"):
353+
handler.chat_template.render(video=True)
354+
finally:
355+
handler.close()
356+
357+
358+
def test_mtmd_chat_template_strftime_now(tmp_path):
359+
import datetime
360+
361+
multimodal = importlib.import_module("llama_cpp.llama_multimodal")
362+
handler = multimodal.MTMDChatHandler(
363+
mmproj_path=str(tmp_path),
364+
verbose=False,
365+
chat_template_override="{{ strftime_now('%Y') }}",
366+
)
367+
try:
368+
assert handler.chat_template.render() == datetime.datetime.now().strftime("%Y")
369+
finally:
370+
handler.close()
371+
372+
339373
def test_mtmd_base_image_loader_uses_subclass_byte_loader():
340374
import io
341375
from PIL import Image

0 commit comments

Comments
 (0)