-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Fix: Prevents merging of tool arguments during preprocessing #2909
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
winglian
merged 4 commits into
axolotl-ai-cloud:main
from
greenhestu:fix/redundant-none-chat-template
Jul 15, 2025
Merged
Changes from all commits
Commits
Show all changes
4 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
75 changes: 75 additions & 0 deletions
75
tests/prompt_strategies/test_chat_template_ds_schema_unification.py
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,75 @@ | ||
| """ | ||
| Tests for chat template prompt strategy with schema unification for none fields | ||
| """ | ||
|
|
||
| import json | ||
|
|
||
| import pytest | ||
| from datasets import Dataset | ||
| from transformers import AutoTokenizer | ||
|
|
||
| from axolotl.prompt_strategies.chat_template import StrategyLoader | ||
| from axolotl.utils.dict import DictDefault | ||
|
|
||
|
|
||
| @pytest.fixture(name="messages_w_tools") | ||
| def fixture_messages_w_tools(): | ||
| jsons = """ | ||
| {"messages":[{"role":"user","content":"move to (0, 1)"},{"role":"assistant","content":"","tool_calls":[{"function":{"name":"move","arguments":{"x":0,"y":1}}}]}],"tools":[{"type":"function","function":{"name":"move","description":"Move to a given location measured in meters","parameters":{"type":"object","properties":{"x":{"type":"number","description":"The x coordinate of the location, negative values are to the left, positive values are to the right"},"y":{"type":"number","description":"The y coordinate of the location, negative values are backward, positive values are forward"}},"required":["x","y"]}}},{"type":"function","function":{"name":"turn","description":"Turn the robot to a given direction","parameters":{"type":"object","properties":{"theta":{"type":"integer","description":"The angle to turn to, in degrees, positive values are counter-clockwise, negative values are clockwise"}},"required":["theta"]}}},{"type":"function","function":{"name":"invalid_prompt","description":"call when the user's prompt is invalid","parameters":{"type":"object","properties":{"message":{"type":"string","description":"why the prompt is invalid"}},"required":["message"]}}}],"add_generation_prompt":false} | ||
| {"messages":[{"role":"user","content":"turn 270 degree"},{"role":"assistant","content":"","tool_calls":[{"function":{"name":"turn","arguments":{"theta": 270}}}]}],"tools":[{"type":"function","function":{"name":"move","description":"Move to a given location measured in meters","parameters":{"type":"object","properties":{"x":{"type":"number","description":"The x coordinate of the location, negative values are to the left, positive values are to the right"},"y":{"type":"number","description":"The y coordinate of the location, negative values are backward, positive values are forward"}},"required":["x","y"]}}},{"type":"function","function":{"name":"turn","description":"Turn the robot to a given direction","parameters":{"type":"object","properties":{"theta":{"type":"integer","description":"The angle to turn to, in degrees, positive values are counter-clockwise, negative values are clockwise"}},"required":["theta"]}}},{"type":"function","function":{"name":"invalid_prompt","description":"call when the user's prompt is invalid","parameters":{"type":"object","properties":{"message":{"type":"string","description":"why the prompt is invalid"}},"required":["message"]}}}],"add_generation_prompt":false} | ||
| {"messages":[{"role":"user","content":"jump high"},{"role":"assistant","content":"","tool_calls":[{"function":{"name":"invalid_prompt","arguments":{"message": "jump is not a valid action"}}}]}],"tools":[{"type":"function","function":{"name":"move","description":"Move to a given location measured in meters","parameters":{"type":"object","properties":{"x":{"type":"number","description":"The x coordinate of the location, negative values are to the left, positive values are to the right"},"y":{"type":"number","description":"The y coordinate of the location, negative values are backward, positive values are forward"}},"required":["x","y"]}}},{"type":"function","function":{"name":"turn","description":"Turn the robot to a given direction","parameters":{"type":"object","properties":{"theta":{"type":"integer","description":"The angle to turn to, in degrees, positive values are counter-clockwise, negative values are clockwise"}},"required":["theta"]}}},{"type":"function","function":{"name":"invalid_prompt","description":"call when the user's prompt is invalid","parameters":{"type":"object","properties":{"message":{"type":"string","description":"why the prompt is invalid"}},"required":["message"]}}}],"add_generation_prompt":false} | ||
| """.strip().split( | ||
| "\n" | ||
| ) | ||
| rows = [json.loads(row) for row in jsons] | ||
| return Dataset.from_list(rows) | ||
|
|
||
|
|
||
| @pytest.fixture(name="qwen3_tokenizer") | ||
| def qwen3_tokenizer_fixture( | ||
| download_qwen3_half_billion_model, | ||
| ): # pylint: disable=unused-argument | ||
| tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen3-0.6B") | ||
|
|
||
| return tokenizer | ||
|
|
||
|
|
||
| @pytest.fixture(name="qwen3_prompt_strategy") | ||
| def qwen3_chat_template_strategy(qwen3_tokenizer): | ||
| cfg = DictDefault( | ||
| sequence_len=2048, | ||
| chat_template="qwen3", | ||
| eot_tokens=["<|im_end|>"], | ||
| ) | ||
| ds_cfg = DictDefault( | ||
| type="chat_template", | ||
| ) | ||
| load = StrategyLoader() | ||
| strat = load(qwen3_tokenizer, cfg, ds_cfg) | ||
| return strat | ||
|
|
||
|
|
||
| class TestSchemaUnification: | ||
| """ | ||
| Test class on handling null fields for tool calling | ||
| """ | ||
|
|
||
| def test_schema_unification_single_prompt( | ||
| self, messages_w_tools, qwen3_prompt_strategy, qwen3_tokenizer | ||
| ): | ||
| for row in messages_w_tools: | ||
| inputs = qwen3_prompt_strategy.tokenize_prompt(row) | ||
| decoded = qwen3_tokenizer.decode(inputs["input_ids"]) | ||
| tool_call = decoded.split("<tool_call>")[-1].split("</tool_call>")[0] | ||
| assert '"message": null' not in tool_call | ||
| assert '"theta": null' not in tool_call | ||
|
|
||
| def test_schema_unification_batched( | ||
| self, messages_w_tools, qwen3_prompt_strategy, qwen3_tokenizer | ||
| ): | ||
| rows = messages_w_tools.map(qwen3_prompt_strategy.tokenize_prompt, batched=True) | ||
| for row in rows: | ||
| decoded = qwen3_tokenizer.decode(row["input_ids"]) | ||
| tool_call = decoded.split("<tool_call>")[-1].split("</tool_call>")[0] | ||
| assert '"message": null' not in tool_call | ||
| assert '"theta": null' not in tool_call | ||
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.
Uh oh!
There was an error while loading. Please reload this page.