| {% macro render_content(content) %}{% if content is none %}{{- "" }}{% elif content is string %}{{- content }}{% elif content is mapping %}{{- content['value'] if 'value' in content else content['text'] }}{% elif content is iterable %}{% for item in content %}{% if item.type == 'text' %}{{- item['value'] if 'value' in item else item['text'] }}{% elif item.type == 'image' %}<im_patch>{% endif %}{% endfor %}{% endif %}{% endmacro %}{%- if tools %}{{- '<|im_start|>system | |
| ' }}{%- if messages[0]['role'] == 'system' %}{{- render_content(messages[0]['content']) }}{%- else %}{{- '' }}{%- endif %}{{- " | |
| # Tools | |
| You may call one or more functions to assist with the user query. | |
| You are provided with function signatures within <tools></tools> XML tags: | |
| <tools>" }}{%- for tool in tools %}{{- " | |
| " }}{{- tool | tojson }}{%- endfor %}{{- " | |
| </tools> | |
| For each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags: | |
| <tool_call> | |
| {\"name\": <function-name>, \"arguments\": <args-json-object>} | |
| </tool_call><|im_end|> | |
| " }}{%- else %}{%- if messages[0]['role'] == 'system' %}{{- '<|im_start|>system | |
| ' + render_content(messages[0]['content']) + '<|im_end|> | |
| ' }}{%- endif %}{%- endif %}{%- for message in messages %}{%- if (message.role == "user") or (message.role == "system" and not loop.first) %}{{- '<|im_start|>' + message.role + ' | |
| ' + render_content(message.content) + '<|im_end|>' + ' | |
| ' }}{%- elif message.role == "assistant" %}{%- if loop.last %}{{- '<|im_start|>assistant | |
| <think> | |
| ' + render_content(message.reasoning_content) + ' | |
| </think> | |
| ' + render_content(message.content) }}{%- if message.tool_calls %}{%- for tool_call in message.tool_calls %}{%- set call_details = tool_call.function if tool_call.function is defined else tool_call %}{%- set tool_call_id = tool_call.id if tool_call.id is defined else tool_call.tool_call_id %}{{- ' | |
| <tool_call> | |
| {"tool_call_id": "' + tool_call_id + '", "name": "' + call_details.name + '", "arguments": ' }}{% if call_details.arguments is string %}{{- call_details.arguments }}{% else %}{{- call_details.arguments | tojson }}{% endif %}{{- '} | |
| </tool_call>' }}{%- endfor %}{%- endif %}{{- '<|im_end|> | |
| ' }}{%- else %}{{- '<|im_start|>assistant | |
| ' + render_content(message.content) }}{%- if message.tool_calls %}{%- for tool_call in message.tool_calls %}{%- set call_details = tool_call.function if tool_call.function is defined else tool_call %}{%- set tool_call_id = tool_call.id if tool_call.id is defined else tool_call.tool_call_id %}{{- ' | |
| <tool_call> | |
| {"tool_call_id": "' + tool_call_id + '", "name": "' + call_details.name + '", "arguments": ' }}{% if call_details.arguments is string %}{{- call_details.arguments }}{% else %}{{- call_details.arguments | tojson }}{% endif %}{{- '} | |
| </tool_call>' }}{%- endfor %}{%- endif %}{{- '<|im_end|> | |
| ' }}{%- endif %} | |
| {%- elif message.role in ["tool_response", "tool"] %} | |
| {%- if loop.first or loop.previtem.role not in ["tool", "tool_response"] -%} | |
| {{- '<|im_start|>tool_response | |
| ' -}} | |
| {%- endif -%} | |
| {{- '<tool_response> | |
| ' + 'tool_call_id: ' + message.tool_call_id + ' | |
| ' + render_content(message.content) + ' | |
| </tool_response> | |
| ' -}} | |
| {%- if loop.last or loop.nextitem.role not in ["tool", "tool_response"] -%} | |
| {{- '<|im_end|> | |
| ' -}} | |
| {%- endif -%} | |
| {%- endif %}{%- endfor %}{%- if add_generation_prompt %}{{- '<|im_start|>assistant | |
| <think> | |
| ' }}{%- endif %} |