Fix chat template: render tool_calls history and add <think> handling

#4
Files changed (1) hide show
  1. chat_template.jinja +38 -3
chat_template.jinja CHANGED
@@ -1,5 +1,27 @@
1
  {{- bos_token -}}
2
  {%- set keep_past_thinking = keep_past_thinking | default(false) -%}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  {%- set ns = namespace(system_prompt="") -%}
4
  {%- if messages[0]["role"] == "system" -%}
5
  {%- set ns.system_prompt = messages[0]["content"] -%}
@@ -22,15 +44,24 @@
22
  {{- "<|im_start|>system\n" + ns.system_prompt + "<|im_end|>\n" -}}
23
  {%- endif -%}
24
  {%- set ns.last_assistant_index = -1 -%}
 
25
  {%- for message in messages -%}
26
  {%- if message["role"] == "assistant" -%}
27
  {%- set ns.last_assistant_index = loop.index0 -%}
28
  {%- endif -%}
 
 
 
29
  {%- endfor -%}
30
  {%- for message in messages -%}
31
  {{- "<|im_start|>" + message["role"] + "\n" -}}
32
- {%- set content = message["content"] -%}
33
- {%- if content is not string -%}
 
 
 
 
 
34
  {%- set content = content | tojson -%}
35
  {%- endif -%}
36
  {%- if message["role"] == "assistant" and not keep_past_thinking and loop.index0 != ns.last_assistant_index -%}
@@ -38,7 +69,11 @@
38
  {%- set content = content.split("</think>")[-1] | trim -%}
39
  {%- endif -%}
40
  {%- endif -%}
41
- {{- content + "<|im_end|>\n" -}}
 
 
 
 
42
  {%- endfor -%}
43
  {%- if add_generation_prompt -%}
44
  {{- "<|im_start|>assistant\n" -}}
 
1
  {{- bos_token -}}
2
  {%- set keep_past_thinking = keep_past_thinking | default(false) -%}
3
+ {%- macro format_arg_value(arg_value) -%}
4
+ {%- if arg_value is string -%}
5
+ {{- "'" + arg_value + "'" -}}
6
+ {%- elif arg_value is mapping -%}
7
+ {{- arg_value | tojson -}}
8
+ {%- else -%}
9
+ {{- arg_value | string -}}
10
+ {%- endif -%}
11
+ {%- endmacro -%}
12
+ {%- macro render_tool_calls(tool_calls) -%}
13
+ {%- set tool_calls_ns = namespace(tool_calls=[]) -%}
14
+ {%- for tool_call in tool_calls -%}
15
+ {%- set func_name = tool_call["function"]["name"] -%}
16
+ {%- set func_args = tool_call["function"]["arguments"] -%}
17
+ {%- set args_ns = namespace(arg_strings=[]) -%}
18
+ {%- for arg_name, arg_value in func_args.items() -%}
19
+ {%- set args_ns.arg_strings = args_ns.arg_strings + [arg_name + "=" + format_arg_value(arg_value)] -%}
20
+ {%- endfor -%}
21
+ {%- set tool_calls_ns.tool_calls = tool_calls_ns.tool_calls + [func_name + "(" + (args_ns.arg_strings | join(", ")) + ")"] -%}
22
+ {%- endfor -%}
23
+ {{- "<|tool_call_start|>[" + (tool_calls_ns.tool_calls | join(", ")) + "]<|tool_call_end|>" -}}
24
+ {%- endmacro -%}
25
  {%- set ns = namespace(system_prompt="") -%}
26
  {%- if messages[0]["role"] == "system" -%}
27
  {%- set ns.system_prompt = messages[0]["content"] -%}
 
44
  {{- "<|im_start|>system\n" + ns.system_prompt + "<|im_end|>\n" -}}
45
  {%- endif -%}
46
  {%- set ns.last_assistant_index = -1 -%}
47
+ {%- set ns.last_user_index = -1 -%}
48
  {%- for message in messages -%}
49
  {%- if message["role"] == "assistant" -%}
50
  {%- set ns.last_assistant_index = loop.index0 -%}
51
  {%- endif -%}
52
+ {%- if message["role"] == "user" -%}
53
+ {%- set ns.last_user_index = loop.index0 -%}
54
+ {%- endif -%}
55
  {%- endfor -%}
56
  {%- for message in messages -%}
57
  {{- "<|im_start|>" + message["role"] + "\n" -}}
58
+ {%- if message["role"] == "assistant" and message.get("thinking") and (keep_past_thinking or loop.index0 > ns.last_user_index) -%}
59
+ {{- "<think>" + message["thinking"] + "</think>" -}}
60
+ {%- endif -%}
61
+ {%- set content = message.get("content") -%}
62
+ {%- if not content -%}
63
+ {%- set content = "" -%}
64
+ {%- elif content is not string -%}
65
  {%- set content = content | tojson -%}
66
  {%- endif -%}
67
  {%- if message["role"] == "assistant" and not keep_past_thinking and loop.index0 != ns.last_assistant_index -%}
 
69
  {%- set content = content.split("</think>")[-1] | trim -%}
70
  {%- endif -%}
71
  {%- endif -%}
72
+ {{- content -}}
73
+ {%- if message["role"] == "assistant" and message.get("tool_calls") -%}
74
+ {{- render_tool_calls(message["tool_calls"]) -}}
75
+ {%- endif -%}
76
+ {{- "<|im_end|>\n" -}}
77
  {%- endfor -%}
78
  {%- if add_generation_prompt -%}
79
  {{- "<|im_start|>assistant\n" -}}