-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSubTaskB-rag-flashrank.py
251 lines (189 loc) · 9.37 KB
/
SubTaskB-rag-flashrank.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
import json
import os
import random
import time
from concurrent.futures import ThreadPoolExecutor, as_completed
import pandas as pd
from openai import AzureOpenAI
from sklearn.metrics import f1_score
from tqdm import tqdm
import chromadb
from chromadb.utils import embedding_functions
from flashrank import Ranker, RerankRequest
random.seed(42)
SYSTEM_PROMPT = """
Analyze the following tweet and classify who the target of the hate speech is. Use the identified patterns and specific examples from the training data for classification. The categories are:
## Categories
1. Individual - Involves direct attacks on specific individuals. Common examples include derogatory remarks about individuals like "Trump" or "Greta Thunberg". Look for usage of individual names and personal attacks.
2. Organization - Involves criticisms targeted at larger entities such as governments, companies, or specific organizations. Key examples include attacks on 'Government', 'Big oil companies', 'Australia' (referring to its government), 'Wilderness Committee', and the 'EU'. Look for mentions of these entities and critiques of their policies or actions.
3. Community - Involves attacks on broader communities or societal groups. Typical terms used include 'White, middle class, educated, low earners', 'humans', 'adult society', and 'politicians'. This category shifts the focus from a single party to collective human behavior, demographic groups, or societal constructs.
Use chain of thought reasoning to explain your classification. After analyzing the tweet, classify it as "Prediction: 1" for an individual, "Prediction: 2" for an organization, or "Prediction: 3" for a community. Pick only one option and put it on a new line. If the tweet is a factual statement, classify its target as described above.
## Examples
"""
api_version = "2023-07-01-preview"
endpoint = os.getenv("OPENAI_BASE_URL")
client = AzureOpenAI(
api_version=api_version,
azure_endpoint=endpoint,
api_key=os.getenv("OPENAI_API_KEY"),
)
def predict(system_prompt, user_prompt) -> str:
for attempt in range(25):
try:
completion = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": f"Input tweet: {user_prompt}"},
],
temperature=0,
)
return completion.choices[0].message.content
except Exception as e:
print(f"Attempt {attempt + 1} failed with error: {e}")
if "rate limit" in str(e).lower():
time_to_wait = random.randint(5, 15)
print(
f"Rate limited. Waiting for {time_to_wait} seconds before retrying..."
)
time.sleep(time_to_wait)
elif "Azure OpenAI's content management policy" in str(e):
print("ContentFilterError: returning prediction 1")
return "Prediction: 1 (indicating it is hate speech)."
else:
time.sleep(random.randint(5, 15))
return "Prediction failed after multiple attempts."
def parse_prediction(completion: str) -> int:
individual_answer_options = [
"Prediction: 1",
"'Prediction: 1'",
"'Prediction: 1'.",
"Prediction: 1 (Individual)"
]
organization_answer_options = [
"Prediction: 2",
"'Prediction: 2'",
"'Prediction: 2'.",
"Prediction: 2 (Organization)"
]
community_answer_options = [
"Prediction: 3",
"'Prediction: 3'",
"'Prediction: 3'.",
]
if any(completion.endswith(option) or completion.startswith(option) for option in individual_answer_options):
return 1
elif any(completion.endswith(option) or completion.startswith(option) for option in organization_answer_options):
return 2
elif any(completion.endswith(option) or completion.startswith(option) for option in community_answer_options):
return 3
else: # TODO: Failed to parse, raise an error instead
print(f"Failed to parse prediction: {completion}")
return False
def classify_example(system_prompt, user_prompt) -> bool:
index_name = "subtask_b_index"
index_path = "indexes/subtask_b_index"
# client = chromadb.PersistentClient(path=str(index_path))
# collection = client.get_collection(name=index_name)
# index_name = "subtask_a_index_all-mpnet-base-v2"
# index_path = "indexes/subtask_a_index_all-mpnet-base-v2"
# embedding_model = "all-mpnet-base-v2"
client = chromadb.PersistentClient(path=str(index_path))
collection = client.get_collection(name=index_name)
result = collection.query(query_texts=[user_prompt], n_results=12)
# embedding_fn = embedding_functions.SentenceTransformerEmbeddingFunction(model_name=embedding_model)
# result = collection.query(query_embeddings=embedding_fn([user_prompt]), n_results=12)
results = [
{
"id": id,
"text": text,
"meta": metadata
} for id, text, metadata in zip(*result['ids'], *result['documents'], *result['metadatas'])
]
ranker = Ranker(model_name="rank-T5-flan", cache_dir="cache")
rerank_request = RerankRequest(
query=user_prompt,
passages=results,
)
results = ranker.rerank(rerank_request)
examples = ""
for result in results[:8]:
text = result["text"]
metadata = result["meta"]
examples += f"Input tweet: {text}\n\nPrediction: {metadata['label']}\n\n"
completion = predict(system_prompt + "\n" + examples, user_prompt)
return parse_prediction(completion)
def classify_test_set_parallel(filename, system_prompt, output_filename):
file = pd.read_csv(filename, index_col=0)
results = []
with ThreadPoolExecutor(max_workers=10) as executor:
future_to_tweet = {
executor.submit(classify_example, system_prompt, row["tweet"]): row.name
for index, row in file.iterrows()
}
for future in tqdm(as_completed(future_to_tweet), total=len(file)):
original_index = future_to_tweet[future]
try:
prediction = future.result()
results.append(
{"index": original_index, "prediction": prediction}
)
except Exception as exc:
print(f"Tweet at index {original_index} generated an exception: {exc}")
results = sorted(results, key=lambda x: x["index"])
with open(output_filename, "w") as outfile:
for result in results:
outfile.write(json.dumps(result) + "\n")
def read_true_labels(training_set_filename):
training_set = pd.read_csv(training_set_filename, index_col="index")
return training_set["label"]
def calculate_f1_score(predictions_filename, training_set_filename):
true_labels = read_true_labels(training_set_filename)
with open(predictions_filename, "r") as file:
predictions = json.load(file)
predicted_labels = {item["index"]: item["prediction"] for item in predictions}
aligned_predictions = [predicted_labels.get(idx, 0) for idx in true_labels.index]
return f1_score(true_labels, aligned_predictions)
def find_patterns_in_dataset():
file = pd.read_csv("SubTask-A-train.csv")
hate_speech_texts = set(file[file["label"] == 1]["tweet"])
non_hate_speech_texts = set(file[file["label"] == 0]["tweet"])
random.shuffle(list(hate_speech_texts))
random.shuffle(list(non_hate_speech_texts))
n_examples = 30
# hate_speech_texts_without_greta = [text for text in hate_speech_texts if "You've been fooled by Greta" not in text]
hate_speech_texts_without_greta_formatted = ""
for idx, text in enumerate(list(hate_speech_texts)[:n_examples]):
hate_speech_texts_without_greta_formatted += f"{idx + 1}. {text}\n---\n"
non_hate_speech_texts_formatted = ""
for idx, text in enumerate(list(non_hate_speech_texts)[:n_examples]):
non_hate_speech_texts_formatted += f"{idx + 1}. {text}\n---\n"
all_texts_formatted = ""
all_texts_formatted += (
f"\n\n>>>> Hate speech:\n{hate_speech_texts_without_greta_formatted}\n---\n"
)
all_texts_formatted += (
f"\n\n>>>> Non-hate speech:\n{non_hate_speech_texts_formatted}\n---\n"
)
system_prompt = f"""You will be given {n_examples} tweets that were classified as hate speech. Your task is to find a
common " "pattern these texts share and figure out why they were classified as hate speech. For a good "
"comparison, I will also send you {n_examples} non-hate speech tweets so you have something to compare it to.
Since these are tweets, focus on hashtags (#)."""
completion = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": all_texts_formatted},
],
)
return completion.choices[0].message.content
if __name__ == "__main__":
# reasoning = find_patterns_in_dataset()
# print(reasoning)
classify_test_set_parallel(
"SubTask-B(index,tweet)test.csv", SYSTEM_PROMPT,
"test_set_predictions_b.jsonl"
) # Generates json ready for submission
# classify_test_set_parallel("SubTask-A-train.csv", SYSTEM_PROMPT)
# f1 = calculate_f1_score('test_set_predictions.json', 'SubTask-A-train.csv')
# print(f"F1 Score: {f1}")