Adapters
khulnasoft commited on
Commit
0cbdc6a
·
verified ·
1 Parent(s): ae1cb98

Create utils.py

Browse files
Files changed (1) hide show
  1. prompt_injection/mutators/utils.py +44 -0
prompt_injection/mutators/utils.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import pickle
3
+ import pandas as pd
4
+
5
+ from typing import List
6
+
7
+ import tqdm
8
+
9
+ from prompt_injection.mutators.base import PromptMutator
10
+
11
+
12
+ def init_mutator_result_object(output_path,evaluator_list):
13
+ result={'idx':[],'Prompt':[]}
14
+ for evaluator in evaluator_list:
15
+ result[evaluator.get_name()]=[]
16
+
17
+
18
+ if os.path.exists(output_path):
19
+ with open(output_path,'rb') as f:
20
+ result=pickle.load(f)
21
+
22
+ if os.path.exists(output_path):
23
+ with open(output_path,'rb') as f:
24
+ result=pickle.load(f)
25
+ return result
26
+
27
+
28
+ def mutate_all(prompts,mutators_list:List[PromptMutator],output_path):
29
+ result=init_mutator_result_object(output_path,mutators_list)
30
+
31
+ for i in tqdm.tqdm(range(len(prompts))):
32
+ if i in result["idx"]:
33
+ continue
34
+
35
+ prompt=prompts[i]
36
+ result['idx'].append(i)
37
+ result['Prompt'].append(prompt)
38
+ for mutator in mutators_list:
39
+ result[mutator.get_name()].append(mutator.mutate(prompt))
40
+
41
+ with open(output_path,'wb') as f:
42
+ pickle.dump(result, f, protocol=pickle.HIGHEST_PROTOCOL)
43
+
44
+ return pd.DataFrame.from_dict(result)