qgallouedec's picture
qgallouedec HF Staff
Upload MuseSparkForConditionalGeneration (#2)
c9e83e8
Raw History Blame Contribute Delete
16.3 kB
{%- set tools = tools | default(none) -%}
{%- set add_generation_prompt = add_generation_prompt | default(false) -%}
{%- set reasoning_effort = reasoning_effort | default('xhigh', true) -%}
{%- set cutoff_date = cutoff_date | default('2026-01-04') -%}
{%- set current_date = current_date | default(strftime_now('%Y-%m-%d') if strftime_now is defined else none) -%}
{%- set reasoning_effort_map = {'low': 8, 'medium': 32, 'high': 256, 'xhigh': 512} -%}
{%- if not (reasoning_effort is string and reasoning_effort in reasoning_effort_map) -%}
{{- raise_exception('Unsupported reasoning_effort: ' ~ reasoning_effort ~ '. Expected one of: low, medium, high, xhigh.') -}}
{%- endif -%}
{%- set deferred_render_mode = deferred_render_mode | default('tool_name_and_description') -%}
{#- Neutralise control sequences in caller-supplied text so it cannot be
tokenized as structure. "<|" is broken because every special token has the
form "<|...|>"; the bare media placeholders are broken because the
multimodal processor rewrites them positionally and a literal one in text
would desynchronise placeholders from the attached media. -#}
{%- macro esc(value) -%}
{{- value | string
| replace('<|', '< |')
| replace('<image>', '< image>')
| replace('<video>', '< video>')
| replace('<audio>', '< audio>') -}}
{%- endmacro -%}
{%- macro esc_header(value) -%}
{{- esc(value) | replace('\r', ' ') | replace('\n', ' ') -}}
{%- endmacro -%}
{%- macro esc_attr(value) -%}
{{- esc_header(value)
| replace('&', '&amp;')
| replace('"', '&quot;')
| replace("'", '&#39;')
| replace('<', '&lt;')
| replace('>', '&gt;') -}}
{%- endmacro -%}
{%- macro render_content(content) -%}
{%- if content is string -%}
{{- esc(content) -}}
{%- elif content is not none -%}
{%- for part in content -%}
{%- set part_type = part.get('type', '') -%}
{%- if part_type in ['image', 'input_image', 'image_url'] -%}
{{- '<|patch|>' -}}
{%- elif part_type in ['video', 'input_video', 'video_url'] -%}
{{- '<|video|>' -}}
{%- elif part_type in ['audio', 'input_audio', 'audio_url'] -%}
{{- '<|audio|>' -}}
{%- elif part_type in ['text', 'input_text', 'output_text'] -%}
{{- esc(part.get('text', '')) -}}
{%- endif -%}
{%- endfor -%}
{%- endif -%}
{%- endmacro -%}
{%- macro render_atem(tc) -%}
{%- set args = tc.function.arguments -%}
{%- if args is not mapping -%}
{{- raise_exception('This chat template requires tool_call.function.arguments to be a dict (mapping). The jinja sandbox has no JSON parser, so deserialize the arguments string before calling apply_chat_template.') -}}
{%- endif -%}
{{- '<atem:function_calls>\n<atem:invoke name="' -}}{{- esc_attr(tc.function.name) -}}{{- '">\n' -}}
{%- for k, v in args.items() -%}
{{- '<atem:parameter name="' -}}{{- esc_attr(k) -}}{{- '">' -}}
{%- if v is boolean -%}
{%- if v -%}
true
{%- else -%}
false
{%- endif -%}
{%- elif v is none -%}
null
{%- elif v is mapping or (v is iterable and v is not string) -%}
{{- esc(v | tojson) -}}
{%- else -%}
{{- esc(v) -}}
{%- endif -%}
{{- '</atem:parameter>\n' -}}
{%- endfor -%}
{{- '</atem:invoke>\n</atem:function_calls>' -}}
{%- endmacro -%}
{#- One function line, as the training renderer prints it. Deferred: name plus description and annotation
(tool_name_and_description) or annotation only (tool_name_only). Full: "additionalProperties": false is added
when the schema lacks it, and empty parameters become an empty object schema. -#}
{%- macro fn_line(name, fn, deferred) -%}
{%- set desc = fn.description if fn.description is defined and fn.description else '' -%}
{%- set p = fn.parameters if fn.parameters is defined and fn.parameters else {} -%}
{%- if deferred -%}
{%- set desc = ('' if deferred_render_mode == 'tool_name_only' else desc ~ ' ') ~ '[deferred: use tool_search to load full schema]' -%}
{{- '\n{"name": ' ~ esc(name | tojson) ~ ', "description": ' ~ esc(desc | tojson) ~ ', "defer_loading": true}' -}}
{%- else -%}
{%- if p is mapping and p | length == 0 -%}
{%- set p = '{"type": "object", "properties": {}, "additionalProperties": false}' -%}
{%- elif p is mapping and 'additionalProperties' not in p -%}
{%- set p = (p | tojson)[:-1] ~ ', "additionalProperties": false}' -%}
{%- else -%}
{%- set p = p | tojson -%}
{%- endif -%}
{{- '\n{"name": ' ~ esc(name | tojson) ~ ', "description": ' ~ esc(desc | tojson) ~ ', "parameters": ' ~ esc(p) ~ '}' -}}
{%- endif -%}
{%- endmacro -%}
{#- tools: OpenAI function tools, plus namespace tools {"type": "namespace", "name", "description", "defer_loading",
"tools": [function, ...]}. In a deferred namespace, functions without their own flag inherit the deferral; only
when some are true and some explicitly false do the explicitly eager ones keep their full schema. -#}
{%- macro render_tool_defs(tools) -%}
{{- 'In this environment you have access to a set of tools you can use to answer the user\'s question.\n\n' -}}
{{- 'You can invoke a function by writing a "<atem:function_calls>" block like the following as part of your reply to the user:\n' -}}
{{- '<atem:function_calls>\n<atem:invoke name="$FUNCTION_NAME">\n<atem:parameter name="$PARAMETER_NAME">$PARAMETER_VALUE</atem:parameter>\n...\n</atem:invoke>\n</atem:function_calls>\n\n' -}}
{{- 'String and scalar parameters should be specified as is, while lists and objects should use JSON format. Note that spaces for string values are not stripped. The output is not expected to be valid XML and is parsed with regular expressions.\n' -}}
{{- 'Here are the functions available in JSONSchema format:\n' -}}
{%- set td = namespace(meta='', fns='', loose=false) -%}
{%- for tool in tools -%}
{%- if tool.type is defined and tool.type == 'namespace' -%}
{%- set ns_deferred = tool.defer_loading is defined and tool.defer_loading -%}
{%- set ns_desc = tool.description if tool.description is defined and tool.description else '' -%}
{%- set members = tool.tools if tool.tools is defined and tool.tools else [] -%}
{%- set flags = namespace(t=false, f=false) -%}
{%- for m in members -%}
{%- set d = (m.function if m.function is defined else m).defer_loading -%}
{%- set flags.t = flags.t or d is not sameas false -%}
{%- set flags.f = flags.f or d is sameas false -%}
{%- endfor -%}
{%- if ns_deferred -%}
{%- set ns_desc = '[deferred: use tool_search to load descriptions and schemas]' if deferred_render_mode == 'tool_name_only' else (ns_desc ~ ' ' if ns_desc else '') ~ '[deferred: use tool_search to discover functions]' -%}
{%- endif -%}
{%- set td.meta = td.meta ~ '\n{"name": ' ~ esc(tool.name | tojson) ~ ', "description": ' ~ esc(ns_desc | tojson) ~ (', "has_deferred_functions": true}' if ns_deferred else '}') -%}
{%- for m in members -%}
{%- set mfn = m.function if m.function is defined else m -%}
{%- set eager = flags.t and flags.f and mfn.defer_loading is defined and mfn.defer_loading is sameas false -%}
{%- if not ns_deferred or eager or deferred_render_mode == 'tool_name_only' -%}
{%- set td.fns = td.fns ~ fn_line(tool.name ~ '.' ~ mfn.name, mfn, (not eager) if ns_deferred else (mfn.defer_loading is defined and mfn.defer_loading)) -%}
{%- endif -%}
{%- endfor -%}
{%- else -%}
{%- set fn = tool.function if tool.function is defined else tool -%}
{%- set td.meta = td.meta ~ ('' if td.loose else '\n{"name": "", "description": ""}') -%}
{%- set td.loose = true -%}
{%- set td.fns = td.fns ~ fn_line(fn.name, fn, fn.defer_loading is defined and fn.defer_loading) -%}
{%- endif -%}
{%- endfor -%}
{{- '// Tool metadata' ~ td.meta ~ '\n// Function schemas' ~ td.fns -}}
{{- '\n\n' -}}
{{- 'Here are examples of how to call functions in the tool set. To make parallel tool calls, emit each call as its own message (one tool call per message) in consecutive messages, as shown below:\n' -}}
{{- '(If the tool namespace is not specified, invoke the function directly as `example_function_name_1` rather than `example_tool_name_1.example_function_name_1`)\n\n' -}}
{{- 'to=example_tool_name_1.example_function_name_1\n\n' -}}
{{- '<atem:function_calls>\n<atem:invoke name="example_tool_name_1.example_function_name_1">\n' -}}
{{- '<atem:parameter name="example_parameter_1">value_1</atem:parameter>\n' -}}
{{- '</atem:invoke>\n</atem:function_calls>\n\n' -}}
{{- 'to=example_tool_name_2.example_function_name_2\n\n' -}}
{{- '<atem:function_calls>\n<atem:invoke name="example_tool_name_2.example_function_name_2">\n' -}}
{{- '<atem:parameter name="example_parameter_2">This is the value for the second parameter\nthat can span\n"multiple" lines\n</atem:parameter>\n' -}}
{{- '<atem:parameter name="example_parameter_3">value_3</atem:parameter>\n' -}}
{{- '</atem:invoke>\n</atem:function_calls>' -}}
{%- endmacro -%}
{%- macro render_valid_recipients(tools) -%}
{%- set rns = namespace(recipients=[]) -%}
{{- 'Use the appropriate recipient for each message:\n\n' -}}
{%- set rns.recipients = rns.recipients + ['"self"'] -%}
{{- '- "self": private reasoning and tool planning.\n' -}}
{%- set rns.recipients = rns.recipients + ['"commentary"'] -%}
{{- '- "commentary": user-visible intermediate updates while the assistant will continue working, including messages sent to the user before or between tool calls.\n' -}}
{%- if tools -%}
{%- set rt = namespace(first=none, ns=[]) -%}
{%- for tool in tools -%}
{%- if tool.type is defined and tool.type == 'namespace' -%}
{%- set rt.ns = rt.ns + ['"' ~ esc_header(tool.name) ~ '.*"'] -%}
{%- elif rt.first is none -%}
{%- set rt.first = (tool.function if tool.function is defined else tool).name -%}
{%- endif -%}
{%- endfor -%}
{%- set rns.recipients = rns.recipients + (['"[func_name] (e.g., ' ~ esc_header(rt.first) ~ ')"'] if rt.first is not none else []) + rt.ns -%}
{{- '- "tool" or "[tool name]": the name of the tool to call.\n' -}}
{%- endif -%}
{%- set rns.recipients = rns.recipients + ['"user"'] -%}
{{- '- "user": messages that end the assistant turn, such as a completed response or clarification question that waits for the user\'s reply; do not use for pre-tool updates or partial responses.\n' -}}
{{- '\n# Valid recipients: ' ~ rns.recipients | join(', ') ~ '.' -}}
{%- endmacro -%}
{{- bos_token -}}
{%- set ns = namespace(instructions=none, rest=messages) -%}
{%- if messages and messages[0]['role'] in ['system', 'developer'] -%}
{%- set ns.instructions = render_content(messages[0]['content'])
| replace('Reasoning effort', 'Reasoning strength')
| replace('Reasoning Effort', 'Reasoning Strength')
| replace('reasoning effort', 'reasoning strength')
| replace('REASONING EFFORT', 'REASONING STRENGTH') -%}
{%- set ns.rest = messages[1:] -%}
{%- endif -%}
{%- set sys = namespace(parts=['You are a helpful AI assistant.', 'Knowledge cutoff: ' ~ cutoff_date ~ '.']) -%}
{%- if current_date -%}
{%- set sys.parts = sys.parts + ['Current date: ' ~ current_date ~ '.'] -%}
{%- endif -%}
{%- if ns.instructions -%}
{%- set sys.parts = sys.parts + [ns.instructions] -%}
{%- endif -%}
{%- if tools -%}
{%- set sys.parts = sys.parts + [render_tool_defs(tools) | trim] -%}
{%- endif -%}
{%- if 'reasoning strength:' not in ((ns.instructions if ns.instructions else '') | lower) -%}
{%- set sys.parts = sys.parts + ['Reasoning strength: ' ~ reasoning_effort_map[reasoning_effort] ~ '.'] -%}
{%- endif -%}
{%- set sys.parts = sys.parts + [render_valid_recipients(tools) | trim] -%}
{{- '<|start|>system<|message|>' ~ sys.parts | join('\n\n') ~ '<|eot|>' -}}
{%- for message in ns.rest -%}
{%- set role = message['role'] -%}
{%- set end_token = '<|eom|>' if (not loop.last and ns.rest[loop.index0 + 1]['role'] == role) else '<|eot|>' -%}
{%- if role in ['system', 'developer'] -%}
{{- '<|start|>' + role + '<|message|>' -}}
{{- render_content(message['content']) -}}
{{- '<|eot|>' -}}
{%- elif role == 'user' -%}
{{- '<|start|>user<|message|>' -}}
{%- if message.get('render_timestamp') and message.get('timestamp') -%}
{%- set ts = message['timestamp'] -%}
{%- if ts is string -%}
{{- '[' ~ esc_header(ts) ~ '] ' -}}
{%- elif ts.strftime is defined -%}
{{- '[' ~ (ts.strftime('%a %Y-%m-%d %H:%M %Z') | trim) ~ '] ' -}}
{%- endif -%}
{%- endif -%}
{{- render_content(message['content']) -}}
{{- '<|eot|>' -}}
{%- elif role == 'tool' -%}
{%- set tname = message.get('name') -%}
{%- if not tname -%}
{%- set tcid = message.get('tool_call_id') -%}
{%- set tns = namespace(name=tcid if tcid else '') -%}
{%- for m in messages -%}
{%- if m.get('tool_calls') -%}
{%- for tc in m['tool_calls'] -%}
{%- if tcid is not none and tc.id is defined and tc.id == tcid -%}
{%- set tns.name = tc.function.name -%}
{%- endif -%}
{%- endfor -%}
{%- endif -%}
{%- endfor -%}
{%- set tname = tns.name -%}
{%- endif -%}
{%- if not tname -%}
{{- raise_exception('tool message has no name and no matching tool_call_id') -}}
{%- endif -%}
{{- '<|start|>tool ' -}}{{- esc_header(tname) -}}{{- '<|message|><tool_output name="' -}}{{- esc_attr(tname) -}}{{- '">\n' -}}
{{- render_content(message['content']) -}}
{{- '\n</tool_output><|eot|>' -}}
{%- elif role == 'assistant' -%}
{%- generation %}
{%- set reasoning = message.get('reasoning_content') or message.get('reasoning') -%}
{%- if reasoning -%}
{{- '<|start|>assistant to=self<|message|>' -}}{{- esc(reasoning) -}}{{- '<|eom|>' -}}
{%- endif -%}
{%- set body = render_content(message.get('content')) -%}
{%- if message.get('tool_calls') -%}
{%- if body -%}
{{- '<|start|>assistant to=commentary<|message|>' + body + '<|eom|>' -}}
{%- endif -%}
{%- for tc in message['tool_calls'] -%}
{{- '<|start|>assistant to=' -}}{{- esc_header(tc.function.name) -}}{{- '<|message|>' -}}
{{- render_atem(tc) -}}
{{- (end_token if loop.last else '<|eom|>') -}}
{%- endfor -%}
{%- elif body or not reasoning -%}
{%- set recipient = message.get('recipient') or 'user' -%}
{%- if recipient == 'user' -%}
{%- set end_turn = true -%}
{%- elif recipient in ['self', 'commentary'] -%}
{%- set end_turn = false -%}
{%- elif message.get('end_turn') is not none -%}
{%- set end_turn = message.get('end_turn') -%}
{%- else -%}
{%- set end_turn = false -%}
{%- endif -%}
{{- '<|start|>assistant to=' -}}{{- esc_header(recipient) -}}{{- '<|message|>' -}}
{{- body -}}
{{- ('<|eot|>' if end_turn else '<|eom|>') -}}
{%- endif -%}
{%- endgeneration %}
{%- endif -%}
{%- endfor -%}
{%- if add_generation_prompt -%}
{{- '<|start|>assistant' -}}
{%- endif -%}