forked from kimbochen/slime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_with_retool.py
More file actions
378 lines (307 loc) · 13.6 KB
/
Copy pathgenerate_with_retool.py
File metadata and controls
378 lines (307 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# Adapted from https://github.com/volcengine/verl/blob/cb809d66e46dfd3342d008628891a14a054fa424/recipe/retool/retool.py
import re
from typing import Any
try:
from jinja2 import Template
except ImportError as e:
raise ImportError("Jinja2 is required. Please install it with: pip install jinja2") from e
from slime.rollout.sglang_rollout import GenerateState
from slime.utils.http_utils import post
from slime.utils.types import Sample
# Import reward models
try:
from slime.rollout.rm_hub.math_dapo_utils import compute_score as math_dapo_compute_score
except ImportError as e:
raise ImportError("MathDapo is not installed") from e
# Import tool sandbox functionality
from tool_sandbox import SEMAPHORE, TOOL_CONFIGS, tool_registry
# Jinja2 template for tool-enabled conversations
TOOL_TEMPLATE = """<|im_start|>system
{%- if messages[0]['role'] == 'system' %}
{{- messages[0]['content'] }}
{%- else %}
You are a helpful assistant.
{%- endif %}
{%- if tools %}
# Tools
You may call one or more functions to assist with the user query.
You are provided with function signatures within <tools></tools> XML tags:
<tools>
{%- for tool in tools %}
{{- tool | tojson }}
{%- endfor %}
</tools>
For each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:
<tool_call>
{"name": <function-name>, "arguments": <args-json-object>}
</tool_call>
{%- endif %}
<|im_end|>
{%- for message in messages %}
{%- if message['role'] == 'user' %}
<|im_start|>user
{{- message['content'] }}<|im_end|>
{%- elif message['role'] == 'assistant' %}
<|im_start|>assistant
{{- message['content'] }}<|im_end|>
{%- endif %}
{%- endfor %}
<|im_start|>assistant
"""
def format_conversation_with_tools(
prompt: str, tools: list[dict[str, Any]] = None, system_prompt: str = None, messages: list[dict[str, Any]] = None
) -> str:
"""Format conversation using Jinja2 template with tool support"""
template = Template(TOOL_TEMPLATE)
# Prepare messages
messages_to_render = []
# Always add system message - use provided one or default
if system_prompt:
system_content = system_prompt
else:
system_content = (
"You are a helpful assistant that can use Python "
"tools to solve mathematical problems. When you need "
"to perform calculations, use the code_interpreter "
"tool to execute code and get results."
)
messages_to_render.append({"role": "system", "content": system_content})
# Add user message if provided
if prompt:
messages_to_render.append({"role": "user", "content": prompt})
# Add assistant responses from previous turns if provided
if messages:
messages_to_render.extend(messages)
# Render template
formatted_text = template.render(messages=messages_to_render, tools=tools or [])
return formatted_text
def postprocess_predictions(prediction: str):
"""Extract action and content from prediction string"""
# Check for Answer: \boxed{...} format (only format we need for math_dapo)
# Use a more robust regex that handles nested braces
answer_pattern = r"Answer:\s*\\boxed\{((?:[^{}]|\{[^{}]*\})*)\}"
answer_match = re.search(answer_pattern, prediction, re.DOTALL)
if answer_match:
content = answer_match.group(1).strip()
return "answer", content
# Then check for <tool_call> tags (new format from Jinja2 template)
tool_call_pattern = r"<tool_call>\s*(\{.*?\})\s*</tool_call>"
tool_call_match = re.search(tool_call_pattern, prediction, re.DOTALL)
if tool_call_match:
try:
import json
# Clean up the JSON string by removing newlines and extra
# whitespace
json_str = tool_call_match.group(1)
# Replace newlines in string values with \n
json_str = json_str.replace("\n", "\\n")
tool_call_data = json.loads(json_str)
tool_name = tool_call_data.get("name")
arguments = tool_call_data.get("arguments", {})
if tool_name == "code_interpreter":
code = arguments.get("code", "")
if code.strip():
return "code", code
except (json.JSONDecodeError, KeyError, AttributeError):
pass
# Then check for <code> tags
code_pattern = r"<code>(.*?)</code>"
code_match = re.search(code_pattern, prediction, re.DOTALL)
if code_match:
content = code_match.group(1).strip()
return "code", content
# Finally check for ```python code blocks (lowest priority)
python_code_pattern = r"```python\s*(.*?)\s*```"
python_code_match = re.search(python_code_pattern, prediction, re.DOTALL)
if python_code_match:
content = python_code_match.group(1).strip()
return "code", content
return None, ""
def postprocess_responses(resp: str) -> str:
"""Post-process response to ensure tag completeness"""
# Handle <tool_call> tags (new format from Jinja2 template)
if "<tool_call>" in resp:
# Find the last occurrence of <tool_call>...</tool_call>
tool_call_pattern = r"<tool_call>\s*\{.*?\}\s*</tool_call>"
matches = list(re.finditer(tool_call_pattern, resp, re.DOTALL))
if matches:
last_match = matches[-1]
return resp[: last_match.end()]
# Handle <code> tags
if "</code>" in resp:
return resp.split("</code>")[0] + "</code>"
# Handle ```python code blocks
if "```python" in resp:
# Find the last occurrence of ```python...```
python_pattern = r"```python\s*.*?```"
matches = list(re.finditer(python_pattern, resp, re.DOTALL))
if matches:
last_match = matches[-1]
return resp[: last_match.end()]
# Handle Answer: \boxed{...} format (only format we need for math_dapo)
if "Answer:" in resp and "\\boxed{" in resp:
# Find the last occurrence of Answer: \boxed{...} with nested braces support
answer_pattern = r"Answer:\s*\\boxed\{((?:[^{}]|\{[^{}]*\})*)\}"
matches = list(re.finditer(answer_pattern, resp, re.DOTALL))
if matches:
last_match = matches[-1]
return resp[: last_match.end()]
return resp
async def execute_predictions(prediction: str) -> str:
"""Execute predictions and return results"""
action, content = postprocess_predictions(prediction)
if action == "code":
# Content is already the Python code (extracted by
# postprocess_predictions)
code = content.strip()
if code:
async with SEMAPHORE:
result = await tool_registry.execute_tool("code_interpreter", {"code": code})
next_obs = f"\n\n<interpreter>\n{result}\n</interpreter>\n\n"
done = False
else:
next_obs = "\n\n<interpreter>\nError: No Python code found" "\n</interpreter>\n\n"
done = False
elif action == "answer":
next_obs = ""
done = True
else:
next_obs = (
"\nMy previous action is invalid. "
"If I want to execute code, I should put the code between "
"<code> and </code>. "
"If I want to give the final answer, I should use the format "
"'Answer: \\boxed{answer}'. Let me try again.\n"
)
done = False
return next_obs, done
async def generate(args, sample: Sample, sampling_params) -> Sample:
"""Custom generation function supporting tool calls"""
assert not args.partial_rollout, "Partial rollout is not supported for " "this function at the moment."
state = GenerateState(args)
url = f"http://{args.sglang_router_ip}:{args.sglang_router_port}/generate"
# Set up the initial prompt with system prompt and tools (outside the loop)
tool_specs = tool_registry.get_tool_specs()
prompt = format_conversation_with_tools(prompt=sample.prompt, tools=tool_specs)
prompt_tokens_ids = state.tokenizer(prompt, add_special_tokens=False)["input_ids"]
response = ""
response_token_ids = []
loss_masks = []
tool_call_count = 0 # Track actual tool call rounds
for turn in range(TOOL_CONFIGS["max_turns"]):
# Check if total length exceeds max context length
total_length = len(prompt_tokens_ids) + len(response_token_ids)
if args.rollout_max_context_len is not None:
max_context_length = args.rollout_max_context_len
else:
max_context_length = args.context_parallel_size * args.max_tokens_per_gpu
if total_length >= max_context_length:
sample.status = Sample.Status.TRUNCATED
break
# Use token IDs instead of text
current_token_ids = prompt_tokens_ids + response_token_ids
payload = {
"input_ids": current_token_ids,
"sampling_params": sampling_params,
"return_logprob": True, # Request log probabilities for training
}
# Log payload to wandb for debugging
try:
import wandb
if wandb.run is not None:
# Count available tools (from tool_specs)
available_tools = len(tool_specs)
# Count tools used in the current response
tools_used = response.count("<interpreter>")
wandb.log(
{
"debug/payload_length": len(prompt + response),
"debug/available_tools": available_tools,
"debug/tools_used": tools_used,
"debug/turn": turn,
}
)
except ImportError:
pass # wandb not available
output = await post(url, payload)
# Handle abort
if output["meta_info"]["finish_reason"]["type"] == "abort":
sample.status = Sample.Status.ABORTED
return sample
if "output_token_logprobs" in output["meta_info"]:
cur_response_token_ids = [item[1] for item in output["meta_info"]["output_token_logprobs"]]
cur_response = state.tokenizer.decode(cur_response_token_ids)
cur_log_probs = [item[0] for item in output["meta_info"]["output_token_logprobs"]]
if sample.rollout_log_probs is None:
sample.rollout_log_probs = []
sample.rollout_log_probs += cur_log_probs
else:
cur_response = output["text"]
cur_response = postprocess_responses(cur_response)
cur_response_token_ids = state.tokenizer(cur_response, add_special_tokens=False)["input_ids"]
response += cur_response
response_token_ids += cur_response_token_ids
loss_masks += [1] * len(cur_response_token_ids)
# Check length limit
if output["meta_info"]["finish_reason"]["type"] == "length":
break
next_obs, done = await execute_predictions(cur_response)
if done:
break
# Count tool calls (when we get interpreter output, it means a tool
# was called)
if "<interpreter>" in next_obs:
tool_call_count += 1
assert next_obs != "", "Next observation should not be empty."
obs_tokens_ids = state.tokenizer(next_obs, add_special_tokens=False)["input_ids"]
response += next_obs
response_token_ids += obs_tokens_ids
loss_masks += [0] * len(obs_tokens_ids)
# Add dummy log probs for observation tokens (they won't be used due to loss_mask=0)
# Check if maximum tool call count reached
if sample.rollout_log_probs is not None:
sample.rollout_log_probs += [0.0] * len(obs_tokens_ids)
assert len(response_token_ids) == len(
sample.rollout_log_probs
), f"Token/logp length mismatch at turn {turn}: {len(response_token_ids)} tokens vs {len(sample.rollout_log_probs)} logps"
if tool_call_count >= TOOL_CONFIGS["max_tool_calls"]:
break
# Set sample attributes
sample.tokens = prompt_tokens_ids + response_token_ids
sample.response_length = len(response_token_ids)
sample.response = response
sample.loss_mask = loss_masks
# Store payload information for wandb logging
sample.payload_text = prompt + response
sample.payload_has_system = "<|im_start|>system" in prompt + response
sample.payload_has_tools = "# Tools" in prompt + response
# Store tool call count for reward calculation
sample.tool_call_count = tool_call_count
# Set status
match output["meta_info"]["finish_reason"]["type"]:
case "length":
sample.status = Sample.Status.TRUNCATED
case "abort":
sample.status = Sample.Status.ABORTED
case "stop":
sample.status = Sample.Status.COMPLETED
return sample
async def reward_func(args, sample, **kwargs):
"""Tool call reward function using math_dapo as primary reward model"""
if not isinstance(sample, Sample):
raise TypeError("Sample must be an instance of Sample class.")
# Build complete solution string
solution_str = sample.prompt + sample.response
# Get ground truth answer - label is a string, not a dict
ground_truth = sample.label if sample.label is not None else ""
# Get tool call count as num_turns
num_turns = getattr(sample, "tool_call_count", 0)
# use \\boxed{...} answer
result = math_dapo_compute_score(solution_str, ground_truth, strict_box_verify=True)
# encourage model to call tools
if result["score"] < 0:
tool_call_reward = (num_turns - 2) / 2 * 0.1
result["score"] = min(-0.6, result["score"] + tool_call_reward)
if result["pred"] is None:
result["pred"] = ""
return result