feat: AI moderation feature - #288
Open
Alam-2U wants to merge 1 commit into
Open
Conversation
Alam-2U
force-pushed
the
feat/ai-moderation
branch
from
August 28, 2026 06:14
0fb6b6e to
cea3944
Compare
Alam-2U
marked this pull request as ready for review
August 28, 2026 07:17
This was referenced Aug 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Adds provider-agnostic AI moderation to forum-v2.
New threads and comments are classified by a configured AI backend. Content classified as spam is flagged, optionally deleted, and recorded in an audit log.
Forum provides the moderation interface and workflow, but does not include a specific AI provider.
AI_MODERATION_BACKENDhas no default; deployments configure the backend they want to use, and each backend handles its own provider-specific credentials and model settings.What's Included
Moderation Pipeline
Located under
forum/ai_moderation/:backends/base.py— AddsBaseModerationBackendas the backend interface andHTTPModerationBackendas a reusable base for JSON-over-HTTP providers.classify()returns{classification, reasoning, confidence_score, full_api_response}orNone. Classification failures never raise exceptions, allowing posting to continue if moderation is unavailable.service.py— Runs the moderation workflow:classify → flag → optionally delete → audit-log. Spam verdicts are cached using a content hash to avoid repeated API calls for identical content. Clean verdicts are not cached.defaults.py— Provides provider-independent defaults, including a ready-to-use spam classification prompt.Hook Points
The following operations trigger moderation after successful content creation:
create_thread,create_parent_comment,create_child_comment.Moderation runs inside a broad
try/except. Any moderation failure is logged and does not cause the original post/comment creation to fail.Data Model
Migration
0006adds:is_spamboolean field toCommentThreadandComment, with indexes onis_spam,(course_id, is_spam), and(author, course_id, is_spam).ModerationAuditLogcontaining content body, full classifier response, reasoning, classification, actions taken, confidence score, original author,moderator_override, andoverride_reason.flag_content_as_spamandunflag_content_as_spambackend methods.Flagged content is attributed to the configured AI moderation user through
abuse_flaggers, allowing it to surface through the existing report path.Admin
Adds
is_spamto thread and comment list displays and filters. Adds a read-onlyModerationAuditLogadmin with search across author, moderator, reasoning, and body.Documentation
Adds
docs/how-tos/configure_ai_moderation.rst, covering backend implementation, required and optional settings, and enabling the course-level waffle flags.Configuration
Required
AI_MODERATION_BACKENDAI_MODERATION_API_URLAI_MODERATION_USER_IDOptional
AI_MODERATION_SYSTEM_MESSAGE,AI_MODERATION_CONNECTION_TIMEOUT(1.0s),AI_MODERATION_READ_TIMEOUT(30s),AI_MODERATION_FLAGGED_CACHE_TTL(24h),AI_MODERATION_FLAGGED_CACHE_PREFIX.Course Waffle Flags
Both flags are off by default:
discussions.enable_ai_moderation,discussions.enable_ai_auto_delete_spam.Testing
Added and updated tests covering service behaviour, caching, flagging, deletion, audit logging, failure handling, backend interface, and HTTP backend.
tests/test_ai_moderation.pytests/test_ai_moderation_backends.pytest_utils/moderation.pytests/test_backends/test_mysql/test_api.pyandtests/test_backends/test_mysql/test_models.pyRollout
The feature has zero impact when unconfigured. With no
AI_MODERATION_BACKENDconfigured and both waffle flags disabled, no moderation calls are made and existing forum behaviour remains unchanged.AI moderation can be enabled on a per-course basis.
Related PRs