@@ -561,7 +561,7 @@ def write_tokenizer(tokenizer_path: str, save_dir: str, instruct: bool = False):
561
561
{%- if not loop.last %}
562
562
{{- ",\n " }}
563
563
{%- else %}
564
- {{- "\n " }}
564
+ {{- ", \n " }}
565
565
{%- endif -%}
566
566
{%- endfor %}
567
567
{{- "}) => any;\n \n " }}
@@ -696,6 +696,15 @@ def write_tokenizer(tokenizer_path: str, save_dir: str, instruct: bool = False):
696
696
{%- endif %}
697
697
{%- endif %}
698
698
{%- if "tool_calls" in message %}
699
+ {#- We need very careful handling here - we want to drop the tool call analysis message if the model #}
700
+ {#- has output a later <|final|> message, but otherwise we want to retain it. This is the only case #}
701
+ {#- when we render CoT/analysis messages in inference. #}
702
+ {%- set future_final_message = namespace(found=false) %}
703
+ {%- for future_message in loop_messages[loop.index:] %}
704
+ {%- if future_message.role == 'assistant' and "tool_calls" not in future_message %}
705
+ {%- set future_final_message.found = true %}
706
+ {%- endif %}
707
+ {%- endfor %}
699
708
{#- We assume max 1 tool call per message, and so we infer the tool call name #}
700
709
{#- in "tool" messages from the most recent assistant tool call name #}
701
710
{%- set tool_call = message.tool_calls[0] %}
@@ -704,9 +713,9 @@ def write_tokenizer(tokenizer_path: str, save_dir: str, instruct: bool = False):
704
713
{%- endif %}
705
714
{%- if message.content and message.thinking %}
706
715
{{- raise_exception("Cannot pass both content and thinking in an assistant message with tool calls! Put the analysis message in one or the other, but not both.") }}
707
- {%- elif message.content %}
716
+ {%- elif message.content and not future_final_message.found %}
708
717
{{- "<|start|>assistant<|channel|>analysis<|message|>" + message.content + "<|end|>" }}
709
- {%- elif message.thinking %}
718
+ {%- elif message.thinking and not future_final_message.found %}
710
719
{{- "<|start|>assistant<|channel|>analysis<|message|>" + message.thinking + "<|end|>" }}
711
720
{%- endif %}
712
721
{{- "<|start|>assistant to=" }}
0 commit comments