Lapa-function-calling / chat_template.jinja
TymofiiNasobko's picture
update
d0106eb verified
{{ bos_token }}
{%- if tools -%}
{{- '<start_of_turn>' + 'system' + '
' -}}
{%- if messages[0]['role'] == "system" -%}
{%- set loop_messages = messages[1:] -%}
{%- if messages[0]['content'] is string -%}
{{- messages[0]['content'] + '
' + '
' -}}
{%- else -%}
{{- messages[0]['content'][0]['text'] + '
' + '
' -}}
{%- endif -%}
{%- else -%}
{%- set loop_messages = messages -%}
{%- 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 -%}
{{- "\n" -}}
{{- tool | tojson -}}
{%- endfor -%}
{{-'
' + "</tools>\n\nFor 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>" -}}
{{- '<end_of_turn>' + '
' -}}
{%- elif messages[0]['role'] == "system" -%}
{%- set loop_messages = messages[1:] -%}
{{- '<start_of_turn>' + 'system' + '
' -}}
{%- if messages[0]['content'] is string -%}
{{- messages[0]['content'] -}}
{%- else -%}
{{- messages[0]['content'][0]['text'] -}}
{%- endif -%}
{{- '<end_of_turn>
' }}
{%- else -%}
{%- set loop_messages = messages -%}
{%- endif -%}
{%- for message in loop_messages -%}
{%- if message['role'] == 'assistant' or message['role'] == 'model' -%}
{%- set role = "model" -%}
{%- else -%}
{%- set role = message['role'] -%}
{%- endif -%}
{{ '<start_of_turn>' + role + '
' }}
{%- if message['content'] is string -%}
{%- if role == "model" -%}
{% generation %}
{{- message['content'] | trim -}}
{% if message['tool_calls'] %}
{% for tool_call in message['tool_calls'] %}
{% if tool_call['function'] is defined %}
{% set tool_call = tool_call['function'] %}
{% endif %}
{{- "
" -}}
<tool_call>
{"name": "{{ tool_call['name'] }}", "arguments": {{ tool_call['arguments'] | tojson }}}
</tool_call>
{%- endfor -%}
{%- endif -%}
{% endgeneration %}
{%- else -%}
{{ message['content'] | trim }}
{%- endif -%}
{%- elif message['content'] is iterable -%}
{%- for item in message['content'] -%}
{%- if item['type'] == 'image' -%}
{{ '<start_of_image>' }}
{%- elif item['type'] == 'text' -%}
{%- if role == "model" -%}
{% generation %}
{{- item['text'] | trim -}}
{% if message['tool_calls'] %}
{% for tool_call in message['tool_calls'] %}
{% if tool_call['function'] is defined %}
{% set tool_call = tool_call['function'] %}
{% endif %}
{{- "
" -}}
<tool_call>
{"name": "{{ tool_call['name'] }}", "arguments": {{ tool_call['arguments'] | tojson }}}
</tool_call>
{%- endfor -%}
{%- endif -%}
{% endgeneration %}
{%- else -%}
{{ item['text'] | trim }}
{%- endif -%}
{%- endif -%}
{%- endfor -%}
{%- else -%}
{{ raise_exception("Invalid content type") }}
{%- endif -%}
{%- if role == "model" -%}
{% generation %}
{{ '<end_of_turn>
' }}{% endgeneration %}
{%- else -%}
{{ '<end_of_turn>
' }}
{%- endif -%}
{%- endfor -%}
{%- if add_generation_prompt -%}
{{'<start_of_turn>model
'}}
{%- endif -%}