-
Notifications
You must be signed in to change notification settings - Fork 238
Gpt-oss chat template modifications to support built-in tools, added license, switch to default low reasoning #3945
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
dkalinowski
wants to merge
3
commits into
main
Choose a base branch
from
chat-template-builtin-tools
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.
+41
−16
Open
Changes from all commits
Commits
Show all changes
3 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
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,4 +1,20 @@ | ||||||||||||||||||||||||
| {#- | ||||||||||||||||||||||||
| ***************************************************************************** | ||||||||||||||||||||||||
| Copyright 2025 Intel Corporation | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||||||||||||||||||||||||
| you may not use this file except in compliance with the License. | ||||||||||||||||||||||||
| You may obtain a copy of the License at | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| Unless required by applicable law or agreed to in writing, software | ||||||||||||||||||||||||
| distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||||||||||||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||||||||||||
| See the License for the specific language governing permissions and | ||||||||||||||||||||||||
| limitations under the License. | ||||||||||||||||||||||||
| ***************************************************************************** | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| Modifications to original chat template: | ||||||||||||||||||||||||
| * Allowing reasoning_effort=none; this automatically adds empty reasoning channel at the end. It should force the model to follow with regular response/tool call immediately. | ||||||||||||||||||||||||
| IMPORTANT: When none used, reasoning_effort is rendered as low in the reasoning definition slot (as it is the lowest possible reasoning during model training). | ||||||||||||||||||||||||
|
|
@@ -12,13 +28,18 @@ | |||||||||||||||||||||||
| BFCL sends empty array and chat template accessed index 0 assuming there always is some tool call. New chat template ignores empty arrays now. | ||||||||||||||||||||||||
| * Removed "|tojson" from tool argument rendering. This introduced string escaping drastically influenced following generations. OpenAI Harmony format assumes no escaping of arguments. | ||||||||||||||||||||||||
| This was related to both: function call output (result from mcp servers) and function call arguments (input to mcp servers) in chat history. | ||||||||||||||||||||||||
| * Improved built-in tool history rendering based on documentation: https://developers.openai.com/cookbook/articles/openai-harmony/#function-calling | ||||||||||||||||||||||||
| Built-in tools are supposed to be rendered in analysis channel (contrary to user-defined functions rendered in commentary). | ||||||||||||||||||||||||
| Fixed rendering of message constrain in histories to follow documentation. | ||||||||||||||||||||||||
| Added rendering previous reasoning_content messages as those are crucial for multi-step chain of thought. | ||||||||||||||||||||||||
| * Switched default reasoning mode to low | ||||||||||||||||||||||||
| #} | ||||||||||||||||||||||||
| {#- | ||||||||||||||||||||||||
| In addition to the normal inputs of `messages` and `tools`, this template also accepts the | ||||||||||||||||||||||||
| following kwargs: | ||||||||||||||||||||||||
| - "builtin_tools": A list, can contain "browser" and/or "python". | ||||||||||||||||||||||||
| - "model_identity": A string that optionally describes the model identity. | ||||||||||||||||||||||||
| - "reasoning_effort": A string that describes the reasoning effort, defaults to "medium". | ||||||||||||||||||||||||
| - "reasoning_effort": A string that describes the reasoning effort, defaults to "low". | ||||||||||||||||||||||||
| #} | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| {#- Tool Definition Rendering ============================================== #} | ||||||||||||||||||||||||
|
|
@@ -216,7 +237,7 @@ | |||||||||||||||||||||||
| {{- "Knowledge cutoff: 2024-06\n" }} | ||||||||||||||||||||||||
| {{- "Current date: " + strftime_now("%Y-%m-%d") + "\n\n" }} | ||||||||||||||||||||||||
| {%- if reasoning_effort is not defined %} | ||||||||||||||||||||||||
| {%- set reasoning_effort = "medium" %} | ||||||||||||||||||||||||
| {%- set reasoning_effort = "low" %} | ||||||||||||||||||||||||
| {%- endif %} | ||||||||||||||||||||||||
| {#- {{- "Reasoning: " + reasoning_effort + "\n\n" }} #} | ||||||||||||||||||||||||
| {%- set display_effort = reasoning_effort %} | ||||||||||||||||||||||||
|
|
@@ -296,18 +317,12 @@ | |||||||||||||||||||||||
| {#- when we render CoT/analysis messages in inference. #} | ||||||||||||||||||||||||
| {%- set future_final_message = namespace(found=false) %} | ||||||||||||||||||||||||
| {%- for future_message in loop_messages[loop.index:] %} | ||||||||||||||||||||||||
| {%- if future_message.role == 'assistant' and "tool_calls" not in future_message %} | ||||||||||||||||||||||||
| {#- {%- if future_message.role == 'assistant' and "tool_calls" not in future_message %} #} | ||||||||||||||||||||||||
| {%- if future_message.role == 'assistant' and "content" in future_message %} | ||||||||||||||||||||||||
| {%- set future_final_message.found = true %} | ||||||||||||||||||||||||
| {%- endif %} | ||||||||||||||||||||||||
| {%- endfor %} | ||||||||||||||||||||||||
| {%- if message.content and message.reasoning_content and not future_final_message.found %} | ||||||||||||||||||||||||
| {#- Original: {{- raise_exception("Cannot pass both content and reasoning_content in an assistant message with tool calls! Put the analysis message in one or the other, but not both.") }} #} | ||||||||||||||||||||||||
| {#- Mod: Exception suppressed, multi-turn BFCL benchmark contains such situations. #} | ||||||||||||||||||||||||
| {#- Prefer rendering content over reasoning when both are available, looks like it contains more information. #} | ||||||||||||||||||||||||
| {{- "<|start|>assistant<|channel|>analysis<|message|>" + message.content + "<|end|>" }} | ||||||||||||||||||||||||
| {%- elif message.content and not future_final_message.found %} | ||||||||||||||||||||||||
| {{- "<|start|>assistant<|channel|>analysis<|message|>" + message.content + "<|end|>" }} | ||||||||||||||||||||||||
| {%- elif message.reasoning_content and not future_final_message.found %} | ||||||||||||||||||||||||
| {%- if message.reasoning_content %} | ||||||||||||||||||||||||
| {{- "<|start|>assistant<|channel|>analysis<|message|>" + message.reasoning_content + "<|end|>" }} | ||||||||||||||||||||||||
| {%- endif %} | ||||||||||||||||||||||||
| {#- Mod: this check was not present, causing crashes if tool_calls array was empty #} | ||||||||||||||||||||||||
|
|
@@ -318,17 +333,23 @@ | |||||||||||||||||||||||
| {%- if tool_call.function %} | ||||||||||||||||||||||||
| {%- set tool_call = tool_call.function %} | ||||||||||||||||||||||||
| {%- endif %} | ||||||||||||||||||||||||
| {{- "<|start|>assistant to=" }} | ||||||||||||||||||||||||
| {{- "functions." + tool_call.name + " <|channel|>commentary " }} | ||||||||||||||||||||||||
| {{- (tool_call.content_type if tool_call.content_type is defined else "json") + "<|message|>" }} | ||||||||||||||||||||||||
| {#- Check if this is a builtin tool (browser, python) or a custom function #} | ||||||||||||||||||||||||
| {{- "<|start|>assistant" }} | ||||||||||||||||||||||||
| {%- if tool_call.name in ['python', 'browser.search', 'browser.open', 'browser.find'] %} | ||||||||||||||||||||||||
| {{- "<|channel|>analysis to=" + tool_call.name + " <|message|>" }} | ||||||||||||||||||||||||
| {%- set last_tool_call.name = tool_call.name %} | ||||||||||||||||||||||||
| {%- else %} | ||||||||||||||||||||||||
| {{- "<|channel|>commentary to=functions." + tool_call.name + " " }} | ||||||||||||||||||||||||
| {{- (tool_call.content_type if tool_call.content_type is defined else "<|constrain|>json") + "<|message|>" }} | ||||||||||||||||||||||||
| {%- set last_tool_call.name = tool_call.name %} | ||||||||||||||||||||||||
| {%- endif %} | ||||||||||||||||||||||||
|
Comment on lines
+340
to
+345
|
||||||||||||||||||||||||
| {%- set last_tool_call.name = tool_call.name %} | |
| {%- else %} | |
| {{- "<|channel|>commentary to=functions." + tool_call.name + " " }} | |
| {{- (tool_call.content_type if tool_call.content_type is defined else "<|constrain|>json") + "<|message|>" }} | |
| {%- set last_tool_call.name = tool_call.name %} | |
| {%- endif %} | |
| {%- else %} | |
| {{- "<|channel|>commentary to=functions." + tool_call.name + " " }} | |
| {{- (tool_call.content_type if tool_call.content_type is defined else "<|constrain|>json") + "<|message|>" }} | |
| {%- endif %} | |
| {%- set last_tool_call.name = tool_call.name %} |
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.
The assignment of last_tool_call.name is duplicated in both branches of the if-else statement. This can be moved outside the conditional block (after line 344) to avoid redundancy.