Skip to content

Commit

Permalink
Add message moderation in handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
Flagro committed Nov 6, 2024
1 parent 9117b76 commit f9347c0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
14 changes: 14 additions & 0 deletions bot/models/base_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ async def get_localized_text(
async def initialize_context(self, person: Person, context: Context) -> None:
raise NotImplementedError

@abstractmethod
async def moderate_message(self, message: Message) -> bool:
raise NotImplementedError

@property
def permissions(self) -> List[BasePermission]:
return self._initialized_permissions
Expand Down Expand Up @@ -90,6 +94,10 @@ async def handle(
return self.get_localized_response(
CommandResponse(text="not_authenticated"), context
)
if not await self.moderate_message(message):
return self.get_localized_response(
CommandResponse(text="message_moderation_failed"), context
)
await self._log_handler_request(person, context, message, args)
response = await self.get_response(person, context, message, args)
if response is None:
Expand All @@ -106,6 +114,12 @@ async def stream_handle(
context,
)
return
if not await self.moderate_message(message):
yield self.get_localized_response(
CommandResponse(text="message_moderation_failed"),
context,
)
return
await self._log_handler_request(person, context, message, args)
async for chunk in self.stream_get_response(person, context, message, args):
if chunk is None:
Expand Down
6 changes: 5 additions & 1 deletion bot/rp_bot/rp_bot_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from .db import DB
from ai_agent.ai import AI
from ai_agent.moderation import moderate_user_message
from .prompt_manager import PromptManager
from .auth import Auth
from .localizer import Localizer
Expand All @@ -13,7 +14,7 @@
BaseMessageHandler,
)
from ..models.base_auth import BasePermission
from ..models.handlers_input import Person, Context
from ..models.handlers_input import Person, Context, Message


class RPBotHandlerMixin(ABC):
Expand Down Expand Up @@ -47,6 +48,9 @@ def get_initialized_permissions(self) -> List[BasePermission]:
result.append(permission_class(self.auth))
return result

async def moderate_message(self, message: Message) -> bool:
return moderate_user_message(message)

async def get_localized_text(
self, text: str, kwargs: Optional[dict] = None, context: Optional[Context] = None
) -> Optional[str]:
Expand Down
2 changes: 2 additions & 0 deletions config/localizer_translations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ LocalizerTranslations:
english: "{response_text}"
streaming_message_response:
english: "{response_text}"
message_moderation_failed:
english: "Message moderation failed"
usage_limit_exceeded:
english: "Usage limit exceeded for {user_handle} with {user_limit} limit"
content_not_acceptable:
Expand Down

0 comments on commit f9347c0

Please sign in to comment.