utdawn commited on
Commit
a37151b
·
verified ·
1 Parent(s): 33ee043

Create chat_template.jinja

Browse files
Files changed (1) hide show
  1. chat_template.jinja +159 -0
chat_template.jinja ADDED
@@ -0,0 +1,159 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {#-
2
+ LLaDA22 chat template — NO-THINK variant.
3
+ Usage:
4
+ start_header: "<role>assistant</role>"
5
+ end_header: "<|role_end|>"
6
+ end_learnable_inclusive: true
7
+ #}
8
+
9
+ {#- Message Content Rendering ============================================== #}
10
+ {%- macro render_content(content) -%}
11
+ {%- if content is string -%}
12
+ {{- content -}}
13
+ {%- elif content is iterable and content is not mapping -%}
14
+ {%- for item in content -%}
15
+ {%- if item is mapping -%}
16
+ {%- if 'text' in item -%}
17
+ {{- item.text -}}
18
+ {%- elif 'image' in item or 'image_url' in item or item.get('type') == 'image' -%}
19
+ {{- raise_exception('Constraint Violation: Image data detected.') -}}
20
+ {%- elif 'video' in item or item.get('type') == 'video' -%}
21
+ {{- raise_exception('Constraint Violation: Video data detected.') -}}
22
+ {%- else -%}
23
+ {{- raise_exception('Invalid mapping structure: Missing "text" key.') -}}
24
+ {%- endif -%}
25
+ {%- elif item is string -%}
26
+ {{- item -}}
27
+ {%- else -%}
28
+ {{- raise_exception('Invalid item type: Must be string or mapping.') -}}
29
+ {%- endif -%}
30
+ {%- endfor -%}
31
+ {%- elif content is none or content is undefined -%}
32
+ {{- '' -}}
33
+ {%- else -%}
34
+ {{- raise_exception('Fatal error: Content must be a string, iterable, or None.') -}}
35
+ {%- endif -%}
36
+ {%- endmacro -%}
37
+
38
+ {%- macro render_toolcalls(message, ns_tool) -%}
39
+ {{- '<|tool_calls_section_begin|>' -}}
40
+ {%- for tool_call in message.get('tool_calls', []) -%}
41
+ {%- set original_id = tool_call.get('id', '') -%}
42
+ {%- set func_name = tool_call.get('function', {}).get('name', 'unknown') -%}
43
+
44
+ {%- set tool_id = 'functions.' + func_name + ':' + ns_tool.index|string -%}
45
+
46
+ {%- set ns_tool.id_map = ns_tool.id_map + [[original_id, tool_id]] -%}
47
+ {%- set ns_tool.index = ns_tool.index + 1 -%}
48
+
49
+ {%- set args = tool_call.get('function', {}).get('arguments', '') -%}
50
+ {{- '<|tool_call_begin|>' + tool_id + '<|tool_call_argument_begin|>' -}}
51
+ {%- if args is string -%}
52
+ {{- args -}}
53
+ {%- else -%}
54
+ {{- args | tojson -}}
55
+ {%- endif -%}
56
+ {{- '<|tool_call_end|>' -}}
57
+ {%- endfor -%}
58
+ {{- '<|tool_calls_section_end|>' -}}
59
+ {%- endmacro -%}
60
+
61
+ {%- if not messages -%}
62
+ {{- raise_exception('No messages provided.') -}}
63
+ {%- endif -%}
64
+
65
+ {# 1. System Instruction, Model Identity & Tools Injection #}
66
+ {%- set ns_sys = namespace(has_system=false, content='') -%}
67
+
68
+
69
+ {%- if messages[0].role == 'system' -%}
70
+ {%- set ns_sys.has_system = true -%}
71
+ {%- if ns_sys.content -%}
72
+ {%- set ns_sys.content = ns_sys.content + '\n\n' -%}
73
+ {%- endif -%}
74
+ {%- set ns_sys.content = ns_sys.content + render_content(messages[0].content) -%}
75
+ {%- endif -%}
76
+
77
+ {%- if tools or ns_sys.content -%}
78
+ {{- '<role>system</role>' -}}
79
+
80
+ {%- if ns_sys.content -%}
81
+ {{- ns_sys.content -}}
82
+ {%- if tools -%}{{ '\n\n' }}{%- endif -%}
83
+ {%- endif -%}
84
+
85
+ {#- Tool Definition Rendering -#}
86
+ {%- if tools and tools is iterable and tools is not mapping -%}
87
+ {{- "<tools>\n" -}}
88
+ {%- if tools_ts_str -%}
89
+ {{- tools_ts_str -}}
90
+ {%- else -%}
91
+ {%- for tool in tools -%}
92
+ {{- tool | tojson(ensure_ascii=False) -}}{{ '\n' -}}
93
+ {%- endfor -%}
94
+ {%- endif -%}
95
+ {{- "</tools>\n" -}}
96
+ {%- endif -%}
97
+
98
+ {{- '<|role_end|>' -}}
99
+ {%- endif -%}
100
+
101
+ {# 2. Main Message Rendering Loop #}
102
+ {%- set ns_tool = namespace(index=0, id_map=[]) -%}
103
+
104
+ {%- for message in messages -%}
105
+ {# Skip the first System message as it is pre-rendered above #}
106
+ {%- if not (loop.first and message.role == 'system') -%}
107
+
108
+ {%- set content_text = render_content(message.content) -%}
109
+ {%- set role_name = message.get('name') or message.role -%}
110
+
111
+ {%- if message.role == 'user' or message.role == 'system' -%}
112
+ {{- '<role>' + role_name + '</role>' -}}
113
+ {{- content_text + '<|role_end|>' -}}
114
+
115
+ {%- elif message.role == 'assistant' -%}
116
+ {{- '<role>' + role_name + '</role>' -}}
117
+
118
+ {#- NO-THINK: completely ignore reasoning_content, strip <think> from content -#}
119
+ {%- set final_content = content_text -%}
120
+
121
+ {#- Strip embedded <think>...</think> from content if present -#}
122
+ {%- if '<think>' in final_content and '</think>' in final_content -%}
123
+ {%- set parts = final_content.split('</think>', 1) -%}
124
+ {%- set final_content = parts[1].strip() -%}
125
+ {%- endif -%}
126
+
127
+ {#- Output content directly, no think tags -#}
128
+ {%- if final_content -%}
129
+ {{- final_content -}}
130
+ {%- endif -%}
131
+
132
+ {%- if message.tool_calls -%}
133
+ {{- render_toolcalls(message, ns_tool) -}}
134
+ {%- endif -%}
135
+
136
+ {{- '<|role_end|>' -}}
137
+
138
+ {%- elif message.role == 'tool' -%}
139
+ {{- '<role>tool</role>' -}}
140
+ {%- set original_id = message.get('tool_call_id', '') -%}
141
+
142
+ {%- set ns_lookup = namespace(mapped_id=original_id) -%}
143
+ {%- for pair in ns_tool.id_map -%}
144
+ {%- if pair[0] == original_id -%}
145
+ {%- set ns_lookup.mapped_id = pair[1] -%}
146
+ {%- endif -%}
147
+ {%- endfor -%}
148
+
149
+ {{- '## Return of ' + ns_lookup.mapped_id + '\n' -}}
150
+ {{- content_text + '\n<|role_end|>' -}}
151
+ {%- endif -%}
152
+
153
+ {%- endif -%}
154
+ {%- endfor -%}
155
+
156
+ {# 3. Generation Prompt Suffix — no think tag #}
157
+ {%- if add_generation_prompt -%}
158
+ {{- '<role>assistant</role>' -}}
159
+ {%- endif -%}