|
36 | 36 | {%- endif %} |
37 | 37 | {%- endif %} |
38 | 38 | {%- endfor %} |
39 | | -
|
40 | 39 | {% if tools is defined and tools is not none %} |
41 | 40 | {% set tool_ns = namespace(text='## Tools\nYou have access to the following tools:\n') %} |
42 | 41 | {% for tool in tools %} |
43 | 42 | {% set tool_ns.text = tool_ns.text + '\n### ' + tool.function.name + '\nDescription: ' + tool.function.description + '\n\nParameters: ' + (tool.function.parameters | tojson) + '\n' %} |
44 | 43 | {% endfor %} |
45 | 44 | {% 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" %} |
46 | | - {% set ns.system_prompt = ns.system_prompt + '\n\n' + tool_ns.text %} |
| 45 | + {% set ns.system_prompt = (ns.system_prompt + '\n\n' if ns.system_prompt else '') + tool_ns.text %} |
47 | 46 | {% endif %} |
48 | | -
|
49 | | -{{ bos_token }}{{ ns.system_prompt }} |
| 47 | +{{- bos_token }}{{ ns.system_prompt }} |
50 | 48 | {%- for message in messages %} |
51 | 49 | {%- if message['role'] == 'user' %} |
52 | 50 | {%- set ns.is_tool = false -%} |
53 | 51 | {%- set ns.is_first = false -%} |
54 | 52 | {%- set ns.is_last_user = true -%} |
55 | | - {{'<|User|>' + message['content']}} |
| 53 | + {{- '<|User|>' + message['content'] -}} |
56 | 54 | {%- endif %} |
57 | 55 | {%- if message['role'] == 'assistant' and message['tool_calls'] is defined and message['tool_calls'] is not none %} |
58 | | - {%- if ns.is_last_user %} |
59 | | - {{'<|Assistant|></think>'}} |
| 56 | + {%- if ns.is_last_user -%} |
| 57 | + {{- '<|Assistant|></think>' -}} |
60 | 58 | {%- endif %} |
61 | 59 | {%- set ns.is_last_user = false -%} |
62 | 60 | {%- set ns.is_first = false %} |
63 | 61 | {%- set ns.is_tool = false -%} |
64 | 62 | {%- for tool in message['tool_calls'] %} |
65 | 63 | {%- if not ns.is_first %} |
66 | 64 | {%- if message['content'] is none %} |
67 | | - {{'<|tool▁calls▁begin|><|tool▁call▁begin|>'+ tool['function']['name'] + '<|tool▁sep|>' + tool['function']['arguments']|tojson + '<|tool▁call▁end|>'}} |
| 65 | + {{- '<|tool▁calls▁begin|><|tool▁call▁begin|>'+ tool['function']['name'] + '<|tool▁sep|>' + tool['function']['arguments']|tojson + '<|tool▁call▁end|>' -}} |
68 | 66 | {%- else %} |
69 | | - {{message['content'] + '<|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['function']['name'] + '<|tool▁sep|>' + tool['function']['arguments']|tojson + '<|tool▁call▁end|>'}} |
| 67 | + {{- message['content'] + '<|tool▁calls▁begin|><|tool▁call▁begin|>' + tool['function']['name'] + '<|tool▁sep|>' + tool['function']['arguments']|tojson + '<|tool▁call▁end|>' -}} |
70 | 68 | {%- endif %} |
71 | 69 | {%- set ns.is_first = true -%} |
72 | 70 | {%- else %} |
73 | | - {{'<|tool▁call▁begin|>'+ tool['function']['name'] + '<|tool▁sep|>' + tool['function']['arguments']|tojson + '<|tool▁call▁end|>'}} |
| 71 | + {{- '<|tool▁call▁begin|>'+ tool['function']['name'] + '<|tool▁sep|>' + tool['function']['arguments']|tojson + '<|tool▁call▁end|>' -}} |
74 | 72 | {%- endif %} |
75 | 73 | {%- endfor %} |
76 | | - {{'<|tool▁calls▁end|><|end▁of▁sentence|>'}} |
| 74 | + {{- '<|tool▁calls▁end|><|end▁of▁sentence|>' -}} |
77 | 75 | {%- endif %} |
78 | 76 | {%- if message['role'] == 'assistant' and (message['tool_calls'] is not defined or message['tool_calls'] is none) %} |
79 | | - {%- if ns.is_last_user %} |
80 | | - {{'<|Assistant|>'}} |
81 | | - {%- if message['prefix'] is defined and message['prefix'] and thinking %} |
82 | | - {{'<think>'}} |
83 | | - {%- else %} |
84 | | - {{'</think>'}} |
| 77 | + {%- if ns.is_last_user -%} |
| 78 | + {{- '<|Assistant|>' -}} |
| 79 | + {%- if message['prefix'] is defined and message['prefix'] and thinking -%} |
| 80 | + {{- '<think>' -}} |
| 81 | + {%- else -%} |
| 82 | + {{- '</think>' -}} |
85 | 83 | {%- endif %} |
86 | 84 | {%- endif %} |
87 | 85 | {%- set ns.is_last_user = false -%} |
88 | 86 | {%- if ns.is_tool %} |
89 | | - {{message['content'] + '<|end▁of▁sentence|>'}} |
| 87 | + {{- message['content'] + '<|end▁of▁sentence|>' -}} |
90 | 88 | {%- set ns.is_tool = false -%} |
91 | 89 | {%- else %} |
92 | 90 | {%- set content = message['content'] -%} |
93 | 91 | {%- if '</think>' in content %} |
94 | 92 | {%- set content = content.split('</think>', 1)[1] -%} |
95 | 93 | {%- endif %} |
96 | | - {{content + '<|end▁of▁sentence|>'}} |
| 94 | + {{- content + '<|end▁of▁sentence|>' -}} |
97 | 95 | {%- endif %} |
98 | 96 | {%- endif %} |
99 | 97 | {%- if message['role'] == 'tool' %} |
100 | 98 | {%- set ns.is_last_user = false -%} |
101 | 99 | {%- set ns.is_tool = true -%} |
102 | | - {{'<|tool▁output▁begin|>' + message['content'] + '<|tool▁output▁end|>'}} |
| 100 | + {{- '<|tool▁output▁begin|>' + message['content'] + '<|tool▁output▁end|>' -}} |
103 | 101 | {%- endif %} |
104 | 102 | {%- endfor -%} |
105 | | -{%- if add_generation_prompt and ns.is_last_user and not ns.is_tool %} |
106 | | - {{'<|Assistant|>'}} |
107 | | - {%- if not thinking %} |
108 | | - {{'</think>'}} |
109 | | - {%- else %} |
110 | | - {{'<think>'}} |
| 103 | +{%- if add_generation_prompt and ns.is_last_user and not ns.is_tool -%} |
| 104 | + {{- '<|Assistant|>' -}} |
| 105 | + {%- if not thinking -%} |
| 106 | + {{- '</think>' -}} |
| 107 | + {%- else -%} |
| 108 | + {{- '<think>' -}} |
111 | 109 | {%- endif %} |
112 | | -{% endif %}""" |
| 110 | +{%- endif %}""" |
113 | 111 |
|
114 | 112 |
|
115 | 113 | class DeepseekV31Renderer(ReasoningToolBaseRenderer): |
|
0 commit comments