← Back to docs

Recipe: Block keywords, profanity, and off-topic

Power user

Recipe: Block keywords and stay on-topic

Sometimes you want to refuse to engage with certain content rather than letting the LLM improvise. Rules are the right place.

Block profanity

PROFANITY = {"badword1", "badword2", "badword3"}  # add your own

def respond(message, context):
    msg = message.lower()
    if any(w in msg for w in PROFANITY):
        return "I'd rather not engage with that kind of language. Could you rephrase your question?"
    return None

Block competitor questions

If a visitor asks "is your product better than CompetitorX?" and you don't want the LLM to answer, intercept it:

COMPETITORS = {"competitorx", "competitory", "rival co"}

def respond(message, context):
    msg = message.lower()
    if any(c in msg for c in COMPETITORS):
        return ("I'm not going to compare us to other tools — that's for you to decide. "
                "Happy to talk about what we do well, though. What matters most to you?")
    return None

Stay strictly on-topic

For very narrow bots (e.g. a bot for a plumbing company shouldn't answer questions about cooking recipes):

OFF_TOPIC_HINTS = {
    "cook", "recipe", "weather", "football", "cricket",
    "politics", "election", "celebrity", "movie",
}

def respond(message, context):
    msg = message.lower()
    if any(h in msg for h in OFF_TOPIC_HINTS):
        return ("I can only help with plumbing, heating, and our services. "
                "Is there something I can help you with on those?")
    return None

Block prompt-injection attempts

Visitors occasionally try to "jailbreak" the bot. Catch the obvious ones:

INJECTION_PHRASES = [
    "ignore previous instructions",
    "ignore your prompt",
    "you are now",
    "pretend you are",
    "system prompt",
]

def respond(message, context):
    msg = message.lower()
    for phrase in INJECTION_PHRASES:
        if phrase in msg:
            return "I can only help with questions about our company and services."
    return None

Order matters

Rules run top to bottom. Put your safety blocks at the top of rules.py so they always fire first. Then FAQ shortcuts. Then anything else.