dgr237 commited on
Commit
83c2aaf
·
verified ·
1 Parent(s): d3c5bda

Upload chat_template.jinja with huggingface_hub

Browse files
Files changed (1) hide show
  1. chat_template.jinja +157 -0
chat_template.jinja ADDED
@@ -0,0 +1,157 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {%- set image_count = namespace(value=0) %}
2
+ {%- set video_count = namespace(value=0) %}
3
+ {%- macro render_content(content, do_vision_count, is_system_content=false) %}
4
+ {%- if content is string %}
5
+ {{- content }}
6
+ {%- elif content is iterable and content is not mapping %}
7
+ {%- for item in content %}
8
+ {%- if 'image' in item or 'image_url' in item or item.type == 'image' %}
9
+ {%- if is_system_content %}
10
+ {{- raise_exception('System message cannot contain images.') }}
11
+ {%- endif %}
12
+ {%- if do_vision_count %}
13
+ {%- set image_count.value = image_count.value + 1 %}
14
+ {%- endif %}
15
+ {%- if add_vision_id %}
16
+ {{- 'Picture ' ~ image_count.value ~ ': ' }}
17
+ {%- endif %}
18
+ {{- '<|vision_start|><|image_pad|><|vision_end|>' }}
19
+ {%- elif 'video' in item or item.type == 'video' %}
20
+ {%- if is_system_content %}
21
+ {{- raise_exception('System message cannot contain videos.') }}
22
+ {%- endif %}
23
+ {%- if do_vision_count %}
24
+ {%- set video_count.value = video_count.value + 1 %}
25
+ {%- endif %}
26
+ {%- if add_vision_id %}
27
+ {{- 'Video ' ~ video_count.value ~ ': ' }}
28
+ {%- endif %}
29
+ {{- '<|vision_start|><|video_pad|><|vision_end|>' }}
30
+ {%- elif 'text' in item %}
31
+ {{- item.text }}
32
+ {%- else %}
33
+ {{- raise_exception('Unexpected item type in content.') }}
34
+ {%- endif %}
35
+ {%- endfor %}
36
+ {%- elif content is none or content is undefined %}
37
+ {{- '' }}
38
+ {%- else %}
39
+ {{- raise_exception('Unexpected content type.') }}
40
+ {%- endif %}
41
+ {%- endmacro %}
42
+ {%- if messages %}
43
+ {%- if tools and tools is iterable and tools is not mapping %}
44
+ {{- '<|im_start|>system\n' }}
45
+ {{- "# Tools\n\nYou have access to the following functions:\n\n<tools>" }}
46
+ {%- for tool in tools %}
47
+ {{- "\n" }}
48
+ {{- tool | tojson }}
49
+ {%- endfor %}
50
+ {{- "\n</tools>" }}
51
+ {{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n<tool_call>\n<function=example_function_name>\n<parameter=example_parameter_1>\nvalue_1\n</parameter>\n<parameter=example_parameter_2>\nThis is the value for the second parameter\nthat can span\nmultiple lines\n</parameter>\n</function>\n</tool_call>\n\n<IMPORTANT>\nReminder:\n- Function calls MUST follow the specified format: an inner <function=...></function> block must be nested within <tool_call></tool_call> XML tags\n- Required parameters MUST be specified\n- You may provide optional reasoning for your function call in natural language BEFORE the function call, but NOT after\n- If there is no function call available, answer the question like normal with your current knowledge and do not tell the user about function calls\n</IMPORTANT>' }}
52
+ {%- if messages[0].role == 'system' %}
53
+ {%- set content = render_content(messages[0].content, false, true)|trim %}
54
+ {%- if content %}
55
+ {{- '\n\n' + content }}
56
+ {%- endif %}
57
+ {%- endif %}
58
+ {{- '<|im_end|>\n' }}
59
+ {%- else %}
60
+ {%- if messages[0].role == 'system' %}
61
+ {%- set content = render_content(messages[0].content, false, true)|trim %}
62
+ {{- '<|im_start|>system\n' + content + '<|im_end|>\n' }}
63
+ {%- endif %}
64
+ {%- endif %}
65
+ {%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
66
+ {%- for message in messages[::-1] %}
67
+ {%- set index = (messages|length - 1) - loop.index0 %}
68
+ {%- if ns.multi_step_tool and message.role == "user" %}
69
+ {%- set content = render_content(message.content, false)|trim %}
70
+ {%- if not(content.startswith('<tool_response>') and content.endswith('</tool_response>')) %}
71
+ {%- set ns.multi_step_tool = false %}
72
+ {%- set ns.last_query_index = index %}
73
+ {%- endif %}
74
+ {%- endif %}
75
+ {%- endfor %}
76
+ {%- for message in messages %}
77
+ {%- set content = render_content(message.content, true)|trim %}
78
+ {%- if message.role == "system" %}
79
+ {%- if not loop.first %}
80
+ {{- raise_exception('System message must be at the beginning.') }}
81
+ {%- endif %}
82
+ {%- elif message.role == "user" %}
83
+ {{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>' + '\n' }}
84
+ {%- elif message.role == "assistant" %}
85
+ {%- set reasoning_content = '' %}
86
+ {%- if message.reasoning_content is string %}
87
+ {%- set reasoning_content = message.reasoning_content %}
88
+ {%- else %}
89
+ {%- if '</think>' in content %}
90
+ {%- set reasoning_content = content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %}
91
+ {%- set content = content.split('</think>')[-1].lstrip('\n') %}
92
+ {%- endif %}
93
+ {%- endif %}
94
+ {%- set reasoning_content = reasoning_content|trim %}
95
+ {%- if (preserve_thinking is defined and preserve_thinking is true) or (loop.index0 > ns.last_query_index) %}
96
+ {{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content + '\n</think>\n\n' + content }}
97
+ {%- else %}
98
+ {{- '<|im_start|>' + message.role + '\n' + content }}
99
+ {%- endif %}
100
+ {%- if message.tool_calls and message.tool_calls is iterable and message.tool_calls is not mapping %}
101
+ {%- for tool_call in message.tool_calls %}
102
+ {%- if tool_call.function is defined %}
103
+ {%- set tool_call = tool_call.function %}
104
+ {%- endif %}
105
+ {%- if loop.first %}
106
+ {%- if content|trim %}
107
+ {{- '\n\n' }}
108
+ {%- endif %}
109
+ {%- else %}
110
+ {{- '\n' }}
111
+ {%- endif %}
112
+ {%- if tool_call.arguments is mapping %}
113
+ {{- '<tool_call>\n<function=' + tool_call.name + '>\n' }}
114
+ {%- for args_name, args_value in tool_call.arguments|items %}
115
+ {{- '<parameter=' + args_name + '>\n' }}
116
+ {%- set args_value = args_value | string if args_value is string else args_value | tojson | safe %}
117
+ {{- args_value }}
118
+ {{- '\n</parameter>\n' }}
119
+ {%- endfor %}
120
+ {{- '</function>\n</tool_call>' }}
121
+ {%- else %}
122
+ {{- '<tool_call>\n{"name": ' + tool_call.name|tojson + ', "arguments": ' }}
123
+ {%- if tool_call.arguments is string %}
124
+ {{- tool_call.arguments }}
125
+ {%- else %}
126
+ {{- tool_call.arguments | tojson | safe }}
127
+ {%- endif %}
128
+ {{- '}\n</tool_call>' }}
129
+ {%- endif %}
130
+ {%- endfor %}
131
+ {%- endif %}
132
+ {{- '<|im_end|>\n' }}
133
+ {%- elif message.role == "tool" %}
134
+ {%- if loop.previtem and loop.previtem.role != "tool" %}
135
+ {{- '<|im_start|>user' }}
136
+ {%- endif %}
137
+ {{- '\n<tool_response>\n' }}
138
+ {{- content }}
139
+ {{- '\n</tool_response>' }}
140
+ {%- if not loop.last and loop.nextitem.role != "tool" %}
141
+ {{- '<|im_end|>\n' }}
142
+ {%- elif loop.last %}
143
+ {{- '<|im_end|>\n' }}
144
+ {%- endif %}
145
+ {%- else %}
146
+ {{- raise_exception('Unexpected message role.') }}
147
+ {%- endif %}
148
+ {%- endfor %}
149
+ {%- if add_generation_prompt %}
150
+ {{- '<|im_start|>assistant\n' }}
151
+ {%- if enable_thinking is defined and enable_thinking is false %}
152
+ {{- '<think>\n\n</think>\n\n' }}
153
+ {%- else %}
154
+ {{- '<think>\n' }}
155
+ {%- endif %}
156
+ {%- endif %}
157
+ {%- endif %}