-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Add GLM-4-MoE tool calling support #5463
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
Open
qgallouedec
wants to merge
24
commits into
main
Choose a base branch
from
glm4moe-tool-calling
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
4b3aa51
Narrow prefix-preserving check to the actual requirement
qgallouedec 0894910
Merge branch 'main' into narrow-prefix-preserving-check
qgallouedec 730070b
Update chat template examples to use multiplication function calls
qgallouedec 4622d77
style
qgallouedec 08d4c51
Move chat templates from inline strings to `.jinja` files
qgallouedec 276559d
tools in dummy
qgallouedec 673c35d
Add chat template files to MANIFEST.in
qgallouedec 604c476
Enhance chat template handling to include tool call formatting in mes…
qgallouedec 83a7ef6
align grpo and async
qgallouedec 0f28384
Merge branch 'main' into chat-templates-files
qgallouedec e5d7cdf
revert no content
qgallouedec a618809
docstyle ignore
qgallouedec a0b81b1
Merge branch 'main' into chat-templates-files
qgallouedec 2384da5
Add GLM-4-MoE tool calling support
qgallouedec 14ac6a7
Merge branch 'main' into glm4moe-tool-calling
qgallouedec 0b85443
Apply suggestions from code review
qgallouedec c9dfa1f
doc
qgallouedec e06d88c
Merge branch 'main' into glm4moe-tool-calling
qgallouedec 303eac5
Merge branch 'main' into glm4moe-tool-calling
qgallouedec 87e23ed
Merge branch 'main' into glm4moe-tool-calling
qgallouedec d8985b2
Merge branch 'main' into glm4moe-tool-calling
qgallouedec dff9615
Apply suggestion from @qgallouedec
qgallouedec c31e258
Merge branch 'main' into glm4moe-tool-calling
qgallouedec 3fc2ca8
Merge branch 'main' into glm4moe-tool-calling
qgallouedec 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
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
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,103 @@ | ||
| [gMASK]<sop> | ||
| {%- if tools -%} | ||
| <|system|> | ||
| # Tools | ||
|
|
||
| You may call one or more functions to assist with the user query. | ||
|
|
||
| You are provided with function signatures within <tools></tools> XML tags: | ||
| <tools> | ||
| {% for tool in tools %} | ||
| {{ tool | tojson(ensure_ascii=False) }} | ||
| {% endfor %} | ||
| </tools> | ||
|
|
||
| For each function call, output the function name and arguments within the following XML format: | ||
| <tool_call>{function-name} | ||
| <arg_key>{arg-key-1}</arg_key> | ||
| <arg_value>{arg-value-1}</arg_value> | ||
| <arg_key>{arg-key-2}</arg_key> | ||
| <arg_value>{arg-value-2}</arg_value> | ||
| ... | ||
| </tool_call>{%- endif -%} | ||
| {%- macro visible_text(content) -%} | ||
| {%- if content is string -%} | ||
| {{- content }} | ||
| {%- elif content is iterable and content is not mapping -%} | ||
| {%- for item in content -%} | ||
| {%- if item is mapping and item.type == 'text' -%} | ||
| {{- item.text }} | ||
| {%- elif item is string -%} | ||
| {{- item }} | ||
| {%- endif -%} | ||
| {%- endfor -%} | ||
| {%- else -%} | ||
| {{- content }} | ||
| {%- endif -%} | ||
| {%- endmacro -%} | ||
| {%- set ns = namespace(last_user_index=-1) %} | ||
| {%- for m in messages %} | ||
| {%- if m.role == 'user' %} | ||
| {% set ns.last_user_index = loop.index0 -%} | ||
| {%- endif %} | ||
| {%- endfor %} | ||
| {% for m in messages %} | ||
| {%- if m.role == 'user' -%}<|user|> | ||
| {{ visible_text(m.content) }} | ||
| {{- '/nothink' if (enable_thinking is defined and not enable_thinking and not visible_text(m.content).endswith("/nothink")) else '' -}} | ||
| {%- elif m.role == 'assistant' -%} | ||
| <|assistant|> | ||
| {%- set reasoning_content = '' %} | ||
| {%- set content = visible_text(m.content) %} | ||
| {%- if m.reasoning_content is string %} | ||
| {%- set reasoning_content = m.reasoning_content %} | ||
| {%- else %} | ||
| {%- if '</think>' in content %} | ||
| {%- set reasoning_content = content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %} | ||
| {%- set content = content.split('</think>')[-1].lstrip('\n') %} | ||
| {%- endif %} | ||
| {%- endif %} | ||
| {%- if loop.index0 > ns.last_user_index and reasoning_content -%} | ||
| {{ '\n<think>' + reasoning_content.strip() + '</think>'}} | ||
| {%- else -%} | ||
| {{ '\n<think></think>' }} | ||
| {%- endif -%} | ||
| {%- if content.strip() -%} | ||
| {{ '\n' + content.strip() }} | ||
| {%- endif -%} | ||
| {% if m.tool_calls %} | ||
| {% for tc in m.tool_calls %} | ||
| {%- if tc.function %} | ||
| {%- set tc = tc.function %} | ||
| {%- endif %} | ||
| {{ '\n<tool_call>' + tc.name }} | ||
| {% set _args = tc.arguments %} | ||
| {% for k, v in _args.items() %} | ||
| <arg_key>{{ k }}</arg_key> | ||
| <arg_value>{{ v | tojson(ensure_ascii=False) if v is not string else v }}</arg_value> | ||
| {% endfor %} | ||
| </tool_call>{% endfor %} | ||
| {% endif %} | ||
| {%- elif m.role == 'tool' -%} | ||
| {%- if m.content is string -%} | ||
| {%- if loop.first or (messages[loop.index0 - 1].role != "tool") %} | ||
| {{- '<|observation|>' }} | ||
| {%- endif %} | ||
| {{- '\n<tool_response>\n' }} | ||
| {{- m.content }} | ||
| {{- '\n</tool_response>' }} | ||
| {%- else -%} | ||
| <|observation|>{% for tr in m.content %} | ||
|
|
||
| <tool_response> | ||
| {{ tr.output if tr.output is defined else tr }} | ||
| </tool_response>{% endfor -%} | ||
| {% endif -%} | ||
| {%- elif m.role == 'system' -%} | ||
| <|system|> | ||
| {{ visible_text(m.content) }} | ||
| {%- endif -%} | ||
| {%- endfor -%} | ||
| {%- if add_generation_prompt -%} | ||
| <|assistant|>{{- '\n<think></think>' if (enable_thinking is defined and not enable_thinking) else '' -}} | ||
| {%- endif -%} |
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.
Missing return statement in GLM-4-MoE branch
High Severity
The
glm4moebranch inadd_response_schemasets theresponse_schemabut is missing areturn tokenizerstatement. Execution falls through to the finalValueError, causingadd_response_schemato always fail for GLM-4-MoE tokenizers. This breaks tool-calling support, for example, duringGRPOTrainerinitialization.Reviewed by Cursor Bugbot for commit 3fc2ca8. Configure here.