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