Yuan-lab commited on
Commit
f52cd63
·
verified ·
1 Parent(s): d2ad10e

Update chat_template.jinja

Browse files
Files changed (1) hide show
  1. chat_template.jinja +101 -11
chat_template.jinja CHANGED
@@ -7,13 +7,45 @@
7
  {{- '<|begin_of_sentence|>' -}}
8
 
9
  {%- set system_message = namespace(value=none) -%}
 
 
 
 
 
 
10
  {%- for message in messages if message.role == 'system' -%}
11
  {%- set system_message.value = message.content -%}
12
  {%- endfor -%}
13
- {%- if system_message.value -%}
14
- {{- system_message.value -}}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  {%- endif -%}
16
 
 
 
 
 
 
 
 
 
 
 
 
17
  {%- for message in messages -%}
18
  {%- if message.role == "user" -%}
19
  {{- '<|User|>' -}}
@@ -26,21 +58,79 @@
26
  {%- endif -%}
27
 
28
  {%- elif message.role == "assistant" -%}
29
- {%- set thinking_tag = "" -%}
30
- {%- if enable_thinking is defined -%}
31
- {%- set thinking_tag = "</think>" if not enable_thinking else "<think>" -%}
 
 
 
 
 
 
 
32
  {%- endif -%}
33
- {{- '<|Assistant|>' + thinking_tag -}}
34
 
35
- {%- if message.content is string -%}
36
- {{- message.content -}}
37
- {%- elif message.content is iterable and message.content is not string -%}
38
- {%- for item in message.content if item.type == "text" -%}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  {{- item.text -}}
40
  {%- endfor -%}
41
  {%- endif -%}
42
 
43
- {{- '<|end_of_sentence|>' -}}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
  {%- endif -%}
45
  {%- endfor -%}
46
 
 
7
  {{- '<|begin_of_sentence|>' -}}
8
 
9
  {%- set system_message = namespace(value=none) -%}
10
+ {%- set has_tools = namespace(value=false) -%}
11
+
12
+ {%- if tools is defined and tools and tools|length > 0 -%}
13
+ {%- set has_tools.value = true -%}
14
+ {%- endif -%}
15
+
16
  {%- for message in messages if message.role == 'system' -%}
17
  {%- set system_message.value = message.content -%}
18
  {%- endfor -%}
19
+
20
+ {%- if system_message.value or has_tools.value -%}
21
+ {%- if has_tools.value -%}
22
+ {%- if system_message.value -%}
23
+ {{- '\n\n' -}}
24
+ {%- endif -%}
25
+ {{- '# Tools\n\nYou have access to the following functions. To call functions, please respond with a python list of the calls. ' -}}
26
+ {{- 'Respond in the format [func_name1(params_name1="string_value", params_name2=123...), func_name2(params)] ' -}}
27
+ {{- "String parameters MUST be wrapped in double quotes. Do not use variables.\n\n" }}
28
+ {%- for tool in tools -%}
29
+ {{- tool | tojson -}}
30
+ {{- '\n\n' -}}
31
+ {%- endfor -%}
32
+ {%- endif -%}
33
+ {%- if system_message.value -%}
34
+ {{- system_message.value -}}
35
+ {%- endif -%}
36
  {%- endif -%}
37
 
38
+ {#- 计算最后一个非工具响应的用户查询索引(用于多轮工具调用判断)-#}
39
+ {%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) -%}
40
+ {%- for message in messages[::-1] -%}
41
+ {%- set index = (messages|length - 1) - loop.index0 -%}
42
+ {%- if ns.multi_step_tool and message.role == "user" and not(message.content is string and (message.content.startswith('tool_response') or message.content.startswith('<tool_response>'))) -%}
43
+ {%- set ns.multi_step_tool = false -%}
44
+ {%- set ns.last_query_index = index -%}
45
+ {%- endif -%}
46
+ {%- endfor -%}
47
+
48
+
49
  {%- for message in messages -%}
50
  {%- if message.role == "user" -%}
51
  {{- '<|User|>' -}}
 
58
  {%- endif -%}
59
 
60
  {%- elif message.role == "assistant" -%}
61
+ {#- 提取 reasoning_content -#}
62
+ {%- set content = message.content -%}
63
+ {%- set reasoning_content = '' -%}
64
+ {%- if message.reasoning_content is defined and message.reasoning_content is not none -%}
65
+ {%- set reasoning_content = message.reasoning_content -%}
66
+ {%- else -%}
67
+ {%- if content is string and '</think>' in content -%}
68
+ {%- set content = content.split('</think>')[-1].lstrip('\n') -%}
69
+ {%- set reasoning_content = content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') -%}
70
+ {%- endif -%}
71
  {%- endif -%}
 
72
 
73
+ {#- 判断是否输出 think 块:只有在多轮工具调用的最终回答,且是最后一轮或有 reasoning_content -#}
74
+ {%- set show_think = false -%}
75
+ {%- if loop.index0 > ns.last_query_index -%}
76
+ {%- if loop.last or (not loop.last and reasoning_content) -%}
77
+ {%- set show_think = true -%}
78
+ {%- endif -%}
79
+ {%- endif -%}
80
+
81
+ {{- '<|Assistant|>' -}}
82
+
83
+ {%- if show_think and reasoning_content -%}
84
+ {#- 最终回答,输出完整的 think 块 -#}
85
+ {{- '<think>\n' + reasoning_content.strip('\n') + '\n</think>\n\n' -}}
86
+ {%- elif enable_thinking is defined and not enable_thinking -%}
87
+ {#- enable_thinking=false:输出空 think 标签 -#}
88
+ {{- '</think>' -}}
89
+ {%- endif -%}
90
+
91
+ {%- if content is string -%}
92
+ {{- content -}}
93
+ {%- elif content is iterable and content is not string -%}
94
+ {%- for item in content if item.type == "text" -%}
95
  {{- item.text -}}
96
  {%- endfor -%}
97
  {%- endif -%}
98
 
99
+ {%- if message.tool_calls is defined and message.tool_calls -%}
100
+ {{- '[' -}}
101
+ {%- for tool_call in message.tool_calls -%}
102
+ {%- if tool_call.function is defined -%}
103
+ {%- set tool_call_func = tool_call.function -%}
104
+ {%- else -%}
105
+ {%- set tool_call_func = tool_call -%}
106
+ {%- endif -%}
107
+ {{- tool_call_func.name + '(' -}}
108
+ {%- for param in tool_call_func.arguments -%}
109
+ {{- param + '=' -}}
110
+ {%- if tool_call_func.arguments[param] is string -%}
111
+ {{- '"' + tool_call_func.arguments[param] + '"' -}}
112
+ {%- else -%}
113
+ {{- tool_call_func.arguments[param] | tojson -}}
114
+ {%- endif -%}
115
+ {%- if not loop.last -%}, {% endif -%}
116
+ {%- endfor -%}
117
+ {{- ')' -}}
118
+ {%- if not loop.last -%}, {% endif -%}
119
+ {%- endfor -%}
120
+ {{- ']' -}}
121
+ {{- '<|end_of_sentence|>' -}}
122
+ {%- endif -%}
123
+
124
+
125
+ {%- elif message.role == "tool" -%}
126
+ {%- if loop.first or (messages[loop.index0 - 1].role != "tool") -%}
127
+ {{- '<|User|>tool_response\n' -}}
128
+ {%- endif -%}
129
+ {%- if message.content is mapping -%}
130
+ {{- message.content | tojson -}}
131
+ {%- else -%}
132
+ {{- '{ "output": ' + (message.content | tojson) + ' }\n' -}}
133
+ {%- endif -%}
134
  {%- endif -%}
135
  {%- endfor -%}
136