| {%- set ns = namespace(enable_thinking=true) -%} |
| {%- for message in messages -%} |
| {%- if message['content'] is string -%} |
| {%- if message['role'] == 'user' or message['role'] == 'system' -%} |
| {%- if '/think' in message['content'] -%} |
| {%- set ns.enable_thinking = true -%} |
| {%- elif '/no_think' in message['content'] -%} |
| {%- set ns.enable_thinking = false -%} |
| {%- endif -%} |
| {%- endif -%} |
| {%- else -%} |
| {%- for content in message['content'] -%} |
| {%- if content['type'] == 'text' -%} |
| {%- if message['role'] == 'user' or message['role'] == 'system' -%} |
| {%- if '/think' in content['text'] -%} |
| {%- set ns.enable_thinking = true -%} |
| {%- elif '/no_think' in content['text'] -%} |
| {%- set ns.enable_thinking = false -%} |
| {%- endif -%} |
| {%- endif -%} |
| {%- endif -%} |
| {%- endfor -%} |
| {%- endif -%} |
| {%- endfor -%} |
| {%- for message in messages -%} |
| {%- if loop.first -%} |
| {%- if message['role'] != 'system' -%} |
| {{- '<SPECIAL_10>System\n\n' }} |
| {%- endif -%} |
| {%- endif -%} |
| |
| {%- if message['role'] == 'system' -%} |
| {{- '<SPECIAL_10>System\n' }} |
| {%- if message['content'] is string -%} |
| {{- message['content'].replace('/think', '').replace('/no_think', '').strip() }} |
| {%- else -%} |
| {%- for content in message['content'] -%} |
| {%- if content['type'] == 'image' -%} |
| {{- '' }} |
| {%- elif content['type'] == 'text' -%} |
| {{- content['text'].replace('/think', '').replace('/no_think', '').strip() }} |
| {%- endif -%} |
| {%- endfor -%} |
| {%- endif -%} |
| |
| {%- if tools -%} |
| {%- if message['content'].replace('/think', '').replace('/no_think', '').strip() != '' -%} |
| {{- '\n\n' }} |
| {%- endif -%} |
| |
| {{- 'You can use the following tools to assist the user if required:\n<AVAILABLE_TOOLS>[' }} |
| {%- for tool in tools -%} |
| {{- (tool.function if tool.function is defined else tool) | tojson -}} |
| {{- ', ' if not loop.last else '' -}} |
| {%- endfor -%} |
| {{- ']</AVAILABLE_TOOLS>\n\nIf you decide to call any tool(s), use the following format:\n<TOOLCALL>[{{\"name\": \"tool_name1\", \"arguments\": "\tool_args1\"}}, {{\"name\": \"tool_name2\", \"arguments\": \"tool_args2\"}}]</TOOLCALL>\n\nThe user will execute tool-calls and return responses from tool(s) in this format:\n<TOOL_RESPONSE>[{{\"tool_response1\"}}, {{\"tool_response2\"}}]</TOOL_RESPONSE>\n\nBased on the tool responses, you can call additional tools if needed, correct tool calls if any errors are found, or just respond to the user.' -}} |
| {%- endif -%} |
| {{- '\n' -}} |
| |
| {%- elif message['role'] == 'user' -%} |
| {{- '<SPECIAL_11>User\n' }} |
| {%- if message['content'] is string -%} |
| {{- message['content'].replace('/think', '').replace('/no_think', '').strip() }} |
| {%- else -%} |
| {%- for content in message['content'] -%} |
| {%- if content['type'] == 'image' -%} |
| {{- '' }} |
| {%- elif content['type'] == 'text' -%} |
| {{- content['text'].replace('/think', '').replace('/no_think', '').strip() }} |
| {%- endif -%} |
| {%- endfor -%} |
| {%- endif -%} |
| {{- '\n' -}} |
| |
| {%- elif message['role'] == 'tool' -%} |
| {%- if loop.first or (messages[loop.index0 - 1].role != 'tool') -%} |
| {{- '<SPECIAL_11>User\n<TOOL_RESPONSE>[' }} |
| {%- endif -%} |
| |
| {{- message.content }} |
| {{- ', ' if not loop.last and (messages[loop.index0 + 1].role == 'tool') else '' -}} |
| |
| {%- if loop.last or (messages[loop.index0 + 1].role != 'tool') -%} |
| {{- ']</TOOL_RESPONSE>\n' -}} |
| {%- endif -%} |
| |
| {%- elif message['role'] == 'assistant' -%} |
| {%- if '</think>' in content -%} |
| {%- set content = content.split('</think>')[1].strip() -%} |
| {%- endif -%} |
| |
| {{- '<SPECIAL_11>Assistant\n' + content.strip() }} |
| |
| {%- if message.tool_calls -%} |
| {%- if content.strip() != '' -%} |
| {{- '\n\n' -}} |
| {%- endif -%} |
| |
| {{- '<TOOLCALL>[' -}} |
| {%- for call in message.tool_calls -%} |
| {%- set fn = call.function if call.function is defined else call -%} |
| {{- '{"name": "' + fn.name + '", "arguments": ' -}} |
| {%- if fn.arguments is string -%} |
| {{- fn.arguments -}} |
| {%- else -%} |
| {{- fn.arguments | tojson -}} |
| {%- endif -%} |
| {{- '}' + (', ' if not loop.last else '') -}} |
| {%- endfor -%} |
| {{- ']</TOOLCALL>' -}} |
| {%- endif -%} |
| {{- '\n<SPECIAL_12>\n' -}} |
| {%- endif -%} |
| {%- endfor -%} |
| {%- if not add_generation_prompt is defined -%} |
| {%- set add_generation_prompt = false -%} |
| {%- endif -%} |
| {%- if add_generation_prompt -%} |
| {{- '<SPECIAL_11>Assistant\n' }} |
| {%- if ns.enable_thinking is defined and ns.enable_thinking is false -%} |
| {{- '<think></think>' }} |
| {%- else -%} |
| {{- '<think>\n' }} |
| {%- endif -%} |
| {%- endif -%} |