danielhanchen commited on
Commit
844d7a5
·
verified ·
1 Parent(s): dcd9737

Add developer role + tool-call fixes to chat_template.jinja (thinking ON default)

Browse files
Files changed (1) hide show
  1. chat_template.jinja +22 -19
chat_template.jinja CHANGED
@@ -42,6 +42,19 @@
42
  {%- if not messages %}
43
  {{- raise_exception('No messages provided.') }}
44
  {%- endif %}
 
 
 
 
 
 
 
 
 
 
 
 
 
45
  {%- if tools and tools is iterable and tools is not mapping %}
46
  {{- '<|im_start|>system\n' }}
47
  {{- "# Tools\n\nYou have access to the following functions:\n\n<tools>" }}
@@ -51,17 +64,13 @@
51
  {%- endfor %}
52
  {{- "\n</tools>" }}
53
  {{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n<tool_call>\n<function=example_function_name>\n<parameter=example_parameter_1>\nvalue_1\n</parameter>\n<parameter=example_parameter_2>\nThis is the value for the second parameter\nthat can span\nmultiple lines\n</parameter>\n</function>\n</tool_call>\n\n<IMPORTANT>\nReminder:\n- Function calls MUST follow the specified format: an inner <function=...></function> block must be nested within <tool_call></tool_call> XML tags\n- Required parameters MUST be specified\n- You may provide optional reasoning for your function call in natural language BEFORE the function call, but NOT after\n- If there is no function call available, answer the question like normal with your current knowledge and do not tell the user about function calls\n</IMPORTANT>' }}
54
- {%- if messages[0].role == 'system' %}
55
- {%- set content = render_content(messages[0].content, false, true)|trim %}
56
- {%- if content %}
57
- {{- '\n\n' + content }}
58
- {%- endif %}
59
  {%- endif %}
60
  {{- '<|im_end|>\n' }}
61
  {%- else %}
62
- {%- if messages[0].role == 'system' %}
63
- {%- set content = render_content(messages[0].content, false, true)|trim %}
64
- {{- '<|im_start|>system\n' + content + '<|im_end|>\n' }}
65
  {%- endif %}
66
  {%- endif %}
67
  {%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
@@ -75,16 +84,10 @@
75
  {%- endif %}
76
  {%- endif %}
77
  {%- endfor %}
78
- {%- if ns.multi_step_tool %}
79
- {{- raise_exception('No user query found in messages.') }}
80
- {%- endif %}
81
  {%- for message in messages %}
 
82
  {%- set content = render_content(message.content, true)|trim %}
83
- {%- if message.role == "system" %}
84
- {%- if not loop.first %}
85
- {{- raise_exception('System message must be at the beginning.') }}
86
- {%- endif %}
87
- {%- elif message.role == "user" %}
88
  {{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>' + '\n' }}
89
  {%- elif message.role == "assistant" %}
90
  {%- set reasoning_content = '' %}
@@ -141,8 +144,7 @@
141
  {%- elif loop.last %}
142
  {{- '<|im_end|>\n' }}
143
  {%- endif %}
144
- {%- else %}
145
- {{- raise_exception('Unexpected message role.') }}
146
  {%- endif %}
147
  {%- endfor %}
148
  {%- if add_generation_prompt %}
@@ -152,4 +154,5 @@
152
  {%- else %}
153
  {{- '<think>\n' }}
154
  {%- endif %}
155
- {%- endif %}
 
 
42
  {%- if not messages %}
43
  {{- raise_exception('No messages provided.') }}
44
  {%- endif %}
45
+ {%- set num_sys = 0 %}
46
+ {%- set merged_system = '' %}
47
+ {%- if messages[0].role == 'system' or messages[0].role == 'developer' %}
48
+ {%- set first = render_content(messages[0].content, false, true)|trim %}
49
+ {%- if messages|length > 1 and (messages[1].role == 'system' or messages[1].role == 'developer') %}
50
+ {%- set second = render_content(messages[1].content, false, true)|trim %}
51
+ {%- set merged_system = first + '\n' + second %}
52
+ {%- set num_sys = 2 %}
53
+ {%- else %}
54
+ {%- set merged_system = first %}
55
+ {%- set num_sys = 1 %}
56
+ {%- endif %}
57
+ {%- endif %}
58
  {%- if tools and tools is iterable and tools is not mapping %}
59
  {{- '<|im_start|>system\n' }}
60
  {{- "# Tools\n\nYou have access to the following functions:\n\n<tools>" }}
 
64
  {%- endfor %}
65
  {{- "\n</tools>" }}
66
  {{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n<tool_call>\n<function=example_function_name>\n<parameter=example_parameter_1>\nvalue_1\n</parameter>\n<parameter=example_parameter_2>\nThis is the value for the second parameter\nthat can span\nmultiple lines\n</parameter>\n</function>\n</tool_call>\n\n<IMPORTANT>\nReminder:\n- Function calls MUST follow the specified format: an inner <function=...></function> block must be nested within <tool_call></tool_call> XML tags\n- Required parameters MUST be specified\n- You may provide optional reasoning for your function call in natural language BEFORE the function call, but NOT after\n- If there is no function call available, answer the question like normal with your current knowledge and do not tell the user about function calls\n</IMPORTANT>' }}
67
+ {%- if merged_system %}
68
+ {{- '\n\n' + merged_system }}
 
 
 
69
  {%- endif %}
70
  {{- '<|im_end|>\n' }}
71
  {%- else %}
72
+ {%- if merged_system %}
73
+ {{- '<|im_start|>system\n' + merged_system + '<|im_end|>\n' }}
 
74
  {%- endif %}
75
  {%- endif %}
76
  {%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
 
84
  {%- endif %}
85
  {%- endif %}
86
  {%- endfor %}
 
 
 
87
  {%- for message in messages %}
88
+ {%- if loop.index0 >= num_sys and message.role != "system" and message.role != "developer" %}
89
  {%- set content = render_content(message.content, true)|trim %}
90
+ {%- if message.role == "user" %}
 
 
 
 
91
  {{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>' + '\n' }}
92
  {%- elif message.role == "assistant" %}
93
  {%- set reasoning_content = '' %}
 
144
  {%- elif loop.last %}
145
  {{- '<|im_end|>\n' }}
146
  {%- endif %}
147
+ {%- endif %}
 
148
  {%- endif %}
149
  {%- endfor %}
150
  {%- if add_generation_prompt %}
 
154
  {%- else %}
155
  {{- '<think>\n' }}
156
  {%- endif %}
157
+ {%- endif %}
158
+ {#- Unsloth fixes - developer role, tool calling #}