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

#68
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
@@ -78,11 +78,11 @@ For each function call, output the function name and arguments within the follow
78
  {%- macro render_tool_response(m) -%}
79
  {%- if m.content is string -%}
80
  {{- tool_response(m.content) -}}
81
- {%- elif m.content and m.content is not mapping and m.content.0.type == "tool_reference" -%}
82
  {{- tool_references_to_response(m.content) -}}
83
  {%- elif is_list_of_outputs(m) -%}
84
  {%- for tr in m.content -%}
85
- {%- 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" -%}
86
  {{- tool_references_to_response(tr.output) -}}
87
  {%- else -%}
88
  {{- tool_response(visible_text(tr.output)) -}}
@@ -100,7 +100,7 @@ For each function call, output the function name and arguments within the follow
100
  {%- endif -%}
101
  {%- endmacro -%}
102
  {%- macro is_list_of_outputs(m) -%}
103
- {%- if m.content and m.content.0.output is defined -%}1{%- endif -%}
104
  {%- endmacro -%}
105
  {%- macro has_dup_tool_result_id(lo, hi, target) -%}
106
  {%- set ns_cnt = namespace(n=0) -%}
@@ -129,6 +129,16 @@ For each function call, output the function name and arguments within the follow
129
  {%- endfor -%}
130
  {%- if ns_f.found -%}1{%- endif -%}
131
  {%- endmacro -%}
 
 
 
 
 
 
 
 
 
 
132
  {%- set ns = namespace(last_user_index=-1) -%}
133
  {%- for m in messages %}
134
  {%- if m.role == 'user' %}
@@ -201,9 +211,9 @@ For each function call, output the function name and arguments within the follow
201
  {%- endfor -%}
202
  {%- if '' in ns_chk.res or '' in ns_chk.tcs -%}
203
  {%- set ns_chk.can_sort = false -%}
204
- {%- elif ns_chk.res | unique(case_sensitive=true) | list | length != ns_chk.res | length -%}
205
  {%- set ns_chk.can_sort = false -%}
206
- {%- elif ns_chk.tcs | unique(case_sensitive=true) | list | length != ns_chk.tcs | length -%}
207
  {%- set ns_chk.can_sort = false -%}
208
  {%- else -%}
209
  {%- for eid in ns_chk.res -%}
@@ -233,7 +243,7 @@ For each function call, output the function name and arguments within the follow
233
  {%- set m = messages[ns_chk.mk[pair.p]] -%}
234
  {%- if ns_chk.ek[pair.p] >= 0 -%}
235
  {%- set entry = m.content[ns_chk.ek[pair.p]] -%}
236
- {%- 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" -%}
237
  {{- tool_references_to_response(entry.output) -}}
238
  {%- else -%}
239
  {{- tool_response(visible_text(entry.output)) -}}
 
78
  {%- macro render_tool_response(m) -%}
79
  {%- if m.content is string -%}
80
  {{- tool_response(m.content) -}}
81
+ {%- elif m.content and m.content is not mapping and m.content[0].type == "tool_reference" -%}
82
  {{- tool_references_to_response(m.content) -}}
83
  {%- elif is_list_of_outputs(m) -%}
84
  {%- for tr in m.content -%}
85
+ {%- 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" -%}
86
  {{- tool_references_to_response(tr.output) -}}
87
  {%- else -%}
88
  {{- tool_response(visible_text(tr.output)) -}}
 
100
  {%- endif -%}
101
  {%- endmacro -%}
102
  {%- macro is_list_of_outputs(m) -%}
103
+ {%- if m.content and m.content[0].output is defined -%}1{%- endif -%}
104
  {%- endmacro -%}
105
  {%- macro has_dup_tool_result_id(lo, hi, target) -%}
106
  {%- set ns_cnt = namespace(n=0) -%}
 
129
  {%- endfor -%}
130
  {%- if ns_f.found -%}1{%- endif -%}
131
  {%- endmacro -%}
132
+ {%- macro has_duplicates(ids) -%}
133
+ {%- set ns_dup = namespace(seen=[], found=false) -%}
134
+ {%- for x in ids -%}
135
+ {%- if x in ns_dup.seen -%}
136
+ {%- set ns_dup.found = true -%}
137
+ {%- endif -%}
138
+ {%- set ns_dup.seen = ns_dup.seen + [x] -%}
139
+ {%- endfor -%}
140
+ {%- if ns_dup.found -%}1{%- endif -%}
141
+ {%- endmacro -%}
142
  {%- set ns = namespace(last_user_index=-1) -%}
143
  {%- for m in messages %}
144
  {%- if m.role == 'user' %}
 
211
  {%- endfor -%}
212
  {%- if '' in ns_chk.res or '' in ns_chk.tcs -%}
213
  {%- set ns_chk.can_sort = false -%}
214
+ {%- elif has_duplicates(ns_chk.res) -%}
215
  {%- set ns_chk.can_sort = false -%}
216
+ {%- elif has_duplicates(ns_chk.tcs) -%}
217
  {%- set ns_chk.can_sort = false -%}
218
  {%- else -%}
219
  {%- for eid in ns_chk.res -%}
 
243
  {%- set m = messages[ns_chk.mk[pair.p]] -%}
244
  {%- if ns_chk.ek[pair.p] >= 0 -%}
245
  {%- set entry = m.content[ns_chk.ek[pair.p]] -%}
246
+ {%- 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" -%}
247
  {{- tool_references_to_response(entry.output) -}}
248
  {%- else -%}
249
  {{- 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
  }