-
Notifications
You must be signed in to change notification settings - Fork 1
/
SubTaskA-rag.py
226 lines (171 loc) · 8.69 KB
/
SubTaskA-rag.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
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
random.seed(42)
SYSTEM_PROMPT = """
Analyze the input tweet to determine if it is hate speech or not, based on the following criteria:
## Hate Speech Patterns
1. Presence of "You've been fooled by Greta Thunberg" or #FridaysForFuture in the tweet.
2. Embodies aggression or contempt towards specific groups or institutions, including dismissive attitudes towards climate activists, criticism of world leaders for climate inaction, or strong sentiments against companies investing in fossil fuels.
3. Frequent use of negative language, such as 'shame', 'lie', 'greedy', 'fake', 'idiot', to express dissatisfaction or attack others.
4. Highlights a strong ideological alignment or belief, often against fossil fuels and blaming capitalism for the climate crisis, indicating belief-driven intolerance.
5. The tone is accusatory, confrontational, and not oriented towards dialogue or understanding.
## Non-Hate Speech Patterns
1. Expresses concern about climate change and promotes action without aggression or contempt. Advocates for policy changes, shares environmental information, and encourages collective action rather than targeting individuals or groups.
2. Lacks negative language or personal attacks.
3. Presents a clear ideological stance on climate change in a constructive or informative manner, aiming to educate or raise awareness rather than cast blame.
4. The tone is conversational and informative, promoting understanding and engagement rather than confrontation.
## Evaluation
- If the tweet aligns more with the Hate Speech Patterns, output: 'Prediction: 1' (indicating it is hate speech).
- If the tweet aligns more with the Non-Hate Speech Patterns, output: 'Prediction: 0' (indicating it is not hate speech).
## 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.1,
)
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) -> bool:
hate_answer_options = [
"Prediction: 1",
"'Prediction: 1'",
"'Prediction: 1'.",
"Prediction: 1 (indicating it is hate speech).",
"'Prediction: 1' (indicating it is hate speech).",
]
non_hate_answer_options = [
"Prediction: 0",
"'Prediction: 0'",
"'Prediction: 0'.",
"Prediction: 0 (indicating it is not hate speech).",
"'Prediction: 0' (indicating it is not hate speech).",
]
if any(completion.endswith(option) for option in hate_answer_options):
return True
elif any(completion.endswith(option) for option in non_hate_answer_options):
return False
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_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)
embedding_fn = embedding_functions.SentenceTransformerEmbeddingFunction(model_name=embedding_model)
results = collection.query(query_embeddings=embedding_fn([user_prompt]), n_results=8)
examples = ""
for text, metadata in zip(*results['documents'], *results['metadatas']):
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):
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": 1 if prediction else 0}
)
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("test_set_predictions.jsonl", "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-A-(index,tweet)test.csv", SYSTEM_PROMPT
) # 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}")