1
+ from datasets import load_dataset
2
+ import re
3
+ import os
4
+ import pandas as pd
5
+ from collections import defaultdict
6
+
7
+ # Define environments to extract
8
+ ENVIRONMENTS = [
9
+ "alworld" , "babyai" , "lmrlgym_maze" , "lmrlgum_wordle" ,
10
+ "sciworld" , "sqlgym" , "textcraft" , "movie" ,
11
+ "todo" , "weather" , "webshop"
12
+ ]
13
+
14
+ def make_prefix (question , environment ):
15
+ """
16
+ Create instruction prefix for the OpenManus agent.
17
+
18
+ Args:
19
+ question: The question or task to be solved
20
+ environment: The environment type
21
+
22
+ Returns:
23
+ Formatted prefix with OpenManus template
24
+ """
25
+ prefix = f"""You are an OpenManus agent tasked to solve the following problem.
26
+ You must conduct reasoning inside <think> and </think> tags first every time you get new information.
27
+ After reasoning, you can perform actions using <act> action_description </act> tags.
28
+ When you have a final answer, provide it inside <answer> and </answer> tags, without detailed illustrations.
29
+
30
+ Task: { question } \n """
31
+ return prefix
32
+
33
+ def extract_solution (solution_str ):
34
+ """
35
+ Extract numerical solution from a string.
36
+
37
+ Args:
38
+ solution_str: String containing the solution
39
+
40
+ Returns:
41
+ Extracted numerical value
42
+ """
43
+ solution = re .search ("#### (\\ -?[0-9\\ .\\ ,]+)" , solution_str )
44
+ assert solution is not None
45
+ final_solution = solution .group (0 )
46
+ final_solution = final_solution .split ('#### ' )[1 ].replace (',' , '' )
47
+ return final_solution
48
+
49
+ def process_group_data (group_name , group_samples ):
50
+ """
51
+ Process samples for a specific environment group.
52
+
53
+ Args:
54
+ group_name: Name of the environment group
55
+ group_samples: List of samples belonging to this group
56
+
57
+ Returns:
58
+ List of processed data samples
59
+ """
60
+ processed_data = []
61
+
62
+ for idx , sample in enumerate (group_samples ):
63
+ item_id = sample ['item_id' ]
64
+ conversations = sample ['conversations' ]
65
+
66
+ # Process each conversation
67
+ dialog_data = []
68
+ for conversation in conversations :
69
+ dialog_data .append ({
70
+ "from" : conversation ['from' ],
71
+ "value" : conversation ['value' ],
72
+ "loss" : conversation ['loss' ]
73
+ })
74
+
75
+ # Extract question/task from the first user message
76
+ user_messages = [conv ['value' ] for conv in conversations if conv ['from' ] == 'human' ]
77
+ question = user_messages [0 ] if user_messages else "No question found"
78
+
79
+ # Create formatted prompt
80
+ formatted_question = make_prefix (question , group_name )
81
+
82
+ # Build final data structure
83
+ data = {
84
+ "data_source" : group_name , # Use environment name as data source
85
+ "item_id" : item_id ,
86
+ "conversations" : dialog_data ,
87
+ "prompt" : [{
88
+ "role" : "user" ,
89
+ "content" : formatted_question ,
90
+ }],
91
+ "ability" : "agent-reasoning" ,
92
+ "reward_model" : {
93
+ "style" : "rule" ,
94
+ "ground_truth" : {
95
+ "environment" : group_name ,
96
+ "task_id" : item_id
97
+ }
98
+ },
99
+ "extra_info" : {
100
+ 'split' : group_name ,
101
+ 'index' : idx ,
102
+ }
103
+ }
104
+ processed_data .append (data )
105
+
106
+ return processed_data
107
+
108
+ def group_samples_by_environment (data , environments ):
109
+ """
110
+ Group data samples by their environment based on item_id.
111
+
112
+ Args:
113
+ data: Dataset samples
114
+ environments: List of environment names to look for
115
+
116
+ Returns:
117
+ Dictionary with environment names as keys and sample lists as values
118
+ """
119
+ env_groups = defaultdict (list )
120
+ prefix_pattern = re .compile (r'^([^\d]+)' ) # Regex to extract prefix before numbers
121
+
122
+ for sample in data :
123
+ item_id = sample ['item_id' ]
124
+
125
+ # Extract item_id prefix until digits start
126
+ match = prefix_pattern .match (item_id )
127
+ if match :
128
+ item_id_prefix = match .group (1 )
129
+ else :
130
+ item_id_prefix = item_id
131
+
132
+ # Check if item_id contains any of the specified environments
133
+ for env in environments :
134
+ if env in item_id :
135
+ env_groups [env ].append (sample )
136
+ break # If matched to one environment, don't check others
137
+
138
+ return env_groups
139
+
140
+ def save_environment_data (env_groups , output_dir , txt_output_dir ):
141
+ """
142
+ Save grouped data to parquet and txt files.
143
+
144
+ Args:
145
+ env_groups: Dictionary of environment groups
146
+ output_dir: Directory to save parquet files
147
+ txt_output_dir: Directory to save txt files
148
+ """
149
+ # Create output directories
150
+ os .makedirs (output_dir , exist_ok = True )
151
+ os .makedirs (txt_output_dir , exist_ok = True )
152
+
153
+ # Save each environment group as parquet and txt
154
+ for env , samples in env_groups .items ():
155
+ print (f"Processing group: { env } " )
156
+
157
+ # Process samples for this environment
158
+ processed_samples = process_group_data (env , samples )
159
+
160
+ # Convert processed data to DataFrame
161
+ df = pd .DataFrame (processed_samples )
162
+
163
+ # Generate file paths
164
+ parquet_file_path = os .path .join (output_dir , f"{ env } .parquet" )
165
+ txt_file_path = os .path .join (txt_output_dir , f"{ env } .txt" )
166
+
167
+ # Save as Parquet file
168
+ df .to_parquet (parquet_file_path )
169
+ print (f"Saved data for environment '{ env } ' to { parquet_file_path } " )
170
+
171
+ # Save as TXT file
172
+ with open (txt_file_path , 'w' , encoding = 'utf-8' ) as txt_file :
173
+ for sample in processed_samples :
174
+ txt_file .write (str (sample ) + '\n ' )
175
+ print (f"Saved data for environment '{ env } ' to { txt_file_path } " )
176
+
177
+ def main ():
178
+ """
179
+ Main function to process and save AgentGym dataset by environment.
180
+ """
181
+ # Load the dataset
182
+ print ("Loading dataset..." )
183
+ dataset = load_dataset ("AgentGym/AgentTraj-L" )
184
+ train_data = dataset ['train' ]
185
+
186
+ # Group samples by environment
187
+ print ("Grouping samples by environment..." )
188
+ env_groups = group_samples_by_environment (train_data , ENVIRONMENTS )
189
+
190
+ # Print group statistics
191
+ for env , samples in env_groups .items ():
192
+ print (f"Environment: { env } , Number of samples: { len (samples )} " )
193
+
194
+ # Save grouped data
195
+ print ("Saving environment data..." )
196
+ save_environment_data (
197
+ env_groups ,
198
+ output_dir = 'output_env_groups' ,
199
+ txt_output_dir = 'output_txt_files'
200
+ )
201
+
202
+ print ("Processing complete!" )
203
+
204
+ if __name__ == "__main__" :
205
+ main ()
0 commit comments