july-2-v0-func-desc / chat_template.jinja
arda-altera's picture
Upload folder using huggingface_hub
e80d915 verified
{{- bos_token -}}
{%- set system_message = None %}
{%- set loop_messages = messages %}
{%- if messages[0]['role'] == 'system' -%}
{%- set system_message = messages[0]['content'] %}
{%- set loop_messages = messages[1:] %}
{%- else -%}
{%- set system_message = "You are an expert in composing functions." %}
{%- set loop_messages = messages %}
{%- endif %}
{%- set ns = namespace(in_user_turn=False, skip_until=-1) %}
{%- for message in loop_messages %}
{%- set idx = loop.index0 %}
{%- if idx > ns.skip_until %}
{%- if message['role'] == 'user' or message['role'] in ['tool', 'tool_results'] %}
{%- if not ns.in_user_turn %}
{{- "<start_of_turn>user" }}{{- "\n" -}}
{%- set ns.in_user_turn = True %}
{%- endif %}
{%- if message['role'] == 'user' %}
{%- if idx == 0 and system_message is not none -%}
{{- system_message }}{{- "\n" -}}
{%- if tools is not none %}
{%- for t in tools %}
{{- t | tojson() }}
{{-","-}}
{%- endfor %}
{{-"\n"-}}
{%- endif %}
{%- endif %}
{{- message.content -}}
{%- elif message['role'] in ['tool', 'tool_results'] %}
{{- "\n" -}}{{- "<unused96>" }}{{- "\n" -}}
{{- message.content -}}
{{- "\n" -}}{{- "<unused97>" }}
{%- endif %}
{%- set last_processed_idx = idx %}
{%- for next_idx in range(idx + 1, loop_messages|length) %}
{%- set next_message = loop_messages[next_idx] %}
{%- if next_message['role'] == 'user' or next_message['role'] in ['tool', 'tool_results'] %}
{%- if next_message['role'] == 'user' %}
{{- "\n" -}}{{- next_message.content -}}
{%- elif next_message['role'] in ['tool', 'tool_results'] %}
{{- "\n" -}}{{- "<unused96>" }}{{- "\n" -}}
{{- next_message.content -}}
{{- "\n" -}}{{- "<unused97>" }}
{%- endif %}
{%- set last_processed_idx = next_idx %}
{%- else %}
{%- break %}
{%- endif %}
{%- endfor %}
{%- set ns.skip_until = last_processed_idx %}
{%- elif message['role'] == 'assistant' %}
{%- if ns.in_user_turn %}
{{- "<end_of_turn>" }}{{- "\n" -}}
{%- set ns.in_user_turn = False %}
{%- endif %}
{{- "<start_of_turn>model" }}{{- "\n" -}}
{%- if message.get("content") %}
{{- message.content }}
{%- endif %}
{%- if message.get("tool_calls") %}
{%- if message.get("content") %}{{- "\n" -}}{%- endif %}
{{- "<unused94>" }}{{- "\n" -}}
[
{%- for tool in message.get("tool_calls") -%}
{%- if tool is mapping and tool.get("type") == "function" and tool.get("function") is mapping -%}
{%- set func = tool.get("function") -%}
{%- if not loop.first %}, {% endif -%}
{{- func.get("name", "") }}(
{%- if func.get("arguments") is string -%}
{{- func.get("arguments") -}}
{%- elif func.get("arguments") is mapping -%}
{%- for arg_name, arg_value in func.get("arguments").items() -%}
{%- if not loop.first %}, {% endif -%}
{%- if arg_value is string -%}
{{- arg_name }}='{{ arg_value -}}'
{%- else -%}
{{- arg_name }}={{ arg_value|tojson -}}
{%- endif -%}
{%- endfor -%}
{%- endif -%}
)
{%- endif -%}
{%- endfor -%}
]{{- "\n" -}}
{{- "<unused95>" }}
{%- endif %}
{{- "<end_of_turn>" }}
{%- endif %}
{%- endif %}
{%- endfor %}
{%- if ns.in_user_turn %}
{{- "<end_of_turn>" }}
{%- endif %}
{%- if add_generation_prompt %}
{{- "\n" -}}{{ "<start_of_turn>model" }}{{- "\n" -}}
{%- endif %}