-
Notifications
You must be signed in to change notification settings - Fork 2
/
transform.py
39 lines (33 loc) · 1.35 KB
/
transform.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
import os
import openai
def handler(data, log):
content = data['message']
is_spam = detect_spam_openai(content)
if is_spam == 1:
data['spam_detection_openai'] = "spam"
else:
data['spam_detection_openai'] = "not_spam"
return data
def detect_spam_openai(message):
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
response = openai.chat.completions.create(
model="gpt-4o",
response_format={"type": "text"},
messages=[
{
"role": "system",
"content": """Your task is to act as a content moderator. Review the following message from a chat user and classify it based on the criteria below:
1 - Spam: Unsolicited promotions, advertisements, or deceptive content. Note that messages with typos or about typical topics seen in spam messages are not necessarily spam.
0 - Non-Spam: Legitimate conversations or unclear cases.
Return only the corresponding number (1 or 0). Any additional information will result in penalties.
Ensure your judgment is unbiased and consistent with the criteria. Think step-by-step to make an accurate classification.
"""},
{
"role": "user",
"content": f"{message}"
}
],
max_tokens=1,
temperature=0.5,
)
return int(response.choices[0].message.content)