-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathllm_gen_task.py
More file actions
583 lines (498 loc) · 23.9 KB
/
llm_gen_task.py
File metadata and controls
583 lines (498 loc) · 23.9 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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
import json
import random
import re
from model.init_model import init_language_model
import argparse
from llm_gen_prompt import *
from pipeline.utils import format_string
from datetime import datetime
parser = argparse.ArgumentParser()
parser.add_argument("--api_model", type=str, default="qwen3-235b-a22b", help="api model")
parser.add_argument('--host', type=str, default="127.0.0.1", help='the host of the server')
parser.add_argument("--port", type=int, default=25565, help="the port of the server")
parser.add_argument('--agent_num', type=int, default=2, help='how many agents in the task')
parser.add_argument('--task_num', type=int, default=1, help='number of task to generate')
parser.add_argument('--position', type=str, default="inventory", help='the position of tool')
args = parser.parse_args()
with open("data/gen_example.json", "r") as f:
example = json.load(f)
all_objs = example["all_objs"]
all_objs_inventory = example["all_objs_inventory"]
action_list = example["action_list"]
concreting_examples = example["concreting_examples"]
api_key_list = json.load(open("API_KEY_LIST", "r"))["AGENT_KEY"]
llm_config = {
"api_key": api_key_list[0],
"api_base": "https://dashscope.aliyuncs.com/compatible-mode/v1",
# "api_base": "https://api.deepseek.com",
# "api_model": "deepseek-reasoner",
# "api_model": "qwen-max",
"api_model": "qwen3-235b-a22b",
"api_key_list": api_key_list
}
llm = init_language_model(llm_config)
def filter_emoji(text: str) -> str:
ret_str = []
for c in text:
try:
c.encode('gbk')
ret_str.append(c)
except UnicodeEncodeError:
continue
return ''.join(ret_str)
def remove_even_hashed_segments(text):
"""
删除偶数个#之间的内容(包括#自身)
示例:
>>> remove_even_hashed_segments("abc#123#def##456#ghi")
'abcdefghi'
>>> remove_even_hashed_segments("a#b#c#d#e")
'ace'
"""
# 使用正则表达式匹配两个#之间的内容
pattern = r'#.*?#'
while True:
new_text = re.sub(pattern, '', text, count=1)
if new_text == text: # 如果没有替换发生,退出循环
break
text = new_text
return text
def createVABreadthPrompt(instruction):
# randomly select 3 actions from action_list
selected_actions = random.sample(action_list, 3)
# create the action string
action_string = ""
for action in selected_actions:
action_string += "- " + action + "\r\n"
prompt = format_string(VA_base_instruction, {"agent_num": args.agent_num})
prompt += instruction_actions + action_string
prompt += "#Given Task#: \r\n {}".format(instruction)
# prompt += "No blueprints in your generated task\r\n"
prompt += "#Created Task#:\r\n"
# print("-" * 40 + "gen_task" + "-" * 40)
# print(prompt)
# print("-" * 100)
return prompt
def createVAVolumePrompt(input):
prompt = VA_Volume_base_instruction
for example in input['examples']:
simple = example['simple']
augmentation = example['augmentation']
prompt += "#Given Simple Task#: \r\n {} \r\n".format(simple)
prompt += "#Given Augmented Task#: \r\n {} \r\n".format(augmentation)
# prompt += "#Given Simple Task#: \r\n {}".format(
# instruction)
# prompt += "#Given Augmented Task#: \r\n{}".format(
# instruction)
prompt += "#Simple Task Input#: \r\n{}\r\n".format(input['input'])
# prompt += "No blueprints in your generated task\r\n"
prompt += "#Augmented Task Output#: \r\n"
return prompt
def createBlueprintPrompt(background):
prompt = blueprint_base_instruction
example = example_string
# prompt = large_scale_instruction
prompt += "#Example#: \r\n {}".format(example)
prompt += "#Task#: \r\n {}".format(background) + "\n"
# prompt += "#To-Build#: \r\n {}".format(
# to_build)
# prompt += "No blueprints in your generated task\r\n"
prompt += "Do not include anything other than a JSON object in your output. You may insert comments if needed.\r\n"
prompt += "#Designed Building#:\r\n"
# print("-" * 40 + "blueprint" + "-" * 40)
# print(prompt)
# print("-" * 100)
return prompt
def extract_outermost_braces_content(text):
"""
提取第一个最外层的完整 {...} 内容,确保括号正确匹配
返回提取到的内容字符串,或 None(如果未找到)
"""
start_index = text.find('{')
if start_index == -1:
return None
brace_depth = 1
current_index = start_index + 1
while current_index < len(text) and brace_depth > 0:
char = text[current_index]
if char == '{':
brace_depth += 1
elif char == '}':
brace_depth -= 1
current_index += 1
if brace_depth == 0:
return text[start_index:current_index]
else:
return None # 括号不匹配
def remove_json_comments(json_str):
"""移除JSON字符串中的注释(//开头或行内)"""
lines = []
for line in json_str.split('\n'):
# 移除整行注释
stripped = line.strip()
if stripped.startswith('//'):
continue
# 移除行内注释
line = re.sub(r'//.*', '', line)
lines.append(line)
return '\n'.join(lines)
def str2dict(raw_str, filename):
# 1. 提取最外层 {...} 内容
json_block = extract_outermost_braces_content(raw_str)
if not json_block:
print("错误:未找到完整的花括号内容块")
return False
# 2. 移除注释
clean_json_str = remove_json_comments(json_block)
# 3. 解析为字典
try:
data_dict = json.loads(clean_json_str)
return data_dict
except json.JSONDecodeError as e:
print(f"JSON解析错误: {e}")
print("有问题的JSON内容:")
print(clean_json_str)
return False
def is_contained(outer, inner):
"""判断outer立方体是否完全包含inner立方体"""
# outer的边界
ox1, oy1, oz1 = outer[0][0], outer[0][1], outer[0][2]
ox2, oy2, oz2 = outer[1][0], outer[1][1], outer[1][2]
outer_min = [min(ox1, ox2), min(oy1, oy2), min(oz1, oz2)]
outer_max = [max(ox1, ox2), max(oy1, oy2), max(oz1, oz2)]
# inner的边界
ix1, iy1, iz1 = inner[0][0], inner[0][1], inner[0][2]
ix2, iy2, iz2 = inner[1][0], inner[1][1], inner[1][2]
inner_min = [min(ix1, ix2), min(iy1, iy2), min(iz1, iz2)]
inner_max = [max(ix1, ix2), max(iy1, iy2), max(iz1, iz2)]
# 检查包含关系
return all(outer_min[d] <= inner_min[d] and outer_max[d] >= inner_max[d] for d in range(3))
def rearrange_blocks(blocks):
"""重新排列立方体,确保被包含的立方体出现在包含者之后"""
def location_filter(block):
if block["type"] == "single" or block["type"] == "tree":
return [block["position"], block["position"]]
else:
return [block["from"], block["to"]]
n = len(blocks)
# 记录每个立方体被哪些立方体包含
contained_by = [[] for _ in range(n)]
# 构建包含关系图
for i in range(n):
for j in range(n):
if i != j and is_contained(location_filter(blocks[j]), location_filter(blocks[i])):
contained_by[i].append(j) # 立方体i被立方体j包含
# 拓扑排序:被包含的立方体要排在包含者之后
visited = [False] * n
result = []
def visit(cube_idx):
if not visited[cube_idx]:
visited[cube_idx] = True
# 先处理所有包含当前立方体的立方体
for container in contained_by[cube_idx]:
visit(container)
result.append(blocks[cube_idx])
# 按原始顺序访问,保持其他立方体的相对顺序
for i in range(n):
visit(i)
return result
def clean_env_dict(data:dict, task_description):
# 规范blueprint里block和entity的生成
# return block, entity
blocks = data.get("blocks", [])
entities = data.get("entities", [])
with open("data/items.json", "r") as f:
item_list = json.load(f)
item_name_list = [item["name"] for item in item_list]
item_name_list.append("redstone_wire")
cleaned_block = []
cleaned_entity = []
tree_list = ["acacia", "birch", "spruce", "oak", "jungle", "dark_oak", "mangrove"]
tree_weight = [5, 30, 5, 50, 4, 3, 3]
color_list = ["black", "blue", "brown", "cyan", "gray", "green", "light_blue", "light_gray", "lime", "magenta", "orange", "pink", "purple", "red", "white", "yellow"]
color = random.choice(color_list)
wooden_material = random.choices(tree_list, tree_weight)[0]
for block in blocks:
if block["type"] == "tree":
if block["name"] not in tree_list:
block["name"] = random.choices(tree_list, tree_weight)[0]
continue
if any(item in block["name"] for item in ["door", "bed", "sign"]):
block["type"] = "single"
if "planks" in block["name"] and all(block["name"] != f"{material}_planks" for material in tree_list):
block["name"] = wooden_material + "_planks"
if "wool" in block["name"] and all(block["name"] != f"{temp_color}_wool" for temp_color in color_list):
block["name"] = color + "_wool"
if "fence_gate" in block["name"] and all(block["name"] != f"{material}_fence_gate" for material in tree_list):
block["name"] = wooden_material + "_fence_gate"
if "fence_gate" not in block["name"] and "fence" in block["name"] and all(block["name"] != f"{material}_fence" for material in tree_list):
block["name"] = wooden_material + "_fence"
if "door" in block["name"]:
if all(block["name"] != f"{material}_door" for material in tree_list):
block["name"] = wooden_material + "_door"
block["half"] = "lower"
if "facing" not in block:
block["facing"] = random.choice(["east", "west", "north", "south"])
upper_block = {
"type": "single",
"position": [block["position"][0], block["position"][1] + 1, block["position"][2]],
"name": block["name"],
"facing": block["facing"],
"half": "upper"
}
cleaned_block.append(upper_block)
if "bed" in block["name"]:
if all(block["name"] != f"{temp_color}_bed" for temp_color in color_list):
block["name"] = color + "_bed"
block["part"] = "head"
if "facing" not in block:
block["facing"] = random.choice(["east", "west", "north", "south"])
bed_offset = {
"west": [1, 0, 0],
"east": [-1, 0, 0],
"north": [0, 0, 1],
"south": [0, 0, -1]
}
foot_block = {
"type": "single",
"position": [base + offset for base, offset in zip(block["position"], bed_offset[block["facing"]])],
"name": block["name"],
"facing": block["facing"],
"part": "foot"
}
cleaned_block.append(foot_block)
if "sign" in block["name"]:
if all(block["name"] != f"{material}_sign" for material in tree_list):
block["name"] = wooden_material + "_sign"
if "rotation" not in block:
block["rotation"] = random.randint(0, 9)
if "facing" in block:
block.pop("facing")
if block["name"] == "chest":
if "items" not in block:
block["items"] = []
for item in block["items"]:
if "planks" in item["name"] and all(item["name"] != f"{material}_planks" for material in tree_list):
item["name"] = wooden_material + "_planks"
if "door" in item["name"] and all(item["name"] != f"{material}_door" for material in tree_list):
item["name"] = wooden_material + "_door"
if "wool" in item["name"] and all(item["name"] != f"{temp_color}_wool" for temp_color in color_list):
item["name"] = color + "_wool"
if "bed" in item["name"] and all(item["name"] != f"{temp_color}_bed" for temp_color in color_list):
item["name"] = color + "_bed"
if "fence_gate" in item["name"] and all(item["name"] != f"{material}_fence_gate" for material in tree_list):
item["name"] = wooden_material + "_fence_gate"
if "fence_gate" not in block["name"] and "fence" in block["name"] and all(block["name"] != f"{material}_fence" for material in tree_list):
item["name"] = wooden_material + "_fence"
if item["name"] not in item_name_list:
item["name"] = llm.few_shot_generate_thoughts(system_prompt=
format_string(CORRECTOR_PROMPT, {
"Task": task_description,
"Item": item["name"],
"Item_list": item_name_list
# "Item_para": item
}))
if item["name"] not in item_name_list:
print(f"INVALID LLM CORRECTOR IN CHEST:\n {item['name']}")
else:
for item_info in item_list:
if item["name"] == item_info["name"] and item["count"] > item_info["stackSize"]: # 堆叠数量超限
if item["count"] < 10:
for i in range((item["count"]-1) // item_info["stackSize"]):
block["items"].append({
"name": item["name"],
"count": item_info["stackSize"]
})
item["count"] = item_info["stackSize"]
if any(item["name"] in set(["carrot", "potato"]) or "_seeds" in item["name"] for item in block["items"]):
if all("_hoe" not in item["name"] for item in block["items"]):
block["items"].append({
"name": "iron_hoe",
"count": 1
})
if block["name"] not in item_name_list and block["name"] != "water" and block["name"] != "lava":
block["name"] = llm.few_shot_generate_thoughts(system_prompt=
format_string(CORRECTOR_PROMPT, {
"Task": task_description,
"Item": block["name"],
"Item_list": item_name_list
# "Item_para": item
}))
if block["name"] not in item_name_list and block["name"] != "water" and block["name"] != "lava":
print(f"INVALID LLM CORRECTOR:\n {block['name']}")
cleaned_block.append(block)
if args.position == "chest":
return {
"blocks": rearrange_blocks(cleaned_block),
"entities": cleaned_entity,
}
elif args.position == "inventory":
no_chest_block = []
items_dict = {}
cleaned_item = []
for block in cleaned_block:
if block["name"] != "chest" or block["name"] == "chest" and len(block.get("items",[])) == 0:
no_chest_block.append(block)
continue
for item in block["items"]:
if item["name"] not in items_dict:
items_dict[item["name"]] = item["count"]
else:
items_dict[item["name"]] += item["count"]
for item in items_dict.keys():
cleaned_item.append({
"name": item,
"count": items_dict[item]
})
return {
"blocks": rearrange_blocks(no_chest_block),
"entities": cleaned_entity,
"items": cleaned_item
}
else:
print("INVALID POSITION!")
raise RuntimeError
def rewriting(orig_text) -> str:
ret = llm.few_shot_generate_thoughts(system_prompt="You are a rewriter.", example_prompt=format_string(task_goal_prompt,{"orig_sen": orig_text}))
# print(ret)
return ret
def gen_task(times = 1):
evol_objs = []
for i in range(times):
evol_prompts = []
# choose 2 samples from the data
samples = random.sample(all_objs, 2)
format_str = "Example 1: {}\n Example 2: {}\n"
cur_obj = format_str.format(samples[0], samples[1])
# cur_obj = samples[0]
instruction = cur_obj + "\n"
evol_prompts.append(createVABreadthPrompt(instruction))
# print("evol_prompts:", evol_prompts)
selected_evol_prompt = random.choice(evol_prompts)
evol_instruction = llm.few_shot_generate_thoughts(system_prompt="You are a helpful assistant", example_prompt=selected_evol_prompt)
evol_objs.append({"instruction": remove_even_hashed_segments(filter_emoji(evol_instruction))})
return evol_objs
def concreting_task(task_list):
evol_objs = []
prompt = {"examples": concreting_examples, "input": ""}
for cur_obj in task_list:
instruction = cur_obj['instruction'].strip()
prompt["input"] = instruction
evol_prompts = createVAVolumePrompt(prompt)
selected_evol_prompt = evol_prompts
evol_instruction = llm.few_shot_generate_thoughts(system_prompt="You are a helpful assistant", example_prompt=selected_evol_prompt)
evol_objs.append({"instruction": evol_instruction})
return evol_objs
def create_blueprint(concreted_task, times = 1):
blueprint_prompt = []
for i in range(times):
# instruction = cur_obj["instruction"]
instruction = concreted_task
blueprint_prompt.append(createBlueprintPrompt(instruction))
selected_evol_prompt = blueprint_prompt[-1]
evol_instruction = llm.few_shot_generate_thoughts(system_prompt="You are a helpful assistant", example_prompt=selected_evol_prompt)
return str2dict(evol_instruction, 'blueprints.json')
# ret = str2dict(evol_instruction, 'blueprints.json')
# if not ret:
# print(evol_instruction)
# return {}
# else:
# return ret
def create_config(task_description, blueprint, task_id, api_model = "qwen_max", host = "127.0.0.1", port = 25565, agent_num = 2):
config = {
"api_model": api_model,
"api_base": "https://dashscope.aliyuncs.com/compatible-mode/v1",
"task_type": "gen",
"task_idx": task_id,
"agent_num": agent_num,
"dig_needed": False,
"task_goal": task_description,
"blueprint": blueprint,
"host": host,
"port": port,
"task_name": f"gen_{args.position}_{task_id}_{agent_num}p"
}
return config
def apply_coordinate_offset(input_str, offset = [41, -60, 122]):
"""
给字符串中的所有坐标数组添加偏移量
参数:
input_str (str): 包含坐标数组的字符串,如 "点A[2, -5, 10]和点B[3, 4, -1]"
offset (list): 偏移量[x, y, z],如 [1, 2, -3]
返回:
str: 替换后的字符串
"""
# 确保偏移量是3D的
if len(offset) != 3:
raise ValueError("偏移量必须是[x, y, z]格式的三元素列表")
# 匹配所有形如[数字, 数字, 数字]的模式
pattern = r'\[(-?\d+),\s*(-?\d+),\s*(-?\d+)\]'
def replace_match(match):
# 提取匹配到的三个数字
x = int(match.group(1))
y = int(match.group(2))
z = int(match.group(3))
# 应用偏移量
new_x = x + offset[0]
new_y = y + offset[1]
new_z = z + offset[2]
# 返回新格式的字符串
return f"[{new_x}, {new_y}, {new_z}]"
# 使用正则表达式替换所有匹配项
if args.position == "inventory":
input_str = llm.few_shot_generate_thoughts(system_prompt=INVENTORY_SYSTEM_PROMPT, example_prompt=format_string(INVENTORY_USER_PROMPT, {
"given_task": input_str,
}))
input_str += '\n' + rewriting("- All items or tools agents may use can be found in their inventory.When searching for them, agents should first check their inventory.")
elif args.position == "chest":
input_str += '\n' + rewriting("- All items or tools agents may use can be found in the chest.When searching for them, agents should first check all chests in the environment.")
else:
print("INVALID POSITION!")
raise RuntimeError
result = re.sub(pattern, replace_match, input_str)
return remove_even_hashed_segments(filter_emoji(result))
def extract_env(text):
"""
提取"**Environment:**"和"**Task**"之间的内容
参数:
text (str): 包含标记的字符串
返回:
str: 两个标记之间的内容(不包含标记本身)
"""
# 使用正则表达式匹配两个标记之间的内容
pattern = r'\*\*Environment:\*\*(.*?)\*\*Task:\*\*'
match = re.search(pattern, text, re.DOTALL)
if match:
# 返回匹配到的内容,并去除首尾空白
return match.group(1).strip()
else:
return None
# temp_task = '''
# Work together to rescue a horse trapped in an enclosure at (-1, 0, 4) by activating the lever at (-2, 0, 5). Guide it to the stable at (3, 0, 10) by alternating feeding hay at (2, 0, 9) and opening the
# gate at (4, 0, 11) using the pressure plate at (5, 0, 12). Coordinate timing to ensure the horse moves safely through the path.
# '''
if __name__ == "__main__":
config_list = []
for t in range(args.task_num):
print(f"{t+1}/{args.task_num} task:")
task = gen_task(1)
# task = [{"instruction": temp_task}]
task_description = task[0]["instruction"]
print(task_description)
# print("\n\n")
# task_description = apply_coordinate_offset(task_description, [-41, 60, -122])
# concreted_task = concreting_task(task)
# print(extract_env(concreted_task[0]["instruction"]))
# blueprint = create_blueprint(concreted_task, 1)
blueprint = create_blueprint(task_description, 1)
# with open("tmp/dict_format.json", "w") as f:
# json.dump(blueprint, f, indent = 4)
# print("-"*100)
# print(blueprint)
config_list.append(create_config(apply_coordinate_offset(task_description), clean_env_dict(blueprint, task_description), t+1, args.api_model, args.host, args.port, args.agent_num))
for config in config_list:
current_mdh = datetime.now()
config["task_name"] += current_mdh.strftime("_%m%d")
with open(f"{args.api_model}_gen_{args.agent_num}p_config.json", "w", encoding='utf-8') as f:
json.dump(config_list, f, indent=4)
print("gen task saved!")