Skip to content

[BugFix] Fix Qwen parsing for tools #3087

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
merged 1 commit into from
Jul 23, 2025
Merged

[BugFix] Fix Qwen parsing for tools #3087

merged 1 commit into from
Jul 23, 2025

Conversation

vmoens
Copy link
Collaborator

@vmoens vmoens commented Jul 21, 2025

Fixes Qwen parsing for tools:

from torchrl.data.llm.history import History
from tensordict import set_list_to_stack
from transformers import AutoTokenizer

set_list_to_stack(True).set()

tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-7B-Instruct")

h = History(
    role=["system", "user", "assistant", "tool"],
    content=["You are a helpful assistant. Answer in <answer>...</answer> blocks, think in <think>...</think> blocks.", 
             "Write a python script that gives the capital of France or Germany.", 
             """<think>The capital of France is Paris, the capital of Germany is Berlin.</think> <answer><python>
capitals = {
    "France": "Paris",
    "Germany": "Berlin"
}
def get_capital(country):
    return capitals.get(country, "Unknown")
</python></answer>""", ""],
    tool_calls=[None, None, None, [
    {
        "id": "call_123",  # Unique identifier for this call
        "type": "function",  # Usually "function"
        "function": {
            "name": "get_capital",
            "arguments": '{"country": "Germany"}'  # JSON string
        }
    }
]],
    batch_size=4, tool_responses=[None, None, None, "Berlin"]
)

parsed = h.apply_chat_template(tokenizer=tokenizer, add_generation_prompt=False)
print(parsed)

produces

<|im_start|>system
You are a helpful assistant. Answer in <answer>...</answer> blocks, think in <think>...</think> blocks.<|im_end|>
<|im_start|>user
Write a python script that gives the capital of France or Germany.<|im_end|>
<|im_start|>assistant
<think>The capital of France is Paris, the capital of Germany is Berlin.</think> <answer><python>
capitals = {
    "France": "Paris",
    "Germany": "Berlin"
}
def get_capital(country):
    return capitals.get(country, "Unknown")
</python></answer><|im_end|>
    <|im_start|>tool
<tool_response>
Berlin
</tool_response><|im_end|>

Notice

  • the role is now tool
  • the tool response is printed

Copy link

pytorch-bot bot commented Jul 21, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/rl/3087

Note: Links to docs will display an error until the docs builds have been completed.

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

❌ 3 New Failures, 6 Unrelated Failures

As of commit f282268 with merge base b6d2156 (image):

NEW FAILURES - The following jobs have failed:

BROKEN TRUNK - The following jobs failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jul 21, 2025
@vmoens vmoens added the bug Something isn't working label Jul 23, 2025
@vmoens vmoens merged commit 5a40c0b into main Jul 23, 2025
94 of 103 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant