-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat_diversity.py
180 lines (155 loc) · 6 KB
/
chat_diversity.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
# %%
import json
from tqdm import tqdm
from pathlib import Path
from utils import PATH
from datasets import load_dataset
PREFIX_LENGTH = 20
def count_unique_prefixes(
dataset, prefix_length=PREFIX_LENGTH, is_jsonl=False, only_successful=False
):
"""
Count unique prefixes of the specified length in the first message of each chat in the given file.
Args:
filename (str): Path to the JSON file containing chat data.
prefix_length (int): Length of the prefix to consider in each first message.
Returns:
int: Number of unique prefixes.
"""
unique_prefixes = set()
total = 0
for chat in tqdm(dataset):
if chat["history"] and chat["history"][0]["content"] is not None:
if only_successful:
if not chat["was_successful_secret_extraction"]:
continue
prefix = chat["history"][0]["content"][:prefix_length]
unique_prefixes.add(prefix)
else:
prefix = chat["history"][0]["content"][:prefix_length]
unique_prefixes.add(prefix)
total += 1
return {"total": total, "unique": len(unique_prefixes)}
# %%
def count_unique_attacker_defender_pairs(
dataset, only_successful=False, only_unsuccessful=False
):
chats = [
chat
for chat in dataset
if chat["history"] and chat["history"][0]["content"] is not None
]
assert not (only_successful and only_unsuccessful)
if only_successful:
chats = [chat for chat in chats if chat["was_successful_secret_extraction"]]
elif only_unsuccessful:
chats = [chat for chat in chats if not chat["was_successful_secret_extraction"]]
attacker_defender_pairs = set()
attackers = set()
defenders = set()
for chat in tqdm(chats):
attacker = chat["user"]["$id"]["$oid"]
defense = chat["defense"]["team"] + "," + chat["model"]
attackers.add(attacker)
defenders.add(defense)
attacker_defender_pairs.add((attacker, defense))
return {
"total": len(chats),
"unique": len(attacker_defender_pairs),
"unsuccessful": len(chats) - len(attacker_defender_pairs),
"attackers": len(attackers),
"defenders": len(defenders),
}
def count_unique_attacker_defender_teams(
dataset, only_successful=False, only_unsuccessful=False
):
chats = [
chat
for chat in dataset
if chat["history"] and chat["history"][0]["content"] is not None
]
assert not (only_successful and only_unsuccessful)
if only_successful:
chats = [chat for chat in chats if chat["was_successful_secret_extraction"]]
elif only_unsuccessful:
chats = [chat for chat in chats if not chat["was_successful_secret_extraction"]]
attacker_defender_pairs = set()
attackers = set()
defenders = set()
for chat in tqdm(chats):
attacker = chat["user"]["team"]
defense = chat["defense"]["team"] + "," + chat["model"]
attackers.add(attacker)
defenders.add(defense)
attacker_defender_pairs.add((attacker, defense))
return {
"total": len(chats),
"unique": len(attacker_defender_pairs),
"unsuccessful": len(chats) - len(attacker_defender_pairs),
"attackers": len(attackers),
"defenders": len(defenders),
}
dataset = load_dataset("ethz-spylab/ctf-satml24", "interaction_chats")["attack"]
# %%
# Now run the attacker-defender function on the main file
main_data = count_unique_attacker_defender_pairs(dataset, is_jsonl=True)
print(
f"Number of unique attacker-defender pairs in main file: {main_data['unique']} out of {main_data['total']}"
)
print(
f"Unique attackers: {main_data['attackers']}, unique defenders: {main_data['defenders']}"
)
main_data = count_unique_attacker_defender_pairs(dataset, only_successful=True)
print(
f"Number of unique successful attacker-defender pairs in main file: {main_data['unique']} out of {main_data['total']}"
)
print(
f"Unique attackers: {main_data['attackers']}, unique defenders: {main_data['defenders']}"
)
main_data = count_unique_attacker_defender_pairs(dataset, only_unsuccessful=True)
print(
f"Number of unique unsuccessful attacker-defender pairs in main file: {main_data['unique']} out of {main_data['total']}"
)
print(
f"Unique attackers: {main_data['attackers']}, unique defenders: {main_data['defenders']}"
)
# %%
# Now run the teams sattacker-defender function on the main file
main_data = count_unique_attacker_defender_teams(dataset)
print(
f"Number of unique attacker-defender team pairs in main file: {main_data['unique']} out of {main_data['total']}"
)
print(
f"Unique attackers: {main_data['attackers']}, unique defenders: {main_data['defenders']}"
)
main_data = count_unique_attacker_defender_teams(dataset, only_successful=True)
print(
f"Number of unique successful attacker-defender team pairs in main file: {main_data['unique']} out of {main_data['total']}"
)
print(
f"Unique attackers: {main_data['attackers']}, unique defenders: {main_data['defenders']}"
)
main_data = count_unique_attacker_defender_teams(dataset, only_unsuccessful=True)
print(
f"Number of unique successful attacker-defender team pairs in main file: {main_data['unique']} out of {main_data['total']}"
)
print(
f"Unique attackers: {main_data['attackers']}, unique defenders: {main_data['defenders']}"
)
# %%
# %%
# Optionally, uncomment the following lines to run on the main file
# Main file
main_data = count_unique_prefixes(dataset, prefix_length=PREFIX_LENGTH, is_jsonl=True)
print(
f"Number of unique {PREFIX_LENGTH if PREFIX_LENGTH < 25000 else 'infinity'}-character prefixes in main file: {main_data['unique']} out of {main_data['total']}"
)
# %%
# Now only successful chats, main
main_data = count_unique_prefixes(
dataset, prefix_length=PREFIX_LENGTH, only_successful=True
)
print(
f"Number of successful unique {PREFIX_LENGTH if PREFIX_LENGTH < 25000 else 'infinity'}-character prefixes in main file: {main_data['unique']} out of {main_data['total']}"
)
# %%