Files
gemma-3-1b-it-qwen3-tool-te…/chat_template.jinja

103 lines
4.1 KiB
Plaintext
Raw Normal View History

{%- macro render_content(content) -%}
{%- if content is none or content is undefined -%}
{{- "" -}}
{%- elif content is string -%}
{{- content | trim -}}
{%- elif content is iterable -%}
{%- for item in content -%}
{%- if item['type'] == 'image' -%}
{{- '<start_of_image>' -}}
{%- elif item['type'] == 'text' -%}
{{- item['text'] | trim -}}
{%- endif -%}
{%- endfor -%}
{%- else -%}
{{- raise_exception("Invalid content type") -}}
{%- endif -%}
{%- endmacro -%}
{{ bos_token }}
{%- if messages and messages[0]['role'] == 'system' -%}
{%- set first_system = messages[0]['content'] -%}
{%- set loop_messages = messages[1:] -%}
{%- else -%}
{%- set first_system = "" -%}
{%- set loop_messages = messages -%}
{%- endif -%}
{%- set has_tools = (tools is defined and tools) -%}
{%- if has_tools or first_system -%}
{{- '<start_of_turn>system\n' -}}
{%- if has_tools -%}
{{- "# Tools\n\nYou may call one or more functions to assist with the user query.\n" -}}
{{- "If no tool call is needed, respond normally without any <tool_call> tags.\n\n" -}}
{{- "You are provided with function signatures within <tools></tools> XML tags:\n<tools>" -}}
{%- for tool in tools -%}
{{- "\n" -}}{{- tool | tojson -}}
{%- endfor -%}
{{- "\n</tools>\n\n" -}}
{{- "For each function call, return a JSON object with function name and arguments within <tool_call></tool_call> XML tags:\n" -}}
{{- "<tool_call>\n{\"name\": <function-name>, \"arguments\": <args-json-object>}\n</tool_call>\n" -}}
{{- "You may emit multiple <tool_call> blocks in a single assistant turn.\n" -}}
{%- if first_system -%}
{{- "\n" -}}
{%- endif -%}
{%- endif -%}
{%- if first_system -%}
{{- render_content(first_system) -}}
{%- endif -%}
{{- '<end_of_turn>\n' -}}
{%- endif -%}
{%- for message in loop_messages -%}
{%- if message['role'] == 'system' -%}
{{- '<start_of_turn>system\n' -}}
{{- render_content(message['content']) -}}
{{- '<end_of_turn>\n' -}}
{%- elif message['role'] == 'tool' -%}
{{- '<start_of_turn>tool\n' -}}
{{- '<tool_response>\n' -}}
{%- if message['content'] is string -%}
{{- message['content'] | trim -}}
{%- else -%}
{{- message['content'] | tojson -}}
{%- endif -%}
{{- '\n</tool_response>' -}}
{{- '<end_of_turn>\n' -}}
{%- else -%}
{%- set role = message['role'] -%}
{%- if message['role'] == 'assistant' -%}
{%- set role = 'model' -%}
{%- endif -%}
{{- '<start_of_turn>' + role + '\n' -}}
{{- render_content(message['content']) -}}
{%- if message['role'] == 'assistant' and message.tool_calls -%}
{%- set content_has_text = false -%}
{%- if message['content'] is string -%}
{%- set content_has_text = (message['content'] | trim) != '' -%}
{%- elif message['content'] is iterable -%}
{%- set content_has_text = (message['content'] | length) > 0 -%}
{%- endif -%}
{%- for tool_call in message.tool_calls -%}
{%- if (loop.first and content_has_text) or (not loop.first) -%}
{{- '\n' -}}
{%- endif -%}
{%- if tool_call.function -%}
{%- set tool_call = tool_call.function -%}
{%- endif -%}
{{- '<tool_call>\n{\"name\": \"' -}}
{{- tool_call.name -}}
{{- '\", \"arguments\": ' -}}
{%- if tool_call.arguments is string -%}
{{- tool_call.arguments -}}
{%- else -%}
{{- tool_call.arguments | tojson -}}
{%- endif -%}
{{- '}\n</tool_call>' -}}
{%- endfor -%}
{%- endif -%}
{{- '<end_of_turn>\n' -}}
{%- endif -%}
{%- endfor -%}
{%- if add_generation_prompt -%}
{{- '<start_of_turn>model\n' -}}
{%- endif -%}