Hyun9junn commited on
Commit
facee65
·
verified ·
1 Parent(s): 3ba9c19

Add 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
+ {% set image_count = namespace(value=0) %}
2
+ {% set video_count = namespace(value=0) %}
3
+
4
+ {%- set role_indicators = {
5
+ 'user': '<|user|>\n',
6
+ 'assistant': '<|assistant|>\n',
7
+ 'system': '<|system|>\n',
8
+ 'tool': '<|tool|>\n',
9
+ 'tool_declare': '<|tool_declare|>\n'
10
+ } %}
11
+ {%- set end_of_turn = '<|endofturn|>\n' %}
12
+
13
+
14
+ {%- macro declare_available_tools(tools) %}
15
+ {{- "# Tools" }}
16
+ {{- "\n" }}
17
+ {%- for tool in tools %}
18
+ {{- "<tool>" }}
19
+ {{- tool | tojson(ensure_ascii=False) | safe }}
20
+ {{- "</tool>\n" }}
21
+ {%- endfor %}
22
+ {%- endmacro %}
23
+
24
+
25
+ {%- set ns = namespace(last_query_index = messages|length - 1, last_query_index_not_yet_determined = true) %}
26
+ {%- for message in messages[::-1] %}
27
+ {%- set index = (messages|length - 1) - loop.index0 %}
28
+ {%- if ns.last_query_index_not_yet_determined and message.role == "user" and message.content is string %}
29
+ {%- set ns.last_query_index = index -%}
30
+ {%- set ns.last_query_index_not_yet_determined = false -%}
31
+ {%- endif %}
32
+ {%- endfor %}
33
+
34
+ {%- if tools is defined and tools %}
35
+ {{- role_indicators['tool_declare'] }}
36
+ {{- declare_available_tools(tools) }}
37
+ {{- end_of_turn -}}
38
+ {%- endif %}
39
+
40
+ {%- for i in range(messages | length) %}
41
+ {%- set msg = messages[i] %}
42
+ {%- set role = msg.role %}
43
+ {%- if role not in role_indicators %}
44
+ {{- raise_exception('Unknown role: ' ~ role) }}
45
+ {%- endif %}
46
+
47
+ {%- if i == 0 %}
48
+ {%- if role == 'system' %}
49
+ {{- role_indicators['system'] }}
50
+ {{- msg.content }}
51
+ {{- end_of_turn -}}
52
+ {%- continue %}
53
+ {%- endif %}
54
+ {%- endif %}
55
+
56
+ {%- if role == 'assistant' %}
57
+ {{- role_indicators['assistant'] }}
58
+
59
+ {%- set content = (msg.content if (msg.content is defined and msg.content) else "") -%}
60
+ {%- set reasoning = none -%}
61
+
62
+ {%- if msg.reasoning_content is defined and msg.reasoning_content%}
63
+ {%- set reasoning = msg.reasoning_content.strip() -%}
64
+ {%- elif content and "</think>" in content %}
65
+ {%- set _parts = content.split('</think>') -%}
66
+ {%- set reasoning = _parts[0].lstrip('<think>').strip() -%}
67
+ {%- set content = _parts[-1].strip() -%}
68
+ {%- endif %}
69
+
70
+ {%- if not (reasoning and i > ns.last_query_index) or (skip_think is defined and skip_think) %}
71
+ {%- set reasoning = none %}
72
+ {%- endif %}
73
+
74
+ {%- set content = content.strip() -%}
75
+
76
+ {{- "<think>\n" }}
77
+ {{- (reasoning if reasoning is not none else "") }}
78
+ {{- "\n</think>\n\n" }}
79
+
80
+ {{- content }}
81
+
82
+ {%- if msg.tool_calls %}
83
+ {%- if content is defined and content %}
84
+ {{- "\n" }}
85
+ {%- endif %}
86
+ {%- for tool_call in msg.tool_calls %}
87
+ {%- if tool_call.function is defined %}
88
+ {%- set tool_call = tool_call.function %}
89
+ {%- endif %}
90
+
91
+ {%- if tool_call.arguments is defined %}
92
+ {%- set arguments = tool_call.arguments %}
93
+ {%- elif tool_call.parameters is defined %}
94
+ {%- set arguments = tool_call.parameters %}
95
+ {%- else %}
96
+ {{- raise_exception('arguments or parameters are mandatory: ' ~ tool_call) }}
97
+ {%- endif %}
98
+ {%- if arguments is string %}
99
+ {{- "<tool_call>" }}{"name": "{{- tool_call.name }}", "arguments": {{ arguments }}}{{- "</tool_call>" }}
100
+ {%- else %}
101
+ {{- "<tool_call>" }}{"name": "{{- tool_call.name }}", "arguments": {{ arguments | tojson(ensure_ascii=False) | safe }}}{{- "</tool_call>" }}
102
+ {%- endif %}
103
+ {%- if not loop.last %}
104
+ {{- "\n" }}
105
+ {%- endif %}
106
+
107
+ {%- endfor %}
108
+ {%- endif %}
109
+ {{- end_of_turn -}}
110
+
111
+ {%- elif role == "tool" %}
112
+ {%- if i == 0 or messages[i - 1].role != "tool" %}
113
+ {{- role_indicators['tool'] }}
114
+ {%- endif %}
115
+ {%- if msg.content is defined %}
116
+ {%- if msg.content is string %}
117
+ {{- "<tool_result>" }}{{ msg.content }}{{- "</tool_result>" }}
118
+ {%- else %}
119
+ {{- "<tool_result>" }}{{ msg.content | tojson(ensure_ascii=False) | safe }}{{- "</tool_result>" }}
120
+ {%- endif %}
121
+ {%- endif %}
122
+ {%- if loop.last or messages[i + 1].role != "tool" %}
123
+ {{- end_of_turn -}}
124
+ {%- else %}
125
+ {{- "\n" }}
126
+ {%- endif %}
127
+
128
+ {%- else %}
129
+ {{- role_indicators[role] }}
130
+ {%- if msg.content is string %}
131
+ {{- msg.content }}
132
+ {%- else %}
133
+ {%- for content in msg.content %}
134
+ {%- if content.type == 'image' %}
135
+ {%- set image_count.value = image_count.value + 1 %}
136
+ {%- if add_vision_id %}Picture {{ image_count.value }}: {% endif %}<vision><image_pad></vision>
137
+ {%- elif content.type == 'video' %}
138
+ {%- set video_count.value = video_count.value + 1 %}
139
+ {%- if add_vision_id %}Video {{ video_count.value }}: {% endif %}<vision><video_pad></vision>
140
+ {%- elif content.type == 'text' %}
141
+ {{- content.text }}
142
+ {%- else %}
143
+ {{- content.text }}
144
+ {%- endif %}
145
+ {%- endfor %}
146
+ {%- endif %}
147
+ {{- end_of_turn -}}
148
+ {%- endif %}
149
+ {% endfor %}
150
+
151
+
152
+ {%- if add_generation_prompt %}
153
+ {{- role_indicators['assistant'] }}
154
+ {%- if enable_thinking is not defined or enable_thinking is true %}
155
+ {{- "<think>\n" }}
156
+ {%- else %}
157
+ {{- "<think>\n\n</think>\n\n" }}
158
+ {%- endif %}
159
+ {%- endif %}