Update chat_template.jinja
#20
by dluthyc - opened
Title: Fix partial assistant prefills when continue_final_message=True
Summary:
The current chat_template.jinja always appends <|im_end|> after assistant turns.
When callers disable add_generation_prompt and use a final assistant message as a prefill, this closes the assistant message before generation starts, so the model tends to emit an empty continuation or start a new turn incorrectly.
This patch makes the final assistant turn honor Hugging Face's continue_final_message contract by skipping the closing <|im_end|> only when:
- the current message is the last message
continue_final_messageis definedcontinue_final_messageistrue
All other rendering paths are unchanged.
Minimal repro:
messages = [
{"role": "user", "content": "Reply in JSON."},
{"role": "assistant", "content": '{"name": "'},
]
tokenizer.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=False,
continue_final_message=True,
)
Before:
<|im_start|>user
Reply in JSON.<|im_end|>
<|im_start|>assistant
{"name": "<|im_end|>
After:
<|im_start|>user
Reply in JSON.<|im_end|>
<|im_start|>assistant
{"name": "