pcuenq HF Staff commited on
Commit
f74affe
·
verified ·
1 Parent(s): 4dca31d

Transformers processor and chat template support

Browse files
Files changed (2) hide show
  1. chat_template.jinja +129 -0
  2. processor_config.json +46 -0
chat_template.jinja ADDED
@@ -0,0 +1,129 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {%- set effort_map = {"none": 0.0, "minimal": 0.1, "low": 0.2, "medium": 0.7, "high": 0.9, "max": 0.99} -%}
2
+ {%- set role_token = {"user": "<|message_user|>", "assistant": "<|message_model|>", "system": "<|message_system|>", "tool": "<|message_tool|>"} -%}
3
+
4
+ {%- macro emit_thinking_effort() -%}
5
+ {%- set eff = reasoning_effort if reasoning_effort is defined and reasoning_effort is not none else 0.9 -%}
6
+ {%- if eff is string -%}
7
+ {%- set key = eff | trim -%}
8
+ {%- if key not in effort_map -%}
9
+ {{- raise_exception("Unknown reasoning_effort: " ~ eff) -}}
10
+ {%- endif -%}
11
+ {%- set num = effort_map[key] -%}
12
+ {%- else -%}
13
+ {%- set num = eff | float -%}
14
+ {%- endif -%}
15
+ {%- if num < 0.0 or num > 0.99 -%}
16
+ {{- raise_exception("reasoning_effort must be in [0.0, 0.99]") -}}
17
+ {%- endif -%}
18
+ {{- "<|message_system|><|content_text|>Thinking effort level: " -}}
19
+ {%- if num == 0.0 -%}0{%- else -%}{{ num }}{%- endif -%}
20
+ {{- "<|end_message|>" -}}
21
+ {%- endmacro -%}
22
+
23
+ {%- if tools -%}
24
+ {%- set tool_state = namespace(specs=[]) -%}
25
+ {%- for tool in tools -%}
26
+ {%- set fn = tool.function if tool.function is defined else tool -%}
27
+ {%- set spec = {
28
+ "description": (fn.description if fn.description is defined and fn.description else ""),
29
+ "name": fn.name,
30
+ "parameters": (fn.parameters if fn.parameters is defined and fn.parameters else {}),
31
+ "type": (tool.type if tool.type is defined and tool.type else "function"),
32
+ } -%}
33
+ {%- set tool_state.specs = tool_state.specs + [spec] -%}
34
+ {%- endfor -%}
35
+ {{- "<|message_system|>tool_declare<|content_xml|>" -}}
36
+ {{- tool_state.specs | tojson(sort_keys=true, separators=(",", ":")) -}}
37
+ {{- "<|end_message|>" -}}
38
+ {%- endif -%}
39
+
40
+ {%- set state = namespace(effort_emitted=false) -%}
41
+ {%- for message in messages -%}
42
+ {%- if message.role not in role_token -%}
43
+ {{- raise_exception("Unknown message role: " ~ message.role) -}}
44
+ {%- endif -%}
45
+ {%- if not state.effort_emitted and message.role != "system" -%}
46
+ {{- emit_thinking_effort() -}}
47
+ {%- set state.effort_emitted = true -%}
48
+ {%- endif -%}
49
+
50
+ {%- set rtok = role_token[message.role] -%}
51
+
52
+ {%- if message.role == "tool" -%}
53
+ {%- set tool_name_state = namespace(name="") -%}
54
+ {%- if message.name is defined and message.name -%}
55
+ {%- set tool_name_state.name = message.name -%}
56
+ {%- elif message.tool_call_id is defined and message.tool_call_id -%}
57
+ {%- for prev in messages -%}
58
+ {%- if prev.role == "assistant" and prev.tool_calls -%}
59
+ {%- for tc in prev.tool_calls -%}
60
+ {%- if tc.id is defined and tc.id == message.tool_call_id and tc.function.name is defined -%}
61
+ {%- set tool_name_state.name = tc.function.name -%}
62
+ {%- endif -%}
63
+ {%- endfor -%}
64
+ {%- endif -%}
65
+ {%- endfor -%}
66
+ {%- endif -%}
67
+ {{- rtok -}}
68
+ {%- if tool_name_state.name -%}{{- tool_name_state.name -}}{%- endif -%}
69
+ {{- "<|content_text|>" -}}
70
+ {%- if message.content is string -%}{{- message.content -}}{%- endif -%}
71
+ {{- "<|end_message|>" -}}
72
+
73
+ {%- else -%}
74
+ {%- if message.role == "assistant" and message.reasoning_content is defined and message.reasoning_content -%}
75
+ {{- "<|message_model|><|content_thinking|>" ~ message.reasoning_content ~ "<|end_message|>" -}}
76
+ {%- endif -%}
77
+
78
+ {%- if message.content is string -%}
79
+ {{- rtok ~ "<|content_text|>" ~ message.content ~ "<|end_message|>" -}}
80
+ {%- elif message.content -%}
81
+ {%- for part in message.content -%}
82
+ {%- if part is string -%}
83
+ {{- rtok ~ "<|content_text|>" ~ part ~ "<|end_message|>" -}}
84
+ {%- elif part.type is not defined or part.type in ("text", "input_text") -%}
85
+ {%- set text_part = (part.text if part.text is defined and part.text is string else "") -%}
86
+ {{- rtok ~ "<|content_text|>" ~ text_part ~ "<|end_message|>" -}}
87
+ {%- elif part.type in ("image", "input_image", "image_url") -%}
88
+ {{- rtok ~ "<|content_image|><|unused_200054|><|end_message|>" -}}
89
+ {%- elif part.type in ("audio", "input_audio", "audio_url") -%}
90
+ {{- rtok ~ "<|content_audio_input|><|unused_200053|><|audio_end|><|end_message|>" -}}
91
+ {%- else -%}
92
+ {{- raise_exception("Unsupported content part type: " ~ part.type) -}}
93
+ {%- endif -%}
94
+ {%- endfor -%}
95
+ {%- endif -%}
96
+
97
+ {%- if message.role == "assistant" and message.tool_calls -%}
98
+ {%- for tc in message.tool_calls -%}
99
+ {%- set fn = tc.function -%}
100
+ {%- if fn.name is not defined or fn.name is not string -%}
101
+ {{- raise_exception("tool call function name must be a string") -}}
102
+ {%- endif -%}
103
+ {%- set args = fn.arguments if fn.arguments is defined and fn.arguments else {} -%}
104
+ {%- if args is string -%}
105
+ {{- raise_exception("tool call arguments must be a parsed object, not a JSON string; canonicalize upstream") -}}
106
+ {%- endif -%}
107
+ {%- if args is not mapping -%}
108
+ {{- raise_exception("tool call arguments must be an object") -}}
109
+ {%- endif -%}
110
+ {{- "<|message_model|>" ~ fn.name ~ "<|content_invoke_tool_json|>" -}}
111
+ {{- '{"name":' ~ (fn.name | tojson(sort_keys=true, separators=(",", ":"))) ~ ',"args":' -}}
112
+ {{- (args | tojson(sort_keys=true, separators=(",", ":"))) -}}
113
+ {{- "}<|end_message|>" -}}
114
+ {%- endfor -%}
115
+ {%- endif -%}
116
+
117
+ {%- if message.role == "assistant" -%}
118
+ {{- "<|content_model_end_sampling|>" -}}
119
+ {%- endif -%}
120
+ {%- endif -%}
121
+ {%- endfor -%}
122
+
123
+ {%- if not state.effort_emitted -%}
124
+ {{- emit_thinking_effort() -}}
125
+ {%- endif -%}
126
+
127
+ {%- if add_generation_prompt -%}
128
+ {{- "<|message_model|>" -}}
129
+ {%- endif -%}
processor_config.json ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "audio_token": "<|unused_200053|>",
3
+ "audio_bos_token": "<|content_audio_input|>",
4
+ "dmel_max_value": 2.0,
5
+ "dmel_min_value": -7.0,
6
+ "feature_extractor": {
7
+ "audio_token_duration_s": 0.05,
8
+ "feature_extractor_type": "InklingFeatureExtractor",
9
+ "feature_size": 80,
10
+ "hop_length": 800,
11
+ "n_fft": 1600,
12
+ "padding_side": "right",
13
+ "padding_value": 0.0,
14
+ "return_attention_mask": true,
15
+ "sampling_rate": 16000,
16
+ "window_size": 1600,
17
+ "window_size_multiplier": 2.0
18
+ },
19
+ "image_processor": {
20
+ "do_convert_rgb": true,
21
+ "do_normalize": true,
22
+ "do_rescale": true,
23
+ "do_resize": true,
24
+ "image_mean": [
25
+ 0.48145466,
26
+ 0.4578275,
27
+ 0.40821073
28
+ ],
29
+ "image_processor_type": "InklingImageProcessor",
30
+ "image_std": [
31
+ 0.26862954,
32
+ 0.26130258,
33
+ 0.27577711
34
+ ],
35
+ "resample": 3,
36
+ "rescale_factor": 0.00392156862745098,
37
+ "size": {
38
+ "height": 40,
39
+ "width": 40
40
+ }
41
+ },
42
+ "image_token": "<|unused_200054|>",
43
+ "image_bos_token": "<|content_image|>",
44
+ "num_dmel_bins": 16,
45
+ "processor_class": "InklingProcessor"
46
+ }