{%- 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 XML tags: {%- for tool in tools %} {{ "\n" ~ (tool | tojson) }} {% endfor %} For each function call, return a json object with function name and arguments within XML tags: {"name": , "arguments": } {%- 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('') and message.content.endswith('')) -%} {%- 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 -%} {{- '\n{"name": "' ~ tc.name ~ '", "arguments": ' ~ args_json ~ '}\n' -}} {%- else -%} {{- '\n\n{"name": "' ~ tc.name ~ '", "arguments": ' ~ args_json ~ '}\n' -}} {%- endif -%} {%- endfor -%} {%- endif -%} {{- '<|eot_id|>' -}}{%- endgeneration -%} {# --- tool messages are wrapped as synthetic user messages with --- #} {%- 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 -%} {{- '\n' -}} {%- else -%} {{- '\n\n' -}} {%- endif -%} {{- content -}} {{- '\n' -}} {%- 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 -%}