Fix broken chat template

#80

{% if add_generation_prompt and not ns.is_last_user and not ns.is_tool %}

Should be

{% if add_generation_prompt and ns.is_last_user and not ns.is_tool %}

Thanks to Hot Aisle for lending me the excelent mi300x node used to validate this.

There is also an operational bug.
In the current chat template, even when tool_calls == [], it is incorrectly treated as if there is a tool call.

{%- if message['role'] == 'assistant' and message['tool_calls'] is defined and message['tool_calls'] is not none %}

Wouldn't it be better to add a length check and store the presence or absence of a tool call as a separate variable, as shown below?

{% set has_tools = message['tool_calls'] is defined and message['tool_calls'] is not none and message['tool_calls']|length > 0 %}
{%- if has_tools and message['role'] == 'assistant' %}
  [Code for the case when tools are present]
{%- endif %}
{%- if not has_tools and message['role'] == 'assistant' %}
  [Code for the case when no tools are present]
{%- endif %}
{%- if message['role'] == 'tool' %}

Here is the updated template that addresses the two reported issues. Please refer to it for your reference.
https://huggingface.co/spaces/huggingfacejs/chat-template-playground?modelId=yasu-oh%2FLlama-3-Swallow-Infused-R1776-70B

Ready to merge
This branch is ready to get merged automatically.

Sign up or log in to comment