Instructions to use unsloth/DeepSeek-V4-Flash with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use unsloth/DeepSeek-V4-Flash with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="unsloth/DeepSeek-V4-Flash") messages = [ {"role": "user", "content": "Who are you?"}, ] pipe(messages)# Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("unsloth/DeepSeek-V4-Flash") model = AutoModelForCausalLM.from_pretrained("unsloth/DeepSeek-V4-Flash", device_map="auto") messages = [ {"role": "user", "content": "Who are you?"}, ] inputs = tokenizer.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=40) print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use unsloth/DeepSeek-V4-Flash with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "unsloth/DeepSeek-V4-Flash" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "unsloth/DeepSeek-V4-Flash", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker
docker model run hf.co/unsloth/DeepSeek-V4-Flash
- SGLang
How to use unsloth/DeepSeek-V4-Flash with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "unsloth/DeepSeek-V4-Flash" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "unsloth/DeepSeek-V4-Flash", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "unsloth/DeepSeek-V4-Flash" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "unsloth/DeepSeek-V4-Flash", "messages": [ { "role": "user", "content": "What is the capital of France?" } ] }' - Unsloth Studio
How to use unsloth/DeepSeek-V4-Flash with Unsloth Studio:
Install Unsloth Studio (macOS, Linux, WSL)
curl -fsSL https://unsloth.ai/install.sh | sh # Run unsloth studio unsloth studio -H 0.0.0.0 -p 8888 # Then open http://localhost:8888 in your browser # Search for unsloth/DeepSeek-V4-Flash to start chatting
Install Unsloth Studio (Windows)
irm https://unsloth.ai/install.ps1 | iex # Run unsloth studio unsloth studio -H 0.0.0.0 -p 8888 # Then open http://localhost:8888 in your browser # Search for unsloth/DeepSeek-V4-Flash to start chatting
Using HuggingFace Spaces for Unsloth
# No setup required # Open https://huggingface.co/spaces/unsloth/studio in your browser # Search for unsloth/DeepSeek-V4-Flash to start chatting
Load model with FastModel
pip install unsloth from unsloth import FastModel model, tokenizer = FastModel.from_pretrained( model_name="unsloth/DeepSeek-V4-Flash", max_seq_length=2048, ) - Docker Model Runner
How to use unsloth/DeepSeek-V4-Flash with Docker Model Runner:
docker model run hf.co/unsloth/DeepSeek-V4-Flash
Upload folder using huggingface_hub
Browse files- tokenizer_config.json +1 -1
tokenizer_config.json
CHANGED
|
@@ -10279,4 +10279,4 @@
|
|
| 10279 |
"tokenizer_class": "PreTrainedTokenizerFast",
|
| 10280 |
"unk_token": null,
|
| 10281 |
"chat_template": "{#- Unsloth template fixes #}\n{%- if not add_generation_prompt is defined -%}\n {%- set add_generation_prompt = false -%}\n{%- endif -%}\n{%- if not thinking is defined -%}\n {%- if enable_thinking is defined -%}\n {%- set thinking = enable_thinking -%}\n {%- else -%}\n {%- set thinking = false -%}\n {%- endif -%}\n{%- endif -%}\n{%- if not reasoning_effort is defined -%}\n {%- set reasoning_effort = none -%}\n{%- endif -%}\n{%- set dsml_token = '|DSML|' -%}\n{%- set thinking_start_token = '<think>' -%}\n{%- set thinking_end_token = '</think>' -%}\n{%- set reasoning_effort_max = 'Reasoning Effort: Absolute maximum with no shortcuts permitted.\\nYou MUST be very thorough in your thinking and comprehensively decompose the problem to resolve the root cause, rigorously stress-testing your logic against all potential paths, edge cases, and adversarial scenarios.\\nExplicitly write out your entire deliberation process, documenting every intermediate step, considered alternative, and rejected hypothesis to ensure absolutely no assumption is left unchecked.\\n\\n' -%}\n{%- set tools_header = '## 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_token + 'tool_calls>\" block like the following:\\n\\n<' + dsml_token + 'tool_calls>\\n<' + dsml_token + 'invoke name=\"$TOOL_NAME\">\\n<' + dsml_token + 'parameter name=\"$PARAMETER_NAME\" string=\"true|false\">$PARAMETER_VALUE</' + dsml_token + 'parameter>\\n...\\n</' + dsml_token + 'invoke>\\n<' + dsml_token + 'invoke name=\"$TOOL_NAME2\">\\n...\\n</' + dsml_token + 'invoke>\\n</' + dsml_token + 'tool_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 ' + thinking_start_token + '), you MUST output your complete reasoning inside ' + thinking_start_token + '...' + thinking_end_token + ' BEFORE any tool calls or final response.\\n\\nOtherwise, output directly after ' + thinking_end_token + ' with tool calls or final response.\\n\\n### Available Tool Schemas\\n\\n' -%}\n{%- set tools_footer = '\\nYou MUST strictly follow the above defined tool name and parameter schemas to invoke tool calls.\\n' -%}\n{%- set response_format_header = '## Response Format:\\n\\nYou MUST strictly adhere to the following schema to reply:\\n' -%}\n{#- Detect tools anywhere (top-level param or attached to any message): DeepSeek-V4's\n encoder disables reasoning-dropping entirely when tools are present. -#}\n{%- set tp = namespace(has=false) -%}\n{%- if tools is defined and tools -%}\n {%- set tp.has = true -%}\n{%- endif -%}\n{%- for message in messages -%}\n {%- if message['tools'] is defined and message['tools'] -%}\n {%- set tp.has = true -%}\n {%- endif -%}\n{%- endfor -%}\n{#- Build system prompt from all system messages (+ optional per-message response_format). -#}\n{%- set ns = namespace(system_prompt='', is_first_sp=true) -%}\n{%- for message in messages -%}\n {%- if message['role'] == 'system' -%}\n {%- if ns.is_first_sp -%}\n {%- set ns.system_prompt = ns.system_prompt + (message['content'] or '') -%}\n {%- set ns.is_first_sp = false -%}\n {%- else -%}\n {%- set ns.system_prompt = ns.system_prompt + '\\n\\n' + (message['content'] or '') -%}\n {%- endif -%}\n {%- if message['response_format'] is defined and message['response_format'] -%}\n {%- set ns.system_prompt = ns.system_prompt + '\\n\\n' + response_format_header + (message['response_format'] | tojson) -%}\n {%- endif -%}\n {%- endif -%}\n{%- endfor -%}\n{%- if tools is defined and tools -%}\n {%- set ts = namespace(schemas='') -%}\n {%- for tool in tools -%}\n {%- if tool['type'] == 'function' -%}\n {%- set ts.schemas = ts.schemas + (tool['function'] | tojson) + '\\n' -%}\n {%- endif -%}\n {%- endfor -%}\n {#- Match the reference: tools attach to the system message, so a system message\n that exists but is empty still contributes its \"\" + \"\\n\\n\" separator. -#}\n {%- if not ns.is_first_sp -%}\n {%- set ns.system_prompt = ns.system_prompt + '\\n\\n' + tools_header + ts.schemas + tools_footer -%}\n {%- else -%}\n {%- set ns.system_prompt = tools_header + ts.schemas + tools_footer -%}\n {%- endif -%}\n{%- endif -%}\n{{- bos_token -}}\n{%- if thinking and reasoning_effort == 'max' -%}\n {{- reasoning_effort_max -}}\n{%- endif -%}\n{{- ns.system_prompt -}}\n{#- Last user-like index: controls which turns keep reasoning. The encoder merges tool\n results into user messages before computing this, so tool turns count as user here. -#}\n{%- set last_user_idx = namespace(value=-1) -%}\n{%- for message in messages -%}\n {%- if message['role'] == 'user' or message['role'] == 'developer' or message['role'] == 'tool' -%}\n {%- set last_user_idx.value = loop.index0 -%}\n {%- endif -%}\n{%- endfor -%}\n{%- set state = namespace(in_user=false) -%}\n{%- for message in messages -%}\n {%- if message['role'] == 'user' -%}\n {%- if state.in_user -%}\n {{- '\\n\\n' -}}\n {%- else -%}\n {{- '<|User|>' -}}\n {%- set state.in_user = true -%}\n {%- endif -%}\n {{- message['content'] or '' -}}\n {%- if message['task'] is defined and message['task'] and (loop.last or messages[loop.index0 + 1]['role'] in ['assistant', 'latest_reminder']) -%}\n {%- set state.in_user = false -%}\n {%- if message['task'] == 'action' -%}\n {{- '<|Assistant|>' -}}\n {%- if thinking -%}{{- thinking_start_token -}}{%- else -%}{{- thinking_end_token -}}{%- endif -%}\n {{- '<|action|>' -}}\n {%- else -%}\n {{- '<|' + message['task'] + '|>' -}}\n {%- endif -%}\n {%- endif -%}\n {%- elif message['role'] == 'tool' -%}\n {%- if state.in_user -%}\n {{- '\\n\\n' -}}\n {%- else -%}\n {{- '<|User|>' -}}\n {%- set state.in_user = true -%}\n {%- endif -%}\n {{- '<tool_result>' + (message['content'] or '') + '</tool_result>' -}}\n {%- elif message['role'] == 'developer' -%}\n {#- The encoder's _drop_thinking_messages drops developer turns that sit before the\n last user turn when in thinking mode with reasoning-dropping active (no tools).\n (No-op under llama.cpp, which remaps developer -> system before templating.) -#}\n {%- if thinking and not tp.has and loop.index0 < last_user_idx.value -%}\n {%- else -%}\n {%- set state.in_user = false -%}\n {{- '<|User|>' + (message['content'] or '') -}}\n {%- if message['tools'] is defined and message['tools'] -%}\n {%- set ds = namespace(schemas='') -%}\n {%- for tool in message['tools'] -%}\n {%- if tool['type'] == 'function' -%}\n {%- set ds.schemas = ds.schemas + (tool['function'] | tojson) + '\\n' -%}\n {%- endif -%}\n {%- endfor -%}\n {{- '\\n\\n' + tools_header + ds.schemas + tools_footer -}}\n {%- endif -%}\n {%- if message['response_format'] is defined and message['response_format'] -%}\n {{- '\\n\\n' + response_format_header + (message['response_format'] | tojson) -}}\n {%- endif -%}\n {%- if message['task'] is defined and message['task'] and (loop.last or messages[loop.index0 + 1]['role'] in ['assistant', 'latest_reminder']) -%}\n {%- if message['task'] == 'action' -%}\n {{- '<|Assistant|>' -}}\n {%- if thinking -%}{{- thinking_start_token -}}{%- else -%}{{- thinking_end_token -}}{%- endif -%}\n {{- '<|action|>' -}}\n {%- else -%}\n {{- '<|' + message['task'] + '|>' -}}\n {%- endif -%}\n {%- endif -%}\n {%- endif -%}\n {%- elif message['role'] == 'latest_reminder' -%}\n {%- set state.in_user = false -%}\n {#- The encoder emits the user/developer -> assistant transition before a\n latest_reminder as well (the opening think stays open across the reminder and is\n closed by the following assistant). Same effective-predecessor rule as assistant. -#}\n {%- set ep = namespace(idx=(loop.index0 - 1), done=false, is_ud=false, has_task=false) -%}\n {%- for _i in range(loop.index0) -%}\n {%- if not ep.done and ep.idx >= 0 -%}\n {%- set _pm = messages[ep.idx] -%}\n {%- if (_pm['role'] == 'developer') and thinking and (not tp.has) and (ep.idx < last_user_idx.value) -%}\n {%- set ep.idx = ep.idx - 1 -%}\n {%- else -%}\n {%- set ep.done = true -%}\n {%- set ep.is_ud = _pm['role'] in ['user', 'developer', 'tool'] -%}\n {%- set ep.has_task = _pm['task'] is defined and _pm['task'] -%}\n {%- endif -%}\n {%- endif -%}\n {%- endfor -%}\n {%- if ep.has_task -%}\n {%- elif ep.is_ud -%}\n {{- '<|Assistant|>' -}}\n {%- if thinking and (tp.has or (loop.index0 > last_user_idx.value)) -%}\n {{- thinking_start_token -}}\n {%- else -%}\n {{- thinking_end_token -}}\n {%- endif -%}\n {%- endif -%}\n {{- '<|latest_reminder|>' + (message['content'] or '') -}}\n {%- elif message['role'] == 'assistant' -%}\n {%- set state.in_user = false -%}\n {#- The encoder emits the \"<|Assistant|>\" + opening think/end token as a trailing\n transition on a user/developer predecessor (tool results merge into user), never\n on the assistant message itself. Find the effective predecessor, skipping any\n developer the encoder drops (developer before the last user in thinking+drop). -#}\n {%- set ep = namespace(idx=(loop.index0 - 1), done=false, is_ud=false, has_task=false) -%}\n {%- for _i in range(loop.index0) -%}\n {%- if not ep.done and ep.idx >= 0 -%}\n {%- set _pm = messages[ep.idx] -%}\n {%- if (_pm['role'] == 'developer') and thinking and (not tp.has) and (ep.idx < last_user_idx.value) -%}\n {%- set ep.idx = ep.idx - 1 -%}\n {%- else -%}\n {%- set ep.done = true -%}\n {%- set ep.is_ud = _pm['role'] in ['user', 'developer', 'tool'] -%}\n {%- set ep.has_task = _pm['task'] is defined and _pm['task'] -%}\n {%- endif -%}\n {%- endif -%}\n {%- endfor -%}\n {%- set keep_reasoning = tp.has or (loop.index0 > last_user_idx.value) -%}\n {%- if ep.has_task -%}\n {#- The predecessor's task token already emitted the assistant prompt. -#}\n {%- elif ep.is_ud -%}\n {{- '<|Assistant|>' -}}\n {%- if keep_reasoning and thinking -%}\n {{- thinking_start_token -}}\n {%- if message['reasoning_content'] is defined and message['reasoning_content'] -%}\n {{- message['reasoning_content'] -}}\n {%- endif -%}\n {{- thinking_end_token -}}\n {%- else -%}\n {{- thinking_end_token -}}\n {%- endif -%}\n {%- else -%}\n {#- Predecessor is not user/developer/tool (first turn, latest_reminder, another\n assistant, or an all-dropped-developer run): the encoder emits no prefix and\n no opening think; a kept reasoning turn still closes with its content + </think>. -#}\n {%- if keep_reasoning and thinking -%}\n {%- if message['reasoning_content'] is defined and message['reasoning_content'] -%}\n {{- message['reasoning_content'] -}}\n {%- endif -%}\n {{- thinking_end_token -}}\n {%- endif -%}\n {%- endif -%}\n {%- if message['content'] is defined and message['content'] -%}\n {{- message['content'] -}}\n {%- endif -%}\n {%- if message['tool_calls'] -%}\n {{- '\\n\\n<' + dsml_token + 'tool_calls>\\n' -}}\n {%- for tool in message['tool_calls'] -%}\n {%- set func = tool['function'] -%}\n {{- '<' + dsml_token + 'invoke name=\"' + func['name'] + '\">\\n' -}}\n {%- set args = func['arguments'] -%}\n {%- if args is string -%}\n {%- set args = args | from_json -%}\n {%- endif -%}\n {%- for key, val in args.items() -%}\n {%- if val is string -%}\n {{- '<' + dsml_token + 'parameter name=\"' + key + '\" string=\"true\">' + val + '</' + dsml_token + 'parameter>\\n' -}}\n {%- else -%}\n {{- '<' + dsml_token + 'parameter name=\"' + key + '\" string=\"false\">' + (val | tojson) + '</' + dsml_token + 'parameter>\\n' -}}\n {%- endif -%}\n {%- endfor -%}\n {{- '</' + dsml_token + 'invoke>\\n' -}}\n {%- endfor -%}\n {{- '</' + dsml_token + 'tool_calls>' -}}\n {%- endif -%}\n {{- '<|end▁of▁sentence|>' -}}\n {%- if message['task'] is defined and message['task'] == 'title' and (loop.last or messages[loop.index0 + 1]['role'] in ['assistant', 'latest_reminder']) -%}\n {{- '<|title|>' -}}\n {%- endif -%}\n {%- endif -%}\n{%- endfor -%}\n{%- if add_generation_prompt -%}\n {#- Suppress the standard generation prefix when the final message already\n emitted a task token (which serves as the generation prompt). -#}\n {%- set _last = messages[messages | length - 1] -%}\n {%- if not (_last['task'] is defined and _last['task']) -%}\n {{- '<|Assistant|>' -}}\n {%- if thinking -%}\n {{- thinking_start_token -}}\n {%- else -%}\n {{- thinking_end_token -}}\n {%- endif -%}\n {%- endif -%}\n{%- endif -%}\n{#- Copyright 2026-present Unsloth. Apache 2.0 License. #}"
|
| 10282 |
-
}
|
|
|
|
| 10279 |
"tokenizer_class": "PreTrainedTokenizerFast",
|
| 10280 |
"unk_token": null,
|
| 10281 |
"chat_template": "{#- Unsloth template fixes #}\n{%- if not add_generation_prompt is defined -%}\n {%- set add_generation_prompt = false -%}\n{%- endif -%}\n{%- if not thinking is defined -%}\n {%- if enable_thinking is defined -%}\n {%- set thinking = enable_thinking -%}\n {%- else -%}\n {%- set thinking = false -%}\n {%- endif -%}\n{%- endif -%}\n{%- if not reasoning_effort is defined -%}\n {%- set reasoning_effort = none -%}\n{%- endif -%}\n{%- set dsml_token = '|DSML|' -%}\n{%- set thinking_start_token = '<think>' -%}\n{%- set thinking_end_token = '</think>' -%}\n{%- set reasoning_effort_max = 'Reasoning Effort: Absolute maximum with no shortcuts permitted.\\nYou MUST be very thorough in your thinking and comprehensively decompose the problem to resolve the root cause, rigorously stress-testing your logic against all potential paths, edge cases, and adversarial scenarios.\\nExplicitly write out your entire deliberation process, documenting every intermediate step, considered alternative, and rejected hypothesis to ensure absolutely no assumption is left unchecked.\\n\\n' -%}\n{%- set tools_header = '## 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_token + 'tool_calls>\" block like the following:\\n\\n<' + dsml_token + 'tool_calls>\\n<' + dsml_token + 'invoke name=\"$TOOL_NAME\">\\n<' + dsml_token + 'parameter name=\"$PARAMETER_NAME\" string=\"true|false\">$PARAMETER_VALUE</' + dsml_token + 'parameter>\\n...\\n</' + dsml_token + 'invoke>\\n<' + dsml_token + 'invoke name=\"$TOOL_NAME2\">\\n...\\n</' + dsml_token + 'invoke>\\n</' + dsml_token + 'tool_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 ' + thinking_start_token + '), you MUST output your complete reasoning inside ' + thinking_start_token + '...' + thinking_end_token + ' BEFORE any tool calls or final response.\\n\\nOtherwise, output directly after ' + thinking_end_token + ' with tool calls or final response.\\n\\n### Available Tool Schemas\\n\\n' -%}\n{%- set tools_footer = '\\nYou MUST strictly follow the above defined tool name and parameter schemas to invoke tool calls.\\n' -%}\n{%- set response_format_header = '## Response Format:\\n\\nYou MUST strictly adhere to the following schema to reply:\\n' -%}\n{#- Detect tools anywhere (top-level param or attached to any message): DeepSeek-V4's\n encoder disables reasoning-dropping entirely when tools are present. -#}\n{%- set tp = namespace(has=false) -%}\n{%- if tools is defined and tools -%}\n {%- set tp.has = true -%}\n{%- endif -%}\n{%- for message in messages -%}\n {%- if message['tools'] is defined and message['tools'] -%}\n {%- set tp.has = true -%}\n {%- endif -%}\n{%- endfor -%}\n{#- Build system prompt from all system messages (+ optional per-message response_format). -#}\n{%- set ns = namespace(system_prompt='', is_first_sp=true) -%}\n{%- for message in messages -%}\n {%- if message['role'] == 'system' -%}\n {%- if ns.is_first_sp -%}\n {%- set ns.system_prompt = ns.system_prompt + (message['content'] or '') -%}\n {%- set ns.is_first_sp = false -%}\n {%- else -%}\n {%- set ns.system_prompt = ns.system_prompt + '\\n\\n' + (message['content'] or '') -%}\n {%- endif -%}\n {%- if message['response_format'] is defined and message['response_format'] -%}\n {%- set ns.system_prompt = ns.system_prompt + '\\n\\n' + response_format_header + (message['response_format'] | tojson) -%}\n {%- endif -%}\n {%- endif -%}\n{%- endfor -%}\n{%- if tools is defined and tools -%}\n {%- set ts = namespace(schemas='') -%}\n {%- for tool in tools -%}\n {%- if tool['type'] == 'function' -%}\n {%- set ts.schemas = ts.schemas + (tool['function'] | tojson) + '\\n' -%}\n {%- endif -%}\n {%- endfor -%}\n {#- Match the reference: tools attach to the system message, so a system message\n that exists but is empty still contributes its \"\" + \"\\n\\n\" separator. -#}\n {%- if not ns.is_first_sp -%}\n {%- set ns.system_prompt = ns.system_prompt + '\\n\\n' + tools_header + ts.schemas + tools_footer -%}\n {%- else -%}\n {%- set ns.system_prompt = tools_header + ts.schemas + tools_footer -%}\n {%- endif -%}\n{%- endif -%}\n{{- bos_token -}}\n{%- if thinking and reasoning_effort == 'max' -%}\n {{- reasoning_effort_max -}}\n{%- endif -%}\n{{- ns.system_prompt -}}\n{#- Last user-like index: controls which turns keep reasoning. The encoder merges tool\n results into user messages before computing this, so tool turns count as user here. -#}\n{%- set last_user_idx = namespace(value=-1) -%}\n{%- for message in messages -%}\n {%- if message['role'] == 'user' or message['role'] == 'developer' or message['role'] == 'tool' -%}\n {%- set last_user_idx.value = loop.index0 -%}\n {%- endif -%}\n{%- endfor -%}\n{%- set state = namespace(in_user=false) -%}\n{%- for message in messages -%}\n {%- if message['role'] == 'user' -%}\n {%- if state.in_user -%}\n {{- '\\n\\n' -}}\n {%- else -%}\n {{- '<|User|>' -}}\n {%- set state.in_user = true -%}\n {%- endif -%}\n {{- message['content'] or '' -}}\n {%- if message['task'] is defined and message['task'] and (loop.last or messages[loop.index0 + 1]['role'] in ['assistant', 'latest_reminder']) -%}\n {%- set state.in_user = false -%}\n {%- if message['task'] == 'action' -%}\n {{- '<|Assistant|>' -}}\n {%- if thinking -%}{{- thinking_start_token -}}{%- else -%}{{- thinking_end_token -}}{%- endif -%}\n {{- '<|action|>' -}}\n {%- else -%}\n {{- '<|' + message['task'] + '|>' -}}\n {%- endif -%}\n {%- endif -%}\n {%- elif message['role'] == 'tool' -%}\n {%- if state.in_user -%}\n {{- '\\n\\n' -}}\n {%- else -%}\n {{- '<|User|>' -}}\n {%- set state.in_user = true -%}\n {%- endif -%}\n {{- '<tool_result>' + (message['content'] or '') + '</tool_result>' -}}\n {%- elif message['role'] == 'developer' -%}\n {#- The encoder's _drop_thinking_messages drops developer turns that sit before the\n last user turn when in thinking mode with reasoning-dropping active (no tools).\n (No-op under llama.cpp, which remaps developer -> system before templating.) -#}\n {%- if thinking and not tp.has and loop.index0 < last_user_idx.value -%}\n {%- else -%}\n {%- set state.in_user = false -%}\n {{- '<|User|>' + (message['content'] or '') -}}\n {%- if message['tools'] is defined and message['tools'] -%}\n {%- set ds = namespace(schemas='') -%}\n {%- for tool in message['tools'] -%}\n {%- if tool['type'] == 'function' -%}\n {%- set ds.schemas = ds.schemas + (tool['function'] | tojson) + '\\n' -%}\n {%- endif -%}\n {%- endfor -%}\n {{- '\\n\\n' + tools_header + ds.schemas + tools_footer -}}\n {%- endif -%}\n {%- if message['response_format'] is defined and message['response_format'] -%}\n {{- '\\n\\n' + response_format_header + (message['response_format'] | tojson) -}}\n {%- endif -%}\n {%- if message['task'] is defined and message['task'] and (loop.last or messages[loop.index0 + 1]['role'] in ['assistant', 'latest_reminder']) -%}\n {%- if message['task'] == 'action' -%}\n {{- '<|Assistant|>' -}}\n {%- if thinking -%}{{- thinking_start_token -}}{%- else -%}{{- thinking_end_token -}}{%- endif -%}\n {{- '<|action|>' -}}\n {%- else -%}\n {{- '<|' + message['task'] + '|>' -}}\n {%- endif -%}\n {%- endif -%}\n {%- endif -%}\n {%- elif message['role'] == 'latest_reminder' -%}\n {%- set state.in_user = false -%}\n {#- The encoder emits the user/developer -> assistant transition before a\n latest_reminder as well (the opening think stays open across the reminder and is\n closed by the following assistant). Same effective-predecessor rule as assistant. -#}\n {%- set ep = namespace(idx=(loop.index0 - 1), done=false, is_ud=false, has_task=false) -%}\n {%- for _i in range(loop.index0) -%}\n {%- if not ep.done and ep.idx >= 0 -%}\n {%- set _pm = messages[ep.idx] -%}\n {%- if (_pm['role'] == 'developer') and thinking and (not tp.has) and (ep.idx < last_user_idx.value) -%}\n {%- set ep.idx = ep.idx - 1 -%}\n {%- else -%}\n {%- set ep.done = true -%}\n {%- set ep.is_ud = _pm['role'] in ['user', 'developer', 'tool'] -%}\n {%- set ep.has_task = _pm['task'] is defined and _pm['task'] -%}\n {%- endif -%}\n {%- endif -%}\n {%- endfor -%}\n {%- if ep.has_task -%}\n {%- elif ep.is_ud -%}\n {{- '<|Assistant|>' -}}\n {%- if thinking and (tp.has or (loop.index0 > last_user_idx.value)) -%}\n {{- thinking_start_token -}}\n {%- else -%}\n {{- thinking_end_token -}}\n {%- endif -%}\n {%- endif -%}\n {{- '<|latest_reminder|>' + (message['content'] or '') -}}\n {%- elif message['role'] == 'assistant' -%}\n {%- set state.in_user = false -%}\n {#- The encoder emits the \"<|Assistant|>\" + opening think/end token as a trailing\n transition on a user/developer predecessor (tool results merge into user), never\n on the assistant message itself. Find the effective predecessor, skipping any\n developer the encoder drops (developer before the last user in thinking+drop). -#}\n {%- set ep = namespace(idx=(loop.index0 - 1), done=false, is_ud=false, has_task=false) -%}\n {%- for _i in range(loop.index0) -%}\n {%- if not ep.done and ep.idx >= 0 -%}\n {%- set _pm = messages[ep.idx] -%}\n {%- if (_pm['role'] == 'developer') and thinking and (not tp.has) and (ep.idx < last_user_idx.value) -%}\n {%- set ep.idx = ep.idx - 1 -%}\n {%- else -%}\n {%- set ep.done = true -%}\n {%- set ep.is_ud = _pm['role'] in ['user', 'developer', 'tool'] -%}\n {%- set ep.has_task = _pm['task'] is defined and _pm['task'] -%}\n {%- endif -%}\n {%- endif -%}\n {%- endfor -%}\n {%- set keep_reasoning = tp.has or (loop.index0 > last_user_idx.value) -%}\n {%- if ep.has_task -%}\n {#- The predecessor's task token already emitted the assistant prompt. -#}\n {%- elif ep.is_ud -%}\n {{- '<|Assistant|>' -}}\n {%- if keep_reasoning and thinking -%}\n {{- thinking_start_token -}}\n {%- if message['reasoning_content'] is defined and message['reasoning_content'] -%}\n {{- message['reasoning_content'] -}}\n {%- endif -%}\n {{- thinking_end_token -}}\n {%- else -%}\n {{- thinking_end_token -}}\n {%- endif -%}\n {%- else -%}\n {#- Predecessor is not user/developer/tool (first turn, latest_reminder, another\n assistant, or an all-dropped-developer run): the encoder emits no prefix and\n no opening think; a kept reasoning turn still closes with its content + </think>. -#}\n {%- if keep_reasoning and thinking -%}\n {%- if message['reasoning_content'] is defined and message['reasoning_content'] -%}\n {{- message['reasoning_content'] -}}\n {%- endif -%}\n {{- thinking_end_token -}}\n {%- endif -%}\n {%- endif -%}\n {%- if message['content'] is defined and message['content'] -%}\n {{- message['content'] -}}\n {%- endif -%}\n {%- if message['tool_calls'] -%}\n {{- '\\n\\n<' + dsml_token + 'tool_calls>\\n' -}}\n {%- for tool in message['tool_calls'] -%}\n {%- set func = tool['function'] -%}\n {{- '<' + dsml_token + 'invoke name=\"' + func['name'] + '\">\\n' -}}\n {%- set args = func['arguments'] -%}\n {%- if args is string -%}\n {%- set args = args | from_json -%}\n {%- endif -%}\n {%- for key, val in args.items() -%}\n {%- if val is string -%}\n {{- '<' + dsml_token + 'parameter name=\"' + key + '\" string=\"true\">' + val + '</' + dsml_token + 'parameter>\\n' -}}\n {%- else -%}\n {{- '<' + dsml_token + 'parameter name=\"' + key + '\" string=\"false\">' + (val | tojson) + '</' + dsml_token + 'parameter>\\n' -}}\n {%- endif -%}\n {%- endfor -%}\n {{- '</' + dsml_token + 'invoke>\\n' -}}\n {%- endfor -%}\n {{- '</' + dsml_token + 'tool_calls>' -}}\n {%- endif -%}\n {{- '<|end▁of▁sentence|>' -}}\n {%- if message['task'] is defined and message['task'] == 'title' and (loop.last or messages[loop.index0 + 1]['role'] in ['assistant', 'latest_reminder']) -%}\n {{- '<|title|>' -}}\n {%- endif -%}\n {%- endif -%}\n{%- endfor -%}\n{%- if add_generation_prompt -%}\n {#- Suppress the standard generation prefix when the final message already\n emitted a task token (which serves as the generation prompt). -#}\n {%- set _last = messages[messages | length - 1] -%}\n {%- if not (_last['task'] is defined and _last['task']) -%}\n {{- '<|Assistant|>' -}}\n {%- if thinking -%}\n {{- thinking_start_token -}}\n {%- else -%}\n {{- thinking_end_token -}}\n {%- endif -%}\n {%- endif -%}\n{%- endif -%}\n{#- Copyright 2026-present Unsloth. Apache 2.0 License. #}"
|
| 10282 |
+
}
|