strata-engine commited on
Commit
75446fe
·
verified ·
1 Parent(s): 9d8bc00

Add chat_template.jinja for Inkling-2-Mini tokenizer/template support

Browse files
Files changed (1) hide show
  1. chat_template.jinja +137 -0
chat_template.jinja ADDED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {%- set effort_map = {"none": 0.0, "minimal": 0.1, "low": 0.2, "medium": 0.7, "high": 0.9, "xhigh": 0.99, "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 header = namespace(text=role_token[message.role]) -%}
51
+ {%- if message.role == "tool" -%}
52
+ {%- if message.name is defined and message.name -%}
53
+ {%- set header.text = header.text ~ message.name -%}
54
+ {%- elif message.tool_call_id is defined and message.tool_call_id -%}
55
+ {%- for prev in messages -%}
56
+ {%- if prev.role == "assistant" and prev.tool_calls -%}
57
+ {%- for tc in prev.tool_calls -%}
58
+ {%- if tc.id is defined and tc.id == message.tool_call_id and tc.function.name is defined -%}
59
+ {%- set header.text = header.text ~ tc.function.name -%}
60
+ {%- endif -%}
61
+ {%- endfor -%}
62
+ {%- endif -%}
63
+ {%- endfor -%}
64
+ {%- endif -%}
65
+ {%- elif message.role == "assistant" and message.name is defined and message.name -%}
66
+ {%- set header.text = header.text ~ message.name -%}
67
+ {%- endif -%}
68
+ {%- set rtok = header.text -%}
69
+ {%- set rendered -%}
70
+
71
+ {%- if message.role == "assistant" and message.reasoning_content is defined and message.reasoning_content -%}
72
+ {{- rtok ~ "<|content_thinking|>" ~ message.reasoning_content ~ "<|end_message|>" -}}
73
+ {%- endif -%}
74
+
75
+ {%- if message.content is string and (message.content or not (message.role == "assistant" and (message.tool_calls or message.reasoning_content))) -%}
76
+ {{- rtok ~ "<|content_text|>" ~ message.content ~ "<|end_message|>" -}}
77
+ {%- elif message.content -%}
78
+ {%- for part in message.content -%}
79
+ {%- if part is string -%}
80
+ {{- rtok ~ "<|content_text|>" ~ part ~ "<|end_message|>" -}}
81
+ {%- elif part.type is not defined or part.type in ("text", "input_text") -%}
82
+ {%- set text_part = (part.text if part.text is defined and part.text is string else "") -%}
83
+ {{- rtok ~ "<|content_text|>" ~ text_part ~ "<|end_message|>" -}}
84
+ {%- elif part.type in ("image", "input_image", "image_url") -%}
85
+ {%- if message.role not in ("user", "tool") -%}
86
+ {{- raise_exception("Image content is only supported for user and tool messages") -}}
87
+ {%- endif -%}
88
+ {{- rtok ~ "<|content_image|><|unused_200054|><|end_message|>" -}}
89
+ {%- elif part.type in ("audio", "input_audio", "audio_url") -%}
90
+ {{- raise_exception("Inkling 2 does not accept audio input") -}}
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
+ {%- if message.name is defined and message.name and message.name != fn.name -%}
104
+ {{- raise_exception("assistant name " ~ message.name ~ " does not match its tool call " ~ fn.name) -}}
105
+ {%- endif -%}
106
+ {%- set args = fn.arguments if fn.arguments is defined and fn.arguments else {} -%}
107
+ {%- if args is string -%}
108
+ {{- raise_exception("tool call arguments must be a parsed object, not a JSON string; canonicalize upstream") -}}
109
+ {%- endif -%}
110
+ {%- if args is not mapping -%}
111
+ {{- raise_exception("tool call arguments must be an object") -}}
112
+ {%- endif -%}
113
+ {{- "<|message_model|>" ~ fn.name ~ "<|content_invoke_tool_json|>" -}}
114
+ {{- '{"name":' ~ (fn.name | tojson(sort_keys=true, separators=(",", ":"))) ~ ',"args":' -}}
115
+ {{- "{" -}}
116
+ {%- for key, value in args | dictsort(case_sensitive=true) -%}
117
+ {{- (key | tojson) ~ ":" ~ (value | tojson(separators=(",", ":"))) -}}
118
+ {%- if not loop.last -%},{%- endif -%}
119
+ {%- endfor -%}
120
+ {{- "}}<|end_message|>" -}}
121
+ {%- endfor -%}
122
+ {%- endif -%}
123
+
124
+ {%- if message.role == "assistant" -%}
125
+ {{- "<|content_model_end_sampling|>" -}}
126
+ {%- endif -%}
127
+ {%- endset -%}
128
+ {%- if message.role == "assistant" -%}
129
+ {%- generation -%}{{- rendered -}}{%- endgeneration -%}
130
+ {%- else -%}
131
+ {{- rendered -}}
132
+ {%- endif -%}
133
+ {%- endfor -%}
134
+
135
+ {%- if not state.effort_emitted -%}
136
+ {{- emit_thinking_effort() -}}
137
+ {%- endif -%}