Rename to 57, extract content check into helper method

This commit is contained in:
James Hush
2026-03-05 11:14:36 +08:00
parent 218ab01070
commit 7d957292e0

View File

@@ -41,8 +41,6 @@ from pipecat.transports.websocket.fastapi import FastAPIWebsocketParams
load_dotenv(override=True) load_dotenv(override=True)
FILTERED_WORDS = ["apple", "banana", "car"]
@dataclass @dataclass
class ContentApprovedFrame(ControlFrame): class ContentApprovedFrame(ControlFrame):
@@ -58,6 +56,9 @@ class ContentRejectedFrame(ControlFrame):
pass pass
FILTERED_WORDS = ["apple", "banana", "car"]
class ContentFilterProcessor(FrameProcessor): class ContentFilterProcessor(FrameProcessor):
"""Checks LLMContextFrames for filtered words and emits signal frames. """Checks LLMContextFrames for filtered words and emits signal frames.
@@ -66,12 +67,9 @@ class ContentFilterProcessor(FrameProcessor):
whether to let the LLM's output through. whether to let the LLM's output through.
""" """
async def process_frame(self, frame: Frame, direction: FrameDirection): def _contains_filtered_words(self, context: "LLMContext") -> bool:
await super().process_frame(frame, direction) """Check if the last message in the context contains any filtered words."""
messages = context.messages
if isinstance(frame, LLMContextFrame):
# Check the last user message for filtered words
messages = frame.context.messages
if messages: if messages:
last_message = messages[-1] last_message = messages[-1]
content = last_message.get("content", "") if isinstance(last_message, dict) else "" content = last_message.get("content", "") if isinstance(last_message, dict) else ""
@@ -79,10 +77,16 @@ class ContentFilterProcessor(FrameProcessor):
content_lower = content.lower() content_lower = content.lower()
if any(word in content_lower for word in FILTERED_WORDS): if any(word in content_lower for word in FILTERED_WORDS):
logger.info(f"Filtered content detected: {content}") logger.info(f"Filtered content detected: {content}")
await self.push_frame(ContentRejectedFrame(), direction) return True
return return False
# Content is clean — approve it async def process_frame(self, frame: Frame, direction: FrameDirection):
await super().process_frame(frame, direction)
if isinstance(frame, LLMContextFrame):
if self._contains_filtered_words(frame.context):
await self.push_frame(ContentRejectedFrame(), direction)
else:
await self.push_frame(ContentApprovedFrame(), direction) await self.push_frame(ContentApprovedFrame(), direction)
return return