KoarAI commited on
Commit
dbeb0b4
·
verified ·
1 Parent(s): 0a0e4be

Upload chat_template.jinja with huggingface_hub

Browse files
Files changed (1) hide show
  1. chat_template.jinja +122 -125
chat_template.jinja CHANGED
@@ -1,125 +1,122 @@
1
- {{- bos_token -}}
2
- {%- set preserve_thinking = preserve_thinking | default(keep_past_thinking | default(false)) -%}
3
-
4
- {%- macro format_arg_value(arg_value) -%}
5
- {%- if arg_value is string -%}
6
- {{- "'" + (arg_value | replace("\\", "\\\\") | replace("'", "\\'") | replace("\n", "\\n") | replace("\r", "\\r")) + "'" -}}
7
- {%- elif arg_value is mapping or arg_value is iterable -%}
8
- {{- arg_value | tojson -}}
9
- {%- else -%}
10
- {{- arg_value | string -}}
11
- {%- endif -%}
12
- {%- endmacro -%}
13
-
14
- {%- macro parse_content(content) -%}
15
- {%- if content is string -%}
16
- {{- content -}}
17
- {%- elif content is mapping -%}
18
- {{- content | tojson -}}
19
- {%- elif content is iterable -%}
20
- {%- set _ns = namespace(result="") -%}
21
- {%- for item in content -%}
22
- {%- if item is string -%}
23
- {%- set _ns.result = _ns.result + item -%}
24
- {%- elif item is mapping and item.get("type") == "image" -%}
25
- {%- set _ns.result = _ns.result + "<image>" -%}
26
- {%- elif item is mapping and item.get("type") == "text" -%}
27
- {%- set _ns.result = _ns.result + ((item.get("text") or "") | string) -%}
28
- {%- else -%}
29
- {%- set _ns.result = _ns.result + (item | tojson) -%}
30
- {%- endif -%}
31
- {%- endfor -%}
32
- {{- _ns.result -}}
33
- {%- endif -%}
34
- {%- endmacro -%}
35
-
36
- {%- macro render_tool_calls(tool_calls) -%}
37
- {%- set tool_calls_ns = namespace(tool_calls=[]) -%}
38
- {%- for tool_call in tool_calls -%}
39
- {%- set func = tool_call["function"] if "function" in tool_call else tool_call -%}
40
- {%- set func_name = func["name"] -%}
41
- {%- set func_args = func.get("arguments") -%}
42
- {%- set args_ns = namespace(arg_strings=[]) -%}
43
- {%- if func_args is mapping -%}
44
- {%- for arg_name, arg_value in func_args.items() -%}
45
- {%- set args_ns.arg_strings = args_ns.arg_strings + [arg_name + "=" + format_arg_value(arg_value)] -%}
46
- {%- endfor -%}
47
- {%- elif func_args is string and (func_args | trim) not in ["", "{}", "null"] -%}
48
- {{- raise_exception("Tool call arguments must be a mapping, got a JSON-encoded string: parse arguments with json.loads() before applying the chat template") -}}
49
- {%- endif -%}
50
- {%- set tool_calls_ns.tool_calls = tool_calls_ns.tool_calls + [func_name + "(" + (args_ns.arg_strings | join(", ")) + ")"] -%}
51
- {%- endfor -%}
52
- {{- "<|tool_call_start|>[" + (tool_calls_ns.tool_calls | join(", ")) + "]<|tool_call_end|>" -}}
53
- {%- endmacro -%}
54
-
55
- {%- set ns = namespace(system_prompt="", last_assistant_index=-1) -%}
56
- {%- if messages and messages[0]["role"] == "system" -%}
57
- {%- if messages[0].get("content") -%}
58
- {%- set ns.system_prompt = parse_content(messages[0]["content"]) -%}
59
- {%- endif -%}
60
- {%- set messages = messages[1:] -%}
61
- {%- endif -%}
62
- {%- if tools -%}
63
- {%- set ns.system_prompt = ns.system_prompt + ("\n" if ns.system_prompt else "") + "List of tools: [" -%}
64
- {%- for tool in tools -%}
65
- {%- if tool is not string -%}
66
- {%- set tool = tool | tojson -%}
67
- {%- endif -%}
68
- {%- set ns.system_prompt = ns.system_prompt + tool -%}
69
- {%- if not loop.last -%}
70
- {%- set ns.system_prompt = ns.system_prompt + ", " -%}
71
- {%- endif -%}
72
- {%- endfor -%}
73
- {%- set ns.system_prompt = ns.system_prompt + "]" -%}
74
- {%- endif -%}
75
- {%- if ns.system_prompt -%}
76
- {{- "<|im_start|>system\n" + ns.system_prompt + "<|im_end|>\n" -}}
77
- {%- endif -%}
78
- {%- for message in messages -%}
79
- {%- if message["role"] == "assistant" -%}
80
- {%- set ns.last_assistant_index = loop.index0 -%}
81
- {%- endif -%}
82
- {%- endfor -%}
83
- {%- for message in messages -%}
84
- {{- "<|im_start|>" + message.role + "\n" -}}
85
- {%- if message.role == "assistant" -%}
86
- {%- generation -%}
87
- {%- set keep_thinking = preserve_thinking or loop.index0 == ns.last_assistant_index -%}
88
- {%- set thinking = message.thinking or message.reasoning or message.reasoning_content -%}
89
- {%- set thinking = thinking if thinking is string else "" -%}
90
- {%- if thinking and keep_thinking -%}
91
- {{- "<think>" + thinking + "</think>" -}}
92
- {%- endif -%}
93
- {%- set _cfm_tag = "CONTINUE_FINAL_MESSAGE_TAG " -%}
94
- {%- set _has_cfm = false -%}
95
- {%- set content = "" -%}
96
- {%- if message.get("content") -%}
97
- {%- set content = parse_content(message.content) -%}
98
- {%- endif -%}
99
- {%- if not keep_thinking and "</think>" in content -%}
100
- {%- set content = content.split("</think>")[-1] | trim -%}
101
- {%- endif -%}
102
- {%- if content.endswith(_cfm_tag) -%}
103
- {%- set _has_cfm = true -%}
104
- {%- set _trunc_len = (content | length) - (_cfm_tag | length) -%}
105
- {%- set content = content[:_trunc_len] -%}
106
- {%- endif -%}
107
- {{- content -}}
108
- {%- if message.tool_calls -%}
109
- {{- render_tool_calls(message.tool_calls) -}}
110
- {%- endif -%}
111
- {%- if _has_cfm -%}
112
- {{- _cfm_tag -}}
113
- {%- endif -%}
114
- {{- "<|im_end|>\n" -}}
115
- {%- endgeneration -%}
116
- {%- else %}
117
- {%- if message.get("content") -%}
118
- {{- parse_content(message["content"]) -}}
119
- {%- endif -%}
120
- {{- "<|im_end|>\n" -}}
121
- {%- endif %}
122
- {%- endfor -%}
123
- {%- if add_generation_prompt -%}
124
- {{- "<|im_start|>assistant\n" -}}
125
- {%- endif -%}
 
1
+ {{- bos_token -}}
2
+ {%- set preserve_thinking = preserve_thinking | default(keep_past_thinking | default(false)) -%}
3
+ {%- macro format_arg_value(arg_value) -%}
4
+ {%- if arg_value is string -%}
5
+ {{- "'" + arg_value | replace("\\", "\\\\") | replace("'", "\\'") | replace("\n", "\\n") | replace("\r", "\\r") + "'" -}}
6
+ {%- elif arg_value is mapping or arg_value is iterable -%}
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
+ {%- elif content is mapping -%}
16
+ {{- content | tojson -}}
17
+ {%- elif content is iterable -%}
18
+ {%- set _ns = namespace(result="") -%}
19
+ {%- for item in content -%}
20
+ {%- if item is string -%}
21
+ {%- set _ns.result = _ns.result + item -%}
22
+ {%- elif item is mapping and item.get("type") == "image" -%}
23
+ {%- set _ns.result = _ns.result + "<image>" -%}
24
+ {%- elif item is mapping and item.get("type") == "text" -%}
25
+ {%- set _ns.result = _ns.result + (item.get("text") or "") | string -%}
26
+ {%- else -%}
27
+ {%- set _ns.result = _ns.result + item | tojson -%}
28
+ {%- endif -%}
29
+ {%- endfor -%}
30
+ {{- _ns.result -}}
31
+ {%- endif -%}
32
+ {%- endmacro -%}
33
+ {%- macro render_tool_calls(tool_calls) -%}
34
+ {%- set tool_calls_ns = namespace(tool_calls=[]) -%}
35
+ {%- for tool_call in tool_calls -%}
36
+ {%- set func = tool_call["function"] if "function" in tool_call else tool_call -%}
37
+ {%- set func_name = func["name"] -%}
38
+ {%- set func_args = func.get("arguments") -%}
39
+ {%- set args_ns = namespace(arg_strings=[]) -%}
40
+ {%- if func_args is mapping -%}
41
+ {%- for (arg_name, arg_value) in func_args.items() -%}
42
+ {%- set args_ns.arg_strings = args_ns.arg_strings + [arg_name + "=" + format_arg_value(arg_value)] -%}
43
+ {%- endfor -%}
44
+ {%- elif func_args is string and func_args | trim not in ["", "{}", "null"] -%}
45
+ {{- raise_exception("Tool call arguments must be a mapping, got a JSON-encoded string: parse arguments with json.loads() before applying the chat template") -}}
46
+ {%- endif -%}
47
+ {%- set tool_calls_ns.tool_calls = tool_calls_ns.tool_calls + [func_name + "(" + args_ns.arg_strings | join(", ") + ")"] -%}
48
+ {%- endfor -%}
49
+ {{- "<|tool_call_start|>[" + tool_calls_ns.tool_calls | join(", ") + "]<|tool_call_end|>" -}}
50
+ {%- endmacro -%}
51
+ {%- set ns = namespace(system_prompt="", last_assistant_index=-1) -%}
52
+ {%- if messages and messages[0]["role"] == "system" -%}
53
+ {%- if messages[0].get("content") -%}
54
+ {%- set ns.system_prompt = parse_content(messages[0]["content"]) -%}
55
+ {%- endif -%}
56
+ {%- set messages = messages[1:] -%}
57
+ {%- endif -%}
58
+ {%- if tools -%}
59
+ {%- set ns.system_prompt = ns.system_prompt + ("\n" if ns.system_prompt else "") + "List of tools: [" -%}
60
+ {%- for tool in tools -%}
61
+ {%- if tool is not string -%}
62
+ {%- set tool = tool | tojson -%}
63
+ {%- endif -%}
64
+ {%- set ns.system_prompt = ns.system_prompt + tool -%}
65
+ {%- if not loop.last -%}
66
+ {%- set ns.system_prompt = ns.system_prompt + ", " -%}
67
+ {%- endif -%}
68
+ {%- endfor -%}
69
+ {%- set ns.system_prompt = ns.system_prompt + "]" -%}
70
+ {%- endif -%}
71
+ {%- if ns.system_prompt -%}
72
+ {{- "<|im_start|>system\n" + ns.system_prompt + "<|im_end|>\n" -}}
73
+ {%- endif -%}
74
+ {%- for message in messages -%}
75
+ {%- if message["role"] == "assistant" -%}
76
+ {%- set ns.last_assistant_index = loop.index0 -%}
77
+ {%- endif -%}
78
+ {%- endfor -%}
79
+ {%- for message in messages -%}
80
+ {{- "<|im_start|>" + message.role + "\n" -}}
81
+ {%- if message.role == "assistant" -%}
82
+ {%- set keep_thinking = preserve_thinking or loop.index0 == ns.last_assistant_index -%}
83
+ {%- set thinking = message.thinking or message.reasoning or message.reasoning_content -%}
84
+ {%- set thinking = thinking if thinking is string else "" -%}
85
+ {%- if thinking and keep_thinking -%}
86
+ {{- "<think>" + thinking + "</think>" -}}
87
+ {%- endif -%}
88
+ {%- set _cfm_tag = "CONTINUE_FINAL_MESSAGE_TAG " -%}
89
+ {%- set _has_cfm = false -%}
90
+ {%- set content = "" -%}
91
+ {%- if message.get("content") -%}
92
+ {%- set content = parse_content(message.content) -%}
93
+ {%- endif -%}
94
+ {%- if not keep_thinking and "</think>" in content -%}
95
+ {%- set content = content.split("</think>")[-1] | trim -%}
96
+ {%- endif -%}
97
+ {%- if content.endswith(_cfm_tag) -%}
98
+ {%- set _has_cfm = true -%}
99
+ {%- set _trunc_len = content | length - _cfm_tag | length -%}
100
+ {%- set content = content[:_trunc_len] -%}
101
+ {%- endif -%}
102
+ {{- content -}}
103
+ {%- if message.tool_calls -%}
104
+ {{- render_tool_calls(message.tool_calls) -}}
105
+ {%- endif -%}
106
+ {%- if _has_cfm -%}
107
+ {{- _cfm_tag -}}
108
+ {%- endif -%}
109
+ {{- "<|im_end|>\n" -}}
110
+ {%- else -%}
111
+ {%- if message.get("content") -%}
112
+ {{- parse_content(message["content"]) -}}
113
+ {%- endif -%}
114
+ {{- "<|im_end|>\n" -}}
115
+ {%- endif -%}
116
+ {%- endfor -%}
117
+ {%- if add_generation_prompt -%}
118
+ {{- "<|im_start|>assistant\n" -}}
119
+ {%- if enable_thinking is not defined or enable_thinking -%}
120
+ {{- "<think>\n" -}}
121
+ {%- endif -%}
122
+ {%- endif -%}