Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions mergekit/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,15 @@ def _set_chat_template(
LOG.info(f"Auto-selected chat template: {chat_template}")

elif (
t := importlib.resources.files(chat_templates).joinpath(
chat_template + ".jinja"
)
).is_file():
len(chat_template) < 256
and "\n" not in chat_template
and "{" not in chat_template
and (
t := importlib.resources.files(chat_templates).joinpath(
chat_template + ".jinja"
)
).is_file()
):
chat_template = t.read_text()

elif len(chat_template) < 20 or "{" not in chat_template:
Expand Down
24 changes: 24 additions & 0 deletions tests/test_chat_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,27 @@ def test_template_literal_jinja(self, model_base, model_b):
config,
validate=lambda p: check_chat_template(p, "{{messages[0]['content']}}"),
)

def test_template_long_literal_jinja(self, model_base, model_b):
template = (
"{% for message in messages %}"
+ "{{ message['role'] }}: {{ message['content'] }}\n"
+ "{% endfor %}"
+ "{# "
+ "x" * 300
+ " #}"
)
config = MergeConfiguration(
merge_method="linear",
models=[
InputModelDefinition(model=model_base, parameters={"weight": 0.5}),
InputModelDefinition(model=model_b, parameters={"weight": 0.5}),
],
base_model=model_base,
dtype="bfloat16",
chat_template=template,
)
run_and_check_merge(
config,
validate=lambda p: check_chat_template(p, "x" * 300),
)
Loading