OliviaRossi commited on
Commit
aaaa2bf
·
verified ·
1 Parent(s): b94d17d

Enable reasoning-effort tiers and preserve-thinking in chat_template.jinja

Browse files
Files changed (1) hide show
  1. chat_template.jinja +66 -4
chat_template.jinja CHANGED
@@ -46,10 +46,51 @@
46
  {{- raise_exception('Unexpected content type.') }}
47
  {%- endif %}
48
  {%- endmacro %}
 
49
  {%- if not messages %}
50
  {{- raise_exception('No messages provided.') }}
51
  {%- endif %}
52
- {%- if tools and tools is iterable and tools is not mapping %}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  {{- '<|im_start|>system\n' }}
54
  {{- "# Tools\n\nYou have access to the following functions:\n\n<tools>" }}
55
  {%- for tool in tools %}
@@ -64,13 +105,23 @@
64
  {{- '\n\n' + content }}
65
  {%- endif %}
66
  {%- endif %}
 
 
 
67
  {{- '<|im_end|>\n' }}
68
  {%- else %}
69
  {%- if messages[0].role == 'system' %}
70
  {%- set content = render_content(messages[0].content, false, true)|trim %}
 
 
 
71
  {{- '<|im_start|>system\n' + content + '<|im_end|>\n' }}
 
 
72
  {%- endif %}
73
  {%- endif %}
 
 
74
  {%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
75
  {%- for message in messages[::-1] %}
76
  {%- set index = (messages|length - 1) - loop.index0 %}
@@ -85,6 +136,7 @@
85
  {%- if ns.multi_step_tool %}
86
  {{- raise_exception('No user query found in messages.') }}
87
  {%- endif %}
 
88
  {%- for message in messages %}
89
  {%- set content = render_content(message.content, true)|trim %}
90
  {%- if message.role == "system" %}
@@ -104,11 +156,19 @@
104
  {%- endif %}
105
  {%- endif %}
106
  {%- set reasoning_content = reasoning_content|trim %}
107
- {%- if loop.index0 > ns.last_query_index %}
108
- {{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content + '\n</think>\n\n' + content }}
 
 
 
 
 
 
109
  {%- else %}
110
  {{- '<|im_start|>' + message.role + '\n' + content }}
111
  {%- endif %}
 
 
112
  {%- if message.tool_calls and message.tool_calls is iterable and message.tool_calls is not mapping %}
113
  {%- for tool_call in message.tool_calls %}
114
  {%- if tool_call.function is defined %}
@@ -151,9 +211,11 @@
151
  {{- raise_exception('Unexpected message role: ' ~ message.role) }}
152
  {%- endif %}
153
  {%- endfor %}
 
 
154
  {%- if add_generation_prompt %}
155
  {{- '<|im_start|>assistant\n' }}
156
- {%- if enable_thinking is defined and enable_thinking is false %}
157
  {{- '<think>\n\n</think>\n\n' }}
158
  {%- else %}
159
  {{- '<think>\n' }}
 
46
  {{- raise_exception('Unexpected content type.') }}
47
  {%- endif %}
48
  {%- endmacro %}
49
+
50
  {%- if not messages %}
51
  {{- raise_exception('No messages provided.') }}
52
  {%- endif %}
53
+
54
+ {# --- 1. REASONING EFFORT RESOLUTION & PRESERVE-THINKING DISCOVERY --- #}
55
+ {%- if preserve_reasoning is defined and preserve_reasoning is not none %}
56
+ {%- set _preserve_thinking = preserve_reasoning %}
57
+ {%- elif preserve_thinking is defined and preserve_thinking is not none %}
58
+ {%- set _preserve_thinking = preserve_thinking %}
59
+ {%- else %}
60
+ {%- set _preserve_thinking = true %}
61
+ {%- endif %}
62
+
63
+ {%- set _raw_effort = (reasoning_effort | string | lower) if reasoning_effort is defined and reasoning_effort is not none else ((thinking_effort | string | lower) if thinking_effort is defined and thinking_effort is not none else 'medium') %}
64
+ {%- set _enable_thinking = true %}
65
+ {%- if enable_thinking is defined and enable_thinking is false %}
66
+ {%- set _enable_thinking = false %}
67
+ {%- elif _raw_effort in ['none', 'off', 'false', '0'] %}
68
+ {%- set _enable_thinking = false %}
69
+ {%- endif %}
70
+
71
+ {%- set _resolved_effort = 'medium' %}
72
+ {%- if _raw_effort in ['minimal', 'min', 'low', '1'] %}
73
+ {%- set _resolved_effort = 'low' %}
74
+ {%- elif _raw_effort in ['high', '3'] %}
75
+ {%- set _resolved_effort = 'high' %}
76
+ {%- elif _raw_effort in ['xhigh', 'max', 'extreme', 'ultracode', '4'] %}
77
+ {%- set _resolved_effort = 'xhigh' %}
78
+ {%- endif %}
79
+
80
+ {%- set _effort_prompt = '' %}
81
+ {%- if _enable_thinking %}
82
+ {%- if _resolved_effort == 'low' %}
83
+ {%- set _effort_prompt = 'Reasoning effort is set to low. Keep your thinking concise, efficient, and direct to the point.' %}
84
+ {%- elif _resolved_effort == 'high' %}
85
+ {%- set _effort_prompt = 'Reasoning effort is set to high. Think thoroughly through all steps, verify logic, and test potential edge cases.' %}
86
+ {%- elif _resolved_effort == 'xhigh' %}
87
+ {%- set _effort_prompt = 'Reasoning effort is set to xhigh. Please think exhaustively through the task, systematically exploring alternative hypotheses and proving edge cases before answering.' %}
88
+ {%- endif %}
89
+ {%- endif %}
90
+
91
+ {# --- 2. SYSTEM PROMPT & TOOL DECLARATIONS --- #}
92
+ {%- set _has_tools = (tools and tools is iterable and tools is not mapping) %}
93
+ {%- if _has_tools %}
94
  {{- '<|im_start|>system\n' }}
95
  {{- "# Tools\n\nYou have access to the following functions:\n\n<tools>" }}
96
  {%- for tool in tools %}
 
105
  {{- '\n\n' + content }}
106
  {%- endif %}
107
  {%- endif %}
108
+ {%- if _effort_prompt %}
109
+ {{- '\n\n' + _effort_prompt }}
110
+ {%- endif %}
111
  {{- '<|im_end|>\n' }}
112
  {%- else %}
113
  {%- if messages[0].role == 'system' %}
114
  {%- set content = render_content(messages[0].content, false, true)|trim %}
115
+ {%- if _effort_prompt %}
116
+ {%- set content = content + ('\n\n' + _effort_prompt if content else _effort_prompt) %}
117
+ {%- endif %}
118
  {{- '<|im_start|>system\n' + content + '<|im_end|>\n' }}
119
+ {%- elif _effort_prompt %}
120
+ {{- '<|im_start|>system\n' + _effort_prompt + '<|im_end|>\n' }}
121
  {%- endif %}
122
  {%- endif %}
123
+
124
+ {# --- 3. MULTI-TURN MESSAGE EXTRACTION & REASONING PRESERVATION --- #}
125
  {%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
126
  {%- for message in messages[::-1] %}
127
  {%- set index = (messages|length - 1) - loop.index0 %}
 
136
  {%- if ns.multi_step_tool %}
137
  {{- raise_exception('No user query found in messages.') }}
138
  {%- endif %}
139
+
140
  {%- for message in messages %}
141
  {%- set content = render_content(message.content, true)|trim %}
142
  {%- if message.role == "system" %}
 
156
  {%- endif %}
157
  {%- endif %}
158
  {%- set reasoning_content = reasoning_content|trim %}
159
+
160
+ {# Honor preserve_thinking: keep prior thoughts if true, strip older thoughts if false #}
161
+ {%- if _preserve_thinking or loop.index0 > ns.last_query_index %}
162
+ {%- if reasoning_content %}
163
+ {{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content + '\n</think>\n\n' + content }}
164
+ {%- else %}
165
+ {{- '<|im_start|>' + message.role + '\n' + content }}
166
+ {%- endif %}
167
  {%- else %}
168
  {{- '<|im_start|>' + message.role + '\n' + content }}
169
  {%- endif %}
170
+
171
+ {# Function call serialization #}
172
  {%- if message.tool_calls and message.tool_calls is iterable and message.tool_calls is not mapping %}
173
  {%- for tool_call in message.tool_calls %}
174
  {%- if tool_call.function is defined %}
 
211
  {{- raise_exception('Unexpected message role: ' ~ message.role) }}
212
  {%- endif %}
213
  {%- endfor %}
214
+
215
+ {# --- 4. GENERATION PROMPT PREFILL --- #}
216
  {%- if add_generation_prompt %}
217
  {{- '<|im_start|>assistant\n' }}
218
+ {%- if not _enable_thinking %}
219
  {{- '<think>\n\n</think>\n\n' }}
220
  {%- else %}
221
  {{- '<think>\n' }}