benjaminradio commited on
Commit
6505048
·
verified ·
1 Parent(s): e032f52

Added chat_template.jinja

Browse files
Files changed (1) hide show
  1. chat_template.jinja +134 -0
chat_template.jinja ADDED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {%- if tools %}
2
+ {{- "<|im_start|>system\n" }}
3
+ {%- if messages[0].role == 'system' and messages[0].content %}
4
+ {{- messages[0].content + '\n\n' }}
5
+ {%- endif %}
6
+ {{- "\n# Tools\nYou may call one or more functions to assist with the user query.\n\nYou are provided with function signatures within <tools></tools> XML tags:\n<tools>" }}
7
+ {%- for tool in tools %}
8
+ {{- "\n" }}
9
+ {{- tool | tojson }}
10
+ {%- endfor %}
11
+ {{- "\n</tools>\n\nFor each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:\n<tool_call>\n{\"name\": <function-name>, \"arguments\": <args-json-object>}\n</tool_call><|im_end|>\n" }}
12
+ {%- else %}
13
+ {%- if messages[0].role == 'system' %}
14
+ {%- if messages[0].get('tools') %}
15
+ {%- if messages[0].content %}
16
+ {{- '<|im_start|>system\n' + messages[0].content + '\n\n' }}
17
+ {%- else %}
18
+ {{- '<|im_start|>system\n\n' }}
19
+ {%- endif %}
20
+ {{- "# Tools\nYou may call one or more functions to assist with the user query.\n\nYou are provided with function signatures within <tools></tools> XML tags:\n<tools>" }}
21
+ {%- for tool in messages[0]['tools'] %}
22
+ {{- "\n" }}
23
+ {{- tool | tojson }}
24
+ {%- endfor %}
25
+ {{- "\n</tools>\n\nFor each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:\n<tool_call>\n{\"name\": <function-name>, \"arguments\": <args-json-object>}\n</tool_call><|im_end|>\n" }}
26
+ {%- else %}
27
+ {{- '<|im_start|>system\n' + messages[0].content + '<|im_end|>\n' }}
28
+ {%- endif %}
29
+ {%- endif %}
30
+ {%- endif %}
31
+ {%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
32
+ {%- for message in messages[::-1] %}
33
+ {%- set index = (messages|length - 1) - loop.index0 %}
34
+ {%- if ns.multi_step_tool and message.role == "user" and message.content is string and not(message.content.startswith('<tool_response>') and message.content.endswith('</tool_response>')) %}
35
+ {%- set ns.multi_step_tool = false %}
36
+ {%- set ns.last_query_index = index %}
37
+ {%- endif %}
38
+ {%- endfor %}
39
+ {%- for message in messages %}
40
+ {%- if message.content is string %}
41
+ {%- set content = message.content %}
42
+ {%- else %}
43
+ {%- set content = '' %}
44
+ {%- endif %}
45
+ {%- if (message.role == "user") or (message.role == "system" and not loop.first) %}
46
+ {{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>' + '\n' }}
47
+ {%- elif message.role == "assistant" %}
48
+ {%- set thinking_content = '' %}
49
+ {%- set think_tag = '' %}
50
+ {%- if message.think is defined and message.think is string %}
51
+ {%- set thinking_content = message.think %}
52
+ {%- set think_tag = 'think' %}
53
+ {%- elif message.think_fast is defined and message.think_fast is string %}
54
+ {%- set thinking_content = message.think_fast %}
55
+ {%- set think_tag = 'think_fast' %}
56
+ {%- elif message.think_faster is defined and message.think_faster is string %}
57
+ {%- set thinking_content = message.think_faster %}
58
+ {%- set think_tag = 'think_faster' %}
59
+ {%- else %}
60
+ {%- if '</think>' in content %}
61
+ {%- set thinking_content = content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %}
62
+ {%- set content = content.split('</think>')[-1].lstrip('\n') %}
63
+ {%- set think_tag = 'think' %}
64
+ {%- elif '</think_fast>' in content %}
65
+ {%- set thinking_content = content.split('</think_fast>')[0].rstrip('\n').split('<think_fast>')[-1].lstrip('\n') %}
66
+ {%- set content = content.split('</think_fast>')[-1].lstrip('\n') %}
67
+ {%- set think_tag = 'think_fast' %}
68
+ {%- elif '</think_faster>' in content %}
69
+ {%- set thinking_content = content.split('</think_faster>')[0].rstrip('\n').split('<think_faster>')[-1].lstrip('\n') %}
70
+ {%- set content = content.split('</think_faster>')[-1].lstrip('\n') %}
71
+ {%- set think_tag = 'think_faster' %}
72
+ {%- else %}
73
+ {%- set thinking_content = '' %}
74
+ {%- set think_tag = 'think_faster' %}
75
+ {%- endif %}
76
+ {%- endif %}
77
+ {{- '<|im_start|>' + message.role }}
78
+ {% generation %}
79
+ {%- if loop.index0 > ns.last_query_index %}
80
+ {%- if loop.last and think_tag %}
81
+ {%- if thinking_content %}
82
+ {{- '<' + think_tag + '>\n' + thinking_content + '\n</' + think_tag + '>\n' + content.lstrip('\n') }}
83
+ {%- else %}
84
+ {{- '<' + think_tag + '>\n</' + think_tag + '>\n' + content.lstrip('\n') }}
85
+ {%- endif %}
86
+ {%- else %}
87
+ {{- content }}
88
+ {%- endif %}
89
+ {%- else %}
90
+ {{- content }}
91
+ {%- endif %}
92
+ {%- if message.tool_calls %}
93
+ {%- for tool_call in message.tool_calls %}
94
+ {%- if (loop.first and content) or (not loop.first) %}
95
+ {{- '\n' }}
96
+ {%- endif %}
97
+ {%- if tool_call.function %}
98
+ {%- set tool_call = tool_call.function %}
99
+ {%- endif %}
100
+ {{- '<tool_call>\n{"name": "' }}
101
+ {{- tool_call.name }}
102
+ {{- '", "arguments": ' }}
103
+ {%- if tool_call.arguments is string %}
104
+ {{- tool_call.arguments }}
105
+ {%- else %}
106
+ {{- tool_call.arguments | tojson }}
107
+ {%- endif %}
108
+ {{- '}\n</tool_call>' }}
109
+ {%- endfor %}
110
+ {%- endif %}
111
+ {{- '<|im_end|>' }}
112
+ {% endgeneration %}
113
+ {%- elif message.role == "tool" %}
114
+ {%- if loop.first or (messages[loop.index0 - 1].role != "tool") %}
115
+ {{- '<|im_start|>tool\n' }}
116
+ {%- endif %}
117
+ {{- content }}
118
+ {%- if loop.last or (messages[loop.index0 + 1].role != "tool") %}
119
+ {{- '<|im_end|>\n' }}
120
+ {%- endif %}
121
+ {%- endif %}
122
+ {%- endfor %}
123
+ {%- if add_generation_prompt %}
124
+ {%- set effort = reasoning_effort | default('high') %}
125
+ {%- if effort == 'high' %}
126
+ {{- '<|im_start|>assistant\n<think>\n' }}
127
+ {%- elif effort == 'medium' %}
128
+ {{- '<|im_start|>assistant\n<think_fast>\n' }}
129
+ {%- elif effort == 'low' %}
130
+ {{- '<|im_start|>assistant\n<think_faster>\n' }}
131
+ {%- else %}
132
+ {{- '<|im_start|>assistant\n<think>\n' }}
133
+ {%- endif %}
134
+ {%- endif %}