Make the chat template work in llama.cpp and add a response template

#34
by Rocketknight1 HF Staff - opened
Files changed (2) hide show
  1. chat_template.jinja +16 -6
  2. tokenizer_config.json +48 -1
chat_template.jinja CHANGED
@@ -72,11 +72,11 @@ For each function call, output the function name and arguments within the follow
72
  {%- macro render_tool_response(m) -%}
73
  {%- if m.content is string -%}
74
  {{- tool_response(m.content) -}}
75
- {%- elif m.content and m.content is not mapping and m.content.0.type == "tool_reference" -%}
76
  {{- tool_references_to_response(m.content) -}}
77
  {%- elif is_list_of_outputs(m) -%}
78
  {%- for tr in m.content -%}
79
- {%- if tr.output is iterable and tr.output is not string and tr.output is not mapping and tr.output and tr.output.0.type == "tool_reference" -%}
80
  {{- tool_references_to_response(tr.output) -}}
81
  {%- else -%}
82
  {{- tool_response(visible_text(tr.output)) -}}
@@ -94,7 +94,7 @@ For each function call, output the function name and arguments within the follow
94
  {%- endif -%}
95
  {%- endmacro -%}
96
  {%- macro is_list_of_outputs(m) -%}
97
- {%- if m.content and m.content.0.output is defined -%}1{%- endif -%}
98
  {%- endmacro -%}
99
  {%- macro has_dup_tool_result_id(lo, hi, target) -%}
100
  {%- set ns_cnt = namespace(n=0) -%}
@@ -123,6 +123,16 @@ For each function call, output the function name and arguments within the follow
123
  {%- endfor -%}
124
  {%- if ns_f.found -%}1{%- endif -%}
125
  {%- endmacro -%}
 
 
 
 
 
 
 
 
 
 
126
  {%- set ns = namespace(last_user_index=-1) -%}
127
  {%- for m in messages %}
128
  {%- if m.role == 'user' %}
@@ -195,9 +205,9 @@ For each function call, output the function name and arguments within the follow
195
  {%- endfor -%}
196
  {%- if '' in ns_chk.res or '' in ns_chk.tcs -%}
197
  {%- set ns_chk.can_sort = false -%}
198
- {%- elif ns_chk.res | unique(case_sensitive=true) | list | length != ns_chk.res | length -%}
199
  {%- set ns_chk.can_sort = false -%}
200
- {%- elif ns_chk.tcs | unique(case_sensitive=true) | list | length != ns_chk.tcs | length -%}
201
  {%- set ns_chk.can_sort = false -%}
202
  {%- else -%}
203
  {%- for eid in ns_chk.res -%}
@@ -227,7 +237,7 @@ For each function call, output the function name and arguments within the follow
227
  {%- set m = messages[ns_chk.mk[pair.p]] -%}
228
  {%- if ns_chk.ek[pair.p] >= 0 -%}
229
  {%- set entry = m.content[ns_chk.ek[pair.p]] -%}
230
- {%- if entry.output is iterable and entry.output is not string and entry.output is not mapping and entry.output and entry.output.0.type == "tool_reference" -%}
231
  {{- tool_references_to_response(entry.output) -}}
232
  {%- else -%}
233
  {{- tool_response(visible_text(entry.output)) -}}
 
72
  {%- macro render_tool_response(m) -%}
73
  {%- if m.content is string -%}
74
  {{- tool_response(m.content) -}}
75
+ {%- elif m.content and m.content is not mapping and m.content[0].type == "tool_reference" -%}
76
  {{- tool_references_to_response(m.content) -}}
77
  {%- elif is_list_of_outputs(m) -%}
78
  {%- for tr in m.content -%}
79
+ {%- if tr.output is iterable and tr.output is not string and tr.output is not mapping and tr.output and tr.output[0].type == "tool_reference" -%}
80
  {{- tool_references_to_response(tr.output) -}}
81
  {%- else -%}
82
  {{- tool_response(visible_text(tr.output)) -}}
 
94
  {%- endif -%}
95
  {%- endmacro -%}
96
  {%- macro is_list_of_outputs(m) -%}
97
+ {%- if m.content and m.content[0].output is defined -%}1{%- endif -%}
98
  {%- endmacro -%}
99
  {%- macro has_dup_tool_result_id(lo, hi, target) -%}
100
  {%- set ns_cnt = namespace(n=0) -%}
 
123
  {%- endfor -%}
124
  {%- if ns_f.found -%}1{%- endif -%}
125
  {%- endmacro -%}
126
+ {%- macro has_duplicates(ids) -%}
127
+ {%- set ns_dup = namespace(seen=[], found=false) -%}
128
+ {%- for x in ids -%}
129
+ {%- if x in ns_dup.seen -%}
130
+ {%- set ns_dup.found = true -%}
131
+ {%- endif -%}
132
+ {%- set ns_dup.seen = ns_dup.seen + [x] -%}
133
+ {%- endfor -%}
134
+ {%- if ns_dup.found -%}1{%- endif -%}
135
+ {%- endmacro -%}
136
  {%- set ns = namespace(last_user_index=-1) -%}
137
  {%- for m in messages %}
138
  {%- if m.role == 'user' %}
 
205
  {%- endfor -%}
206
  {%- if '' in ns_chk.res or '' in ns_chk.tcs -%}
207
  {%- set ns_chk.can_sort = false -%}
208
+ {%- elif has_duplicates(ns_chk.res) -%}
209
  {%- set ns_chk.can_sort = false -%}
210
+ {%- elif has_duplicates(ns_chk.tcs) -%}
211
  {%- set ns_chk.can_sort = false -%}
212
  {%- else -%}
213
  {%- for eid in ns_chk.res -%}
 
237
  {%- set m = messages[ns_chk.mk[pair.p]] -%}
238
  {%- if ns_chk.ek[pair.p] >= 0 -%}
239
  {%- set entry = m.content[ns_chk.ek[pair.p]] -%}
240
+ {%- if entry.output is iterable and entry.output is not string and entry.output is not mapping and entry.output and entry.output[0].type == "tool_reference" -%}
241
  {{- tool_references_to_response(entry.output) -}}
242
  {%- else -%}
243
  {{- tool_response(visible_text(entry.output)) -}}
tokenizer_config.json CHANGED
@@ -29,5 +29,52 @@
29
  "pad_token": "<|endoftext|>",
30
  "padding_side": "left",
31
  "remove_space": false,
32
- "tokenizer_class": "TokenizersBackend"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  }
 
29
  "pad_token": "<|endoftext|>",
30
  "padding_side": "left",
31
  "remove_space": false,
32
+ "tokenizer_class": "TokenizersBackend",
33
+ "response_template": {
34
+ "defaults": {
35
+ "role": "assistant"
36
+ },
37
+ "start_anchor": "<|assistant|>",
38
+ "fields": {
39
+ "reasoning_content": {
40
+ "open_pattern": "<think>|(?=</think>)",
41
+ "close_pattern": "</think>|(?=<tool_call>)",
42
+ "content": "text",
43
+ "content_args": {
44
+ "strip": true
45
+ },
46
+ "repeats": true,
47
+ "join": ""
48
+ },
49
+ "tool_calls": {
50
+ "open_pattern": "\\s*<tool_call>\\s*(?P<name>[^<]*?)\\s*(?=<arg_key>|</tool_call>)",
51
+ "close": "</tool_call>",
52
+ "repeats": true,
53
+ "content": "xml-inline",
54
+ "content_args": {
55
+ "tag_pattern": "<arg_key>\\s*(?P<key>.*?)\\s*</arg_key>\\s*<arg_value>(?P<value>.*?)</arg_value>"
56
+ },
57
+ "transform": {
58
+ "type": "function",
59
+ "function": {
60
+ "name": "{name}",
61
+ "arguments": "{content}"
62
+ }
63
+ }
64
+ },
65
+ "content": {
66
+ "close": [
67
+ "<|user|>",
68
+ "<|observation|>",
69
+ "<|endoftext|>"
70
+ ],
71
+ "content": "text",
72
+ "content_args": {
73
+ "strip": false
74
+ },
75
+ "repeats": true,
76
+ "join": ""
77
+ }
78
+ }
79
+ }
80
  }