{#- Based on Unsloth's DeepSeek-V4.1 template, extracted from unsloth/DeepSeek-V4.1-Flash-1017-GGUF at revision 60ad3651641b5c97679c807e064ab1d7a3ca1455. Adaptations: use DeepSeek's native reasoning-effort aliases and default; accept numeric reasoning_effort strings used by serving APIs; preserve content_blocks on tool messages as in DeepSeek's reference encoder. Image blocks render placeholders; the runtime supplies image embeddings through mmproj. -#} {%- set IMG = "<|deepseek_image|>" -%} {%- set DSML = "|DSML|" -%} {%- if thinking_mode is not defined or thinking_mode is none -%} {%- if enable_thinking is defined and enable_thinking is not none -%} {%- set thinking_mode = "thinking" if enable_thinking else "chat" -%} {%- elif thinking is defined and thinking is not none and thinking is not string -%} {%- set thinking_mode = "thinking" if thinking else "chat" -%} {%- else -%} {%- set thinking_mode = "thinking" -%} {%- endif -%} {%- endif -%} {%- if thinking_mode not in ["chat", "thinking"] -%} {{- raise_exception("Invalid thinking_mode `" ~ thinking_mode ~ "`, should be \"chat\" or \"thinking\"") -}} {%- endif -%} {%- if drop_thinking is not defined or drop_thinking is none -%}{%- set drop_thinking = true -%}{%- endif -%} {%- if add_generation_prompt is not defined -%}{%- set add_generation_prompt = false -%}{%- endif -%} {%- if tools is not defined -%}{%- set tools = none -%}{%- endif -%} {%- if response_format is not defined -%}{%- set response_format = none -%}{%- endif -%} {%- if reasoning_effort is not defined or reasoning_effort is none -%}{%- set reasoning_effort = "high" -%}{%- endif -%} {%- set effort_map = {"low": 50, "high": 75, "max": 100} -%} {%- if reasoning_effort is string and reasoning_effort in effort_map -%} {%- set budget = effort_map[reasoning_effort] -%} {%- elif reasoning_effort is integer and reasoning_effort is not boolean and reasoning_effort >= 1 and reasoning_effort <= 100 -%} {%- set budget = reasoning_effort -%} {%- elif reasoning_effort is string and (reasoning_effort | int | string) == reasoning_effort and (reasoning_effort | int) >= 1 and (reasoning_effort | int) <= 100 -%} {%- set budget = reasoning_effort | int -%} {%- else -%} {{- raise_exception("Invalid reasoning effort: " ~ reasoning_effort ~ ", should be int within [1,100] or one of \"low\", \"high\", \"max\"") -}} {%- endif -%} {#- Splits/validates "ns::name" against an explicit namespace and returns the qualified name. -#} {%- macro qualified_name(name, ns) -%} {%- if ns is mapping -%}{%- set ns = ns.name -%}{%- endif -%} {%- set parts = name.split("::", 1) -%} {%- if parts | length == 2 -%} {%- if ns is not none and ns != parts[0] -%}{{- raise_exception("Conflicting tool namespaces: " ~ ns ~ " != " ~ parts[0]) -}}{%- endif -%} {%- set ns = parts[0] -%} {%- set name = parts[1] -%} {%- endif -%} {%- if "::" in name -%}{{- raise_exception("Tool name must not contain '::': " ~ name) -}}{%- endif -%} {%- if ns is not none and "::" in ns -%}{{- raise_exception("Tool namespace must not contain '::': " ~ ns) -}}{%- endif -%} {{- name if ns is none else ns ~ "::" ~ name -}} {%- endmacro -%} {%- macro render_tools(tool_list) -%} {%- set ts = namespace(schemas=[]) -%} {%- for tool in tool_list -%} {%- set fn = tool.function if tool.function is defined else tool -%} {%- set ns = tool.namespace if tool.namespace is defined and tool.namespace is not none else fn.get("namespace") -%} {#- JSON assembled by hand (key order kept, name/description replaced in place): llama.cpp has no ** unpacking. -#} {%- set qname = qualified_name(fn.name, ns) -%} {%- set sch = namespace(parts=[]) -%} {%- for k, v in fn.items() if k != "namespace" -%} {%- if k == "name" -%}{%- set v = qname -%} {%- elif k == "description" and ns is mapping and ns.description -%}{%- set v = ns.description ~ "\n" ~ (fn.description or "") -%} {%- endif -%} {%- set sch.parts = sch.parts + [(k | tojson) ~ ": " ~ (v | tojson)] -%} {%- endfor -%} {%- if "description" not in fn and ns is mapping and ns.description -%} {%- set sch.parts = sch.parts + ['"description": ' ~ ((ns.description ~ "\n") | tojson)] -%} {%- endif -%} {%- set ts.schemas = ts.schemas + ["{" ~ sch.parts | join(", ") ~ "}"] -%} {%- endfor -%} {{- "## 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\n...\n\n<|DSML| invoke name=\"$TOOL_NAME2\">\n...\n\n\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 ), you MUST output your complete reasoning inside ... BEFORE any tool calls or final response.\n\nOtherwise, output directly after with tool calls or final response.\n\n### Available Tool Schemas\n\n" -}} {{- ts.schemas | join("\n") -}} {{- "\n\nYou MUST strictly follow the above defined tool name and parameter schemas to invoke tool calls.\n" -}} {%- endmacro -%} {#- 1. Merge tool results (and consecutive user turns) into user messages made of content blocks. -#} {%- set mns = namespace(msgs=[]) -%} {%- for msg in messages -%} {%- set content = msg.get("content") -%} {%- if msg.role == "tool" and msg.get("content_blocks") and content is not string -%} {%- set content = msg.content_blocks -%} {%- endif -%} {%- set is_blocks = content is iterable and content is not string and content is not mapping -%} {%- if content is string and IMG in content -%} {{- raise_exception("Message content contains image special token " ~ IMG ~ ". Images should be provided as image content blocks.") -}} {%- endif -%} {%- set rc = msg.reasoning_content if msg.get("reasoning_content") is not none else msg.get("thinking") -%} {%- if rc is string and IMG in rc -%} {{- raise_exception("reasoning_content contains image special token " ~ IMG) -}} {%- endif -%} {%- set prev = mns.msgs[-1] if mns.msgs else none -%} {%- if msg.role == "tool" -%} {%- set tblock = {"type": "tool_result", "tool_use_id": msg.get("tool_call_id", ""), "content": content if content is not none else ""} -%} {%- if prev is not none and prev.role == "user" and prev.content_blocks is defined -%} {%- set mns.msgs = mns.msgs[:-1] + [dict(prev, content_blocks=prev.content_blocks + [tblock])] -%} {%- else -%} {%- set mns.msgs = mns.msgs + [{"role": "user", "content_blocks": [tblock]}] -%} {%- endif -%} {%- elif msg.role == "user" -%} {%- set blocks = msg.content_blocks if msg.get("content_blocks") is not none else (content if is_blocks else [{"type": "text", "text": content or ""}]) -%} {%- if prev is not none and prev.role == "user" and prev.content_blocks is defined and prev.get("task") is none -%} {%- set mns.msgs = mns.msgs[:-1] + [dict(prev, content_blocks=prev.content_blocks + blocks)] -%} {%- else -%} {%- set mns.msgs = mns.msgs + [dict(msg, content_blocks=blocks)] -%} {%- endif -%} {%- else -%} {%- set mns.msgs = mns.msgs + [msg] -%} {%- endif -%} {%- endfor -%} {#- Top-level tools / response_format attach to a leading system message, or to a new empty one. -#} {%- if tools or response_format -%} {%- if not (mns.msgs and mns.msgs[0].role == "system") -%} {%- set mns.msgs = [{"role": "system", "content": ""}] + mns.msgs -%} {%- endif -%} {%- set first = mns.msgs[0] -%} {%- if tools and not first.get("tools") -%}{%- set first = dict(first, tools=tools) -%}{%- endif -%} {%- if response_format and not first.get("response_format") -%}{%- set first = dict(first, response_format=response_format) -%}{%- endif -%} {%- set mns.msgs = [first] + mns.msgs[1:] -%} {%- endif -%} {#- 2. Reorder tool results to match the tool_calls order of the preceding assistant message. -#} {%- set sns = namespace(msgs=[], ids=[]) -%} {%- for m in mns.msgs -%} {%- if m.role == "assistant" and m.get("tool_calls") -%} {%- set sns.ids = [] -%} {%- for tc in m.tool_calls -%} {%- set sns.ids = sns.ids + [tc.get("id") or (tc.function.get("id", "") if tc.function is defined else "")] -%} {%- endfor -%} {%- set sns.msgs = sns.msgs + [m] -%} {%- elif m.role == "user" and m.content_blocks -%} {%- set tb = namespace(blocks=[]) -%} {%- for b in m.content_blocks if b.type == "tool_result" -%} {%- set order = namespace(idx=0) -%} {%- for tc_id in sns.ids -%} {%- if tc_id and tc_id == b.get("tool_use_id", "") -%}{%- set order.idx = loop.index0 -%}{%- endif -%} {%- endfor -%} {%- set tb.blocks = tb.blocks + [dict(b, _order=order.idx)] -%} {%- endfor -%} {%- if tb.blocks | length > 1 and sns.ids -%} {%- set sorted_blocks = tb.blocks | sort(attribute="_order") -%} {%- set rb = namespace(blocks=[], i=0) -%} {%- for b in m.content_blocks -%} {%- if b.type == "tool_result" -%} {%- set rb.blocks = rb.blocks + [sorted_blocks[rb.i]] -%} {%- set rb.i = rb.i + 1 -%} {%- else -%} {%- set rb.blocks = rb.blocks + [b] -%} {%- endif -%} {%- endfor -%} {%- set sns.msgs = sns.msgs + [dict(m, content_blocks=rb.blocks)] -%} {%- else -%} {%- set sns.msgs = sns.msgs + [m] -%} {%- endif -%} {%- else -%} {%- set sns.msgs = sns.msgs + [m] -%} {%- endif -%} {%- endfor -%} {%- set msgs = sns.msgs -%} {#- Last user turn: users and mid-conversation systems count, except user turns made only of tool results. Tools anywhere disable drop_thinking. -#} {%- set state = namespace(last_user=-1, has_tools=false) -%} {%- for m in msgs -%} {%- if m.role == "user" or (m.role == "system" and loop.index0 > 0) -%} {%- set real = namespace(v=true) -%} {%- if m.get("content_blocks") -%} {%- set real.v = false -%} {%- for b in m.content_blocks if b.type in ["text", "image", "image_url"] -%}{%- set real.v = true -%}{%- endfor -%} {%- endif -%} {%- if real.v -%}{%- set state.last_user = loop.index0 -%}{%- endif -%} {%- endif -%} {%- if m.get("tools") -%}{%- set state.has_tools = true -%}{%- endif -%} {%- endfor -%} {%- set effective_drop = drop_thinking and not state.has_tools -%} {%- set is_thinking = thinking_mode == "thinking" -%} {#- Like encoding.py, thinking mode with drop_thinking silently drops messages of other roles before the last user turn. -#} {%- if is_thinking and effective_drop -%} {%- set kept = namespace(msgs=[]) -%} {%- for m in msgs -%} {%- if m.role in ["user", "system", "tool", "latest_reminder", "direct_search_results", "assistant"] or loop.index0 >= state.last_user -%} {%- set kept.msgs = kept.msgs + [m] -%} {%- endif -%} {%- endfor -%} {%- if kept.msgs | length != msgs | length -%} {%- set msgs = kept.msgs -%} {%- set state.last_user = -1 -%} {%- for m in msgs -%} {%- if m.role == "user" or (m.role == "system" and loop.index0 > 0) -%} {%- set real = namespace(v=true) -%} {%- if m.get("content_blocks") -%} {%- set real.v = false -%} {%- for b in m.content_blocks if b.type in ["text", "image", "image_url"] -%}{%- set real.v = true -%}{%- endfor -%} {%- endif -%} {%- if real.v -%}{%- set state.last_user = loop.index0 -%}{%- endif -%} {%- endif -%} {%- endfor -%} {%- endif -%} {%- endif -%} {{- "<|begin▁of▁sentence|>" -}} {#- 3. Render each message. -#} {%- for m in msgs -%} {%- set idx = loop.index0 -%} {%- set content = m.get("content") -%} {%- if content is iterable and content is not string and content is not mapping -%} {%- set sp = namespace(parts=[]) -%} {%- for b in content -%} {%- if b.type == "text" -%}{%- set sp.parts = sp.parts + [b.text or ""] -%} {%- elif b.type in ["image", "image_url"] -%}{%- set sp.parts = sp.parts + [IMG] -%} {%- endif -%} {%- endfor -%} {%- set content = sp.parts | join("\n\n") -%} {%- endif -%} {%- if idx == 0 -%} {%- if is_thinking or m.role == "system" -%}{{- "<|System|>" -}}{%- endif -%} {%- if is_thinking -%}{{- "Reasoning Effort: " ~ budget ~ " (range 1-100, the higher the value, the more thorough the reasoning)\n\n" -}}{%- endif -%} {%- endif -%} {%- if m.role == "system" -%} {%- if idx > 0 -%}{{- "<|System|>" -}}{%- endif -%} {{- content or "" -}} {%- if m.get("tools") -%}{{- "\n\n" ~ render_tools(m.tools) -}}{%- endif -%} {%- if m.get("response_format") -%} {{- "\n\n## Response Format:\n\nYou MUST strictly adhere to the following schema to reply:\n" ~ (m.response_format | tojson) -}} {%- endif -%} {%- elif m.role == "user" -%} {{- "<|User|>" -}} {%- set up = namespace(parts=[]) -%} {%- for b in m.content_blocks -%} {%- if b.type == "text" -%} {%- if IMG in (b.text or "") -%}{{- raise_exception("Text block contains image placeholder " ~ IMG ~ ". Images should be separate content blocks.") -}}{%- endif -%} {%- set up.parts = up.parts + [b.text or ""] -%} {%- elif b.type in ["image", "image_url"] -%} {%- set up.parts = up.parts + [IMG] -%} {%- elif b.type == "tool_result" -%} {%- set tc = b.get("content", "") -%} {%- if tc is iterable and tc is not string and tc is not mapping -%} {%- set sub = namespace(parts=[]) -%} {%- for s in tc -%} {%- if s.type == "text" -%} {%- if IMG in (s.text or "") -%}{{- raise_exception("Text block contains image placeholder " ~ IMG ~ ". Images should be separate content blocks.") -}}{%- endif -%} {%- set sub.parts = sub.parts + [s.get("text", "")] -%} {%- elif s.type in ["image", "image_url"] -%}{%- set sub.parts = sub.parts + [IMG] -%} {%- else -%}{%- set sub.parts = sub.parts + ["[Unsupported " ~ s.type ~ "]"] -%} {%- endif -%} {%- endfor -%} {%- set tc = sub.parts | join("\n\n") -%} {%- endif -%} {%- set up.parts = up.parts + ["" ~ tc ~ ""] -%} {%- else -%} {%- set up.parts = up.parts + ["[Unsupported " ~ b.type ~ "]"] -%} {%- endif -%} {%- endfor -%} {{- up.parts | join("\n\n") -}} {%- elif m.role == "latest_reminder" -%} {{- "<|latest_reminder|>" ~ (content or "") -}} {%- elif m.role == "assistant" -%} {%- set prev_has_task = idx > 0 and msgs[idx - 1].get("task") is not none -%} {%- if is_thinking and not prev_has_task and (not effective_drop or idx > state.last_user) -%} {{- ((m.reasoning_content if m.get("reasoning_content") is not none else m.get("thinking")) or "") ~ "" -}} {%- endif -%} {{- content or "" -}} {%- if m.get("tool_calls") -%} {%- set tcs = namespace(lines=[]) -%} {%- for tc in m.tool_calls -%} {%- set fn = tc.function if tc.function is defined else tc -%} {%- set params = namespace(lines=[]) -%} {%- set args = fn.arguments if fn.arguments is mapping else {"arguments": fn.arguments} -%} {%- for k, v in args.items() -%} {%- 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) ~ ""] -%} {%- endfor -%} {%- set name = qualified_name(fn.name, tc.get("namespace") or fn.get("namespace")) -%} {%- set tcs.lines = tcs.lines + ["<" ~ DSML ~ " invoke name=\"" ~ name ~ "\">\n" ~ params.lines | join("\n") ~ "\n"] -%} {%- endfor -%} {{- "\n\n<" ~ DSML ~ " calls>\n" ~ tcs.lines | join("\n") ~ "\n" -}} {%- endif -%} {%- if not m.get("wo_eos", false) -%}{{- "<|end▁of▁sentence|>" -}}{%- endif -%} {%- else -%} {{- raise_exception("Unknown role: " ~ m.role) -}} {%- endif -%} {#- Transition tokens, only when the next message is an assistant/latest_reminder turn or this is the last one. -#} {%- if loop.last or msgs[idx + 1].role in ["assistant", "latest_reminder"] -%} {%- set task = m.get("task") -%} {%- if task is not none -%} {%- set task_tokens = {"action": "<|action|>", "query": "<|query|>", "authority": "<|authority|>", "domain": "<|domain|>", "title": "<|title|>", "read_url": "<|read_url|>"} -%} {%- if task not in task_tokens -%}{{- raise_exception("Invalid task: '" ~ task ~ "'") -}}{%- endif -%} {%- if task == "action" -%}{{- "<|Assistant|>" ~ ("" if is_thinking else "") -}}{%- endif -%} {{- task_tokens[task] -}} {%- elif (m.role == "user" or (m.role == "system" and idx > 0)) and (add_generation_prompt or not loop.last) -%} {{- "<|Assistant|>" ~ ("" if is_thinking and (not effective_drop or idx >= state.last_user) else "") -}} {%- endif -%} {%- endif -%} {%- endfor -%}