okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

waveringanaserve: restore the DeepSeek V4.1 server paths, tools and headers removed upstream, keeping the native picture tower firstba8745e13

main
17.3 KiB291 linesraw
1{#- Unsloth port of DS41 #}
2{%- set IMG = "<|deepseek_image|>" -%}
3{%- set DSML = "|DSML|" -%}
4
5{%- if thinking_mode is not defined or thinking_mode is none -%}
6    {%- if enable_thinking is defined and enable_thinking is not none -%}
7        {%- set thinking_mode = "thinking" if enable_thinking else "chat" -%}
8    {%- elif thinking is defined and thinking is not none and thinking is not string -%}
9        {%- set thinking_mode = "thinking" if thinking else "chat" -%}
10    {%- else -%}
11        {%- set thinking_mode = "thinking" -%}
12    {%- endif -%}
13{%- endif -%}
14{%- if thinking_mode not in ["chat", "thinking"] -%}
15    {{- raise_exception("Invalid thinking_mode `" ~ thinking_mode ~ "`, should be \"chat\" or \"thinking\"") -}}
16{%- endif -%}
17{%- if drop_thinking is not defined or drop_thinking is none -%}{%- set drop_thinking = true -%}{%- endif -%}
18{%- if add_generation_prompt is not defined -%}{%- set add_generation_prompt = false -%}{%- endif -%}
19{%- if tools is not defined -%}{%- set tools = none -%}{%- endif -%}
20{%- if response_format is not defined -%}{%- set response_format = none -%}{%- endif -%}
21
22{%- if reasoning_effort is not defined or reasoning_effort is none -%}{%- set reasoning_effort = "high" -%}{%- endif -%}
23{%- set effort_map = {"low": 50, "medium": 62, "high": 75, "max": 100} -%}
24{%- if reasoning_effort is string and reasoning_effort in effort_map -%}
25    {%- set budget = effort_map[reasoning_effort] -%}
26{%- elif reasoning_effort is integer and reasoning_effort >= 1 and reasoning_effort <= 100 -%}
27    {%- set budget = reasoning_effort -%}
28{%- else -%}
29    {{- raise_exception("Invalid reasoning effort: " ~ reasoning_effort ~ ", should be int within [1,100] or one of \"low\", \"medium\", \"high\", \"max\"") -}}
30{%- endif -%}
31
32{#- Splits/validates "ns::name" against an explicit namespace and returns the qualified name. -#}
33{%- macro qualified_name(name, ns) -%}
34    {%- if ns is mapping -%}{%- set ns = ns.name -%}{%- endif -%}
35    {%- set parts = name.split("::", 1) -%}
36    {%- if parts | length == 2 -%}
37        {%- if ns is not none and ns != parts[0] -%}{{- raise_exception("Conflicting tool namespaces: " ~ ns ~ " != " ~ parts[0]) -}}{%- endif -%}
38        {%- set ns = parts[0] -%}
39        {%- set name = parts[1] -%}
40    {%- endif -%}
41    {%- if "::" in name -%}{{- raise_exception("Tool name must not contain '::': " ~ name) -}}{%- endif -%}
42    {%- if ns is not none and "::" in ns -%}{{- raise_exception("Tool namespace must not contain '::': " ~ ns) -}}{%- endif -%}
43    {{- name if ns is none else ns ~ "::" ~ name -}}
44{%- endmacro -%}
45
46{%- macro render_tools(tool_list) -%}
47    {%- set ts = namespace(schemas=[]) -%}
48    {%- for tool in tool_list -%}
49        {%- set fn = tool.function if tool.function is defined else tool -%}
50        {%- set ns = tool.namespace if tool.namespace is defined and tool.namespace is not none else fn.get("namespace") -%}
51        {#- JSON assembled by hand (key order kept, name/description replaced in place): llama.cpp has no ** unpacking. -#}
52        {%- set qname = qualified_name(fn.name, ns) -%}
53        {%- set sch = namespace(parts=[]) -%}
54        {%- for k, v in fn.items() if k != "namespace" -%}
55            {%- if k == "name" -%}{%- set v = qname -%}
56            {%- elif k == "description" and ns is mapping and ns.description -%}{%- set v = ns.description ~ "\n" ~ (fn.description or "") -%}
57            {%- endif -%}
58            {%- set sch.parts = sch.parts + [(k | tojson) ~ ": " ~ (v | tojson)] -%}
59        {%- endfor -%}
60        {%- if "description" not in fn and ns is mapping and ns.description -%}
61            {%- set sch.parts = sch.parts + ['"description": ' ~ ((ns.description ~ "\n") | tojson)] -%}
62        {%- endif -%}
63        {%- set ts.schemas = ts.schemas + ["{" ~ sch.parts | join(", ") ~ "}"] -%}
64    {%- endfor -%}
65    {{- "## Tools\n\nYou have access to a set of tools to help answer the user's question. You can invoke tools by writing a \"<|DSML| calls>\" block like the following:\n\n<|DSML| calls>\n<|DSML| invoke name=\"$TOOL_NAME\">\n<|DSML| parameter name=\"$PARAMETER_NAME\" string=\"true|false\">$PARAMETER_VALUE</|DSML| parameter>\n...\n</|DSML| invoke>\n<|DSML| invoke name=\"$TOOL_NAME2\">\n...\n</|DSML| invoke>\n</|DSML| calls>\n\nString parameters should be specified as is and set `string=\"true\"`. For all other types (numbers, booleans, arrays, objects), pass the value in JSON format and set `string=\"false\"`.\n\nIf thinking_mode is enabled (triggered by <think>), you MUST output your complete reasoning inside <think>...</think> BEFORE any tool calls or final response.\n\nOtherwise, output directly after </think> with tool calls or final response.\n\n### Available Tool Schemas\n\n" -}}
66    {{- ts.schemas | join("\n") -}}
67    {{- "\n\nYou MUST strictly follow the above defined tool name and parameter schemas to invoke tool calls.\n" -}}
68{%- endmacro -%}
69
70{#- 1. Merge tool results (and consecutive user turns) into user messages made of content blocks. -#}
71{%- set mns = namespace(msgs=[]) -%}
72{%- for msg in messages -%}
73    {%- set content = msg.get("content") -%}
74    {%- set is_blocks = content is iterable and content is not string and content is not mapping -%}
75    {%- if content is string and IMG in content -%}
76        {{- raise_exception("Message content contains image special token " ~ IMG ~ ". Images should be provided as image content blocks.") -}}
77    {%- endif -%}
78    {%- set rc = msg.reasoning_content if msg.get("reasoning_content") is not none else msg.get("thinking") -%}
79    {%- if rc is string and IMG in rc -%}
80        {{- raise_exception("reasoning_content contains image special token " ~ IMG) -}}
81    {%- endif -%}
82    {%- set prev = mns.msgs[-1] if mns.msgs else none -%}
83    {%- if msg.role == "tool" -%}
84        {%- set tblock = {"type": "tool_result", "tool_use_id": msg.get("tool_call_id", ""), "content": content if content is not none else ""} -%}
85        {%- if prev is not none and prev.role == "user" and prev.content_blocks is defined -%}
86            {%- set mns.msgs = mns.msgs[:-1] + [dict(prev, content_blocks=prev.content_blocks + [tblock])] -%}
87        {%- else -%}
88            {%- set mns.msgs = mns.msgs + [{"role": "user", "content_blocks": [tblock]}] -%}
89        {%- endif -%}
90    {%- elif msg.role == "user" -%}
91        {%- set blocks = msg.content_blocks if msg.get("content_blocks") is not none else (content if is_blocks else [{"type": "text", "text": content or ""}]) -%}
92        {%- if prev is not none and prev.role == "user" and prev.content_blocks is defined and prev.get("task") is none -%}
93            {%- set mns.msgs = mns.msgs[:-1] + [dict(prev, content_blocks=prev.content_blocks + blocks)] -%}
94        {%- else -%}
95            {%- set mns.msgs = mns.msgs + [dict(msg, content_blocks=blocks)] -%}
96        {%- endif -%}
97    {%- else -%}
98        {%- set mns.msgs = mns.msgs + [msg] -%}
99    {%- endif -%}
100{%- endfor -%}
101
102{#- Top-level tools / response_format attach to a leading system message, or to a new empty one. -#}
103{%- if tools or response_format -%}
104    {%- if not (mns.msgs and mns.msgs[0].role == "system") -%}
105        {%- set mns.msgs = [{"role": "system", "content": ""}] + mns.msgs -%}
106    {%- endif -%}
107    {%- set first = mns.msgs[0] -%}
108    {%- if tools and not first.get("tools") -%}{%- set first = dict(first, tools=tools) -%}{%- endif -%}
109    {%- if response_format and not first.get("response_format") -%}{%- set first = dict(first, response_format=response_format) -%}{%- endif -%}
110    {%- set mns.msgs = [first] + mns.msgs[1:] -%}
111{%- endif -%}
112
113{#- 2. Reorder tool results to match the tool_calls order of the preceding assistant message. -#}
114{%- set sns = namespace(msgs=[], ids=[]) -%}
115{%- for m in mns.msgs -%}
116    {%- if m.role == "assistant" and m.get("tool_calls") -%}
117        {%- set sns.ids = [] -%}
118        {%- for tc in m.tool_calls -%}
119            {%- set sns.ids = sns.ids + [tc.get("id") or (tc.function.get("id", "") if tc.function is defined else "")] -%}
120        {%- endfor -%}
121        {%- set sns.msgs = sns.msgs + [m] -%}
122    {%- elif m.role == "user" and m.content_blocks -%}
123        {%- set tb = namespace(blocks=[]) -%}
124        {%- for b in m.content_blocks if b.type == "tool_result" -%}
125            {%- set order = namespace(idx=0) -%}
126            {%- for tc_id in sns.ids -%}
127                {%- if tc_id and tc_id == b.get("tool_use_id", "") -%}{%- set order.idx = loop.index0 -%}{%- endif -%}
128            {%- endfor -%}
129            {%- set tb.blocks = tb.blocks + [dict(b, _order=order.idx)] -%}
130        {%- endfor -%}
131        {%- if tb.blocks | length > 1 and sns.ids -%}
132            {%- set sorted_blocks = tb.blocks | sort(attribute="_order") -%}
133            {%- set rb = namespace(blocks=[], i=0) -%}
134            {%- for b in m.content_blocks -%}
135                {%- if b.type == "tool_result" -%}
136                    {%- set rb.blocks = rb.blocks + [sorted_blocks[rb.i]] -%}
137                    {%- set rb.i = rb.i + 1 -%}
138                {%- else -%}
139                    {%- set rb.blocks = rb.blocks + [b] -%}
140                {%- endif -%}
141            {%- endfor -%}
142            {%- set sns.msgs = sns.msgs + [dict(m, content_blocks=rb.blocks)] -%}
143        {%- else -%}
144            {%- set sns.msgs = sns.msgs + [m] -%}
145        {%- endif -%}
146    {%- else -%}
147        {%- set sns.msgs = sns.msgs + [m] -%}
148    {%- endif -%}
149{%- endfor -%}
150{%- set msgs = sns.msgs -%}
151
152{#- Last user turn: users and mid-conversation systems count, except user turns made only of tool results.
153    Tools anywhere disable drop_thinking. -#}
154{%- set state = namespace(last_user=-1, has_tools=false) -%}
155{%- for m in msgs -%}
156    {%- if m.role == "user" or (m.role == "system" and loop.index0 > 0) -%}
157        {%- set real = namespace(v=true) -%}
158        {%- if m.get("content_blocks") -%}
159            {%- set real.v = false -%}
160            {%- for b in m.content_blocks if b.type in ["text", "image", "image_url"] -%}{%- set real.v = true -%}{%- endfor -%}
161        {%- endif -%}
162        {%- if real.v -%}{%- set state.last_user = loop.index0 -%}{%- endif -%}
163    {%- endif -%}
164    {%- if m.get("tools") -%}{%- set state.has_tools = true -%}{%- endif -%}
165{%- endfor -%}
166{%- set effective_drop = drop_thinking and not state.has_tools -%}
167{%- set is_thinking = thinking_mode == "thinking" -%}
168{#- Like encoding.py, thinking mode with drop_thinking silently drops messages of other roles before the last user turn. -#}
169{%- if is_thinking and effective_drop -%}
170    {%- set kept = namespace(msgs=[]) -%}
171    {%- for m in msgs -%}
172        {%- if m.role in ["user", "system", "tool", "latest_reminder", "direct_search_results", "assistant"] or loop.index0 >= state.last_user -%}
173            {%- set kept.msgs = kept.msgs + [m] -%}
174        {%- endif -%}
175    {%- endfor -%}
176    {%- if kept.msgs | length != msgs | length -%}
177        {%- set msgs = kept.msgs -%}
178        {%- set state.last_user = -1 -%}
179        {%- for m in msgs -%}
180            {%- if m.role == "user" or (m.role == "system" and loop.index0 > 0) -%}
181                {%- set real = namespace(v=true) -%}
182                {%- if m.get("content_blocks") -%}
183                    {%- set real.v = false -%}
184                    {%- for b in m.content_blocks if b.type in ["text", "image", "image_url"] -%}{%- set real.v = true -%}{%- endfor -%}
185                {%- endif -%}
186                {%- if real.v -%}{%- set state.last_user = loop.index0 -%}{%- endif -%}
187            {%- endif -%}
188        {%- endfor -%}
189    {%- endif -%}
190{%- endif -%}
191
192{{- "<|begin▁of▁sentence|>" -}}
193{#- 3. Render each message. -#}
194{%- for m in msgs -%}
195    {%- set idx = loop.index0 -%}
196    {%- set content = m.get("content") -%}
197    {%- if content is iterable and content is not string and content is not mapping -%}
198        {%- set sp = namespace(parts=[]) -%}
199        {%- for b in content -%}
200            {%- if b.type == "text" -%}{%- set sp.parts = sp.parts + [b.text or ""] -%}
201            {%- elif b.type in ["image", "image_url"] -%}{%- set sp.parts = sp.parts + [IMG] -%}
202            {%- endif -%}
203        {%- endfor -%}
204        {%- set content = sp.parts | join("\n\n") -%}
205    {%- endif -%}
206
207    {%- if idx == 0 -%}
208        {%- if is_thinking or m.role == "system" -%}{{- "<|System|>" -}}{%- endif -%}
209        {%- if is_thinking -%}{{- "Reasoning Effort: " ~ budget ~ " (range 1-100, the higher the value, the more thorough the reasoning)\n\n" -}}{%- endif -%}
210    {%- endif -%}
211
212    {%- if m.role == "system" -%}
213        {%- if idx > 0 -%}{{- "<|System|>" -}}{%- endif -%}
214        {{- content or "" -}}
215        {%- if m.get("tools") -%}{{- "\n\n" ~ render_tools(m.tools) -}}{%- endif -%}
216        {%- if m.get("response_format") -%}
217            {{- "\n\n## Response Format:\n\nYou MUST strictly adhere to the following schema to reply:\n" ~ (m.response_format | tojson) -}}
218        {%- endif -%}
219
220    {%- elif m.role == "user" -%}
221        {{- "<|User|>" -}}
222        {%- set up = namespace(parts=[]) -%}
223        {%- for b in m.content_blocks -%}
224            {%- if b.type == "text" -%}
225                {%- if IMG in (b.text or "") -%}{{- raise_exception("Text block contains image placeholder " ~ IMG ~ ". Images should be separate content blocks.") -}}{%- endif -%}
226                {%- set up.parts = up.parts + [b.text or ""] -%}
227            {%- elif b.type in ["image", "image_url"] -%}
228                {%- set up.parts = up.parts + [IMG] -%}
229            {%- elif b.type == "tool_result" -%}
230                {%- set tc = b.get("content", "") -%}
231                {%- if tc is iterable and tc is not string and tc is not mapping -%}
232                    {%- set sub = namespace(parts=[]) -%}
233                    {%- for s in tc -%}
234                        {%- if s.type == "text" -%}
235                            {%- if IMG in (s.text or "") -%}{{- raise_exception("Text block contains image placeholder " ~ IMG ~ ". Images should be separate content blocks.") -}}{%- endif -%}
236                            {%- set sub.parts = sub.parts + [s.get("text", "")] -%}
237                        {%- elif s.type in ["image", "image_url"] -%}{%- set sub.parts = sub.parts + [IMG] -%}
238                        {%- else -%}{%- set sub.parts = sub.parts + ["[Unsupported " ~ s.type ~ "]"] -%}
239                        {%- endif -%}
240                    {%- endfor -%}
241                    {%- set tc = sub.parts | join("\n\n") -%}
242                {%- endif -%}
243                {%- set up.parts = up.parts + ["<tool_result>" ~ tc ~ "</tool_result>"] -%}
244            {%- else -%}
245                {%- set up.parts = up.parts + ["[Unsupported " ~ b.type ~ "]"] -%}
246            {%- endif -%}
247        {%- endfor -%}
248        {{- up.parts | join("\n\n") -}}
249
250    {%- elif m.role == "latest_reminder" -%}
251        {{- "<|latest_reminder|>" ~ (content or "") -}}
252
253    {%- elif m.role == "assistant" -%}
254        {%- set prev_has_task = idx > 0 and msgs[idx - 1].get("task") is not none -%}
255        {%- if is_thinking and not prev_has_task and (not effective_drop or idx > state.last_user) -%}
256            {{- ((m.reasoning_content if m.get("reasoning_content") is not none else m.get("thinking")) or "") ~ "</think>" -}}
257        {%- endif -%}
258        {{- content or "" -}}
259        {%- if m.get("tool_calls") -%}
260            {%- set tcs = namespace(lines=[]) -%}
261            {%- for tc in m.tool_calls -%}
262                {%- set fn = tc.function if tc.function is defined else tc -%}
263                {%- set params = namespace(lines=[]) -%}
264                {%- set args = fn.arguments if fn.arguments is mapping else {"arguments": fn.arguments} -%}
265                {%- for k, v in args.items() -%}
266                    {%- set params.lines = params.lines + ["<" ~ DSML ~ " parameter name=\"" ~ k ~ "\" string=\"" ~ ("true" if v is string else "false") ~ "\">" ~ (v if v is string else v | tojson) ~ "</" ~ DSML ~ " parameter>"] -%}
267                {%- endfor -%}
268                {%- set name = qualified_name(fn.name, tc.get("namespace") or fn.get("namespace")) -%}
269                {%- set tcs.lines = tcs.lines + ["<" ~ DSML ~ " invoke name=\"" ~ name ~ "\">\n" ~ params.lines | join("\n") ~ "\n</" ~ DSML ~ " invoke>"] -%}
270            {%- endfor -%}
271            {{- "\n\n<" ~ DSML ~ " calls>\n" ~ tcs.lines | join("\n") ~ "\n</" ~ DSML ~ " calls>" -}}
272        {%- endif -%}
273        {%- if not m.get("wo_eos", false) -%}{{- "<|end▁of▁sentence|>" -}}{%- endif -%}
274
275    {%- else -%}
276        {{- raise_exception("Unknown role: " ~ m.role) -}}
277    {%- endif -%}
278
279    {#- Transition tokens, only when the next message is an assistant/latest_reminder turn or this is the last one. -#}
280    {%- if loop.last or msgs[idx + 1].role in ["assistant", "latest_reminder"] -%}
281        {%- set task = m.get("task") -%}
282        {%- if task is not none -%}
283            {%- set task_tokens = {"action": "<|action|>", "query": "<|query|>", "authority": "<|authority|>", "domain": "<|domain|>", "title": "<|title|>", "read_url": "<|read_url|>"} -%}
284            {%- if task not in task_tokens -%}{{- raise_exception("Invalid task: '" ~ task ~ "'") -}}{%- endif -%}
285            {%- if task == "action" -%}{{- "<|Assistant|>" ~ ("<think>" if is_thinking else "</think>") -}}{%- endif -%}
286            {{- task_tokens[task] -}}
287        {%- elif (m.role == "user" or (m.role == "system" and idx > 0)) and (add_generation_prompt or not loop.last) -%}
288            {{- "<|Assistant|>" ~ ("<think>" if is_thinking and (not effective_drop or idx >= state.last_user) else "</think>") -}}
289        {%- endif -%}
290    {%- endif -%}
291{%- endfor -%}