Update chat_template.jinja: add generation markers, tool_calls, and thinking support

#17
Files changed (1) hide show
  1. chat_template.jinja +76 -40
chat_template.jinja CHANGED
@@ -1,64 +1,100 @@
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 sys_content = messages[0]["content"] -%}
6
- {%- if sys_content is not string -%}
7
- {%- for item in sys_content -%}
8
- {%- if item["type"] == "text" -%}
9
- {%- set ns.system_prompt = ns.system_prompt + item["text"] -%}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  {%- endif -%}
11
  {%- endfor -%}
12
- {%- else -%}
13
- {%- set ns.system_prompt = sys_content -%}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  {%- endif -%}
15
  {%- set messages = messages[1:] -%}
16
  {%- endif -%}
17
  {%- if tools -%}
18
- {%- set ns.system_prompt = ns.system_prompt + ("\n" if ns.system_prompt else "") + "List of tools: [" -%}
19
- {%- for tool in tools -%}
20
- {%- if tool is not string -%}
21
- {%- set tool = tool | tojson -%}
22
- {%- endif -%}
23
- {%- set ns.system_prompt = ns.system_prompt + tool -%}
24
- {%- if not loop.last -%}
25
- {%- set ns.system_prompt = ns.system_prompt + ", " -%}
26
- {%- endif -%}
27
- {%- endfor -%}
28
- {%- set ns.system_prompt = ns.system_prompt + "]" -%}
29
  {%- endif -%}
30
  {%- if ns.system_prompt -%}
31
  {{- "<|im_start|>system\n" + ns.system_prompt + "<|im_end|>\n" -}}
32
  {%- endif -%}
33
- {%- set ns.last_assistant_index = -1 -%}
34
  {%- for message in messages -%}
35
  {%- if message["role"] == "assistant" -%}
36
  {%- set ns.last_assistant_index = loop.index0 -%}
37
  {%- endif -%}
38
  {%- endfor -%}
39
  {%- for message in messages -%}
40
- {{- "<|im_start|>" + message["role"] + "\n" -}}
41
- {%- set content = message["content"] -%}
42
- {%- if content is not string -%}
43
- {%- set ns.content = "" -%}
44
- {%- for item in content -%}
45
- {%- if item["type"] == "image" -%}
46
- {%- set ns.content = ns.content + "<image>" -%}
47
- {%- elif item["type"] == "text" -%}
48
- {%- set ns.content = ns.content + item["text"] -%}
 
 
 
 
 
 
 
 
 
49
  {%- else -%}
50
- {%- set ns.content = ns.content + item | tojson -%}
51
  {%- endif -%}
52
- {%- endfor -%}
53
- {%- set content = ns.content -%}
54
- {%- endif -%}
55
- {%- if message["role"] == "assistant" and not keep_past_thinking and loop.index0 != ns.last_assistant_index -%}
56
- {%- if "</think>" in content -%}
57
- {%- set content = content.split("</think>")[-1] | trim -%}
58
  {%- endif -%}
59
- {%- endif -%}
60
- {{- content + "<|im_end|>\n" -}}
 
 
 
 
 
 
 
 
 
 
 
 
61
  {%- endfor -%}
62
  {%- if add_generation_prompt -%}
63
  {{- "<|im_start|>assistant\n" -}}
64
- {%- endif -%}
 
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 parse_content(content) -%}
13
+ {%- if content is string -%}
14
+ {{- content -}}
15
+ {%- else -%}
16
+ {%- set _ns = namespace(result="") -%}
17
+ {%- for item in content -%}
18
+ {%- if item["type"] == "image" -%}
19
+ {%- set _ns.result = _ns.result + "<image>" -%}
20
+ {%- elif item["type"] == "text" -%}
21
+ {%- set _ns.result = _ns.result + item["text"] -%}
22
+ {%- else -%}
23
+ {%- set _ns.result = _ns.result + item | tojson -%}
24
  {%- endif -%}
25
  {%- endfor -%}
26
+ {{- _ns.result -}}
27
+ {%- endif -%}
28
+ {%- endmacro -%}
29
+ {%- macro render_tool_calls(tool_calls) -%}
30
+ {%- set tool_calls_ns = namespace(tool_calls=[]) -%}
31
+ {%- for tool_call in tool_calls -%}
32
+ {%- set func_name = tool_call["function"]["name"] -%}
33
+ {%- set func_args = tool_call["function"]["arguments"] -%}
34
+ {%- set args_ns = namespace(arg_strings=[]) -%}
35
+ {%- for arg_name, arg_value in func_args.items() -%}
36
+ {%- set args_ns.arg_strings = args_ns.arg_strings + [arg_name + "=" + format_arg_value(arg_value)] -%}
37
+ {%- endfor -%}
38
+ {%- set tool_calls_ns.tool_calls = tool_calls_ns.tool_calls + [func_name + "(" + (args_ns.arg_strings | join(", ")) + ")"] -%}
39
+ {%- endfor -%}
40
+ {{- "<|tool_call_start|>[" + (tool_calls_ns.tool_calls | join(", ")) + "]<|tool_call_end|>" -}}
41
+ {%- endmacro -%}
42
+ {%- set ns = namespace(system_prompt="", last_assistant_index=-1) -%}
43
+ {%- if messages[0]["role"] == "system" -%}
44
+ {%- if messages[0].get("content") -%}
45
+ {%- set ns.system_prompt = parse_content(messages[0]["content"]) -%}
46
  {%- endif -%}
47
  {%- set messages = messages[1:] -%}
48
  {%- endif -%}
49
  {%- if tools -%}
50
+ {%- set ns.system_prompt = ns.system_prompt + ("\n\n" if ns.system_prompt else "") + "Today's date: " + strftime_now("%Y-%m-%d") + "\n\nList of tools: " + (tools | tojson) -%}
 
 
 
 
 
 
 
 
 
 
51
  {%- endif -%}
52
  {%- if ns.system_prompt -%}
53
  {{- "<|im_start|>system\n" + ns.system_prompt + "<|im_end|>\n" -}}
54
  {%- endif -%}
 
55
  {%- for message in messages -%}
56
  {%- if message["role"] == "assistant" -%}
57
  {%- set ns.last_assistant_index = loop.index0 -%}
58
  {%- endif -%}
59
  {%- endfor -%}
60
  {%- for message in messages -%}
61
+ {{- "<|im_start|>" + message.role + "\n" -}}
62
+ {%- if message.role == "assistant" -%}
63
+ {%- generation -%}
64
+ {%- if message.thinking is defined and (keep_past_thinking or loop.index0 == ns.last_assistant_index) -%}
65
+ {{- "<think>" + message.thinking + "</think>" -}}
66
+ {%- endif -%}
67
+ {%- set _cfm_tag = "CONTINUE_FINAL_MESSAGE_TAG " -%}
68
+ {%- set _has_cfm = false -%}
69
+ {%- if message.content is defined -%}
70
+ {%- set content = parse_content(message.content) -%}
71
+ {%- if not keep_past_thinking and loop.index0 != ns.last_assistant_index -%}
72
+ {%- if "</think>" in content -%}
73
+ {%- set content = content.split("</think>")[-1] | trim -%}
74
+ {%- endif -%}
75
+ {%- endif -%}
76
+ {%- if message.tool_calls is defined and content.endswith(_cfm_tag) -%}
77
+ {%- set _has_cfm = true -%}
78
+ {{- content[:-(_cfm_tag | length)] -}}
79
  {%- else -%}
80
+ {{- content -}}
81
  {%- endif -%}
 
 
 
 
 
 
82
  {%- endif -%}
83
+ {%- if message.tool_calls is defined -%}
84
+ {{- render_tool_calls(message.tool_calls) -}}
85
+ {%- endif -%}
86
+ {%- if _has_cfm -%}
87
+ {{- _cfm_tag -}}
88
+ {%- endif -%}
89
+ {{- "<|im_end|>\n" -}}
90
+ {%- endgeneration -%}
91
+ {%- else %}
92
+ {%- if message.get("content") -%}
93
+ {{- parse_content(message["content"]) -}}
94
+ {%- endif -%}
95
+ {{- "<|im_end|>\n" -}}
96
+ {%- endif %}
97
  {%- endfor -%}
98
  {%- if add_generation_prompt -%}
99
  {{- "<|im_start|>assistant\n" -}}
100
+ {%- endif -%}