main
jinja 91 lines 4.25 KB
Raw
1 {% if not add_generation_prompt is defined %}
2 {% set add_generation_prompt = false %}
3 {% endif %}
4 {% if not thinking is defined %}
5 {% set thinking = false %}
6 {% endif %}
7 {% set ns = namespace(is_first=false, is_tool=false, system_prompt='', is_first_sp=true, is_last_user=false) %}
8 {%- for message in messages %}
9 {%- if message['role'] == 'system' %}
10 {%- if ns.is_first_sp %}
11 {% set ns.system_prompt = ns.system_prompt + message['content'] %}
12 {% set ns.is_first_sp = false %}
13 {%- else %}
14 {% set ns.system_prompt = ns.system_prompt + '\n\n' + message['content'] %}
15 {%- endif %}
16 {%- endif %}
17 {%- endfor %}
18
19 {% if tools is defined and tools is not none %}
20 {% set tool_ns = namespace(text='## Tools\nYou have access to the following tools:\n') %}
21 {% for tool in tools %}
22 {% set tool_ns.text = tool_ns.text + '\n### ' + tool.function.name + '\nDescription: ' + tool.function.description + '\n\nParameters: ' + (tool.function.parameters | tojson) + '\n' %}
23 {% endfor %}
24 {% set tool_ns.text = tool_ns.text + "\nIMPORTANT: ALWAYS adhere to this exact format for tool use:\n<|tool▁calls▁begin|><|tool▁call▁begin|>tool_call_name<|tool▁sep|>tool_call_arguments<|tool▁call▁end|>{{additional_tool_calls}}<|tool▁calls▁end|>\n\nWhere:\n\n- `tool_call_name` must be an exact match to one of the available tools\n- `tool_call_arguments` must be valid JSON that strictly follows the tool's Parameters Schema\n- For multiple tool calls, chain them directly without separators or spaces\n" %}
25 {% set ns.system_prompt = ns.system_prompt + '\n\n' + tool_ns.text %}
26 {% endif %}
27
28 {{ bos_token }}{{ ns.system_prompt }}
29 {%- for message in messages %}
30 {%- if message['role'] == 'user' %}
31 {%- set ns.is_tool = false -%}
32 {%- set ns.is_first = false -%}
33 {%- set ns.is_last_user = true -%}
34 {{'<|User|>' + message['content']}}
35 {%- endif %}
36 {%- if message['role'] == 'assistant' and message['tool_calls'] is defined and message['tool_calls'] is not none %}
37 {%- if ns.is_last_user %}
38 {{'<|Assistant|></think>'}}
39 {%- endif %}
40 {%- set ns.is_last_user = false -%}
41 {%- set ns.is_first = false %}
42 {%- set ns.is_tool = false -%}
43 {%- for tool in message['tool_calls'] %}
44 {%- if not ns.is_first %}
45 {%- if message['content'] is none %}
46 {{'<|tool▁calls▁begin|><|tool▁call▁begin|>'+ tool['function']['name'] + '<|tool▁sep|>' + tool['function']['arguments']|tojson + '<|tool▁call▁end|>'}}
47 {%- else %}
48 {{message['content'] + '<|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['function']['name'] + '<|tool▁sep|>' + tool['function']['arguments']|tojson + '<|tool▁call▁end|>'}}
49 {%- endif %}
50 {%- set ns.is_first = true -%}
51 {%- else %}
52 {{'<|tool▁call▁begin|>'+ tool['function']['name'] + '<|tool▁sep|>' + tool['function']['arguments']|tojson + '<|tool▁call▁end|>'}}
53 {%- endif %}
54 {%- endfor %}
55 {{'<|tool▁calls▁end|><|end▁of▁sentence|>'}}
56 {%- endif %}
57 {%- if message['role'] == 'assistant' and (message['tool_calls'] is not defined or message['tool_calls'] is none) %}
58 {%- if ns.is_last_user %}
59 {{'<|Assistant|>'}}
60 {%- if message['prefix'] is defined and message['prefix'] and thinking %}
61 {{'<think>'}}
62 {%- else %}
63 {{'</think>'}}
64 {%- endif %}
65 {%- endif %}
66 {%- set ns.is_last_user = false -%}
67 {%- if ns.is_tool %}
68 {{message['content'] + '<|end▁of▁sentence|>'}}
69 {%- set ns.is_tool = false -%}
70 {%- else %}
71 {%- set content = message['content'] -%}
72 {%- if '</think>' in content %}
73 {%- set content = content.split('</think>', 1)[1] -%}
74 {%- endif %}
75 {{content + '<|end▁of▁sentence|>'}}
76 {%- endif %}
77 {%- endif %}
78 {%- if message['role'] == 'tool' %}
79 {%- set ns.is_last_user = false -%}
80 {%- set ns.is_tool = true -%}
81 {{'<|tool▁output▁begin|>' + message['content'] + '<|tool▁output▁end|>'}}
82 {%- endif %}
83 {%- endfor -%}
84 {%- if add_generation_prompt and ns.is_last_user and not ns.is_tool %}
85 {{'<|Assistant|>'}}
86 {%- if not thinking %}
87 {{'</think>'}}
88 {%- else %}
89 {{'<think>'}}
90 {%- endif %}
91 {% endif %}