Upload chat_template.jinja with huggingface_hub
Browse files- chat_template.jinja +122 -0
chat_template.jinja
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) -%}
|
| 2 |
+
{%- set emit = namespace(started=false) -%}
|
| 3 |
+
|
| 4 |
+
{# ---------- Build base system message (always emitted) ---------- #}
|
| 5 |
+
{%- set base_system = 'You are rnj-1, a foundation model trained by Essential AI.\n' -%}
|
| 6 |
+
|
| 7 |
+
{# ---------- Optional tools preface as a synthetic system message ---------- #}
|
| 8 |
+
{%- if tools %}
|
| 9 |
+
{%- set sys_preamble -%}
|
| 10 |
+
# Tools
|
| 11 |
+
|
| 12 |
+
You may call one or more functions to assist with the user query.
|
| 13 |
+
|
| 14 |
+
You are provided with function signatures within <tools></tools> XML tags:
|
| 15 |
+
<tools>
|
| 16 |
+
{%- for tool in tools %}
|
| 17 |
+
{{ "\n" ~ (tool | tojson) }}
|
| 18 |
+
{% endfor %}
|
| 19 |
+
</tools>
|
| 20 |
+
|
| 21 |
+
For each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:
|
| 22 |
+
<tool_call>
|
| 23 |
+
{"name": <function-name>, "arguments": <args-json-object>}
|
| 24 |
+
</tool_call>
|
| 25 |
+
{%- endset -%}
|
| 26 |
+
|
| 27 |
+
{# If the first user-provided message is system, include it above the tools preface #}
|
| 28 |
+
{%- set combined_system = (messages and messages[0].role == 'system')
|
| 29 |
+
and (messages[0].content is string) -%}
|
| 30 |
+
{%- set sys_content = (combined_system and (messages[0].content ~ "\n\n" ~ sys_preamble)) or sys_preamble -%}
|
| 31 |
+
|
| 32 |
+
{%- set content = '<|start_header_id|>system<|end_header_id|>\n' ~ base_system ~ '\n' ~ sys_content ~ '<|eot_id|>' -%}
|
| 33 |
+
{%- if not emit.started -%}{%- set content = bos_token ~ content -%}{%- set emit.started = true -%}{%- endif -%}
|
| 34 |
+
{{- content -}}
|
| 35 |
+
{%- else %}
|
| 36 |
+
{# No tools: always emit base_system, and include user's system message if present #}
|
| 37 |
+
{%- set user_system_content = '' -%}
|
| 38 |
+
{%- if messages and messages[0].role == 'system' and (messages[0].content is string) -%}
|
| 39 |
+
{%- set user_system_content = '\n' ~ messages[0].content -%}
|
| 40 |
+
{%- endif -%}
|
| 41 |
+
{%- set content = '<|start_header_id|>system<|end_header_id|>\n' ~ base_system ~ user_system_content ~ '<|eot_id|>' -%}
|
| 42 |
+
{%- if not emit.started -%}{%- set content = bos_token ~ content -%}{%- set emit.started = true -%}{%- endif -%}
|
| 43 |
+
{{- content -}}
|
| 44 |
+
{%- endif -%}
|
| 45 |
+
|
| 46 |
+
{# ---------- Locate last user query for multi-step tool behavior ---------- #}
|
| 47 |
+
{%- for message in messages[::-1] %}
|
| 48 |
+
{%- set index = (messages|length - 1) - loop.index0 -%}
|
| 49 |
+
{%- if ns.multi_step_tool
|
| 50 |
+
and message.role == "user"
|
| 51 |
+
and message.content is string
|
| 52 |
+
and not (message.content.startswith('<tool_response>') and message.content.endswith('</tool_response>')) -%}
|
| 53 |
+
{%- set ns.multi_step_tool = false -%}
|
| 54 |
+
{%- set ns.last_query_index = index -%}
|
| 55 |
+
{%- endif -%}
|
| 56 |
+
{%- endfor -%}
|
| 57 |
+
|
| 58 |
+
{# ---------- Walk all messages and emit in Llama-3 format ---------- #}
|
| 59 |
+
{%- for message in messages %}
|
| 60 |
+
{# normalize content #}
|
| 61 |
+
{%- if message.content is string -%}
|
| 62 |
+
{%- set content = message.content -%}
|
| 63 |
+
{%- else -%}
|
| 64 |
+
{%- set content = '' -%}
|
| 65 |
+
{%- endif -%}
|
| 66 |
+
|
| 67 |
+
{# --- user/system (non-initial system already handled above) --- #}
|
| 68 |
+
{%- if (message.role == "user") or (message.role == "system" and not loop.first) -%}
|
| 69 |
+
{%- set block = '<|start_header_id|>' ~ message.role ~ '<|end_header_id|>\n' ~ content ~ '<|eot_id|>' -%}
|
| 70 |
+
{%- if not emit.started -%}{%- set block = bos_token ~ block -%}{%- set emit.started = true -%}{%- endif -%}
|
| 71 |
+
{{- block -}}
|
| 72 |
+
|
| 73 |
+
{# --- assistant --- #}
|
| 74 |
+
{%- elif message.role == "assistant" -%}
|
| 75 |
+
|
| 76 |
+
{%- set body = content -%}
|
| 77 |
+
{%- set header = '<|start_header_id|>assistant<|end_header_id|>\n' -%}
|
| 78 |
+
{%- if not emit.started -%}{{ bos_token }}{%- set emit.started = true -%}{%- endif -%}
|
| 79 |
+
{{- header -}}
|
| 80 |
+
{% generation %}
|
| 81 |
+
{{- body -}}
|
| 82 |
+
{%- if message.tool_calls -%}
|
| 83 |
+
{%- for tool_call in message.tool_calls -%}
|
| 84 |
+
{%- if tool_call.function -%}{%- set tc = tool_call.function -%}{%- else -%}{%- set tc = tool_call -%}{%- endif -%}
|
| 85 |
+
{%- set args_json = (tc.arguments if (tc.arguments is string) else (tc.arguments | tojson)) -%}
|
| 86 |
+
{%- if loop.first -%}
|
| 87 |
+
{{- '<tool_call>\n{"name": "' ~ tc.name ~ '", "arguments": ' ~ args_json ~ '}\n</tool_call>' -}}
|
| 88 |
+
{%- else -%}
|
| 89 |
+
{{- '\n<tool_call>\n{"name": "' ~ tc.name ~ '", "arguments": ' ~ args_json ~ '}\n</tool_call>' -}}
|
| 90 |
+
{%- endif -%}
|
| 91 |
+
{%- endfor -%}
|
| 92 |
+
{%- endif -%}
|
| 93 |
+
{{- '<|eot_id|>' -}}{%- endgeneration -%}
|
| 94 |
+
{# --- tool messages are wrapped as synthetic user messages with <tool_response> --- #}
|
| 95 |
+
{%- elif message.role == "tool" -%}
|
| 96 |
+
{%- set open_user = (loop.first or (loop.index0 > 0 and messages[loop.index0 - 1].role != "tool")) -%}
|
| 97 |
+
{%- set close_user = (loop.last or (loop.index0 < messages|length - 1 and messages[loop.index0 + 1].role != "tool")) -%}
|
| 98 |
+
|
| 99 |
+
{%- if open_user -%}
|
| 100 |
+
{%- set header = '<|start_header_id|>user<|end_header_id|>\n' -%}
|
| 101 |
+
{%- if not emit.started -%}{%- set header = bos_token ~ header -%}{%- set emit.started = true -%}{%- endif -%}
|
| 102 |
+
{{- header -}}
|
| 103 |
+
{%- endif -%}
|
| 104 |
+
{%- if open_user -%}
|
| 105 |
+
{{- '<tool_response>\n' -}}
|
| 106 |
+
{%- else -%}
|
| 107 |
+
{{- '\n<tool_response>\n' -}}
|
| 108 |
+
{%- endif -%}
|
| 109 |
+
{{- content -}}
|
| 110 |
+
{{- '\n</tool_response>' -}}
|
| 111 |
+
|
| 112 |
+
{%- if close_user -%}
|
| 113 |
+
{{- '<|eot_id|>' -}}
|
| 114 |
+
{%- endif -%}
|
| 115 |
+
{%- endif -%}
|
| 116 |
+
{%- endfor -%}
|
| 117 |
+
|
| 118 |
+
{# ---------- Add generation prompt header for the model to continue ---------- #}
|
| 119 |
+
{%- if add_generation_prompt -%}
|
| 120 |
+
{%- set tail = '<|start_header_id|>assistant<|end_header_id|>\n' -%}
|
| 121 |
+
{{- tail -}}
|
| 122 |
+
{%- endif -%}
|