Skip to content

Commit 899e78d

Browse files
authored
Merge pull request #6706 from akatsoulas/create-spam-flag-llm
Create a spam flag on automatic spam classificaiton
2 parents 5dcca80 + c5221ed commit 899e78d

File tree

2 files changed

+38
-21
lines changed

2 files changed

+38
-21
lines changed

kitsune/llm/tasks.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import waffle
22
from celery import shared_task
33

4+
from kitsune.flagit.models import FlaggedObject
45
from kitsune.llm.questions.classifiers import classify_question
56
from kitsune.users.models import Profile
67

@@ -25,6 +26,8 @@ def question_classifier(question_id):
2526
elif waffle.switch_is_active("flagit-spam-autoflag"):
2627
flag_question(
2728
question,
29+
reason=FlaggedObject.REASON_CONTENT_MODERATION,
30+
status=FlaggedObject.FLAG_PENDING,
2831
by_user=Profile.get_sumo_bot(),
2932
notes=(
3033
"Automatically flagged for topic moderation:"

kitsune/questions/utils.py

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ def flag_question(
150150
question: Question,
151151
by_user: User,
152152
notes: str,
153-
status: int = FlaggedObject.FLAG_PENDING,
154-
reason: str = FlaggedObject.REASON_CONTENT_MODERATION,
153+
status: int = FlaggedObject.FLAG_ACCEPTED,
154+
reason: str = FlaggedObject.REASON_SPAM,
155155
) -> None:
156156
content_type = ContentType.objects.get_for_model(question)
157157
flagged_object, created = FlaggedObject.objects.get_or_create(
@@ -214,17 +214,6 @@ def update_question_fields_from_classification(question, result, sumo_bot):
214214
question.tags.clear()
215215
question.auto_tag()
216216

217-
if topic := update_fields.get("topic"):
218-
flag_question(
219-
question,
220-
by_user=sumo_bot,
221-
notes=(
222-
f"LLM classified as {topic.title}, for the following reason:\n"
223-
f"{topic_result.get('reason', '')}"
224-
),
225-
status=FlaggedObject.FLAG_ACCEPTED,
226-
)
227-
228217

229218
def process_classification_result(
230219
question: Question,
@@ -236,19 +225,44 @@ def process_classification_result(
236225
"""
237226
sumo_bot = Profile.get_sumo_bot()
238227
action = result.get("action")
228+
flag_kwargs = {
229+
"by_user": sumo_bot,
230+
"notes": "",
231+
"question": question,
232+
}
239233

240234
match action:
241235
case ModerationAction.SPAM:
236+
flag_kwargs.update(
237+
{
238+
"notes": (
239+
f"LLM classified as spam, for the following reason:\n"
240+
f"{result.get('spam_result', {}).get('reason', '')}"
241+
),
242+
}
243+
)
242244
question.mark_as_spam(sumo_bot)
243245
case ModerationAction.FLAG_REVIEW:
244-
flag_question(
245-
question,
246-
by_user=sumo_bot,
247-
notes=(
248-
"LLM flagged for manual review, for the following reason:\n"
249-
f"{result.get('spam_result', {}).get('reason', '')}"
250-
),
251-
reason=FlaggedObject.REASON_SPAM,
246+
flag_kwargs.update(
247+
{
248+
"status": FlaggedObject.FLAG_PENDING,
249+
"notes": (
250+
f"LLM flagged for manual review, for the following reason:\n"
251+
f"{result.get('spam_result', {}).get('reason', '')}"
252+
),
253+
}
252254
)
253255
case _:
256+
flag_kwargs.update(
257+
{
258+
"reason": FlaggedObject.REASON_CONTENT_MODERATION,
259+
"notes": (
260+
f"LLM classified as {result.get('topic_result', {}).get('topic', '')}, "
261+
f"for the following reason:\n"
262+
f"{result.get('topic_result', {}).get('reason', '')}"
263+
),
264+
}
265+
)
254266
update_question_fields_from_classification(question, result, sumo_bot)
267+
268+
flag_question(**flag_kwargs)

0 commit comments

Comments
 (0)