Views
No views yet
1from transformers import AutoModelForCausalLM, AutoTokenizer
2import torch
3
4model = AutoModelForCausalLM.from_pretrained(
5 "YOUR_USERNAME/Qwen2.5-7B-Instruct-ToolRL-PPO-Cold",
6 torch_dtype=torch.bfloat16,
7 device_map="auto"
8)
9tokenizer = AutoTokenizer.from_pretrained(
10 "YOUR_USERNAME/Qwen2.5-7B-Instruct-ToolRL-PPO-Cold"
11)
12
13system_prompt = """You are a helpful multi-turn dialogue assistant capable of leveraging tool calls to solve user tasks.
14
15**Available Tools**
161. Name: {tool_name}
17Description: {tool_description}
18Parameters: {tool_params}
19
20**Output Format**
21<think> Your thoughts </think>
22<tool_call>
23{"name": "Tool name", "parameters": {"param": "value"}}
24</tool_call>
25<response> Final response </response>"""
26
27user_prompt = "**Dialogue Records History**\n<user> {question} </user>"
28
29messages = [
30 {"role": "system", "content": system_prompt},
31 {"role": "user", "content": user_prompt}
32]
33
34prompt = tokenizer.apply_chat_template(
35 messages, tokenize=False, add_generation_prompt=True
36)
37inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
38outputs = model.generate(**inputs, max_new_tokens=200, do_sample=False)
39response = tokenizer.decode(outputs[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
40print(response)<think> The user wants to add 1234 and 5678. I will use the calculator tool. </think>
<tool_call>
{"name": "calculator", "parameters": {"a": 1234, "b": 5678, "op": "+"}}
</tool_call><think> The user wants to know the current weather in Tokyo. I will use the get_weather tool. </think>
<tool_call>
{"name": "get_weather", "parameters": {"city": "Tokyo"}}
</tool_call><think> The user wants to know the latest news about AI. I will use the web_search tool. </think>
<tool_call>
{"name": "web_search", "parameters": {"query": "latest news about AI"}}
</tool_call>1{
2 "algorithm": "PPO",
3 "batch_size": 512,
4 "epochs": 15,
5 "actor_lr": 1e-6,
6 "critic_lr": 1e-5,
7 "kl_coef": 0.05,
8 "max_grad_norm": 1.0,
9 "ppo_clip_range": 0.1,
10 "normalize_advantages": True,
11 "max_prompt_length": 1024,
12 "max_response_length": 512,
13}1{
2 "CORRECTMAX1": 1, # Reward range [-1, +1] instead of [-3, +3]
3 "WITHLENGTH": 0, # Length reward disabled
4 "REFINEDREWARD": 0, # Refined reward disabled
5 "COARSEREWARD": 0, # Coarse reward disabled
6 "STRICTMATCH": 0, # Strict match disabled
7}Format score: [0.0, +1.0]
Correctness score: [-1.0, +1.0] ← CORRECTMAX1=1
Total range: [-1.0, +2.0]
Note: Original ToolRL paper uses [-3.0, +3.0] correctness range.
Results not directly comparable to paper without rescaling.1@article{toolrl2025,
2 title={ToolRL: Reward is All Tool Learning Needs},
3 year={2025}
4}