Upload chat_template.jinja with huggingface_hub
Browse files- chat_template.jinja +135 -0
chat_template.jinja
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{# Copyright 2025-present Unsloth. Apache 2.0 License. Unsloth Chat template fixes #}
|
| 2 |
+
{% macro render_item_list(item_list, tag_name='required') %}
|
| 3 |
+
{%- if item_list is defined and item_list is iterable and item_list | length > 0 %}
|
| 4 |
+
{%- if tag_name %}{{- '\n<' ~ tag_name ~ '>' -}}{% endif %}
|
| 5 |
+
{{- '[' }}
|
| 6 |
+
{%- for item in item_list -%}
|
| 7 |
+
{%- if loop.index > 1 %}{{- ", "}}{% endif -%}
|
| 8 |
+
{%- if item is string -%}
|
| 9 |
+
{{ "`" ~ item ~ "`" }}
|
| 10 |
+
{%- else -%}
|
| 11 |
+
{{ item }}
|
| 12 |
+
{%- endif -%}
|
| 13 |
+
{%- endfor -%}
|
| 14 |
+
{{- ']' }}
|
| 15 |
+
{%- if tag_name %}{{- '</' ~ tag_name ~ '>' -}}{% endif %}
|
| 16 |
+
{%- endif %}
|
| 17 |
+
{% endmacro %}
|
| 18 |
+
|
| 19 |
+
{%- if messages[0]["role"] == "system" %}
|
| 20 |
+
{%- set system_message = messages[0]["content"] %}
|
| 21 |
+
{%- set loop_messages = messages[1:] %}
|
| 22 |
+
{%- else %}
|
| 23 |
+
{%- set loop_messages = messages %}
|
| 24 |
+
{%- endif %}
|
| 25 |
+
|
| 26 |
+
{%- if not tools is defined %}
|
| 27 |
+
{%- set tools = [] %}
|
| 28 |
+
{%- endif %}
|
| 29 |
+
|
| 30 |
+
{%- if system_message is defined %}
|
| 31 |
+
{{- "<|im_start|>system\n" + system_message }}
|
| 32 |
+
{%- else %}
|
| 33 |
+
{%- if tools is iterable and tools | length > 0 %}
|
| 34 |
+
{{- "<|im_start|>system\nYou are Qwen, a helpful AI assistant that can interact with a computer to solve tasks." }}
|
| 35 |
+
{%- endif %}
|
| 36 |
+
{%- endif %}
|
| 37 |
+
{%- if tools is iterable and tools | length > 0 %}
|
| 38 |
+
{{- "\n\nYou have access to the following functions:\n\n" }}
|
| 39 |
+
{{- "<tools>" }}
|
| 40 |
+
{%- for tool in tools %}
|
| 41 |
+
{%- if tool.function is defined %}
|
| 42 |
+
{%- set tool = tool.function %}
|
| 43 |
+
{%- endif %}
|
| 44 |
+
{{- "\n<function>\n<name>" ~ tool.name ~ "</name>" }}
|
| 45 |
+
{{- '\n<description>' ~ (tool.description | trim) ~ '</description>' }}
|
| 46 |
+
{{- '\n<parameters>' }}
|
| 47 |
+
{%- for param_name, param_fields in tool.parameters.properties|items %}
|
| 48 |
+
{{- '\n<parameter>' }}
|
| 49 |
+
{{- '\n<name>' ~ param_name ~ '</name>' }}
|
| 50 |
+
{%- if param_fields.type is defined %}
|
| 51 |
+
{{- '\n<type>' ~ (param_fields.type | string) ~ '</type>' }}
|
| 52 |
+
{%- endif %}
|
| 53 |
+
{%- if param_fields.description is defined %}
|
| 54 |
+
{{- '\n<description>' ~ (param_fields.description | trim) ~ '</description>' }}
|
| 55 |
+
{%- endif %}
|
| 56 |
+
{{- render_item_list(param_fields.enum, 'enum') }}
|
| 57 |
+
{%- set handled_keys = ['type', 'description', 'enum', 'required'] %}
|
| 58 |
+
{%- for json_key, json_value in param_fields|items %}
|
| 59 |
+
{%- if json_key not in handled_keys %}
|
| 60 |
+
{%- set normed_json_key = json_key|string %}
|
| 61 |
+
{%- if json_value is mapping %}
|
| 62 |
+
{{- '\n<' ~ normed_json_key ~ '>' ~ (json_value | tojson | safe) ~ '</' ~ normed_json_key ~ '>' }}
|
| 63 |
+
{%- else %}
|
| 64 |
+
{{- '\n<' ~ normed_json_key ~ '>' ~ (json_value | string) ~ '</' ~ normed_json_key ~ '>' }}
|
| 65 |
+
{%- endif %}
|
| 66 |
+
{%- endif %}
|
| 67 |
+
{%- endfor %}
|
| 68 |
+
{{- render_item_list(param_fields.required, 'required') }}
|
| 69 |
+
{{- '\n</parameter>' }}
|
| 70 |
+
{%- endfor %}
|
| 71 |
+
{{- render_item_list(tool.parameters.required, 'required') }}
|
| 72 |
+
{{- '\n</parameters>' }}
|
| 73 |
+
{%- if tool.return is defined %}
|
| 74 |
+
{%- if tool.return is mapping %}
|
| 75 |
+
{{- '\n<return>' ~ (tool.return | tojson | safe) ~ '</return>' }}
|
| 76 |
+
{%- else %}
|
| 77 |
+
{{- '\n<return>' ~ (tool.return | string) ~ '</return>' }}
|
| 78 |
+
{%- endif %}
|
| 79 |
+
{%- endif %}
|
| 80 |
+
{{- '\n</function>' }}
|
| 81 |
+
{%- endfor %}
|
| 82 |
+
{{- "\n</tools>" }}
|
| 83 |
+
{{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n<tool_call>\n<function=example_function_name>\n<parameter=example_parameter_1>\nvalue_1\n</parameter>\n<parameter=example_parameter_2>\nThis is the value for the second parameter\nthat can span\nmultiple lines\n</parameter>\n</function>\n</tool_call>\n\n<IMPORTANT>\nReminder:\n- Function calls MUST follow the specified format: an inner <function=...></function> block must be nested within <tool_call></tool_call> XML tags\n- Required parameters MUST be specified\n- You may provide optional reasoning for your function call in natural language BEFORE the function call, but NOT after\n- If there is no function call available, answer the question like normal with your current knowledge and do not tell the user about function calls\n</IMPORTANT>' }}
|
| 84 |
+
{%- endif %}
|
| 85 |
+
{%- if system_message is defined %}
|
| 86 |
+
{{- '<|im_end|>\n' }}
|
| 87 |
+
{%- else %}
|
| 88 |
+
{%- if tools is iterable and tools | length > 0 %}
|
| 89 |
+
{{- '<|im_end|>\n' }}
|
| 90 |
+
{%- endif %}
|
| 91 |
+
{%- endif %}
|
| 92 |
+
{%- for message in loop_messages %}
|
| 93 |
+
{%- if message.role == "assistant" and message.tool_calls is defined and message.tool_calls is iterable and message.tool_calls | length > 0 %}
|
| 94 |
+
{{- '<|im_start|>' + message.role }}
|
| 95 |
+
{%- if message.content is defined and message.content is string and message.content | trim | length > 0 %}
|
| 96 |
+
{{- '\n' + message.content | trim + '\n' }}
|
| 97 |
+
{%- endif %}
|
| 98 |
+
{%- for tool_call in message.tool_calls %}
|
| 99 |
+
{%- if tool_call.function is defined %}
|
| 100 |
+
{%- set tool_call = tool_call.function %}
|
| 101 |
+
{%- endif %}
|
| 102 |
+
{{- '\n<tool_call>\n<function=' + tool_call.name + '>\n' }}
|
| 103 |
+
{%- if tool_call.arguments is defined %}
|
| 104 |
+
{%- for args_name, args_value in tool_call.arguments|items %}
|
| 105 |
+
{{- '<parameter=' + args_name + '>\n' }}
|
| 106 |
+
{%- set args_value = args_value if args_value is string else args_value | string %}
|
| 107 |
+
{{- args_value }}
|
| 108 |
+
{{- '\n</parameter>\n' }}
|
| 109 |
+
{%- endfor %}
|
| 110 |
+
{%- endif %}
|
| 111 |
+
{{- '</function>\n</tool_call>' }}
|
| 112 |
+
{%- endfor %}
|
| 113 |
+
{{- '<|im_end|>\n' }}
|
| 114 |
+
{%- elif message.role == "user" or message.role == "system" or message.role == "assistant" %}
|
| 115 |
+
{{- '<|im_start|>' + message.role + '\n' + message.content + '<|im_end|>' + '\n' }}
|
| 116 |
+
{%- elif message.role == "tool" %}
|
| 117 |
+
{%- if loop.previtem and loop.previtem.role != "tool" %}
|
| 118 |
+
{{- '<|im_start|>user\n' }}
|
| 119 |
+
{%- endif %}
|
| 120 |
+
{{- '<tool_response>\n' }}
|
| 121 |
+
{{- message.content }}
|
| 122 |
+
{{- '\n</tool_response>\n' }}
|
| 123 |
+
{%- if not loop.last and loop.nextitem.role != "tool" %}
|
| 124 |
+
{{- '<|im_end|>\n' }}
|
| 125 |
+
{%- elif loop.last %}
|
| 126 |
+
{{- '<|im_end|>\n' }}
|
| 127 |
+
{%- endif %}
|
| 128 |
+
{%- else %}
|
| 129 |
+
{{- '<|im_start|>' + message.role + '\n' + message.content + '<|im_end|>\n' }}
|
| 130 |
+
{%- endif %}
|
| 131 |
+
{%- endfor %}
|
| 132 |
+
{%- if add_generation_prompt %}
|
| 133 |
+
{{- '<|im_start|>assistant\n' }}
|
| 134 |
+
{%- endif %}
|
| 135 |
+
{# Copyright 2025-present Unsloth. Apache 2.0 License. Unsloth Chat template fixes #}
|