Instructions to use deepseek-ai/DeepSeek-V4-Flash-0731 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use deepseek-ai/DeepSeek-V4-Flash-0731 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="deepseek-ai/DeepSeek-V4-Flash-0731")# pip install -U transformers accelerate # Load model directly from transformers import AutoTokenizer, AutoModelForCausalLM tokenizer = AutoTokenizer.from_pretrained("deepseek-ai/DeepSeek-V4-Flash-0731") model = AutoModelForCausalLM.from_pretrained("deepseek-ai/DeepSeek-V4-Flash-0731", device_map="auto") - Inference
- HuggingChat
- Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use deepseek-ai/DeepSeek-V4-Flash-0731 with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "deepseek-ai/DeepSeek-V4-Flash-0731" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "deepseek-ai/DeepSeek-V4-Flash-0731", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/deepseek-ai/DeepSeek-V4-Flash-0731
- SGLang
How to use deepseek-ai/DeepSeek-V4-Flash-0731 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 "deepseek-ai/DeepSeek-V4-Flash-0731" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "deepseek-ai/DeepSeek-V4-Flash-0731", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'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 "deepseek-ai/DeepSeek-V4-Flash-0731" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "deepseek-ai/DeepSeek-V4-Flash-0731", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use deepseek-ai/DeepSeek-V4-Flash-0731 with Docker Model Runner:
docker model run hf.co/deepseek-ai/DeepSeek-V4-Flash-0731
Add missing chat_template.jinja
Browse files## Missing chat_template.jinja
This repo ships an `encoding/` folder with a Python reference encoder (`encoding_dsv4.py`) instead of a Jinja `chat_template`. That means any server using the standard `tokenizer.apply_chat_template()` / a generic OpenAI-compatible endpoint (rather than the Python encoder directly) currently has no correct way to format prompts for this model.
This PR adds `chat_template.jinja`, reverse-engineered directly from `encoding/encoding_dsv4.py` and cross-checked against a real, locally-tokenized comparison of 48 hand-built conversations (system/no-system, multi-turn, reasoning retention on/off, all three `reasoning_effort` levels in both `chat_template_kwargs.thinking_mode` values, and tool calling) — all 48 now produce byte-identical token IDs to the reference encoder's output.
It also matches real serving behavior verified against a live vLLM 0.26.0 deployment on actual GPU hardware, including two things worth flagging separately since they affect vLLM's own built-in support for this model (opening a corresponding issue/PR there):
1. vLLM's native `tokenizer_mode=deepseek_v4` path currently produces identical output for `reasoning_effort="low"` and `"high"` (only `"max"` gets a non-empty prefix) — this template correctly distinguishes all three.
2. Tool schemas must arrive as the standard top-level `tools` request field (not embedded in a message) for a template/renderer to see them at all in most serving stacks — this template supports both, in case a caller uses either convention.
Covers: BOS/EOS/turn tokens, system message placement (including no-system), `reasoning_content` retention rules (including the `tools`-present override), `thinking_mode` chat/thinking behavior, `reasoning_effort` prefixes for low/high/max, and DSML-format tool calls (including folding `tool` role results into the preceding user turn).
Not covered (documented in the template's own header comment): the internal `developer` role and quick-instruction task tokens, both explicitly described in this repo's own `encoding/README.md` as internal-pipeline-only, not reachable through a standard `/v1/chat/completions` request.
- chat_template.jinja +122 -0
|
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{#- DeepSeek-V4-Flash-0731 chat template.
|
| 2 |
+
Reverse-engineered from meta/encoding/encoding_dsv4.py (encode_messages), since the model
|
| 3 |
+
does not ship a Jinja template. See findings.md for the derivation and known limitations
|
| 4 |
+
(developer role, quick-instruction task tokens, wo_eos, multi-turn tool-result sorting for
|
| 5 |
+
out-of-order tool_call_id are not implemented — internal-pipeline-only per the encoder's own
|
| 6 |
+
README, not reachable through /v1/chat/completions). -#}
|
| 7 |
+
{%- set thinking_mode = thinking_mode | default('thinking') -%}
|
| 8 |
+
{%- set reasoning_effort = reasoning_effort | default('low') -%}
|
| 9 |
+
{%- set drop_thinking = drop_thinking | default(true) -%}
|
| 10 |
+
|
| 11 |
+
{%- set dsml = '|DSML|' -%}
|
| 12 |
+
{%- set think_open = '<think>' -%}
|
| 13 |
+
{%- set think_close = '</think>' -%}
|
| 14 |
+
{%- set bos = '<|begin▁of▁sentence|>' -%}
|
| 15 |
+
{%- set eos = '<|end▁of▁sentence|>' -%}
|
| 16 |
+
|
| 17 |
+
{%- set effort_prefix = {
|
| 18 |
+
'low': '',
|
| 19 |
+
'high': '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',
|
| 20 |
+
'max': 'Reasoning Effort: Beyond maximum — exhaustive, relentless, and uncompromising.\nYou MUST reason with the utmost depth and rigor, leaving absolutely nothing to chance: exhaustively decompose the problem into its most fundamental components, trace every causal chain to its root, and resolve the underlying cause rather than any surface symptom.\nDo not stop reasoning until you have independently verified the solution from multiple angles and are certain that no assumption remains unchecked and no error remains undiscovered.\n\n'
|
| 21 |
+
}[reasoning_effort] -%}
|
| 22 |
+
|
| 23 |
+
{%- 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 + 'tool_calls>" block like the following:\n\n<' + dsml + 'tool_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 + '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 ' + think_open + '), you MUST output your complete reasoning inside ' + think_open + '...' + think_close + ' BEFORE any tool calls or final response.\n\nOtherwise, output directly after ' + think_close + ' with tool calls or final response.\n\n### Available Tool Schemas\n\n' -%}
|
| 24 |
+
{%- set tools_footer = '\nYou MUST strictly follow the above defined tool name and parameter schemas to invoke tool calls.\n' -%}
|
| 25 |
+
|
| 26 |
+
{#- last_user_idx: index of the last user/tool-result turn; any_tools: whether any message
|
| 27 |
+
defines a `tools` schema (message-level, e.g. `developer` role) OR the top-level `tools`
|
| 28 |
+
kwarg was passed (the real /v1/chat/completions wire-format path: OpenAI clients send
|
| 29 |
+
`tools` as a top-level request field, not embedded in a message; vLLM's serving layer
|
| 30 |
+
only preserves a message-level `tools` key for role=="developer", which is unreachable
|
| 31 |
+
via the real API -- see findings.md). Either source forces full reasoning retention. -#}
|
| 32 |
+
{%- set ns = namespace(any_tools=false, last_user_idx=-1) -%}
|
| 33 |
+
{%- if tools is defined and tools -%}{%- set ns.any_tools = true -%}{%- endif -%}
|
| 34 |
+
{%- for m in messages -%}
|
| 35 |
+
{%- if m.get('tools') -%}{%- set ns.any_tools = true -%}{%- endif -%}
|
| 36 |
+
{%- if m['role'] in ['user', 'tool'] -%}{%- set ns.last_user_idx = loop.index0 -%}{%- endif -%}
|
| 37 |
+
{%- endfor -%}
|
| 38 |
+
{%- set effective_drop_thinking = false if ns.any_tools else drop_thinking -%}
|
| 39 |
+
|
| 40 |
+
{{- bos -}}
|
| 41 |
+
{%- if thinking_mode == 'thinking' -%}{{- effort_prefix -}}{%- endif -%}
|
| 42 |
+
|
| 43 |
+
{%- macro render_tools_block(schema_list) -%}
|
| 44 |
+
{%- set ts = namespace(schemas=[]) -%}
|
| 45 |
+
{%- for tool in schema_list -%}
|
| 46 |
+
{%- set ts.schemas = ts.schemas + [tool['function'] | tojson] -%}
|
| 47 |
+
{%- endfor -%}
|
| 48 |
+
{{- tools_header + ts.schemas | join('\n') + '\n' + tools_footer -}}
|
| 49 |
+
{%- endmacro -%}
|
| 50 |
+
|
| 51 |
+
{#- top-level `tools` (the real wire-format path -- see ns.any_tools comment above) attaches
|
| 52 |
+
to the first message if it's a system message, otherwise a tools-only synthetic system
|
| 53 |
+
turn is emitted first, matching vLLM's own native DeepSeek-V4 encoder behavior. -#}
|
| 54 |
+
{%- set state = namespace(in_user=false, tools_rendered=false) -%}
|
| 55 |
+
{%- if tools is defined and tools and not (messages and messages[0]['role'] == 'system') -%}
|
| 56 |
+
{{- '\n\n' -}}{{- render_tools_block(tools) -}}
|
| 57 |
+
{%- set state.tools_rendered = true -%}
|
| 58 |
+
{%- endif -%}
|
| 59 |
+
{%- for m in messages -%}
|
| 60 |
+
{%- if m['role'] == 'system' -%}
|
| 61 |
+
{{- m['content'] or '' -}}
|
| 62 |
+
{%- set msg_tools = m.get('tools') or (tools if (loop.first and not state.tools_rendered and tools is defined and tools) else none) -%}
|
| 63 |
+
{%- if msg_tools -%}
|
| 64 |
+
{{- '\n\n' -}}{{- render_tools_block(msg_tools) -}}
|
| 65 |
+
{%- set state.tools_rendered = true -%}
|
| 66 |
+
{%- endif -%}
|
| 67 |
+
{%- elif m['role'] in ['user', 'tool'] -%}
|
| 68 |
+
{%- if state.in_user -%}
|
| 69 |
+
{{- '\n\n' -}}
|
| 70 |
+
{%- else -%}
|
| 71 |
+
{{- '<|User|>' -}}
|
| 72 |
+
{%- set state.in_user = true -%}
|
| 73 |
+
{%- endif -%}
|
| 74 |
+
{%- if m['role'] == 'tool' -%}
|
| 75 |
+
{{- '<tool_result>' + (m['content'] or '') + '</tool_result>' -}}
|
| 76 |
+
{%- else -%}
|
| 77 |
+
{{- m['content'] or '' -}}
|
| 78 |
+
{%- endif -%}
|
| 79 |
+
{%- elif m['role'] == 'assistant' -%}
|
| 80 |
+
{%- set state.in_user = false -%}
|
| 81 |
+
{%- set retain_reasoning = (not effective_drop_thinking) or (loop.index0 > ns.last_user_idx) -%}
|
| 82 |
+
{{- '<|Assistant|>' -}}
|
| 83 |
+
{%- if thinking_mode == 'thinking' and retain_reasoning -%}
|
| 84 |
+
{{- think_open + (m.get('reasoning_content') or '') + think_close -}}
|
| 85 |
+
{%- else -%}
|
| 86 |
+
{{- think_close -}}
|
| 87 |
+
{%- endif -%}
|
| 88 |
+
{{- m['content'] or '' -}}
|
| 89 |
+
{%- if m.get('tool_calls') -%}
|
| 90 |
+
{{- '\n\n<' + dsml + 'tool_calls>\n' -}}
|
| 91 |
+
{%- for tc in m['tool_calls'] -%}
|
| 92 |
+
{%- set func = tc['function'] -%}
|
| 93 |
+
{{- '<' + dsml + 'invoke name="' + func['name'] + '">\n' -}}
|
| 94 |
+
{#- func['arguments'] must already be a mapping here, not a JSON string: this
|
| 95 |
+
Jinja environment (transformers) only exposes a `tojson` filter, no
|
| 96 |
+
`from_json`/`fromjson` counterpart, so the caller (server-side request
|
| 97 |
+
handling) must parse OpenAI's wire-format arguments string into a dict
|
| 98 |
+
before building the messages list handed to apply_chat_template. -#}
|
| 99 |
+
{%- set args = func['arguments'] -%}
|
| 100 |
+
{%- for key, val in args.items() -%}
|
| 101 |
+
{%- if val is string -%}
|
| 102 |
+
{{- '<' + dsml + 'parameter name="' + key + '" string="true">' + val + '</' + dsml + 'parameter>\n' -}}
|
| 103 |
+
{%- else -%}
|
| 104 |
+
{{- '<' + dsml + 'parameter name="' + key + '" string="false">' + (val | tojson) + '</' + dsml + 'parameter>\n' -}}
|
| 105 |
+
{%- endif -%}
|
| 106 |
+
{%- endfor -%}
|
| 107 |
+
{{- '</' + dsml + 'invoke>\n' -}}
|
| 108 |
+
{%- endfor -%}
|
| 109 |
+
{{- '</' + dsml + 'tool_calls>' -}}
|
| 110 |
+
{%- endif -%}
|
| 111 |
+
{{- eos -}}
|
| 112 |
+
{%- endif -%}
|
| 113 |
+
{%- endfor -%}
|
| 114 |
+
|
| 115 |
+
{%- if add_generation_prompt and messages[-1]['role'] in ['user', 'tool'] -%}
|
| 116 |
+
{{- '<|Assistant|>' -}}
|
| 117 |
+
{%- if thinking_mode == 'thinking' -%}
|
| 118 |
+
{{- think_open -}}
|
| 119 |
+
{%- else -%}
|
| 120 |
+
{{- think_close -}}
|
| 121 |
+
{%- endif -%}
|
| 122 |
+
{%- endif -%}
|