-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate_instruction.py
1686 lines (1569 loc) · 74.5 KB
/
generate_instruction.py
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
generate domain-specific instruction-tuning data
"""
import copy
import shutil
import time
import json
import os
import random
import re
import string
import logging
from functools import partial
from multiprocessing import Pool
from typing import List, Tuple, Dict, Union, Optional, Any
import argparse
import numpy as np
import tqdm
from rouge_score import rouge_scorer
# import utils
import openai
import fire
from transformers import GPT2TokenizerFast, AutoTokenizer, BertTokenizer
from anytree import AnyNode, Node, PreOrderIter, LevelOrderIter
from anytree.importer import DictImporter, JsonImporter
from anytree.exporter import JsonExporter, DictExporter
import asyncio
level = logging.DEBUG
# level = logger.INFO
logger = logging.getLogger(__name__)
logger.setLevel(level)
c_handler = logging.StreamHandler()
c_handler.setLevel(level)
c_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
c_handler.setFormatter(c_format)
# Add handlers to the logger
logger.addHandler(c_handler)
meta_info_filename = "tree_meta_info.json"
openai.api_key = "YOUR OPENAI API KEY"
claude_key = "YOUR CLAUDE API KEY"
class DomainTree(object):
"""docstring for DomainTree"""
def __init__(
self,
root: AnyNode,
unique_notd_id: str = "task_name",
name_to_node: Dict[str, AnyNode] = None,
**kwargs
):
self.root = root
self.unique_id = unique_notd_id
self.name_to_node = name_to_node if name_to_node is not None \
else {getattr(node, self.unique_id): node for node in PreOrderIter(self.root)}
# TODO: load from config file instead of hard-coded
self.prepare_mine_hparams(**kwargs)
self.prepare_prompt()
self.prepare_tools()
def extend_node_children(
self,
node_to_extend: Union[AnyNode, str],
extend_num: int = None,
extend_batch_size: int = None,
) -> List[AnyNode]:
all_new_nodes = list()
while len(all_new_nodes) < extend_num:
gap = extend_num - len(all_new_nodes)
logger.info(f"Already extended num: {len(all_new_nodes)}. This time gap: {gap}")
new_nodes: List[AnyNode] = self._extend_node_children(
node_to_extend,
extend_num=max(gap, self.min_extend_num),
extend_batch_size=extend_batch_size,
)
all_new_nodes += new_nodes
return all_new_nodes
def _extend_node_children(
self,
node_to_extend: Union[AnyNode, str],
extend_num: int = None,
extend_batch_size: int = None,
) -> List[AnyNode]:
"""
Extending the given node's children
"""
extend_num = self.default_extend_num if extend_num is None else extend_num
extend_batch_size = self.default_extend_batch_size if extend_batch_size is None else extend_batch_size
# locate the node
if type(node_to_extend) is str:
node_to_extend: AnyNode = self.name_to_node[node_to_extend]
node_name = getattr(node_to_extend, self.unique_id)
current_scenario = f"extend_node_children for {node_name}"
logger.info(current_scenario)
# check its existing children
existing_children = list(node_to_extend.children)
existing_children_names = [getattr(child, self.unique_id) for child in node_to_extend.children]
if len(existing_children) >= self.max_child_num:
logger.warning(f"Failed trial to extend node {node_name}: already having {len(existing_children)} children")
return []
if len(existing_children) + extend_num > self.max_child_num:
logger.warning(f"Exceeding max_child_num if extending by {extend_num}, using remaining num instead.")
extend_num = self.max_child_num - len(existing_children)
# formulate the prompt and api request
base_prompt = self.extend_node_prompt
existing_siblings = list(node_to_extend.siblings)
if extend_batch_size > 1:
prompt = []
for _ in range(extend_batch_size):
demonstrate_examples = self.get_demonstrate_examples(node_to_extend)
prompt_tmp = self.encode_prompt(
base_prompt=base_prompt,
demonstrate_examples=demonstrate_examples, # Example triplet
target_task=node_name, # name of node_to_extend
existing_children=existing_children, # list of children of node_to_extend
existing_siblings=existing_siblings, # list of siblings of node_to_extend
num_examples_per_time=self.num_example_extend, # num of triplet for each new mined subtask
extend_num=extend_num,
# new_subtask=new_subtask, # only used when enriching nodes
# new_subtask_reason=new_subtask_reason, # only used when enriching nodes
target_children_num=self.max_child_num,
)
prompt.append(prompt_tmp)
else:
demonstrate_examples = self.get_demonstrate_examples(node_to_extend)
prompt = self.encode_prompt(
base_prompt=base_prompt,
demonstrate_examples=demonstrate_examples, # Example triplet
target_task=node_name, # name of node_to_extend
existing_children=existing_children, # list of children of node_to_extend
existing_siblings=existing_siblings, # list of siblings of node_to_extend
num_examples_per_time=self.num_example_extend, # num of triplet for each new mined subtask
extend_num=extend_num,
# new_subtask=new_subtask, # only used when enriching nodes
# new_subtask_reason=new_subtask_reason, # only used when enriching nodes
target_children_num=self.max_child_num,
)
logger.debug(f"Num of existing_children: {len(existing_children)}")
logger.debug(f"Num of existing_siblings: {len(existing_siblings)}")
logger.debug(f"Final prompt: {prompt}")
logger.info(f"{self.assistant_name}: Online querying...")
result = self.request_func(prompt)
logger.info("Received online querying results")
logger.debug(f"raw request result: {result}")
if result is None:
logger.warning(f"Received error response!!")
return []
if extend_batch_size > 1:
for p, r in zip(prompt, result):
self.write_query_log(p, r)
else:
self.write_query_log(prompt, result)
# new_subtask, new_subtask_reason, new_instructions = \
# self.post_process_gpt3_response_extend(result)
if extend_batch_size > 1:
new_subtask, new_subtask_reason, new_instructions = [], [], []
for r in result:
new_subtask_tmp, new_subtask_reason_tmp, new_instructions_tmp = self.postprocess_extend(r)
new_subtask += new_subtask_tmp
new_subtask_reason += new_subtask_reason_tmp
new_instructions += new_instructions_tmp
else:
new_subtask, new_subtask_reason, new_instructions = \
self.postprocess_extend(result)
logger.debug(f"len(new_subtask): {len(new_subtask)}; new_subtask: {new_subtask};")
add_nodes = list()
if not (len(new_subtask) == len(new_subtask_reason) == len(new_instructions)):
logger.warning(f"{current_scenario}, encountering bad completion result")
if extend_batch_size > 1:
for p, r in zip(prompt, result):
self.handle_bad_completion(r,
prompt=p,
request_scenario=current_scenario, )
else:
self.handle_bad_completion(
result,
prompt=prompt,
request_scenario=current_scenario,
)
return add_nodes
# save file and update tree
for subtask, reason, examples in zip(new_subtask, new_subtask_reason, new_instructions):
subtask_id = self.formalize_taskname(subtask)
# node_save_file = os.path.join(self.general_outdir, f"{subtask_id}.json")
node_info = {
self.unique_id: subtask_id,
"raw_task_name": subtask,
"parent": node_to_extend,
"reason": reason,
"examples": examples,
# "config_file": node_save_file,
"config_filename": f"{subtask_id}.json",
}
new_node = self.add_node(node_info)
add_nodes.append(new_node)
return add_nodes
def write_query_log(self, prompt: str, res: Dict[str, str]):
with open(os.path.join(self.general_outdir, "query_log.jsonl"), "a+", encoding="utf-8") as f_out:
query_log = {
"prompt": prompt,
"res": res,
}
json.dump(
query_log,
f_out,
ensure_ascii=False,
)
f_out.write("\n")
def enrich_node_samples(
self,
node_to_enrich: AnyNode,
enrich_num: int = None,
enrich_batch_size: int = None,
) -> List[Dict[str, str]]:
all_new_examples = list()
while len(all_new_examples) < enrich_num:
gap = enrich_num - len(all_new_examples)
logger.info(f"Already enriched example num: {len(all_new_examples)}. This time gap: {gap}")
new_examples: List[Dict[str, str]] = self._enrich_node_samples(
node_to_enrich,
enrich_num=max(gap, self.min_extend_num),
enrich_batch_size=enrich_batch_size
)
all_new_examples += new_examples
return all_new_examples
def _enrich_node_samples(
self,
node_to_enrich: AnyNode,
enrich_num: int = None,
enrich_batch_size: int = None,
) -> List[Dict[str, str]]:
"""
Given a mined node, add more <instruct, input, output> belonging to this node
"""
enrich_num = self.default_enrich_num if enrich_num is None else enrich_num
enrich_batch_size = self.default_enrich_batch_size if enrich_batch_size is None else enrich_batch_size
# get existing num of example
if type(node_to_enrich) is str:
node_to_enrich: AnyNode = self.name_to_node[node_to_enrich]
new_subtask = node_name = getattr(node_to_enrich, self.unique_id)
current_scenario = f"enrich_node_samples for {node_name}"
logger.info(current_scenario)
new_subtask_reason = getattr(node_to_enrich, "reason", "")
# hard-coded to handle enriching root node
parent_node = node_to_enrich.parent if node_to_enrich.parent is not None else node_to_enrich
parent_node_name = getattr(parent_node, self.unique_id)
existing_examples = getattr(node_to_enrich, "examples", [])
if len(existing_examples) >= self.max_example_num:
logger.warning(f"Failed trial to enrich node {node_name}: already having {len(existing_examples)} examples")
return
if len(existing_examples) + enrich_num > self.max_example_num:
logger.warning(f"Exceeding max_child_num if extending by {enrich_num}, using remaining num instead.")
enrich_num = self.max_example_num - len(existing_examples)
base_prompt = self.enrich_node_prompt
new_instructions = list()
existing_children = list(parent_node.children)
existing_siblings = list(parent_node.siblings)
if enrich_batch_size > 1:
prompt = []
for _ in range(enrich_batch_size):
demonstrate_examples = self.get_demonstrate_examples(node_to_enrich)
# only sampling from the seed tasks
prompt_tmp = self.encode_prompt(
base_prompt=base_prompt,
demonstrate_examples=demonstrate_examples, # Example triplet
target_task=parent_node_name, # name of node_to_extend
existing_children=existing_children, # list of children of node_to_extend,
existing_siblings=existing_siblings, # list of siblings of node_to_extend
num_examples_per_time=self.num_example_enrich, # num of triplet for each new mined subtask
new_subtask=new_subtask, # only used when enriching nodes
new_subtask_reason=new_subtask_reason, # only used when enriching nodes
# extend_num=extend_num, # only used when extending nodes
# target_children_num=self.max_child_num, # only used when extending nodes
)
prompt.append(prompt_tmp)
else:
demonstrate_examples = self.get_demonstrate_examples(node_to_enrich)
# only sampling from the seed tasks
prompt = self.encode_prompt(
base_prompt=base_prompt,
demonstrate_examples=demonstrate_examples, # Example triplet
target_task=parent_node_name, # name of node_to_extend
existing_children=existing_children, # list of children of node_to_extend,
existing_siblings=existing_siblings, # list of siblings of node_to_extend
num_examples_per_time=self.num_example_enrich, # num of triplet for each new mined subtask
new_subtask=new_subtask, # only used when enriching nodes
new_subtask_reason=new_subtask_reason, # only used when enriching nodes
# extend_num=extend_num, # only used when extending nodes
# target_children_num=self.max_child_num, # only used when extending nodes
)
logger.debug(f"Final prompt: {prompt}")
logger.info(f"{self.assistant_name}: Online querying...")
# completion = self.request_openai(prompt)
# result = completion['choices'][0]
result = self.request_func(prompt)
logger.info("Received online querying results")
if enrich_batch_size > 1:
for p, r in zip(prompt, result):
self.write_query_log(p, r)
else:
self.write_query_log(prompt, result)
logger.debug(f"raw request result: {result}")
# _, _, new_instructions = \
# self.post_process_gpt3_response_enrich(result, new_subtask, new_subtask_reason)
if enrich_batch_size > 1:
new_instructions = []
for r in result:
_, _, new_instructions_tmp = self.postprocess_enrich(r, new_subtask, new_subtask_reason)
new_instructions += new_instructions_tmp
else:
_, _, new_instructions = \
self.postprocess_enrich(result, new_subtask, new_subtask_reason)
# update node config
node_to_enrich.examples = existing_examples + new_instructions
self.update_file(node_to_enrich, )
return new_instructions
def request_openai_single(self, prompt: str) -> Dict[str, str]:
response = None
for trial_idx in range(self.max_retry_times):
try:
messages = [
{"role": "user",
"content": prompt},
]
prompt_len = len(self.tokenizer(prompt)['input_ids'])
completion = openai.ChatCompletion.create(
messages=messages,
max_tokens=4096 - 300 - prompt_len,
**self.openai_kwargs,
)
result = completion['choices'][0]
response = {
"raw_response": result.get("message", {}).get("content", ""),
"stop_reason": result.get("finish_reason", ),
}
return response
except Exception as e:
logger.warning(str(e))
logger.warning(f"Trail No. {trial_idx + 1} Failed, now sleep and retrying...")
time.sleep(self.request_sleep_time)
return response
async def dispatch_openai_requests(
self,
messages_list: List[List[Dict[str, Any]]],
model: str,
temperature: float,
max_tokens: int,
top_p: float,
n: int,
logit_bias: dict,
) -> List[str]:
"""Dispatches requests to OpenAI API asynchronously.
Args:
messages_list: List of messages to be sent to OpenAI ChatCompletion API.
model: OpenAI model to use.
temperature: Temperature to use for the model.
max_tokens: Maximum number of tokens to generate.
top_p: Top p to use for the model.
n: Return sentence nums.
logit_bias: logit bias.
Returns:
List of responses from OpenAI API.
"""
async_responses = [
openai.ChatCompletion.acreate(
model=model,
messages=x,
temperature=temperature,
max_tokens=max_tokens,
top_p=top_p,
n=n,
logit_bias=logit_bias,
)
for x in messages_list
]
return await asyncio.gather(*async_responses)
def request_openai_dispatch(self, prompt_list: List[str]) -> List[Dict[str, str]]:
responses = None
for trial_idx in range(self.max_retry_times):
try:
messages_list = [[
{"role": "user",
"content": prompt},
] for prompt in prompt_list]
prompt_len = max([len(self.tokenizer(prompt)['input_ids']) for prompt in prompt_list])
completions = asyncio.run(
self.dispatch_openai_requests(
messages_list=messages_list,
max_tokens=4096 - 300 - prompt_len,
**self.openai_kwargs,
)
)
responses = []
for completion in completions:
result = completion['choices'][0]
response = {
"raw_response": result.get("message", {}).get("content", ""),
"stop_reason": result.get("finish_reason", ),
}
responses.append(response)
return responses
except Exception as e:
logger.warning(str(e))
logger.warning(f"Trail No. {trial_idx + 1} Failed, now sleep and retrying...")
time.sleep(self.request_sleep_time)
return responses
def request_openai(self, prompt):
use_async = not type(prompt) is str
if use_async:
response = self.request_openai_dispatch(prompt)
else:
response = self.request_openai_single(prompt)
return response
def request_claude(self, prompt: str) -> Dict[str, str]:
import anthropic
result = None
for trial_idx in range(self.max_retry_times):
try:
prompt = f"{anthropic.HUMAN_PROMPT} {prompt} {anthropic.AI_PROMPT}"
prompt_len = len(self.tokenizer(prompt)['input_ids'])
resp = self.claude_client.completion(
prompt=prompt,
max_tokens_to_sample=4096 - 300 - prompt_len,
**self.claude_kwargs,
)
result = {
"raw_response": resp["completion"],
"stop_reason": resp["stop_reason"],
}
return result
except Exception as e:
logger.warning(str(e))
logger.warning(f"Trail No. {trial_idx + 1} Failed, now sleep and retrying...")
time.sleep(self.request_sleep_time)
return result
def prepare_mine_hparams(self, **kwargs):
"""
Currently all hard-coded
"""
self.assistant_name = kwargs.get("assistant_name") if kwargs.get("assistant_name") else "openai"
self.assistant_request_map = {
"openai": self.request_openai,
"claude": self.request_claude,
}
self.assistant_postprocess_map = {
"openai": (self.post_process_gpt3_response_extend, self.post_process_gpt3_response_enrich),
"claude": (self.post_process_gpt3_response_extend, self.post_process_gpt3_response_enrich),
}
self.postprocess_extend, self.postprocess_enrich = self.assistant_postprocess_map[self.assistant_name]
self.request_func = self.assistant_request_map[self.assistant_name]
self.default_demonstrate_num = 2 # num of demonstrate example, during extending & enriching
self.num_example_extend = 2 # triplet per new subtask, during extending
self.default_extend_num = 5 # total new subtask num per request, during extending
self.default_enrich_num = 50 # cumulative enrich num, during enriching
self.num_example_enrich = 10 # num example per request, during enriching
self.max_child_num = 12 # maximum children num
self.max_example_num = 60000 # maximum example num per node
self.min_extend_num = 3 # minimum request subtask num, during extending
self.min_enrich_num = 5 # minimum request example num, during enriching
self.default_extend_batch_size = 1 # prompt batch size, during extending
self.default_enrich_batch_size = 1 # prompt batch size, during enriching
self.max_retry_times = 20 # max retry times for online querying
self.request_sleep_time = 20 # sleep time after online query fail
self.attribute_of_interest = [self.unique_id, "reason", "examples", "config_filename", "raw_task_name"]
self.attribute_meta_save = [self.unique_id, "reason", "config_filename", "raw_task_name"]
self.general_outdir = getattr(self.root, "general_outdir",
f"./mined_data_{getattr(self.root, self.unique_id)}")
os.makedirs(self.general_outdir, exist_ok=True)
self.bad_res_outfile = os.path.join(self.general_outdir, "bad_completions.jsonl")
def gather_all_examples(self, ) -> Dict[str, List[Dict[str, str]]]:
node_examples_map, total_count = dict(), 0
for node in PreOrderIter(self.root):
node_exampels = getattr(node, "examples", [])
node_examples_map[getattr(node, self.unique_id)] = node_exampels
total_count += len(node_exampels)
node_examples_map["total_count"] = total_count
return node_examples_map
def prepare_prompt(self, ):
raise NotImplementedError
def prepare_tools(self):
raise NotImplementedError
def get_demonstrate_examples(self, node_to_prepare: AnyNode) -> List[Dict[str, str]]:
demonstrate_node = node_to_prepare.parent if node_to_prepare.parent else node_to_prepare
demonstrate_examples_pool = getattr(demonstrate_node, "examples", [])
demonstrate_examples = random.sample(demonstrate_examples_pool, self.default_demonstrate_num) \
if demonstrate_examples_pool else []
return demonstrate_examples
def add_node(self, node_info: Dict[str, Union[str, List[str]]]) -> AnyNode:
assert self.unique_id in node_info and "parent" in node_info, \
f"Invalid node to add: {node_info}: both .{self.unique_id} and .parent are required!"
new_node = AnyNode(
**node_info,
)
self.update_file(new_node, file_type="node", update_mode="new_file")
self.name_to_node[getattr(new_node, self.unique_id)] = new_node
logger.info(f"Node added: {node_info[self.unique_id]}")
return new_node
def add_node_to_tree(self, new_node: AnyNode):
self.update_file(new_node, file_type="node", update_mode="new_file")
self.name_to_node[getattr(new_node, self.unique_id)] = new_node
logger.info(f"Node added: {getattr(new_node, self.unique_id)}")
def update_file(
self,
node: AnyNode,
update_mode: str = "new_file",
file_type: str = "node",
):
if update_mode == "new_file":
node_save_file = os.path.join(self.general_outdir, node.config_filename)
with open(node_save_file, "w", encoding="utf-8") as f_out:
save_dict = {k: v for k, v in vars(node).items() if k in self.attribute_of_interest}
json.dump(
save_dict,
f_out,
ensure_ascii=False,
)
f_out.write("\n")
def handle_bad_completion(
self,
result: Dict[str, str],
**kwargs,
):
result.update(**kwargs)
with open(self.bad_res_outfile, "a+", encoding="utf-8") as f_out:
json.dump(
result,
f_out,
ensure_ascii=False,
)
f_out.write("\n")
def encode_prompt(
self,
base_prompt: str,
demonstrate_examples: Optional[List[str]] = [],
target_task: str = None,
existing_children: List[AnyNode] = [],
existing_siblings: List[AnyNode] = [],
num_examples_per_time: int = None,
extend_num: int = None,
new_subtask: str = None,
new_subtask_reason: str = None,
target_children_num: int = None,
) -> str:
raise NotImplementedError
def formalize_taskname(self, taskname: str):
raise NotImplementedError
def save_to_local(self):
"""
save meta file and each node info
"""
# first check all node already saved to individual file
for node in PreOrderIter(self.root):
# config_file = getattr(node, "config_file") if getattr(node, "config_file", None)\
# else os.path.join(self.general_outdir, getattr(node, "config_filename"))
config_file = os.path.join(self.general_outdir, getattr(node, "config_filename"))
if not os.path.isfile(config_file):
logger.warning(
f"Node info for {getattr(node, self.unique_id)} is not saved! Now saving to {config_file}")
self.update_file(node, file_type="node", update_mode="new_file")
# export meta file (set attriter in order to )
exporter = DictExporter(
attriter=lambda attrs: [(k, v) for k, v in attrs if k in self.attribute_meta_save]
)
meta_info = exporter.export(self.root)
with open(os.path.join(self.general_outdir, meta_info_filename), "w", encoding="utf-8") as f_out:
json.dump(
meta_info,
f_out,
ensure_ascii=False,
)
f_out.write("\n")
return
def post_process_gpt3_response_enrich(
self,
response: str,
current_new_subtask: str = None,
current_new_subtask_reason: str = None,
) -> Tuple[Union[str, List[str]], Union[str, List[str]], List[Dict[str, str]]]:
raise NotImplementedError
@staticmethod
def parse_node_config(
node: AnyNode,
config_field_name: str = "config_filename",
**kwargs,
) -> AnyNode:
config_filename = getattr(node, config_field_name, None)
base_dir = kwargs.get("base_dir")
config_file = os.path.join(base_dir, config_filename)
if not config_filename or (not os.path.isfile(config_file)):
logger.warning(f"Failed to load node info from {config_file}! Loading fail for {node}")
return
with open(config_file, encoding="utf-8") as f_in:
task_config = json.load(f_in)
for k, v in task_config.items():
# if getattr(node, config_field_name, None):
# logger.warning(f"")
setattr(node, k, v)
def post_process_gpt3_response_extend(
self,
response: str
) -> Tuple[List[str], List[str], List[Dict[str, str]]]:
raise NotImplementedError
@classmethod
def from_tree_dict(
cls,
domain_tree_dict: Dict[str, Union[str, List[Dict[str, str]]]] = {},
save_dir: str = None,
out_dir: str = None,
**kwargs,
):
"""
Currently only for debugging use!
"""
root_node = DictImporter().import_(domain_tree_dict)
for node in PreOrderIter(root_node):
cls.parse_node_config(node, base_dir=save_dir)
if out_dir is not None:
root_node.general_outdir = out_dir
return cls(root_node, **kwargs)
@classmethod
def from_local_dir(
cls,
save_dir: str,
out_dir: str = None,
meta_file: str = meta_info_filename,
**kwargs,
):
# first load meta info, and infill each node info by loading config file
with open(os.path.join(save_dir, meta_file)) as f_in:
meta_info = json.load(f_in)
root_node = DictImporter().import_(meta_info)
for node in PreOrderIter(root_node):
cls.parse_node_config(node, base_dir=save_dir)
root_node.general_outdir = out_dir if out_dir is not None else save_dir
return cls(root_node, **kwargs)
class EnDomainTreeRewrite(DomainTree):
def encode_prompt(
self,
base_prompt: str,
demonstrate_examples: Optional[List[str]] = [],
target_task: str = None,
existing_children: List[AnyNode] = [],
existing_siblings: List[AnyNode] = [],
num_examples_per_time: int = None,
extend_num: int = None,
new_subtask: str = None,
new_subtask_reason: str = None,
target_children_num: int = None,
) -> str:
prompt = base_prompt
prompt += f"\nTarget task: {target_task}\n"
if len(demonstrate_examples) > 0:
prompt += "Examples:\n"
for idx, task_dict in enumerate(demonstrate_examples):
(instruction, input, output) = task_dict["instruction"], task_dict["input"], task_dict["output"]
instruction = re.sub(r"\s+", " ", instruction).strip().rstrip(":")
input = "<noinput>" if input.lower() == "" else input
prompt += "###\n"
prompt += f"{idx + 1}. Instruction: {instruction}\n"
prompt += f"Input: {input}\n"
prompt += f"Output: {output}\n"
prompt += "###\n"
existing_children_names = [getattr(node, self.unique_id) for node in existing_children]
existing_siblings_names = [getattr(node, self.unique_id) for node in existing_siblings]
prompt += f"\nThe list of already existing subtasks for this target task is: {existing_children_names}.\n"
prompt += f"The list of already existing peer tasks for this target task is: {existing_siblings_names}.\n"
if target_children_num is not None: # for extending
prompt += f"\nThe target task should be decomposed into a total of {target_children_num} diverse and complementary subtasks, " \
f"and there are {len(existing_children)} existing subtasks. " \
f"Generate {extend_num} new subtasks with the corresponding reason, then list {num_examples_per_time} examples of this new subtask:"
else: # for enriching
prompt += f"\nList {num_examples_per_time} examples of this new subtask below:"
if new_subtask: # for enriching
prompt += "\n"
prompt += f"\nNew subtask: {new_subtask}\n"
prompt += f"Reason: {new_subtask_reason}"
return prompt
def prepare_prompt(self, ):
self.extend_node_prompt = open("./prompt_bank/prompt_write_assistance_extend.txt").read() + "\n"
self.enrich_node_prompt = open("./prompt_bank/prompt_write_assistance_enrich.txt").read() + "\n"
def prepare_tools(self):
logger.info("Preparint tools...")
self.tokenizer = GPT2TokenizerFast.from_pretrained("gpt2")
self.openai_kwargs = {
"model": "gpt-3.5-turbo", # openai model type
"temperature": 1.0,
"top_p": 1.0,
"n": 1,
"logit_bias": {"50256": -100}, # prevent the <|endoftext|> token from being generated
}
if self.assistant_name == "claude":
import anthropic
self.claude_client = anthropic.Client(claude_key, proxy_url="http://127.0.0.1:2802")
self.claude_kwargs = {
"stop_sequences": [anthropic.HUMAN_PROMPT],
"model": "claude-v1.3", # anthropic model type
}
def formalize_taskname(self, taskname: str):
taskname = re.sub('[^A-Za-z0-9]+', ' ', taskname)
taskname = taskname.strip().replace(" ", "_").lower()
# dedup
if taskname in self.name_to_node:
for i in range(10):
dedup_taskname = f"{taskname}_{i}"
if dedup_taskname not in self.name_to_node:
break
taskname = dedup_taskname
return taskname
def post_process_gpt3_response_enrich(
self,
response: str,
current_new_subtask: str = None,
current_new_subtask_reason: str = None,
) -> Tuple[Union[str, List[str]], Union[str, List[str]], List[Dict[str, str]]]:
if response is None:
return None, current_new_subtask, current_new_subtask_reason
stop_reason = response.get("stop_reason", "")
raw_response = response.get("raw_response", "")
raw_response = raw_response.replace("###", "").replace("### ", "").replace(" ###", "").replace(" ### ", "")
new_subtask = current_new_subtask
new_subtask_reason = current_new_subtask_reason
new_subtask_examples = "\n" + raw_response.lstrip("Examples:")
new_subtask_examples = re.split("\n+\d+\.\s+", new_subtask_examples)
new_subtask_examples = new_subtask_examples[1:]
instructions = []
for idx, inst in enumerate(new_subtask_examples):
# if the decoding stops due to length, the last example is likely truncated so we discard it
if idx == len(new_subtask_examples) - 1 and stop_reason == "length":
continue
idx += 1
splitted_data = re.split("Instruction:|Input:|Output:", inst)
if len(splitted_data) != 4:
continue
inst = splitted_data[1].strip()
input = splitted_data[2].strip()
input = "" if input.lower() == "<noinput>" else input
output = splitted_data[3].strip().strip()
# filter out too short or too long instructions
if len(inst.split()) < 3 or len(inst.split()) > 150:
continue
# filter based on keywords that are not suitable for language models.
blacklist = [
"image",
"images",
"graph",
"graphs",
"picture",
"pictures",
"file",
"files",
"map",
"maps",
"draw",
"plot",
"go to",
"video",
"audio",
"music",
"flowchart",
"diagram",
]
blacklist += []
if any(find_word_in_string(word, inst) for word in blacklist):
continue
# We found that the model tends to add "write a program" to some existing instructions, which lead to a lot of such instructions.
# And it's a bit comfusing whether the model need to write a program or directly output the result.
# Here we filter them out.
# Note this is not a comprehensive filtering for all programming instructions.
if inst.startswith("Write a program"):
continue
# filter those starting with punctuation
if inst[0] in string.punctuation:
continue
# filter those starting with non-english character
if not inst[0].isascii():
continue
# filter un-complete input
if input.startswith("<") and input.endswith(">"):
continue
if input.startswith("(") and input.endswith(")"):
continue
instructions.append({"instruction": inst, "input": input, "output": output})
return new_subtask, new_subtask_reason, instructions
def post_process_gpt3_response_extend(
self,
response: str
) -> Tuple[List[str], List[str], List[List[Dict[str, str]]]]:
stop_reason = response.get("stop_reason", "")
raw_response = response.get("raw_response", "")
raw_response = raw_response.replace("###", "").replace("### ", "").replace(" ###", "").replace(" ### ", "")
raw_response = raw_response.replace("Example:", "Examples:")
split_response = re.split("New subtask:|Reason:|Examples:", raw_response)
split_response = split_response[1:]
num_subtasks = len(split_response) // 3
new_subtasks = []
new_subtasks_reason = []
new_subtasks_example = []
for i in range(num_subtasks):
new_subtask = split_response[i * 3].strip()
new_subtask_reason = split_response[i * 3 + 1].strip()
new_subtask_examples = split_response[i * 3 + 2]
new_subtask_examples = re.split("\n+\d+\.\s+", new_subtask_examples)
new_subtask_examples = new_subtask_examples[1:]
instructions = []
for idx, inst in enumerate(new_subtask_examples):
# if the decoding stops due to length, the last example is likely truncated so we discard it
if idx == len(new_subtask_examples) - 1 and stop_reason == "length":
continue
splitted_data = re.split("Instruction:|Input:|Output:", inst)
if len(splitted_data) != 4:
continue
inst = splitted_data[1].strip()
input = splitted_data[2].strip()
input = "" if input.lower() == "<noinput>" else input
output = splitted_data[3].strip().strip()
# filter out too short or too long instructions
if len(inst.split()) < 3 or len(inst.split()) > 150:
continue
# filter based on keywords that are not suitable for language models.
blacklist = [
"image",
"images",
"graph",
"graphs",
"picture",
"pictures",
"file",
"files",
"map",
"maps",
"draw",
"plot",
"go to",
"video",
"audio",
"music",
"flowchart",
"diagram",
]
blacklist += []
if any(find_word_in_string(word, inst) for word in blacklist):
continue
# We found that the model tends to add "write a program" to some existing instructions, which lead to a lot of such instructions.
# And it's a bit comfusing whether the model need to write a program or directly output the result.
# Here we filter them out.
# Note this is not a comprehensive filtering for all programming instructions.
if inst.startswith("Write a program"):
continue
# filter those starting with punctuation
if inst[0] in string.punctuation:
continue
# filter those starting with non-english character
if not inst[0].isascii():
continue
# filter un-complete input
if input.startswith("<") and input.endswith(">"):
continue
if input.startswith("(") and input.endswith(")"):
continue
instructions.append({"instruction": inst, "input": input, "output": output})
new_subtasks.append(new_subtask)
new_subtasks_reason.append(new_subtask_reason)
new_subtasks_example.append(instructions)
return new_subtasks, new_subtasks_reason, new_subtasks_example
class EnDomainTreeBrainstorming(DomainTree):
def encode_prompt(
self,
base_prompt: str,
demonstrate_examples: Optional[List[str]] = [],
target_task: str = None,
existing_children: List[AnyNode] = [],
existing_siblings: List[AnyNode] = [],
num_examples_per_time: int = None,
extend_num: int = None,
new_subtask: str = None,
new_subtask_reason: str = None,
target_children_num: int = None,
) -> str:
prompt = base_prompt
prompt += f"\nTarget task: {target_task}\n"
if len(demonstrate_examples) > 0:
prompt += "Examples:\n"
for idx, task_dict in enumerate(demonstrate_examples):
(instruction, input, output) = task_dict["instruction"], task_dict["input"], task_dict["output"]
instruction = re.sub(r"\s+", " ", instruction).strip().rstrip(":")
input = "<noinput>" if input.lower() == "" else input
prompt += "###\n"
prompt += f"Instruction: {instruction}\n" # without index
prompt += f"Input: {input}\n"
prompt += f"Output: {output}\n"
prompt += "###\n"
existing_children_names = [getattr(node, self.unique_id) for node in existing_children]
existing_siblings_names = [getattr(node, self.unique_id) for node in existing_siblings]
prompt += f"\nThe list of already existing subtasks for this target task is: {existing_children_names}.\n"
prompt += f"The list of already existing peer tasks for this target task is: {existing_siblings_names}.\n"
if target_children_num is not None: # for extending
prompt += f"\nThe target task should be decomposed into a total of {target_children_num} diverse and complementary subtasks, " \
f"and there are {len(existing_children)} existing subtasks. " \
f"Generate {extend_num} new subtasks with the corresponding reason, then list {num_examples_per_time} examples of this new subtask:"
else: # for enriching
prompt += f"\nList {num_examples_per_time} examples of this new subtask below:"
if new_subtask: # for enriching
prompt += "\n"
prompt += f"\nNew subtask: {new_subtask}\n"
prompt += f"Reason: {new_subtask_reason}"
return prompt
def prepare_prompt(self, ):
self.extend_node_prompt = open("./prompt_bank/prompt_brainstorming_extend.txt").read() + "\n"
self.enrich_node_prompt = open("./prompt_bank/prompt_brainstorming_enrich.txt").read() + "\n"
def prepare_tools(self):
logger.info("Preparint tools...")
self.tokenizer = GPT2TokenizerFast.from_pretrained("gpt2")
self.openai_kwargs = {
"model": "gpt-3.5-turbo", # openai model type
"temperature": 1.0,
"top_p": 1.0,
"n": 1,
"logit_bias": {"50256": -100}, # prevent the <|endoftext|> token from being generated
}
if self.assistant_name == "claude":
import anthropic
self.claude_client = anthropic.Client(claude_key, proxy_url="http://127.0.0.1:2802")
self.claude_kwargs = {
"stop_sequences": [anthropic.HUMAN_PROMPT],
"model": "claude-v1.3", # anthropic model type
}
def formalize_taskname(self, taskname: str):
taskname = re.sub('[^A-Za-z0-9]+', ' ', taskname)
taskname = taskname.strip().replace(" ", "_").lower()
# dedup
if taskname in self.name_to_node:
for i in range(10):