Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {%- set image_count = namespace(value=0) %}
- {%- set video_count = namespace(value=0) %}
- {%- macro render_content(content, do_vision_count, is_system_content=false) %}
- {%- if content is string %}
- {{- content }}
- {%- elif content is iterable and content is not mapping %}
- {%- for item in content %}
- {%- if 'image' in item or 'image_url' in item or item.type == 'image' %}
- {%- if is_system_content %}
- {{- raise_exception('System message cannot contain images.') }}
- {%- endif %}
- {%- if do_vision_count %}
- {%- set image_count.value = image_count.value + 1 %}
- {%- endif %}
- {{- '<|vision_start|><|image_pad|><|vision_end|>' }}
- {%- elif 'video' in item or item.type == 'video' %}
- {%- if is_system_content %}
- {{- raise_exception('System message cannot contain videos.') }}
- {%- endif %}
- {%- if do_vision_count %}
- {%- set video_count.value = video_count.value + 1 %}
- {%- endif %}
- {{- '<|vision_start|><|video_pad|><|vision_end|>' }}
- {%- elif 'text' in item %}
- {{- item.text }}
- {%- else %}
- {{- raise_exception('Unexpected item type in content.') }}
- {%- endif %}
- {%- endfor %}
- {%- elif content is none or content is undefined %}
- {{- '' }}
- {%- else %}
- {{- raise_exception('Unexpected content type.') }}
- {%- endif %}
- {%- endmacro %}
- {%- if not messages %}
- {{- raise_exception('No messages provided.') }}
- {%- endif %}
- {# ========================================= #}
- {# Detect /think ONLY in FIRST system msg #}
- {# ========================================= #}
- {%- set ns_enable = namespace(enable_thinking=false) %}
- {%- if messages[0].role == 'system' %}
- {%- set sys_raw = render_content(messages[0].content, false, true)|trim %}
- {%- if '/think' in sys_raw %}
- {%- set ns_enable.enable_thinking = true %}
- {%- endif %}
- {%- endif %}
- {%- if tools and tools is iterable and tools is not mapping %}
- {{- '<|im_start|>system\n' }}
- {{- "# Tools\n\nYou have access to the following functions:\n\n<tools>" }}
- {%- for tool in tools %}
- {{- "\n" }}
- {{- tool | tojson }}
- {%- endfor %}
- {{- "\n</tools>" }}
- {%- if messages[0].role == 'system' %}
- {%- set content = render_content(messages[0].content, false, true)|trim %}
- {%- set content = content.replace('/think','')|trim %}
- {%- if content %}
- {{- '\n\n' + content }}
- {%- endif %}
- {%- endif %}
- {{- '<|im_end|>\n' }}
- {%- else %}
- {%- if messages[0].role == 'system' %}
- {%- set content = render_content(messages[0].content, false, true)|trim %}
- {%- set content = content.replace('/think','')|trim %}
- {{- '<|im_start|>system\n' + content + '<|im_end|>\n' }}
- {%- endif %}
- {%- endif %}
- {%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
- {%- for message in messages[::-1] %}
- {%- set index = (messages|length - 1) - loop.index0 %}
- {%- if ns.multi_step_tool and message.role == "user" %}
- {%- set content = render_content(message.content, false)|trim %}
- {%- if not(content.startswith('<tool_response>') and content.endswith('</tool_response>')) %}
- {%- set ns.multi_step_tool = false %}
- {%- set ns.last_query_index = index %}
- {%- endif %}
- {%- endif %}
- {%- endfor %}
- {%- if ns.multi_step_tool %}
- {{- raise_exception('No user query found in messages.') }}
- {%- endif %}
- {%- for message in messages %}
- {%- set content = render_content(message.content, true)|trim %}
- {%- if message.role == "system" %}
- {%- if not loop.first %}
- {{- raise_exception('System message must be at the beginning.') }}
- {%- endif %}
- {%- elif message.role == "user" %}
- {{- '<|im_start|>user\n' + content + '<|im_end|>\n' }}
- {%- elif message.role == "assistant" %}
- {{- '<|im_start|>assistant\n' }}
- {%- if ns_enable.enable_thinking and loop.index0 > ns.last_query_index %}
- {%- if message.reasoning_content %}
- {{- '<think>\n' + message.reasoning_content|trim + '\n</think>\n\n' }}
- {%- endif %}
- {%- endif %}
- {{- content }}
- {{- '<|im_end|>\n' }}
- {%- elif message.role == "tool" %}
- {{- '<|im_start|>user\n<tool_response>\n' }}
- {{- content }}
- {{- '\n</tool_response><|im_end|>\n' }}
- {%- else %}
- {{- raise_exception('Unexpected message role.') }}
- {%- endif %}
- {%- endfor %}
- {# ========================================= #}
- {# Final generation prompt control #}
- {# ========================================= #}
- {%- if add_generation_prompt %}
- {{- '<|im_start|>assistant\n' }}
- {%- if ns_enable.enable_thinking %}
- {{- '<think>\n' }}
- {%- else %}
- {{- '<think>\n</think>\n\n' }}
- {%- endif %}
- {%- endif %}
Advertisement
Add Comment
Please, Sign In to add comment