smalinin commited on
Commit
dc06f45
·
verified ·
1 Parent(s): 4df0123

Upload deepseek-ai-DeepSeek-V4.1.jinja

Browse files
Files changed (1) hide show
  1. deepseek-ai-DeepSeek-V4.1.jinja +156 -0
deepseek-ai-DeepSeek-V4.1.jinja ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {%- if not add_generation_prompt is defined -%}
2
+ {%- set add_generation_prompt = false -%}
3
+ {%- endif -%}
4
+ {%- if not thinking is defined -%}
5
+ {%- if enable_thinking is defined -%}
6
+ {%- set thinking = enable_thinking -%}
7
+ {%- else -%}
8
+ {%- set thinking = true -%}
9
+ {%- endif -%}
10
+ {%- endif -%}
11
+ {%- if not drop_thinking is defined -%}
12
+ {%- set drop_thinking = true -%}
13
+ {%- endif -%}
14
+ {%- set dsml_token = '|DSML|' -%}
15
+ {#- tool_calls_block_name: V4.1 -#}
16
+ {%- set thinking_start_token = '<think>' -%}
17
+ {%- set thinking_end_token = '</think>' -%}
18
+ {%- set system_token = '<|System|>' -%}
19
+ {%- set reasoning_effort_text = '' -%}
20
+ {%- if thinking -%}
21
+ {%- if not reasoning_effort is defined or reasoning_effort is none -%}
22
+ {%- set reasoning_budget = 75 -%}
23
+ {%- elif reasoning_effort is integer and reasoning_effort >= 1 and reasoning_effort <= 100 -%}
24
+ {%- set reasoning_budget = reasoning_effort -%}
25
+ {%- elif reasoning_effort == 'low' -%}
26
+ {%- set reasoning_budget = 50 -%}
27
+ {%- elif reasoning_effort == 'high' -%}
28
+ {%- set reasoning_budget = 75 -%}
29
+ {%- elif reasoning_effort == 'max' -%}
30
+ {%- set reasoning_budget = 100 -%}
31
+ {%- else -%}
32
+ {{- raise_exception('DeepSeek-V4.1 reasoning_effort must be low, high, max, or an integer within [1, 100]') -}}
33
+ {%- endif -%}
34
+ {%- set reasoning_effort_text = 'Reasoning Effort: ' + (reasoning_budget | string) + ' (range 1-100, the higher the value, the more thorough the reasoning)\n\n' -%}
35
+ {%- endif -%}
36
+ {%- set response_format_template = '## Response Format:\n\nYou MUST strictly adhere to the following schema to reply:\n' -%}
37
+ {%- 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 + ' calls>" block like the following:\n\n<' + dsml_token + ' 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 + ' 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' -%}
38
+ {%- set tools_footer = '\nYou MUST strictly follow the above defined tool name and parameter schemas to invoke tool calls.\n' -%}
39
+ {%- set has_tools = tools is defined and tools -%}
40
+ {%- set first_is_system = messages and messages[0]['role'] == 'system' -%}
41
+ {{- bos_token -}}
42
+ {%- if thinking or first_is_system or has_tools -%}
43
+ {{- system_token -}}
44
+ {{- reasoning_effort_text -}}
45
+ {%- if first_is_system -%}
46
+ {{- messages[0]['content'] or '' -}}
47
+ {%- endif -%}
48
+ {%- if has_tools -%}
49
+ {%- if first_is_system and messages[0]['content'] -%}
50
+ {{- '\n\n' -}}
51
+ {%- endif -%}
52
+ {{- tools_header -}}
53
+ {%- for tool in tools -%}
54
+ {%- if tool['type'] == 'function' -%}
55
+ {{- tool['function'] | tojson -}}
56
+ {{- '\n' -}}
57
+ {%- endif -%}
58
+ {%- endfor -%}
59
+ {{- tools_footer -}}
60
+ {%- endif -%}
61
+ {%- endif -%}
62
+ {%- if response_format is defined -%}
63
+ {{- '\n\n' + response_format_template + (response_format | tojson) -}}
64
+ {%- endif -%}
65
+ {%- set last_user_idx = namespace(value=-1) -%}
66
+ {%- set state = namespace(in_user=false, has_tool_results=false) -%}
67
+ {%- for message in messages -%}
68
+ {%- if message['role'] == 'user' or message['role'] == 'developer' or (message['role'] == 'system' and loop.index0 > 0) -%}
69
+ {%- set last_user_idx.value = loop.index0 -%}
70
+ {%- endif -%}
71
+ {%- if message['role'] == 'tool' -%}
72
+ {%- set state.has_tool_results = true -%}
73
+ {%- endif -%}
74
+ {%- endfor -%}
75
+ {%- for message in messages -%}
76
+ {%- if message['role'] == 'system' and loop.index0 == 0 -%}
77
+ {%- elif message['role'] == 'system' -%}
78
+ {%- set state.in_user = false -%}
79
+ {{- system_token + (message['content'] or '') -}}
80
+ {%- elif message['role'] == 'latest_reminder' -%}
81
+ {{- '<|latest_reminder|>' + (message['content'] or '') -}}
82
+ {%- elif message['role'] == 'user' or message['role'] == 'developer' -%}
83
+ {%- if state.in_user -%}
84
+ {{- '\n\n' -}}
85
+ {%- else -%}
86
+ {{- '<|User|>' -}}
87
+ {%- set state.in_user = true -%}
88
+ {%- endif -%}
89
+ {{- message['content'] or '' -}}
90
+ {%- elif message['role'] == 'tool' -%}
91
+ {%- if state.in_user -%}
92
+ {{- '\n\n' -}}
93
+ {%- else -%}
94
+ {{- '<|User|>' -}}
95
+ {%- set state.in_user = true -%}
96
+ {%- endif -%}
97
+ {{- '<tool_result>' + (message['content'] or '') + '</tool_result>' -}}
98
+ {%- elif message['role'] == 'assistant' -%}
99
+ {%- set state.in_user = false -%}
100
+ {{- '<|Assistant|>' -}}
101
+ {%- set is_after_last_user = loop.index0 > last_user_idx.value -%}
102
+ {%- set keep_reasoning = thinking and ((not drop_thinking) or has_tools or is_after_last_user or state.has_tool_results) -%}
103
+ {%- if keep_reasoning -%}
104
+ {{- thinking_start_token -}}
105
+ {%- if message['reasoning_content'] is defined and message['reasoning_content'] -%}
106
+ {{- message['reasoning_content'] -}}
107
+ {%- endif -%}
108
+ {{- thinking_end_token -}}
109
+ {%- else -%}
110
+ {{- thinking_end_token -}}
111
+ {%- endif -%}
112
+ {%- if message['content'] is defined and message['content'] -%}
113
+ {{- message['content'] -}}
114
+ {%- endif -%}
115
+ {%- if message['tool_calls'] -%}
116
+ {{- '\n\n<' + dsml_token + ' calls>\n' -}}
117
+ {%- for tool in message['tool_calls'] -%}
118
+ {%- set func = tool['function'] -%}
119
+ {%- set tool_name = func['name'] -%}
120
+ {%- if tool['namespace'] is defined and tool['namespace'] -%}
121
+ {%- set tool_name = tool['namespace'] + '::' + tool_name -%}
122
+ {%- elif func['namespace'] is defined and func['namespace'] -%}
123
+ {%- set tool_name = func['namespace'] + '::' + tool_name -%}
124
+ {%- endif -%}
125
+ {{- '<' + dsml_token + ' invoke name="' + tool_name + '">\n' -}}
126
+ {%- set args = func['arguments'] -%}
127
+ {%- if args is string -%}
128
+ {%- set args = args | from_json -%}
129
+ {%- endif -%}
130
+ {%- for key, val in args.items() -%}
131
+ {%- if val is string -%}
132
+ {{- '<' + dsml_token + ' parameter name="' + key + '" string="true">' + val + '</' + dsml_token + ' parameter>\n' -}}
133
+ {%- else -%}
134
+ {{- '<' + dsml_token + ' parameter name="' + key + '" string="false">' + (val | tojson) + '</' + dsml_token + ' parameter>\n' -}}
135
+ {%- endif -%}
136
+ {%- endfor -%}
137
+ {%- if not args -%}
138
+ {{- '\n' -}}
139
+ {%- endif -%}
140
+ {{- '</' + dsml_token + ' invoke>\n' -}}
141
+ {%- endfor -%}
142
+ {{- '</' + dsml_token + ' calls>' -}}
143
+ {%- endif -%}
144
+ {{- eos_token -}}
145
+ {%- else -%}
146
+ {{- raise_exception('Unsupported DeepSeek-V4.1 message role: ' + (message['role'] | string)) -}}
147
+ {%- endif -%}
148
+ {%- endfor -%}
149
+ {%- if add_generation_prompt -%}
150
+ {{- '<|Assistant|>' -}}
151
+ {%- if thinking -%}
152
+ {{- thinking_start_token -}}
153
+ {%- else -%}
154
+ {{- thinking_end_token -}}
155
+ {%- endif -%}
156
+ {%- endif -%}