File size: 1,140 Bytes
f41ca4f
 
 
 
 
 
 
 
 
 
 
0aa8e87
f41ca4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from langchain_openai import ChatOpenAI
from langchain_core.prompts import PromptTemplate
from src.agents.CallState import CallState

class ModerationAgent:
    """
    Agent that uses LLM to detect and redact obscene words, replacing them with ***.
    Updates the clean_content in the state.
    """

    def __init__(self):
        self.llm = ChatOpenAI(model="gpt-4o", temperature=0)

    def __call__(self, state: CallState) -> CallState:
        text = state.get("clean_content") or state.get("content") or ""
        if not text:
            return state

        prompt = PromptTemplate.from_template(
            "You are a moderation assistant. Review the following text. "
            "If you find any obscene words, profanity, or inappropriate language, replace them entirely with ***. "
            "Do not change any other words. Return only the redacted text.\n\n"
            "Text:\n{text}"
        )
        
        chain = prompt | self.llm
        result = chain.invoke({"text": text})
        
        # Save the redacted text back to clean_content
        state["clean_content"] = result.content
        return state