Views
No views yet
1python -m sglang.launch_server \
2 --model-path rstar2-reproduce/rstar2-agent \
3 --port 30000 \
4 --tensor-parallel-size 4 \
5 --tool-call-parser qwen25--model-path: Path to the rStar2-Agent model--port: Server port (default: 30000)--tensor-parallel-size: Number of GPUs for parallel processing--tool-call-parser: Parser for tool calls (use "qwen25" for this model)1from openai import OpenAI
2import json
3
4# Initialize OpenAI client pointing to SGLang server
5client = OpenAI(
6 base_url="http://localhost:30000/v1", # SGLang server URL
7 api_key="EMPTY" # No API key required for local server
8)
9
10# Define Python code execution tool for the model
11tools = [
12 {
13 "type": "function",
14 "function": {
15 "name": "execute_python_code_with_standard_io",
16 "description": "Execute Python code with standard input and capture standard output.
17This function takes a Python code string and an input string, provides the input string
18through standard input (stdin) to the code, and captures and returns any output produced
19through standard output (stdout). If the executed code raises an exception, the error
20message will be captured and returned instead.",
21 "parameters": {
22 "type": "object",
23 "properties": {
24 "code": {
25 "type": "string",
26 "description": "A string containing Python code to be executed. The code can read from standard input using the input() function."
27 },
28 "input": {
29 "type": "string",
30 "description": "A string that will be provided as standard input to the code when it calls input()."
31 }
32 },
33 "required": ["code", "input"]
34 }
35 }
36 }
37]
38
39# Define Python code execution function
40def execute_python_code_with_standard_io(code, input_data):
41 """
42 Execute Python code with standard input and capture output.
43
44 Args:
45 code (str): Python code to execute
46 input_data (str): Input data to provide to the code
47
48 Returns:
49 str: Output from the executed code or error message
50 """
51 import subprocess
52 import sys
53
54 try:
55 # Create subprocess to execute Python code
56 process = subprocess.Popen(
57 [sys.executable, "-c", code],
58 stdin=subprocess.PIPE,
59 stdout=subprocess.PIPE,
60 stderr=subprocess.PIPE,
61 text=True
62 )
63
64 # Send input and get output
65 stdout, stderr = process.communicate(input=input_data)
66
67 if stderr:
68 return f"Error: {stderr}"
69 return stdout.strip()
70
71 except Exception as e:
72 return f"Execution error: {str(e)}"
73
74# Example: Create a math problem conversation
75messages = [
76 {
77 "role": "user",
78 "content": "You must put your answer inside <answer> </answer> tags, i.e., <answer> answer here </answer>. And your final answer will be extracted automatically by the \\boxed{} tag. Solve this math problem: Find the sum of all prime numbers less than 20."
79 }
80]
81
82# Main conversation loop - handle tool calls until completion
83turn_idx = 0
84while True:
85 print(f'========== Turn: {turn_idx} ==========')
86 turn_idx += 1
87
88 # Get model response with tool support
89 response = client.chat.completions.create(
90 model="rstar2-reproduce/rstar2-agent",
91 messages=messages,
92 tools=tools,
93 tool_choice="auto", # Let model decide when to use tools
94 temperature=0.6 # Adjust for creativity vs consistency
95 )
96
97 # Add the assistant's response to conversation history
98 messages.append(response.choices[0].message)
99
100 print(f'{response.choices[0].message.content}')
101
102 # Check if model wants to use tools
103 if response.choices[0].message.tool_calls:
104 # Process each tool call
105 for tool_call in response.choices[0].message.tool_calls:
106 function_args = json.loads(tool_call.function.arguments)
107
108 print(f">>> Executing Code:
109{function_args['code']}")
110 input_text = function_args.get('input', '')
111 print(f">>> With Input: {input_text if input_text else '(no input)'}")
112
113 # Execute the Python code
114 result = execute_python_code_with_standard_io(
115 function_args["code"],
116 function_args.get("input", "")
117 )
118
119 print(f">>> Tool result: {result}")
120
121 # Add tool response to conversation
122 messages.append({
123 "role": "tool",
124 "tool_call_id": tool_call.id,
125 "content": result
126 })
127 else:
128 # No more tool calls, conversation finished
129 print("✅ No more tool calls. Conversation finished.")
130 break1@misc{shang2025rstar2agentagenticreasoningtechnical,
2 title={rStar2-Agent: Agentic Reasoning Technical Report},
3 author={Ning Shang and Yifei Liu and Yi Zhu and Li Lyna Zhang and Weijiang Xu and Xinyu Guan and Buze Zhang and Bingcheng Dong and Xudong Zhou and Bowen Zhang and Ying Xin and Ziming Miao and Scarlett Li and Fan Yang and Mao Yang},
4 year={2025},
5 eprint={2508.20722},
6 archivePrefix={arXiv},
7 primaryClass={cs.CL},
8 url={https://arxiv.org/abs/2508.20722},
9}