spicyneuron commited on
Commit
3efa24d
·
verified ·
1 Parent(s): b5dd300

Delete chat_template.optional.jinja

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