-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Add GPT-OSS tool calling support #5464
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
Changes from 24 commits
4b3aa51
0894910
730070b
4622d77
08d4c51
276559d
673c35d
604c476
83a7ef6
0f28384
e5d7cdf
a618809
a0b81b1
67ab0af
b18e39e
71ce5a0
8f1ad1e
9b9771d
b3f4481
76a0f66
0890038
3253602
b95dbec
ec81a1e
450b9ef
392dece
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -114,6 +114,49 @@ def clone_chat_template( | |
| return model, tokenizer, added_tokens | ||
|
|
||
|
|
||
| gptoss_schema = { | ||
| # Normalize final content to analysis format so both map to the same "content" group. | ||
| "x-regex-substitutions": [ | ||
| [r"<\|channel\|>final<\|message\|>(.*?)<\|return\|>", r"<|channel|>analysis<|message|>\1<|end|>"], | ||
| ], | ||
| "x-regex": r"^(?:<\|channel\|>analysis<\|message\|>(?P<content>.*?)<\|end\|>(?:<\|start\|>assistant)?)?\s*(?P<tool_calls>to=functions\.\S+<\|channel\|>commentary json<\|message\|>.*?<\|call\|>)?$", | ||
| "type": "object", | ||
| "properties": { | ||
| "role": {"const": "assistant"}, | ||
| "content": {"type": "string"}, | ||
| "tool_calls": { | ||
| "type": "array", | ||
| "x-regex-iterator": r"(to=functions\.\S+<\|channel\|>commentary json<\|message\|>.*?<\|call\|>)", | ||
| "items": { | ||
| # Convert "to=functions.NAME<|channel|>commentary json<|message|>ARGS<|call|>" | ||
| # into '{"name": "NAME", "arguments": ARGS}' so it can be parsed as JSON. | ||
| "x-regex-substitutions": [ | ||
| [ | ||
| r"to=functions\.(\S+)<\|channel\|>commentary json<\|message\|>(.*?)<\|call\|>", | ||
| r'{"name": "\1", "arguments": \2}', | ||
| ], | ||
| ], | ||
| "x-parser": "json", | ||
| "x-parser-args": {"transform": "{type: 'function', function: @}"}, | ||
| "type": "object", | ||
| "properties": { | ||
| "type": {"const": "function"}, | ||
| "function": { | ||
| "type": "object", | ||
| "properties": { | ||
| "name": {"type": "string"}, | ||
| "arguments": { | ||
| "type": "object", | ||
| "additionalProperties": {}, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| # Adapted and corrected versions of the schemas from: | ||
| # https://github.com/huggingface/transformers/blob/main/tests/utils/test_chat_parsing_utils.py | ||
| qwen3_schema = { | ||
|
|
@@ -183,11 +226,13 @@ def clone_chat_template( | |
| }, | ||
| } | ||
|
|
||
|
|
||
| gptoss_chat_template = (_CHAT_TEMPLATES_DIR / "gptoss.jinja").read_text() | ||
|
|
||
| qwen3_chat_template = (_CHAT_TEMPLATES_DIR / "qwen3.jinja").read_text() | ||
|
|
||
| qwen3_5_chat_template_2b_and_below = (_CHAT_TEMPLATES_DIR / "qwen3_5_2b_and_below.jinja").read_text() | ||
|
|
||
|
|
||
| qwen3_5_chat_template_4b_and_above = (_CHAT_TEMPLATES_DIR / "qwen3_5_4b_and_above.jinja").read_text() | ||
|
|
||
|
|
||
|
|
@@ -220,6 +265,9 @@ def add_response_schema(tokenizer: PreTrainedTokenizer) -> PreTrainedTokenizer: | |
| {'role': 'assistant', 'content': '', 'tool_calls': [{'type': 'function', 'function': {'name': 'multiply', 'arguments': {'a': 3, 'b': 4}}}]} | ||
| ``` | ||
| """ | ||
| if tokenizer.chat_template == gptoss_chat_template: | ||
| tokenizer.response_schema = gptoss_schema | ||
| return tokenizer | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prefix-preserving check diverges from suffix extraction constructionLow Severity
Additional Locations (2)Reviewed by Cursor Bugbot for commit 450b9ef. Configure here. |
||
| if tokenizer.chat_template == qwen3_chat_template: | ||
| tokenizer.response_schema = qwen3_schema | ||
| return tokenizer | ||
|
|
||


Uh oh!
There was an error while loading. Please reload this page.