datadabllp

implement some basic safety checks

Aug 20th, 2024
623
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.97 KB | None | 0 0
  1. import re
  2.  
  3. def contains_profanity(text):
  4.     profanity_list = ["bad_word1", "bad_word2", "bad_word3"]  # Simplified list
  5.     return any(word in text.lower() for word in profanity_list)
  6.  
  7. def contains_personal_info(text):
  8.     # Check for patterns that might indicate personal information
  9.     email_pattern = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
  10.     phone_pattern = r'\b\d{3}[-.]?\d{3}[-.]?\d{4}\b'
  11.    
  12.     return bool(re.search(email_pattern, text) or re.search(phone_pattern, text))
  13.  
  14. def safe_generate(prompt):
  15.     response = llm.generate(prompt)
  16.    
  17.     if contains_profanity(response):
  18.         return "I apologize, but I can't provide a response containing inappropriate language."
  19.    
  20.     if contains_personal_info(response):
  21.         return "I'm sorry, but I can't include personal information in my responses."
  22.    
  23.     return response
  24.  
  25. # Usage
  26. print(safe_generate("Tell me a funny joke."))
  27. print(safe_generate("What's your email address?"))
  28.  
Advertisement
Add Comment
Please, Sign In to add comment