To prevent a Language Learning Model (LLM) from replying to inappropriate prompts, you can implement a content moderation layer before the LLM processes the input. This layer can filter out inappropriate content based on a predefined list of inappropriate words or phrases. Here's a simple Python script that demonstrates this:
import re
class ContentModerator:
def __init__(self):
self.inappropriate_words = ['badword1', 'badword2', 'badword3'] # Add more inappropriate words here
def is_appropriate(self, text):
for word in self.inappropriate_words:
if re.search(r'\b' + word + r'\b', text):
return False
return True
class LLM:
def __init__(self, content_moderator):
self.content_moderator = content_moderator
def respond_to_prompt(self, prompt):
if not self.content_moderator.is_appropriate(prompt):
return "I'm sorry, but I can't assist with that."
else:
# Process the prompt and generate a response
response = "This is a placeholder response." # Replace this with actual LLM response
return response
content_moderator = ContentModerator()
llm = LLM(content_moderator)
prompt = "User's prompt goes here"
print(llm.respond_to_prompt(prompt))
In this script, the ContentModerator class has a method is_appropriate that checks if a text contains any inappropriate words. The LLM class uses an instance of ContentModerator to check if a prompt is appropriate before processing it. If the prompt is not appropriate, the LLM returns a message indicating that it can't assist with the prompt.
This is a very basic implementation and might not catch all inappropriate content. For a more robust solution, you might want to use a more sophisticated content moderation system that can detect and filter out not only inappropriate words but also other types of inappropriate content.