axonlabsai commited on
Commit
cbf4aa0
·
verified ·
1 Parent(s): 19c46d8

Upload chat_template.jinja with huggingface_hub

Browse files
Files changed (1) hide show
  1. chat_template.jinja +77 -0
chat_template.jinja ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {%- if not add_generation_prompt is defined %}{%- set add_generation_prompt = false %}{%- endif %}
2
+ {#- Two fixes over the stock DeepSeek-R1-Distill template:
3
+
4
+ 1. `tools` is accepted and rendered. The stock template emits assistant
5
+ tool CALLS using the native <|tool_calls_begin|> tokens but has no way to
6
+ DECLARE the available tools, so passing tools=[...] raised
7
+ "can only concatenate str (not dict) to str" and every tool example was
8
+ silently dropped from training.
9
+
10
+ 2. <think> is preserved on the FINAL assistant message. The stock template
11
+ ran content.split('</think>')[-1] on every assistant turn, which deletes
12
+ the reasoning from the training target — so no amount of think-formatted
13
+ data could ever teach thinking. Stale reasoning is still stripped from
14
+ earlier turns, which is the behaviour you actually want at inference.
15
+ -#}
16
+ {%- set ns = namespace(is_first=false, is_tool=false, is_output_first=true, system_prompt='') %}
17
+ {%- for message in messages %}
18
+ {%- if message['role'] == 'system' %}{%- set ns.system_prompt = message['content'] %}{%- endif %}
19
+ {%- endfor %}
20
+ {{- bos_token }}
21
+ {%- if tools %}
22
+ {{- ns.system_prompt }}
23
+ {{- '\n\n# Tools\n\nYou may call one or more of the following functions. Emit a tool call only when it is actually needed.\n\n<tools>' }}
24
+ {%- for tool in tools %}
25
+ {%- set fn = tool['function'] if tool.get('function') is defined else tool %}
26
+ {{- '\n' + (fn | tojson) }}
27
+ {%- endfor %}
28
+ {{- '\n</tools>' }}
29
+ {%- else %}
30
+ {{- ns.system_prompt }}
31
+ {%- endif %}
32
+ {%- set last = (messages | length) - 1 %}
33
+ {%- for message in messages %}
34
+ {%- if message['role'] == 'user' %}
35
+ {%- set ns.is_tool = false -%}
36
+ {{- '<|User|>' + message['content'] }}
37
+ {%- endif %}
38
+ {%- if message['role'] == 'assistant' and message.get('tool_calls') %}
39
+ {%- set ns.is_tool = false -%}
40
+ {%- for tool in message['tool_calls'] %}
41
+ {%- set fn = tool['function'] %}
42
+ {%- set args = fn['arguments'] if fn['arguments'] is string else (fn['arguments'] | tojson) %}
43
+ {%- if not ns.is_first %}
44
+ {{- '<|Assistant|><|tool▁calls▁begin|><|tool▁call▁begin|>' + (tool.get('type') or 'function') + '<|tool▁sep|>' + fn['name'] + '\n```json\n' + args + '\n```<|tool▁call▁end|>' }}
45
+ {%- set ns.is_first = true -%}
46
+ {%- else %}
47
+ {{- '\n<|tool▁call▁begin|>' + (tool.get('type') or 'function') + '<|tool▁sep|>' + fn['name'] + '\n```json\n' + args + '\n```<|tool▁call▁end|>' }}
48
+ {%- endif %}
49
+ {%- endfor %}
50
+ {{- '<|tool▁calls▁end|><|end▁of▁sentence|>' }}
51
+ {%- set ns.is_first = false -%}
52
+ {%- endif %}
53
+ {%- if message['role'] == 'assistant' and message.get('content') %}
54
+ {%- set content = message['content'] %}
55
+ {#- keep reasoning on the last turn (the training target); drop it from history -#}
56
+ {%- if '</think>' in content and not loop.index0 == last %}
57
+ {%- set content = content.split('</think>')[-1] %}
58
+ {%- endif %}
59
+ {%- if ns.is_tool %}
60
+ {{- '<|tool▁outputs▁end|>' + content + '<|end▁of▁sentence|>' }}
61
+ {%- set ns.is_tool = false -%}
62
+ {%- else %}
63
+ {{- '<|Assistant|>' + content + '<|end▁of▁sentence|>' }}
64
+ {%- endif %}
65
+ {%- endif %}
66
+ {%- if message['role'] == 'tool' %}
67
+ {%- set ns.is_tool = true -%}
68
+ {%- if ns.is_output_first %}
69
+ {{- '<|tool▁outputs▁begin|><|tool▁output▁begin|>' + message['content'] + '<|tool▁output▁end|>' }}
70
+ {%- set ns.is_output_first = false %}
71
+ {%- else %}
72
+ {{- '\n<|tool▁output▁begin|>' + message['content'] + '<|tool▁output▁end|>' }}
73
+ {%- endif %}
74
+ {%- endif %}
75
+ {%- endfor -%}
76
+ {%- if ns.is_tool %}{{- '<|tool▁outputs▁end|>' }}{%- endif %}
77
+ {%- if add_generation_prompt and not ns.is_tool %}{{- '<|Assistant|>' }}{%- endif %}