gemma-3-4B-function-calling-v0.4 / chat_template.json
suzii's picture
(Trained with Unsloth)
554cf1d verified
{
"chat_template": "{{ bos_token }}\n{%- if messages[0]['role'] == 'system' -%}\n {%- if messages[0]['content'] is string -%}\n {%- set first_user_prefix = messages[0]['content'] + '\n\n' -%}\n {%- else -%}\n {%- set first_user_prefix = messages[0]['content'][0]['text'] + '\n\n' -%}\n {%- endif -%}\n {%- set loop_messages = messages[1:] -%}\n{%- else -%}\n {%- set first_user_prefix = \"\" -%}\n {%- set loop_messages = messages -%}\n{%- endif -%}\n{%- for message in loop_messages -%}\n {%- if (message['role'] == 'assistant') -%}\n {%- set role = \"model\" -%}\n {%- elif (message['role'] == 'tool') -%}\n {%- set role = \"user\" -%}\n {%- else -%}\n {%- set role = message['role'] -%}\n {%- endif -%}\n\n {%- if (role == 'user') != (loop.index0 % 2 == 0) -%}\n {{ raise_exception(message['content'] + \"Conversation roles must alternate user/assistant/user/assistant/...\") }}\n {%- endif -%}\n {{ '<start_of_turn>' + role + '\n' + (first_user_prefix if loop.first else \"\") }}\n {%- if message['content'] is string -%}\n {{ message['content'] | trim }}\n {%- if role == 'model' and message['tool_calls'] -%}\n {%- for tool_call in message['tool_calls'] %}\n {%- if (loop.first and message['content']) or (not loop.first) %}\n {{- '\n' }}\n {%- endif %}\n {%- if tool_call['function'] %}\n {%- set tool_call = tool_call['function'] %}\n {%- endif %}\n {{- '<tool_call>\n{\"name\": \"' }}\n {{- tool_call['name'] }}\n {{- '\", \"arguments\": ' }}\n {%- if tool_call['arguments'] is string %}\n {{- tool_call['arguments'] }}\n {%- else %}\n {{- tool_call['arguments'] | tojson }}\n {%- endif %}\n {{- '}\n</tool_call>' }}\n {%- endfor %}\n {%- endif -%}\n {%- elif message['content'] is iterable -%}\n {%- for item in message['content'] -%}\n {%- if item['type'] == 'image' -%}\n {{ '<start_of_image>' }}\n {%- elif item['type'] == 'text' -%}\n {{ item['text'] | trim }}\n {%- endif -%}\n {%- endfor -%}\n {%- else -%}\n {{ raise_exception(\"Invalid content type\") }}\n {%- endif -%}\n {{ '<end_of_turn>\n' }}\n{%- endfor -%}\n{%- if add_generation_prompt -%}\n {{ '<start_of_turn>model\n' }}\n{%- endif -%}\n"
}