-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdvancedAdversarialDatasetExplorer.py
270 lines (228 loc) · 8.42 KB
/
AdvancedAdversarialDatasetExplorer.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
import torch
import numpy as np
import pandas as pd
import datasets
from transformers import AutoTokenizer, AutoModel
import concurrent.futures
import itertools
import json
import logging
class AdvancedAdversarialDatasetExplorer:
def __init__(
self,
embedding_model: str = 'sentence-transformers/all-MiniLM-L6-v2'
):
# Configure logging
logging.basicConfig(level=logging.INFO)
self.logger = logging.getLogger(__name__)
# Load embedding model
self.tokenizer = AutoTokenizer.from_pretrained(embedding_model)
self.embedding_model = AutoModel.from_pretrained(embedding_model)
# Initialize dataset collections
self.toxic_prompt_datasets = [
'offensive_language/civil_comments',
'hate_speech/hatebase_twitter',
'toxicity/wikipedia_toxicity'
]
self.reasoning_attack_datasets = [
'reasoning/logiqa',
'reasoning/commonsense_qa',
'reasoning/strategy_qa'
]
self.adversarial_datasets = [
'adversarial/universal_adversarial_triggers',
'adversarial/jailbreak_prompts',
'adversarial/red_team_attempts'
]
def _load_dataset(
self,
dataset_name: str,
split: str = 'train'
) -> datasets.Dataset:
"""
Load and cache dataset from HuggingFace
"""
try:
return datasets.load_dataset(dataset_name, split=split)
except Exception as e:
self.logger.error(f"Error loading dataset {dataset_name}: {e}")
return None
def analyze_toxic_prompt_patterns(
self,
toxicity_threshold: float = 0.7
) -> Dict[str, Any]:
"""
Comprehensive toxic prompt pattern analysis
"""
toxic_pattern_analysis = {}
for dataset_name in self.toxic_prompt_datasets:
dataset = self._load_dataset(dataset_name)
if dataset is None:
continue
# Extract toxic prompts
toxic_prompts = [
item['text']
for item in dataset
if item.get('toxicity', 0) > toxicity_threshold
]
# Compute embedding clustering
toxic_embeddings = self._compute_embeddings(toxic_prompts)
clustering_analysis = self._cluster_embeddings(toxic_embeddings)
toxic_pattern_analysis[dataset_name] = {
'total_toxic_prompts': len(toxic_prompts),
'clustering_analysis': clustering_analysis,
'toxicity_distribution': self._compute_toxicity_distribution(dataset)
}
return toxic_pattern_analysis
def explore_reasoning_attack_vectors(
self,
attack_type: str = 'logical_contradiction'
) -> Dict[str, Any]:
"""
Advanced reasoning attack vector exploration
"""
reasoning_attack_analysis = {}
for dataset_name in self.reasoning_attack_datasets:
dataset = self._load_dataset(dataset_name)
if dataset is None:
continue
# Extract reasoning samples
reasoning_samples = [
item['question']
for item in dataset
if 'question' in item
]
# Compute attack potential
attack_potential_scores = self._compute_attack_potential(
reasoning_samples,
attack_type
)
reasoning_attack_analysis[dataset_name] = {
'total_samples': len(reasoning_samples),
'attack_potential_distribution': attack_potential_scores
}
return reasoning_attack_analysis
def generate_adversarial_prompt_variants(
self,
base_prompt: str,
num_variants: int = 10
) -> List[str]:
"""
Generate adversarial prompt variants
"""
# Load adversarial datasets
adversarial_triggers = []
for dataset_name in self.adversarial_datasets:
dataset = self._load_dataset(dataset_name)
if dataset is not None:
adversarial_triggers.extend([
item.get('trigger', '')
for item in dataset
])
# Generate prompt variants
prompt_variants = []
for trigger in itertools.islice(adversarial_triggers, num_variants):
variant = f"{base_prompt} {trigger}"
prompt_variants.append(variant)
return prompt_variants
def _compute_embeddings(
self,
texts: List[str]
) -> np.ndarray:
"""
Compute text embeddings
"""
embeddings = []
for text in texts:
inputs = self.tokenizer(
text,
return_tensors='pt',
padding=True,
truncation=True
)
with torch.no_grad():
outputs = self.embedding_model(**inputs)
embedding = outputs.last_hidden_state.mean(dim=1)
embeddings.append(embedding.numpy())
return np.vstack(embeddings)
def _cluster_embeddings(
self,
embeddings: np.ndarray,
num_clusters: int = 5
) -> Dict[str, Any]:
"""
Cluster embeddings using advanced techniques
"""
from sklearn.cluster import DBSCAN
from sklearn.preprocessing import StandardScaler
# Standardize embeddings
scaler = StandardScaler()
scaled_embeddings = scaler.fit_transform(embeddings)
# Apply DBSCAN clustering
clusterer = DBSCAN(eps=0.5, min_samples=3)
cluster_labels = clusterer.fit_predict(scaled_embeddings)
return {
'num_clusters': len(np.unique(cluster_labels)),
'cluster_distribution': np.bincount(cluster_labels + 1)
}
def _compute_attack_potential(
self,
samples: List[str],
attack_type: str
) -> Dict[str, float]:
"""
Compute attack potential for reasoning samples
"""
# Simplified attack potential computation
attack_potential_scores = {}
# Different scoring strategies based on attack type
if attack_type == 'logical_contradiction':
attack_potential_scores = {
'ambiguity_score': np.mean([
len(re.findall(r'\b(maybe|possibly|perhaps)\b', sample.lower()))
for sample in samples
]),
'contradiction_potential': np.mean([
len(re.findall(r'\b(but|however|although)\b', sample.lower()))
for sample in samples
])
}
return attack_potential_scores
def _compute_toxicity_distribution(
self,
dataset: datasets.Dataset
) -> Dict[str, float]:
"""
Compute toxicity distribution
"""
toxicity_scores = [
item.get('toxicity', 0)
for item in dataset
]
return {
'mean_toxicity': np.mean(toxicity_scores),
'median_toxicity': np.median(toxicity_scores),
'max_toxicity': np.max(toxicity_scores)
}
def main():
# Initialize dataset explorer
dataset_explorer = AdvancedAdversarialDatasetExplorer()
# Analyze toxic prompt patterns
toxic_analysis = dataset_explorer.analyze_toxic_prompt_patterns()
print("Toxic Prompt Pattern Analysis:")
print(json.dumps(toxic_analysis, indent=2))
# Explore reasoning attack vectors
reasoning_attacks = dataset_explorer.explore_reasoning_attack_vectors()
print("\nReasoning Attack Vector Analysis:")
print(json.dumps(reasoning_attacks, indent=2))
# Generate adversarial prompt variants
base_prompt = "Explain the principles of ethical AI development"
adversarial_variants = dataset_explorer.generate_adversarial_prompt_variants(
base_prompt,
num_variants=5
)
print("\nAdversarial Prompt Variants:")
for variant in adversarial_variants:
print(variant)
if __name__ == "__main__":
main()