main
jinja 93 lines 4.13 KB
Raw
1 {%- set enable_thinking = enable_thinking | default(false) %}
2 {%- if tools %}
3 {{- '<|im_start|>system\n' }}
4 {%- if messages[0].role == 'system' %}
5 {{- messages[0].content + '\n\n' }}
6 {%- endif %}
7 {{- "# Tools\n\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>" }}
8 {%- for tool in tools %}
9 {{- "\n" }}
10 {{- tool | tojson }}
11 {%- endfor %}
12 {{- "\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" }}
13 {%- else %}
14 {%- if messages[0].role == 'system' %}
15 {{- '<|im_start|>system\n' + messages[0].content + '<|im_end|>\n' }}
16 {%- endif %}
17 {%- endif %}
18
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.index0 %}
22 {%- if ns.multi_step_tool and message.role == "user" 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
28 {%- for message in messages %}
29 {%- if (message.role == "user") or (message.role == "system" and not loop.first) %}
30 {{- '<|im_start|>' + message.role + '\n' + message.content + '<|im_end|>' + '\n' }}
31 {%- elif message.role == "assistant" %}
32 {%- set content = message.content %}
33 {%- set reasoning = '' %}
34 {%- if message.reasoning is defined and message.reasoning is not none %}
35 {%- set reasoning = message.reasoning %}
36 {%- else %}
37 {%- if '</think>' in message.content %}
38 {%- set content = message.content.split('</think>')[-1].lstrip('\n') %}
39 {%- set reasoning = message.content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %}
40 {%- endif %}
41 {%- endif %}
42 {%- if loop.index0 > ns.last_query_index %}
43 {%- if loop.last or (not loop.last and reasoning) %}
44 {{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning.strip('\n') + '\n</think>\n\n' + content.lstrip('\n') }}
45 {%- else %}
46 {{- '<|im_start|>' + message.role + '\n' + content }}
47 {%- endif %}
48 {%- else %}
49 {{- '<|im_start|>' + message.role + '\n' + content }}
50 {%- endif %}
51
52 {%- if message.tool_calls %}
53 {%- for tool_call in message.tool_calls %}
54 {%- if (loop.first and content) or (not loop.first) %}
55 {{- '\n' }}
56 {%- endif %}
57 {%- if tool_call.function %}
58 {%- set tool_call = tool_call.function %}
59 {%- endif %}
60 {{- '<tool_call>\n{"name": "' }}
61 {{- tool_call.name }}
62 {{- '", "arguments": ' }}
63 {%- if tool_call.arguments is string %}
64 {{- tool_call.arguments }}
65 {%- else %}
66 {{- tool_call.arguments | tojson }}
67 {%- endif %}
68 {{- '}\n</tool_call>' }}
69 {%- endfor %}
70 {%- endif %}
71 {{- '<|im_end|>\n' }}
72 {%- elif message.role == "tool" %}
73 {%- if loop.first or (messages[loop.index0 - 1].role != "tool") %}
74 {{- '<|im_start|>user' }}
75 {%- endif %}
76 {{- '\n<tool_response>\n' }}
77 {{- message.content }}
78 {{- '\n</tool_response>' }}
79 {%- if loop.last or (messages[loop.index0 + 1].role != "tool") %}
80 {{- '<|im_end|>\n' }}
81 {%- endif %}
82 {%- endif %}
83 {%- endfor %}
84
85 {%- if add_generation_prompt %}
86 {{- '<|im_start|>assistant\n' }}
87 {%- if enable_thinking is defined and enable_thinking is false %}
88 {{- '<think>\n\n</think>\n\n' }}
89 {%- endif %}
90 {%- if enable_thinking is defined and enable_thinking is true %}
91 {{- '<think>\n' }}
92 {%- endif %}
93 {%- endif %}