okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-022-bench: strata bench run fixes from the first real run942483051

main
9.8 KiB184 linesraw
1{%- set image_count = namespace(value=0) %}
2{%- set video_count = namespace(value=0) %}
3{%- macro render_content(content, do_vision_count, is_system_content=false) %}
4    {%- if content is string %}
5        {{- content }}
6    {%- elif content is iterable and content is not mapping %}
7        {%- for item in content %}
8            {%- if 'image' in item or 'image_url' in item or item.type == 'image' %}
9                {%- if is_system_content %}
10                    {{- raise_exception('System message cannot contain images.') }}
11                {%- endif %}
12                {%- if do_vision_count %}
13                    {%- set image_count.value = image_count.value + 1 %}
14                {%- endif %}
15                {%- if add_vision_id %}
16                    {{- 'Picture ' ~ image_count.value ~ ': ' }}
17                {%- endif %}
18                {{- '<|vision_start|><|image_pad|><|vision_end|>' }}
19            {%- elif 'video' in item or item.type == 'video' %}
20                {%- if is_system_content %}
21                    {{- raise_exception('System message cannot contain videos.') }}
22                {%- endif %}
23                {%- if do_vision_count %}
24                    {%- set video_count.value = video_count.value + 1 %}
25                {%- endif %}
26                {%- if add_vision_id %}
27                    {{- 'Video ' ~ video_count.value ~ ': ' }}
28                {%- endif %}
29                {{- '<|vision_start|><|video_pad|><|vision_end|>' }}
30            {%- elif 'text' in item %}
31                {{- item.text }}
32            {%- else %}
33                {{- raise_exception('Unexpected item type in content.') }}
34            {%- endif %}
35        {%- endfor %}
36    {%- elif content is none or content is undefined %}
37        {{- '' }}
38    {%- else %}
39        {{- raise_exception('Unexpected content type.') }}
40    {%- endif %}
41{%- endmacro %}
42{%- if not messages %}
43    {{- raise_exception('No messages provided.') }}
44{%- endif %}
45{%- set sysns = namespace(count=0, text='') %}
46{%- for message in messages %}
47    {%- if sysns.count == loop.index0 and (message.role == 'system' or message.role == 'developer') %}
48        {%- set sys_content = render_content(message.content, false, true)|trim %}
49        {%- if sys_content %}
50            {%- set sysns.text = sysns.text + ('\n' if sysns.text else '') + sys_content %}
51        {%- endif %}
52        {%- set sysns.count = sysns.count + 1 %}
53    {%- endif %}
54{%- endfor %}
55{%- set num_sys = sysns.count %}
56{%- set merged_system = sysns.text %}
57{%- set reasoning_instructions = '' %}
58{%- if enable_thinking is undefined or enable_thinking is true %}
59    {%- set resolved_reasoning_effort = reasoning_effort|default('xhigh') %}
60    {%- if resolved_reasoning_effort == 'high' %}
61        {%- set resolved_reasoning_effort = 'xhigh' %}
62    {%- endif %}
63    {%- if resolved_reasoning_effort not in ('xhigh', 'medium', 'low') %}
64        {{- raise_exception('Unexpected reasoning effort ' ~ reasoning_effort ~ '. Supported types are xhigh (default), medium, and low.') }}
65    {%- endif %}
66    {%- if resolved_reasoning_effort == 'xhigh' %}
67        {%- set reasoning_instructions = 'Reasoning effort is set to xhigh. Please think carefully through the task, validate key assumptions, consider plausible alternatives, and prioritize correctness, consistency, and clarity in the final answer.' %}
68    {%- elif resolved_reasoning_effort == 'low' %}
69        {%- set reasoning_instructions = 'Reasoning effort is set to low. Keep your thinking brief and focused, moving directly to the conclusion without unnecessary elaboration.' %}
70    {%- endif %}
71{%- endif %}
72{%- if tools and tools is iterable and tools is not mapping %}
73    {{- '<|im_start|>system\n' }}
74    {%- if reasoning_instructions %}
75        {{- reasoning_instructions + '\n\n' }}
76    {%- endif %}
77    {{- "# Tools\n\nYou have access to the following functions:\n\n<tools>" }}
78    {%- for tool in tools %}
79        {{- "\n" }}
80        {{- tool | tojson }}
81    {%- endfor %}
82    {{- "\n</tools>" }}
83    {{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n<tool_call>\n<function=example_function_name>\n<parameter=example_parameter_1>\nvalue_1\n</parameter>\n<parameter=example_parameter_2>\nThis is the value for the second parameter\nthat can span\nmultiple lines\n</parameter>\n</function>\n</tool_call>\n\n<IMPORTANT>\nReminder:\n- Function calls MUST follow the specified format: an inner <function=...></function> block must be nested within <tool_call></tool_call> XML tags\n- Required parameters MUST be specified\n- You may provide optional reasoning for your function call in natural language BEFORE the function call, but NOT after\n- If there is no function call available, answer the question like normal with your current knowledge and do not tell the user about function calls\n</IMPORTANT>' }}
84    {%- if merged_system %}
85        {{- '\n\n' + merged_system }}
86    {%- endif %}
87    {{- '<|im_end|>\n' }}
88{%- else %}
89    {%- if merged_system %}
90        {{- '<|im_start|>system\n' + (reasoning_instructions + '\n\n' if reasoning_instructions else '')  + merged_system + '<|im_end|>\n' }}
91    {%- elif reasoning_instructions %}
92        {{- '<|im_start|>system\n' + reasoning_instructions + '<|im_end|>\n' }}
93    {%- endif %}
94{%- endif %}
95{%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
96{%- for message in messages[::-1] %}
97    {%- set index = (messages|length - 1) - loop.index0 %}
98    {%- if ns.multi_step_tool and message.role == "user" %}
99        {%- set content = render_content(message.content, false)|trim %}
100        {%- if not(content.startswith('<tool_response>') and content.endswith('</tool_response>')) %}
101            {%- set ns.multi_step_tool = false %}
102            {%- set ns.last_query_index = index %}
103        {%- endif %}
104    {%- endif %}
105{%- endfor %}
106{%- for message in messages %}
107    {%- if loop.index0 >= num_sys %}
108    {%- set content = render_content(message.content, true)|trim %}
109    {%- if message.role == "system" or message.role == "developer" %}
110        {{- raise_exception('System message must be at the beginning.') }}
111    {%- elif message.role == "user" %}
112        {{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>' + '\n' }}
113    {%- elif message.role == "assistant" %}
114        {%- set reasoning_content = '' %}
115        {%- if message.reasoning_content is string %}
116            {%- set reasoning_content = message.reasoning_content %}
117        {%- endif %}
118        {%- set reasoning_content = reasoning_content|trim %}
119        {%- if preserve_thinking is undefined or preserve_thinking is true or loop.index0 > ns.last_query_index %}
120            {{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content + '\n</think>\n\n' + content }}
121        {%- else %}
122            {{- '<|im_start|>' + message.role + '\n' + content }}
123        {%- endif %}
124        {%- if message.tool_calls and message.tool_calls is iterable and message.tool_calls is not mapping %}
125            {%- for tool_call in message.tool_calls %}
126                {%- if tool_call.function is defined %}
127                    {%- set tool_call = tool_call.function %}
128                {%- endif %}
129                {%- if tool_call.name is not defined or tool_call.name is none %}
130                    {{- raise_exception('Tool call is missing a function name.') }}
131                {%- endif %}
132                {%- if loop.first %}
133                    {%- if content|trim %}
134                        {{- '\n\n<tool_call>\n<function=' + tool_call.name + '>\n' }}
135                    {%- else %}
136                        {{- '<tool_call>\n<function=' + tool_call.name + '>\n' }}
137                    {%- endif %}
138                {%- else %}
139                    {{- '\n<tool_call>\n<function=' + tool_call.name + '>\n' }}
140                {%- endif %}
141                {%- if tool_call.arguments is mapping %}
142                    {%- for args_name, args_value in tool_call.arguments|items %}
143                        {{- '<parameter=' + args_name + '>\n' }}
144                        {%- set args_value = args_value | string if args_value is string else args_value | tojson | safe %}
145                        {{- args_value }}
146                        {{- '\n</parameter>\n' }}
147                    {%- endfor %}
148                {%- elif tool_call.arguments is string %}
149                    {%- if tool_call.arguments|trim %}
150                        {{- raise_exception('Tool call arguments for function "' + (tool_call.name | string) + '" were passed as a JSON string. Parse them into an object before calling apply_chat_template.') }}
151                    {%- endif %}
152                {%- elif tool_call.arguments is defined and tool_call.arguments is not none %}
153                    {{- raise_exception('Tool call arguments for function "' + (tool_call.name | string) + '" must be an object/mapping or a JSON string.') }}
154                {%- endif %}
155                {{- '</function>\n</tool_call>' }}
156            {%- endfor %}
157        {%- endif %}
158        {{- '<|im_end|>\n' }}
159    {%- elif message.role == "tool" %}
160        {%- if loop.previtem and loop.previtem.role != "tool" %}
161            {{- '<|im_start|>user' }}
162        {%- endif %}
163        {{- '\n<tool_response>\n' }}
164        {{- content }}
165        {{- '\n</tool_response>' }}
166        {%- if not loop.last and loop.nextitem.role != "tool" %}
167            {{- '<|im_end|>\n' }}
168        {%- elif loop.last %}
169            {{- '<|im_end|>\n' }}
170        {%- endif %}
171    {%- else %}
172        {{- raise_exception('Unexpected message role.') }}
173    {%- endif %}
174    {%- endif %}
175{%- endfor %}
176{%- if add_generation_prompt %}
177    {{- '<|im_start|>assistant\n' }}
178    {%- if enable_thinking is defined and enable_thinking is false %}
179        {{- '<think>\n\n</think>\n\n' }}
180    {%- else %}
181        {{- '<think>\n' }}
182    {%- endif %}
183{%- endif %}
184{#- Unsloth fixes - developer role, merged system messages, tool calling #}