itopoly-inc commited on
Commit
15d9b5e
·
verified ·
1 Parent(s): e62f287

Upload g9v3_chat_template_low.jinja with huggingface_hub

Browse files
Files changed (1) hide show
  1. g9v3_chat_template_low.jinja +203 -0
g9v3_chat_template_low.jinja ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {{- bos_token }}{%- set resolved_reasoning_effort = reasoning_effort|default('low') %}
2
+ {%- set reasoning_instructions = '' %}
3
+ {%- if enable_thinking is not defined or enable_thinking is true %}
4
+ {%- if resolved_reasoning_effort == 'low' %}
5
+ {%- set reasoning_instructions = 'Reasoning effort is set to low. Keep your thinking brief and focused, moving directly to the conclusion without unnecessary elaboration.' %}
6
+ {%- elif resolved_reasoning_effort == 'medium' %}
7
+ {%- set reasoning_instructions = 'Reasoning effort is set to medium. Think through the task with a moderate amount of reasoning before answering.' %}
8
+ {%- elif resolved_reasoning_effort == 'xhigh' %}
9
+ {%- set reasoning_instructions = 'Reasoning effort is set to xhigh. Please think carefully through the task, validate key assumptions, consider plausible alternatives, and prioritize correctness, consistency, and clarity in the final answer.' %}
10
+ {%- endif %}
11
+ {%- endif %}
12
+ {%- set tone_instructions = 'Tone and formatting: respond like a friendly, knowledgeable colleague. Use warm, complete sentences in short readable paragraphs. Be concise but never clipped or robotic. Avoid bullet lists and headings unless the user asks for steps or the content is genuinely a list.' %}
13
+ {%- set reasoning_instructions = ((reasoning_instructions ~ '\n\n') if reasoning_instructions else '') ~ tone_instructions %}
14
+ {%- if tools %}
15
+ {%- set tool_definitions %}
16
+ {{- "# Tools\n\nYou are provided with function signatures within <tools></tools> XML tags:\n<tools>" }}
17
+ {%- for tool in tools %}
18
+ {{- "\n" }}
19
+ {{- tool | tojson(ensure_ascii=False) }}
20
+ {%- endfor %}
21
+ {{- '\n</tools>\n\nTool usage guidelines:\n- You may call zero or more functions. If no function calls are needed, just answer normally and do not include any <function ... </function>.\n- When calling a function, return an XML object within <function ... </function> using:\n<function name="function-name"><param name="param-name">param-value</param></function>\n- param-value may be multi-line. If it contains <, & or newline characters, wrap it in a CDATA block: <param name="param-name"><![CDATA[...multi-line value...]]></param>' }}
22
+ {%- endset %}
23
+
24
+ {{- '<|im_start|>system\n' }}
25
+ {%- if messages[0].role == 'system' %}
26
+ {%- if '<tool_def_sep>' in messages[0].content %}
27
+ {{- (reasoning_instructions + '\n\n' if reasoning_instructions else '') + messages[0].content.replace('<tool_def_sep>', tool_definitions) }}
28
+ {%- else %}
29
+ {{- (reasoning_instructions + '\n\n' if reasoning_instructions else '') + messages[0].content + '\n\n' + tool_definitions }}
30
+ {%- endif %}
31
+ {%- else %}
32
+ {{- (reasoning_instructions + '\n\n' if reasoning_instructions else '') + tool_definitions.lstrip() }}
33
+ {%- endif %}
34
+ {{- '<|im_end|>\n' }}
35
+ {%- else %}
36
+ {%- if messages[0].role == 'system' %}
37
+ {{- '<|im_start|>system\n' + (reasoning_instructions + '\n\n' if reasoning_instructions else '') + messages[0].content + '<|im_end|>\n' }}
38
+ {%- elif reasoning_instructions %}
39
+ {{- '<|im_start|>system\n' + reasoning_instructions + '<|im_end|>\n' }}
40
+ {%- endif %}
41
+ {%- endif %}
42
+ {%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
43
+ {%- for message in messages[::-1] %}
44
+ {%- set index = (messages|length - 1) - loop.index0 %}
45
+ {%- if ns.multi_step_tool and message.role == "user" and message.content is string and not(message.content.startswith('<tool_response>') and message.content.endswith('</tool_response>')) %}
46
+ {%- set ns.multi_step_tool = false %}
47
+ {%- set ns.last_query_index = index %}
48
+ {%- endif %}
49
+ {%- endfor %}
50
+ {%- for message in messages %}
51
+ {%- if message.content is string %}
52
+ {%- set content = message.content %}
53
+ {%- else %}
54
+ {%- set content = '' %}
55
+ {%- endif %}
56
+ {%- if (message.role == "user") or (message.role == "system" and not loop.first) %}
57
+ {{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>' + '\n' }}
58
+ {%- elif message.role == "assistant" %}
59
+ {%- set tool_calls_norm = message.tool_calls or ([{'function': message.function_call}] if message.function_call is defined and message.function_call else []) %}
60
+ {%- set reasoning_content = '' %}
61
+ {%- if message.reasoning_content is string %}
62
+ {%- set reasoning_content = message.reasoning_content %}
63
+ {%- else %}
64
+ {%- if '</think>' in content %}
65
+ {%- set reasoning_content = content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %}
66
+ {%- set content = content.split('</think>')[-1].lstrip('\n') %}
67
+ {%- endif %}
68
+ {%- endif %}
69
+
70
+ {%- if tool_calls_norm %}
71
+ {%- set content_parts = content.split('<tool_sep>') %}
72
+ {%- set processed_content = content_parts[0] %}
73
+ {%- set tool_calls_count = tool_calls_norm|length %}
74
+ {%- set tool_sep_count = content_parts|length - 1 %}
75
+ {%- set min_count = [tool_calls_count, tool_sep_count]|min %}
76
+
77
+ {%- for i in range(1, content_parts|length) %}
78
+ {%- set tool_index = i - 1 %}
79
+ {%- if tool_index < tool_calls_count %}
80
+ {%- set tool_call = tool_calls_norm[tool_index] %}
81
+ {%- if tool_call.function %}
82
+ {%- set tool_call = tool_call.function %}
83
+ {%- endif %}
84
+ {%- set single_tool_xml %}
85
+ {{- '<function name="' ~ tool_call.name ~ '">' }}
86
+ {%- if tool_call.arguments %}
87
+ {%- set args_dict = tool_call.arguments %}
88
+ {%- for param_name, param_value in args_dict.items() %}
89
+ {{- '<param name="' ~ param_name ~ '">' }}
90
+ {%- if param_value is string and ('<' in param_value or '&' in param_value or '\n' in param_value) %}
91
+ {{- '<![CDATA[' + param_value + ']]>' }}
92
+ {%- else %}
93
+ {{- param_value }}
94
+ {%- endif %}
95
+ {{- '</param>' }}
96
+ {%- endfor %}
97
+ {%- endif %}
98
+ {{- '</function>' }}
99
+ {%- endset %}
100
+ {%- set processed_content = processed_content + single_tool_xml + content_parts[i] %}
101
+ {%- else %}
102
+ {%- set processed_content = processed_content + content_parts[i] %}
103
+ {%- endif %}
104
+ {%- endfor %}
105
+
106
+ {%- if tool_calls_count > tool_sep_count %}
107
+ {%- for remaining_index in range(tool_sep_count, tool_calls_count) %}
108
+ {%- set tool_call = tool_calls_norm[remaining_index] %}
109
+ {%- if tool_call.function %}
110
+ {%- set tool_call = tool_call.function %}
111
+ {%- endif %}
112
+ {%- set remaining_tool_xml %}
113
+ {{- '<function name="' ~ tool_call.name ~ '">' }}
114
+ {%- if tool_call.arguments %}
115
+ {%- if tool_call.arguments is mapping %}
116
+ {%- set args_dict = tool_call.arguments %}
117
+ {%- for param_name, param_value in args_dict.items() %}
118
+ {{- '<param name="' ~ param_name ~ '">' }}
119
+ {%- if param_value is string and ('<' in param_value or '&' in param_value or '\n' in param_value) %}
120
+ {{- '<![CDATA[' + param_value + ']]>' }}
121
+ {%- else %}
122
+ {{- param_value }}
123
+ {%- endif %}
124
+ {{- '</param>' }}
125
+ {%- endfor %}
126
+ {%- else %}
127
+ {#- legacy function_call: arguments arrives as a JSON string; emit verbatim #}
128
+ {{- tool_call.arguments }}
129
+ {%- endif %}
130
+ {%- endif %}
131
+ {{- '</function>' }}
132
+ {%- endset %}
133
+ {%- set processed_content = processed_content + remaining_tool_xml %}
134
+ {%- endfor %}
135
+ {%- endif %}
136
+
137
+ {%- set content = processed_content %}
138
+ {%- endif %}
139
+
140
+ {%- if reasoning_content %}
141
+ {{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content.strip('\n') + '\n</think>\n\n' + content.lstrip('\n') }}
142
+ {%- elif '<think>' not in content and '</think>' not in content %}
143
+ {{- '<|im_start|>' + message.role + '\n<think>\n\n</think>\n\n' + content.lstrip('\n') }}
144
+ {%- else %}
145
+ {{- '<|im_start|>' + message.role + '\n' + content }}
146
+ {%- endif %}
147
+
148
+ {%- if tool_calls_norm and not has_tool_sep %}
149
+ {%- for tool_call in tool_calls_norm %}
150
+ {%- if (loop.first and content) or (not loop.first) %}
151
+ {{- '\n' }}
152
+ {%- endif %}
153
+ {%- if tool_call.function %}
154
+ {%- set tool_call = tool_call.function %}
155
+ {%- endif %}
156
+ {{- '<function name="' ~ tool_call.name ~ '">' }}
157
+ {%- if tool_call.arguments %}
158
+ {%- if tool_call.arguments is mapping %}
159
+ {%- set args_dict = tool_call.arguments %}
160
+ {%- for param_name, param_value in args_dict.items() %}
161
+ {{- '<param name="' ~ param_name ~ '">' }}
162
+ {%- if param_value is string and ('<' in param_value or '&' in param_value or '\n' in param_value) %}
163
+ {{- '<![CDATA[' + param_value + ']]>' }}
164
+ {%- else %}
165
+ {{- param_value }}
166
+ {%- endif %}
167
+ {{- '</param>' }}
168
+ {%- endfor %}
169
+ {%- else %}
170
+ {#- legacy function_call: arguments arrives as a JSON string; emit verbatim #}
171
+ {{- tool_call.arguments }}
172
+ {%- endif %}
173
+ {%- endif %}
174
+ {{- '</function>' }}
175
+ {%- endfor %}
176
+ {%- endif %}
177
+ {{- '<|im_end|>\n' }}
178
+ {%- elif message.role == "tool" or message.role == "function" %}
179
+ {%- if loop.first or (messages[loop.index0 - 1].role not in ("tool", "function")) %}
180
+ {{- '<|im_start|>user' }}
181
+ {%- endif %}
182
+ {{- '\n<tool_response>\n' }}
183
+ {%- if message.content is string %}
184
+ {{- content }}
185
+ {%- else %}
186
+ {{- message.content | tojson(ensure_ascii=False) }}
187
+ {%- endif %}
188
+ {{- '\n</tool_response>' }}
189
+ {%- if loop.last or (messages[loop.index0 + 1].role not in ("tool", "function")) %}
190
+ {{- '<|im_end|>\n' }}
191
+ {%- endif %}
192
+ {%- endif %}
193
+ {%- endfor %}
194
+ {%- if add_generation_prompt %}
195
+ {{- '<|im_start|>assistant\n' }}
196
+ {%- if enable_thinking is defined %}
197
+ {%- if enable_thinking is false %}
198
+ {{- '<think>\n\n</think>\n\n' }}
199
+ {%- elif enable_thinking is true %}
200
+ {{- '<think>\n' }}
201
+ {%- endif %}
202
+ {%- endif %}
203
+ {%- endif %}