chat_template.jinja
5.2 KB · 129 lines · plaintext Raw
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 {# ---------- Default system prompt if user system is absent ---------- #}
8 {%- set default_system = 'You are a helpful assistant.' -%}
9
10 {# Detect whether the first message is a user-provided system message #}
11 {%- set has_user_system = (messages
12 and messages[0].role == 'system'
13 and (messages[0].content is string)) -%}
14
15 {# The system instruction that should apply (user system wins; else default) #}
16 {%- set effective_system = (has_user_system and messages[0].content) or default_system -%}
17
18
19 {# ---------- Optional tools preface as a synthetic system message ---------- #}
20 {%- if tools %}
21 {%- set sys_preamble -%}
22 # Tools
23
24 You may call one or more functions to assist with the user query.
25
26 You are provided with function signatures within <tools></tools> XML tags:
27 <tools>
28 {%- for tool in tools %}
29 {{ "\n" ~ (tool | tojson) }}
30 {% endfor %}
31 </tools>
32
33 For each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:
34 <tool_call>
35 {"name": <function-name>, "arguments": <args-json-object>}
36 </tool_call>
37 {%- endset -%}
38
39 {# Always include effective_system; user system prevails over default #}
40 {%- set sys_content = effective_system ~ "\n\n" ~ sys_preamble -%}
41
42 {%- set content = '<|start_header_id|>system<|end_header_id|>\n'
43 ~ base_system ~ '\n' ~ sys_content ~ '<|eot_id|>' -%}
44 {%- if not emit.started -%}{%- set content = bos_token ~ content -%}{%- set emit.started = true -%}{%- endif -%}
45 {{- content -}}
46 {%- else %}
47 {# No tools: always emit base_system + effective_system #}
48 {%- set content = '<|start_header_id|>system<|end_header_id|>\n'
49 ~ base_system ~ '\n' ~ effective_system ~ '<|eot_id|>' -%}
50 {%- if not emit.started -%}{%- set content = bos_token ~ content -%}{%- set emit.started = true -%}{%- endif -%}
51 {{- content -}}
52 {%- endif -%}
53
54 {# ---------- Locate last user query for multi-step tool behavior ---------- #}
55 {%- for message in messages[::-1] %}
56 {%- set index = (messages|length - 1) - loop.index0 -%}
57 {%- if ns.multi_step_tool
58 and message.role == "user"
59 and message.content is string
60 and not (message.content.startswith('<tool_response>') and message.content.endswith('</tool_response>')) -%}
61 {%- set ns.multi_step_tool = false -%}
62 {%- set ns.last_query_index = index -%}
63 {%- endif -%}
64 {%- endfor -%}
65
66 {# ---------- Walk all messages and emit in Llama-3 format ---------- #}
67 {%- for message in messages %}
68 {%- if message.content is string -%}
69 {%- set content = message.content -%}
70 {%- else -%}
71 {%- set content = '' -%}
72 {%- endif -%}
73
74 {# Skip the FIRST system message if it existed, since we already embedded it in effective_system #}
75 {%- if loop.first and message.role == "system" -%}
76 {# no-op #}
77
78 {%- elif (message.role == "user") or (message.role == "system") -%}
79 {%- set block = '<|start_header_id|>' ~ message.role ~ '<|end_header_id|>\n' ~ content ~ '<|eot_id|>' -%}
80 {%- if not emit.started -%}{%- set block = bos_token ~ block -%}{%- set emit.started = true -%}{%- endif -%}
81 {{- block -}}
82
83 {%- elif message.role == "assistant" -%}
84 {%- set body = content -%}
85 {%- set header = '<|start_header_id|>assistant<|end_header_id|>\n' -%}
86 {%- if not emit.started -%}{{ bos_token }}{%- set emit.started = true -%}{%- endif -%}
87 {{- header -}}
88 {% generation %}
89 {{- body -}}
90 {%- if message.tool_calls -%}
91 {%- for tool_call in message.tool_calls -%}
92 {%- if tool_call.function -%}{%- set tc = tool_call.function -%}{%- else -%}{%- set tc = tool_call -%}{%- endif -%}
93 {%- set args_json = (tc.arguments if (tc.arguments is string) else (tc.arguments | tojson)) -%}
94 {%- if loop.first -%}
95 {{- '<tool_call>\n{"name": "' ~ tc.name ~ '", "arguments": ' ~ args_json ~ '}\n</tool_call>' -}}
96 {%- else -%}
97 {{- '\n<tool_call>\n{"name": "' ~ tc.name ~ '", "arguments": ' ~ args_json ~ '}\n</tool_call>' -}}
98 {%- endif -%}
99 {%- endfor -%}
100 {%- endif -%}
101 {{- '<|eot_id|>' -}}{%- endgeneration -%}
102
103 {%- elif message.role == "tool" -%}
104 {%- set open_user = (loop.first or (loop.index0 > 0 and messages[loop.index0 - 1].role != "tool")) -%}
105 {%- set close_user = (loop.last or (loop.index0 < messages|length - 1 and messages[loop.index0 + 1].role != "tool")) -%}
106
107 {%- if open_user -%}
108 {%- set header = '<|start_header_id|>user<|end_header_id|>\n' -%}
109 {%- if not emit.started -%}{%- set header = bos_token ~ header -%}{%- set emit.started = true -%}{%- endif -%}
110 {{- header -}}
111 {%- endif -%}
112 {%- if open_user -%}
113 {{- '<tool_response>\n' -}}
114 {%- else -%}
115 {{- '\n<tool_response>\n' -}}
116 {%- endif -%}
117 {{- content -}}
118 {{- '\n</tool_response>' -}}
119
120 {%- if close_user -%}
121 {{- '<|eot_id|>' -}}
122 {%- endif -%}
123 {%- endif -%}
124 {%- endfor -%}
125
126 {%- if add_generation_prompt -%}
127 {{- '<|start_header_id|>assistant<|end_header_id|>\n' -}}
128 {%- endif -%}
129