1+ {% - if tools %}
2+ {{- '<|im_start|>system\n' }}
3+ {% - if messages [0].role == 'system' %}
4+ {{- messages[0] .content + '\n\n' }}
5+ {% - else %}
6+ {{- 'You are a helpful assistant. \n\n' }}
7+ {% - endif %}
8+ {{- "# Tools\n\n您可以调用一个或多个function来协助处理用户查询。\n\n在<tools></tools> XML标签中提供了function的签名(即函数的结构信息):\n<tools>" }}
9+ {% - for tool in tools %}
10+ {{- "\n" }}
11+ {{- tool | tojson }}
12+ {% - endfor %}
13+ {{- "\n</tools>\n\n对于单个function的调用, 返回一个包含function name和参数的 JSON 对象,并用 <tool_call></tool_call> XML 标签包裹,形如:\n<tool_call>\n{\"name\": <function-name>, \"arguments\": <args-json-object>}\n</tool_call><|im_end|>\n" }}
14+ {% - else %}
15+ {% - if messages [0].role == 'system' %}
16+ {{- '<|im_start|>system\n' + messages[0] .content + '<|im_end|>\n' }}
17+ {% - endif %}
18+ {% - endif %}
19+ {% - set ns = namespace (multi_step_tool =true , last_query_index =messages |length - 1) %}
20+ {% - for message in messages [::-1] %}
21+ {% - set index = (messages |length - 1) - loop .index 0 %}
22+ {% - 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>' )) %}
23+ {% - set ns .multi_step_tool = false %}
24+ {% - set ns .last_query_index = index %}
25+ {% - endif %}
26+ {% - endfor %}
27+ {% - for message in messages %}
28+ {% - if message .content is string %}
29+ {% - set content = message .content %}
30+ {% - else %}
31+ {% - set content = '' %}
32+ {% - endif %}
33+ {% - if (message .role == "user" ) or (message .role == "system" and not loop .first ) %}
34+ {{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>' + '\n' }}
35+ {% - elif message .role == "assistant" %}
36+ {% - set reasoning_content = '' %}
37+ {% - if message .reasoning_content is string %}
38+ {% - set reasoning_content = message .reasoning_content %}
39+ {% - else %}
40+ {% - if '</think>' in content %}
41+ {% - set reasoning_content = content .split ('</think>' )[0].rstrip ('\n' ).split ('<think>' )[-1].lstrip ('\n' ) %}
42+ {% - set content = content .split ('</think>' )[-1].lstrip ('\n' ) %}
43+ {% - endif %}
44+ {% - endif %}
45+ {% - if loop .index 0 > ns .last_query_index %}
46+ {% - if loop .last or (not loop .last and reasoning_content ) %}
47+ {{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content.strip('\n') + '\n</think>\n\n' + content.lstrip('\n') }}
48+ {% - else %}
49+ {{- '<|im_start|>' + message.role + '\n' + content }}
50+ {% - endif %}
51+ {% - else %}
52+ {{- '<|im_start|>' + message.role + '\n' + content }}
53+ {% - endif %}
54+ {% - if message .tool_calls %}
55+ {% - for tool_call in message .tool_calls %}
56+ {% - if (loop .first and content ) or (not loop .first ) %}
57+ {{- '\n' }}
58+ {% - endif %}
59+ {% - if tool_call .function %}
60+ {% - set tool_call = tool_call .function %}
61+ {% - endif %}
62+ {{- '<tool_call>\n{"name": "' }}
63+ {{- tool_call.name }}
64+ {{- '", "arguments": ' }}
65+ {% - if tool_call .arguments is string %}
66+ {{- tool_call.arguments }}
67+ {% - else %}
68+ {{- tool_call.arguments | tojson }}
69+ {% - endif %}
70+ {{- '}\n</tool_call>' }}
71+ {% - endfor %}
72+ {% - endif %}
73+ {{- '<|im_end|>\n' }}
74+ {% - elif message .role == "tool" %}
75+ {% - if loop .first or (messages [loop .index 0 - 1].role != "tool" ) %}
76+ {{- '<|im_start|>user' }}
77+ {% - endif %}
78+ {{- '\n<tool_response>\n' }}
79+ {{- content }}
80+ {{- '\n</tool_response>' }}
81+ {% - if loop .last or (messages [loop .index 0 + 1].role != "tool" ) %}
82+ {{- '<|im_end|>\n' }}
83+ {% - endif %}
84+ {% - endif %}
85+ {% - endfor %}
86+ {% - if add_generation_prompt %}
87+ {{- '<|im_start|>assistant\n' }}
88+ {% - endif %}
0 commit comments