chat_template.jinja
8.7 KB · 170 lines · plaintext Raw
1 {%- set image_count = namespace(value=0) %}
2 {%- set video_count = namespace(value=0) %}
3 {%- macro render_content(content, do_vision_count, is_system_content=false) %}
4 {%- if content is string %}
5 {{- content }}
6 {%- elif content is iterable and content is not mapping %}
7 {%- for item in content %}
8 {%- if 'image' in item or 'image_url' in item or item.type == 'image' %}
9 {%- if is_system_content %}
10 {{- raise_exception('System message cannot contain images.') }}
11 {%- endif %}
12 {%- if do_vision_count %}
13 {%- set image_count.value = image_count.value + 1 %}
14 {%- endif %}
15 {%- if add_vision_id %}
16 {{- 'Picture ' ~ image_count.value ~ ': ' }}
17 {%- endif %}
18 {{- '<|vision_start|><|image_pad|><|vision_end|>' }}
19 {%- elif 'video' in item or item.type == 'video' %}
20 {%- if is_system_content %}
21 {{- raise_exception('System message cannot contain videos.') }}
22 {%- endif %}
23 {%- if do_vision_count %}
24 {%- set video_count.value = video_count.value + 1 %}
25 {%- endif %}
26 {%- if add_vision_id %}
27 {{- 'Video ' ~ video_count.value ~ ': ' }}
28 {%- endif %}
29 {{- '<|vision_start|><|video_pad|><|vision_end|>' }}
30 {%- elif 'text' in item %}
31 {{- item.text }}
32 {%- else %}
33 {{- raise_exception('Unexpected item type in content.') }}
34 {%- endif %}
35 {%- endfor %}
36 {%- elif content is none or content is undefined %}
37 {{- '' }}
38 {%- else %}
39 {{- raise_exception('Unexpected content type.') }}
40 {%- endif %}
41 {%- endmacro %}
42 {%- if not messages %}
43 {{- raise_exception('No messages provided.') }}
44 {%- endif %}
45 {%- set reasoning_instructions = '' %}
46 {%- if enable_thinking is undefined or enable_thinking is true %}
47 {%- set resolved_reasoning_effort = reasoning_effort|default('xhigh') %}
48 {%- if resolved_reasoning_effort not in ('xhigh', 'medium', 'low') %}
49 {{- raise_exception('Unexpected reasoning effort ' ~ reasoning_effort ~ '. Supported types are xhigh (default), medium, and low.') }}
50 {%- endif %}
51 {%- if resolved_reasoning_effort == 'xhigh' %}
52 {%- 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.' %}
53 {%- elif resolved_reasoning_effort == 'low' %}
54 {%- set reasoning_instructions = 'Reasoning effort is set to low. Keep your thinking brief and focused, moving directly to the conclusion without unnecessary elaboration.' %}
55 {%- endif %}
56 {%- endif %}
57 {%- if tools and tools is iterable and tools is not mapping %}
58 {{- '<|im_start|>system\n' }}
59 {%- if reasoning_instructions %}
60 {{- reasoning_instructions + '\n\n' }}
61 {%- endif %}
62 {{- "# Tools\n\nYou have access to the following functions:\n\n<tools>" }}
63 {%- for tool in tools %}
64 {{- "\n" }}
65 {{- tool | tojson }}
66 {%- endfor %}
67 {{- "\n</tools>" }}
68 {{- '\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>' }}
69 {%- if messages[0].role == 'system' %}
70 {%- set content = render_content(messages[0].content, false, true)|trim %}
71 {%- if content %}
72 {{- '\n\n' + content }}
73 {%- endif %}
74 {%- endif %}
75 {{- '<|im_end|>\n' }}
76 {%- else %}
77 {%- if messages[0].role == 'system' %}
78 {%- set content = render_content(messages[0].content, false, true)|trim %}
79 {%- if content %}
80 {{- '<|im_start|>system\n' + (reasoning_instructions + '\n\n' if reasoning_instructions else '') + content + '<|im_end|>\n' }}
81 {%- elif reasoning_instructions %}
82 {{- '<|im_start|>system\n' + reasoning_instructions + '<|im_end|>\n' }}
83 {%- endif %}
84 {%- elif reasoning_instructions %}
85 {{- '<|im_start|>system\n' + reasoning_instructions + '<|im_end|>\n' }}
86 {%- endif %}
87 {%- endif %}
88 {%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
89 {%- for message in messages[::-1] %}
90 {%- set index = (messages|length - 1) - loop.index0 %}
91 {%- if ns.multi_step_tool and message.role == "user" %}
92 {%- set content = render_content(message.content, false)|trim %}
93 {%- if not(content.startswith('<tool_response>') and content.endswith('</tool_response>')) %}
94 {%- set ns.multi_step_tool = false %}
95 {%- set ns.last_query_index = index %}
96 {%- endif %}
97 {%- endif %}
98 {%- endfor %}
99 {%- if ns.multi_step_tool %}
100 {{- raise_exception('No user query found in messages.') }}
101 {%- endif %}
102 {%- for message in messages %}
103 {%- set content = render_content(message.content, true)|trim %}
104 {%- if message.role == "system" %}
105 {%- if not loop.first %}
106 {{- raise_exception('System message must be at the beginning.') }}
107 {%- endif %}
108 {%- elif message.role == "user" %}
109 {{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>' + '\n' }}
110 {%- elif message.role == "assistant" %}
111 {%- set reasoning_content = '' %}
112 {%- if message.reasoning_content is string %}
113 {%- set reasoning_content = message.reasoning_content %}
114 {%- endif %}
115 {%- set reasoning_content = reasoning_content|trim %}
116 {%- if preserve_thinking is undefined or preserve_thinking is true or loop.index0 > ns.last_query_index %}
117 {{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content + '\n</think>\n\n' + content }}
118 {%- else %}
119 {{- '<|im_start|>' + message.role + '\n' + content }}
120 {%- endif %}
121 {%- if message.tool_calls and message.tool_calls is iterable and message.tool_calls is not mapping %}
122 {%- for tool_call in message.tool_calls %}
123 {%- if tool_call.function is defined %}
124 {%- set tool_call = tool_call.function %}
125 {%- endif %}
126 {%- if loop.first %}
127 {%- if content|trim %}
128 {{- '\n\n<tool_call>\n<function=' + tool_call.name + '>\n' }}
129 {%- else %}
130 {{- '<tool_call>\n<function=' + tool_call.name + '>\n' }}
131 {%- endif %}
132 {%- else %}
133 {{- '\n<tool_call>\n<function=' + tool_call.name + '>\n' }}
134 {%- endif %}
135 {%- if tool_call.arguments is defined and tool_call.arguments != '' %}
136 {%- for args_name, args_value in tool_call.arguments|items %}
137 {{- '<parameter=' + args_name + '>\n' }}
138 {%- set args_value = args_value | string if args_value is string else args_value | tojson | safe %}
139 {{- args_value }}
140 {{- '\n</parameter>\n' }}
141 {%- endfor %}
142 {%- endif %}
143 {{- '</function>\n</tool_call>' }}
144 {%- endfor %}
145 {%- endif %}
146 {{- '<|im_end|>\n' }}
147 {%- elif message.role == "tool" %}
148 {%- if loop.previtem and loop.previtem.role != "tool" %}
149 {{- '<|im_start|>user' }}
150 {%- endif %}
151 {{- '\n<tool_response>\n' }}
152 {{- content }}
153 {{- '\n</tool_response>' }}
154 {%- if not loop.last and loop.nextitem.role != "tool" %}
155 {{- '<|im_end|>\n' }}
156 {%- elif loop.last %}
157 {{- '<|im_end|>\n' }}
158 {%- endif %}
159 {%- else %}
160 {{- raise_exception('Unexpected message role.') }}
161 {%- endif %}
162 {%- endfor %}
163 {%- if add_generation_prompt %}
164 {{- '<|im_start|>assistant\n' }}
165 {%- if enable_thinking is defined and enable_thinking is false %}
166 {{- '<think>\n\n</think>\n\n' }}
167 {%- else %}
168 {{- '<think>\n' }}
169 {%- endif %}
170 {%- endif %}