-
Notifications
You must be signed in to change notification settings - Fork 21.9k
Muse Glimmer: fix detection of tool calls after EOM #26879
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+261
−2
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,211 @@ | ||
| {# | ||
| Template: Muse Glimmer ATEM Chat Template | ||
| Renders the ATEM tool-calling protocol: reasoning channel (to=self), tool | ||
| channels (to=<tool>), and the user channel, plus tool definitions and the | ||
| valid-recipient list in the system block. | ||
|
|
||
| Whitespace note: every tag uses the {%- -%} / {{- -}} stripping markers, so | ||
| the indentation below is purely for readability and contributes nothing to | ||
| the rendered output. | ||
| #} | ||
| {%- macro render_content(content) -%} | ||
| {%- if content is string -%} | ||
| {{- content -}} | ||
| {%- elif content is not none -%} | ||
| {%- for part in content -%} | ||
| {%- if part['type'] == 'image' -%} | ||
| {{- '<|patch|>' -}} | ||
| {%- elif part['type'] == 'video' -%} | ||
| {{- '<|video|>' -}} | ||
| {%- elif part['type'] == 'text' -%} | ||
| {{- part['text'] -}} | ||
| {%- endif -%} | ||
| {%- endfor -%} | ||
| {%- endif -%} | ||
| {%- endmacro -%} | ||
| {%- macro render_atem(tc) -%} | ||
| {%- set args = tc.function.arguments -%} | ||
| {%- if args is not mapping -%} | ||
| {{- raise_exception('Muse Glimmer ATEM chat template requires tool_call.function.arguments to be a dict (mapping); a JSON string cannot be parsed in the HF jinja sandbox.') -}} | ||
| {%- endif -%} | ||
| {{- '<atem:function_calls>\n<atem:invoke name="' + tc.function.name + '">\n' -}} | ||
| {%- for k, v in args.items() -%} | ||
| {{- '<atem:parameter name="' + 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) -%} | ||
| {{- v | tojson -}} | ||
| {%- else -%} | ||
| {{- v -}} | ||
| {%- endif -%} | ||
| {{- '</atem:parameter>\n' -}} | ||
| {%- endfor -%} | ||
| {{- '</atem:invoke>\n</atem:function_calls>' -}} | ||
| {%- endmacro -%} | ||
| {%- 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:\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' -}} | ||
| {{- '// Tool metadata\n' -}} | ||
| {%- set nsns = namespace(seen=[]) -%} | ||
| {%- for tool in tools -%} | ||
| {%- set fn = tool.function if tool.function is defined else tool -%} | ||
| {%- set tns = fn.name.split('.')[0] -%} | ||
| {%- if tns not in nsns.seen -%} | ||
| {%- set nsns.seen = nsns.seen + [tns] -%} | ||
| {%- endif -%} | ||
| {%- endfor -%} | ||
| {%- set nd = tool_namespace_descriptions if tool_namespace_descriptions is defined else {} -%} | ||
| {%- for tns in nsns.seen -%} | ||
| {{- '{"name": ' + (tns | tojson) + ', "description": ' + ((nd[tns] if tns in nd else '') | tojson) + '}\n' -}} | ||
| {%- endfor -%} | ||
| {{- '// Function schemas' -}} | ||
| {%- for tool in tools -%} | ||
| {%- set fn = tool.function if tool.function is defined else tool -%} | ||
| {{- '\n{"name": ' + (fn.name | tojson) + ', "description": ' + (fn.description | tojson) + ', "parameters": ' + (fn.parameters | tojson) + '}' -}} | ||
| {%- endfor -%} | ||
| {{- '\n\nHere\'s an example of how to call a function in the tool set:\n' -}} | ||
| {{- '(If the tool namespace is not specified, invoke the function directly as `example_function_name` rather than `example_tool_name.example_function_name`)\n\n' -}} | ||
| {{- 'to=example_tool_name.example_function_name\n\n' -}} | ||
| {{- '<atem:function_calls>\n<atem:invoke name="example_tool_name.example_function_name">\n' -}} | ||
| {{- '<atem:parameter name="example_parameter_1">value_1</atem:parameter>\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:invoke>\n</atem:function_calls>' -}} | ||
| {%- endmacro -%} | ||
| {%- macro render_reasoning() -%} | ||
| {%- set rs = reasoning_strength if reasoning_strength is defined and reasoning_strength else 'high' -%} | ||
| {{- 'Reasoning strength: ' + rs + '.' -}} | ||
| {%- endmacro -%} | ||
| {%- macro render_system_meta(tools) -%} | ||
| {%- set rns = namespace(recipients=['"self"'], nslist=[]) -%} | ||
| {%- if tools -%} | ||
| {%- for tool in tools -%} | ||
| {%- set fn = tool.function if tool.function is defined else tool -%} | ||
| {%- set tns = fn.name.split('.')[0] -%} | ||
| {%- if tns not in rns.nslist -%} | ||
| {%- set rns.nslist = rns.nslist + [tns] -%} | ||
| {%- endif -%} | ||
| {%- endfor -%} | ||
| {%- for tns in rns.nslist -%} | ||
| {%- set rns.recipients = rns.recipients + ['"' + tns + '.*"'] -%} | ||
| {%- endfor -%} | ||
| {%- endif -%} | ||
| {%- set rns.recipients = rns.recipients + ['"user"'] -%} | ||
| {{- '# Valid recipients: ' + rns.recipients | join(', ') + '.' -}} | ||
| {%- endmacro -%} | ||
| {{- bos_token -}} | ||
| {%- set ns = namespace(has_system=false) -%} | ||
| {%- for m in messages -%} | ||
| {%- if m['role'] == 'system' -%} | ||
| {%- set ns.has_system = true -%} | ||
| {%- endif -%} | ||
| {%- endfor -%} | ||
| {%- if not ns.has_system -%} | ||
| {{- '<|start|>system<|message|>You are a helpful AI assistant.' -}} | ||
| {%- set kc = knowledge_cutoff if knowledge_cutoff is defined and knowledge_cutoff else '2026-01-04' -%} | ||
| {{- '\nKnowledge cutoff: ' + kc + '.' -}} | ||
| {%- if current_date is defined and current_date -%} | ||
| {{- '\nCurrent date: ' + current_date + '.' -}} | ||
| {%- elif strftime_now is defined -%} | ||
| {{- '\nCurrent date: ' + strftime_now('%Y-%m-%d') + '.' -}} | ||
| {%- endif -%} | ||
| {{- '\n\n' -}} | ||
| {{- render_reasoning() -}} | ||
| {%- if tools -%} | ||
| {{- '\n\n' -}} | ||
| {{- render_tool_defs(tools) -}} | ||
| {%- endif -%} | ||
| {{- '\n\n' -}} | ||
| {{- render_system_meta(tools) -}} | ||
| {{- '<|eot|>' -}} | ||
| {%- endif -%} | ||
| {%- for message in messages -%} | ||
| {%- set role = message['role'] -%} | ||
| {%- set end_token = '<|eom|>' if (not loop.last and messages[loop.index0 + 1]['role'] == role) else '<|eot|>' -%} | ||
| {%- if role == 'system' -%} | ||
| {#- Callers sometimes write the directive into the system prompt themselves. | ||
| Normalise "Reasoning effort" to "Reasoning strength" (jinja has no | ||
| case-insensitive replace, hence the four realistic casings), then skip | ||
| the kwarg-driven line below if the prompt already carries one. -#} | ||
| {%- set sys_text = render_content(message['content']) | ||
| | replace('Reasoning effort', 'Reasoning strength') | ||
| | replace('Reasoning Effort', 'Reasoning Strength') | ||
| | replace('reasoning effort', 'reasoning strength') | ||
| | replace('REASONING EFFORT', 'REASONING STRENGTH') -%} | ||
| {{- '<|start|>system<|message|>' -}} | ||
| {{- sys_text -}} | ||
| {%- if 'reasoning strength' not in (sys_text | lower) -%} | ||
| {{- '\n\n' -}} | ||
| {{- render_reasoning() -}} | ||
| {%- endif -%} | ||
| {%- if tools -%} | ||
| {{- '\n\n' -}} | ||
| {{- render_tool_defs(tools) -}} | ||
| {%- endif -%} | ||
| {{- '\n\n' -}} | ||
| {{- render_system_meta(tools) -}} | ||
| {{- '<|eot|>' -}} | ||
| {%- elif role == 'user' -%} | ||
| {{- '<|start|>user<|message|>' -}} | ||
| {{- render_content(message['content']) -}} | ||
| {{- '<|eot|>' -}} | ||
| {%- elif role == 'tool' -%} | ||
| {%- set tname = message.get('name') -%} | ||
| {%- if not tname -%} | ||
| {%- set tcid = message.get('tool_call_id') -%} | ||
| {%- set rns = 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 rns.name = tc.function.name -%} | ||
| {%- endif -%} | ||
| {%- endfor -%} | ||
| {%- endif -%} | ||
| {%- endfor -%} | ||
| {%- set tname = rns.name -%} | ||
| {%- endif -%} | ||
| {{- '<|start|>tool ' + tname + '<|message|><tool_output name="' + tname + '">\n' -}} | ||
| {{- render_content(message['content']) -}} | ||
| {{- '\n</tool_output><|eot|>' -}} | ||
| {%- elif role == 'assistant' -%} | ||
| {%- if message.get('reasoning_content') -%} | ||
| {{- '<|start|>assistant to=self<|message|>' + message['reasoning_content'] + '<|eom|>' -}} | ||
| {%- endif -%} | ||
| {%- if message.get('tool_calls') -%} | ||
| {%- for tc in message['tool_calls'] -%} | ||
| {{- '<|start|>assistant to=' + tc.function.name + '<|message|>' -}} | ||
| {{- render_atem(tc) -}} | ||
| {%- if loop.last -%} | ||
| {{- end_token -}} | ||
| {%- else -%} | ||
| {{- '<|eom|>' -}} | ||
| {%- endif -%} | ||
| {%- endfor -%} | ||
| {%- else -%} | ||
| {%- set recipient = message.get('recipient') or 'user' -%} | ||
| {%- set end_turn = message.get('end_turn') -%} | ||
| {%- if end_turn is none -%} | ||
| {%- set end_turn = not (recipient and recipient != 'user') -%} | ||
| {%- endif -%} | ||
| {{- '<|start|>assistant' -}} | ||
| {%- if recipient -%} | ||
| {{- ' to=' + recipient -}} | ||
| {%- endif -%} | ||
| {{- '<|message|>' -}} | ||
| {{- render_content(message['content']) -}} | ||
| {{- ('<|eot|>' if end_turn else '<|eom|>') -}} | ||
| {%- endif -%} | ||
| {%- endif -%} | ||
| {%- endfor -%} | ||
| {%- if add_generation_prompt -%} | ||
| {{- '<|start|>assistant' -}} | ||
| {%- endif -%} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OAI uses
reasoning_effort- wondering if that would be a better name? also for consistency with other templates e.g. deepseek and gpt-oss-120bThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The model has been trained explicitly with "reasoning strength", not "reasoning effort". If people actually say reasoning effort the behavior is undefined, I wanted to keep as aligned with training as possible
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see how it would be harmful to set
rstoreasoning_strengthif that is defined, and if not, then set it toreasoning_effortif that is defined. It would certainly improve the consistency and ease of use with other models, and the model never sees the name of the chat template variables.The real problem (in my opinion) is that llama.cpp still has no native concept of reasoning levels or how to map them to what the model is expecting. I don't think users should have to control this with a raw chat template variable. The usability problem is getting worse all the time as more models release with various reasoning levels.
But, my opinion on this stuff doesn't really matter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@coder543 this is my idea:
sobakasu@29d426a
the jinja template for each model would be responsible for mapping OAI resoning_effort to the model's accepted values / equivalent concept.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to confirm, you mean the jinja template that lives in HuggingFace, right? That is usually a standalone model artifact, that is not really aware of llama.cpp innerworks at all.
I think expecting model templates to even be aware of some OpenAI flag compatibility doesn't sound great. Isn't it better if this translation layer lives in llama.cpp directly? If llama.cpp wants a unified "reasoning_effort" flag that's fine, but then there's some code that maps that to each individual template (so in my case reasoning_effort->reasoning_strength) internally before reaching jinja.
Otherwise you would be forcing jinja files in HF to somehow have to work around serving logic that is not model specific
Let me know if Im missing anything
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought we were talking about a jinja file that lives in this repo, not huggingface? And it's not really OpenAI compatibility. Other models like DeepSeek V4, Kimi K3, and Hy3 use the same config variable name of
reasoning_effort.reasoning_efforthas been the standard variable name for models with named reasoning levels across all open weight models in the industry that I'm aware of until this model introducedreasoning_strength.I see it as the same thing as standard
rolenames likeassistantanduser. There's technically nothing that requires models to use those in the jinja template, but breaking compatibility there would make integration with standard inference runtimes harder. Unless there is a good reason for changingreasoning_efforttoreasoning_strength, then I wish companies wouldn't. It only makes things harder for people looking to integrate with this model. There are surely tons of clients that already hard codereasoning_effortwhich will have to be changed unlessllama.cpp(andvLLM, andSGLang, and every other runtime) adds a compatibility shim just for this model.But again, my opinion doesn't matter here. I'm just sharing some of my perspective as someone who has closely tracked open weight models for years. The addition of reasoning levels in this model is awesome, and I appreciate that, although I haven't found any benchmarks that show how much the reasoning levels help.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The template should be the one from the HF repository. The only exceptions are models that don't ship with a template.