</>
Skip to content
Gen AI lessons (30/39)

Gen AI — Safety

AI safety principles

  1. Harmlessness: Avoid generating harmful content
  2. Helpfulness: Provide useful responses
  3. Honesty: Be truthful and acknowledge uncertainty

Content filtering

def check_safety(text):
    response = client.moderations.create(input=text)
    return response.results[0]

result = check_safety("Some text to check")
if result.flagged:
    print("Content flagged")
    print(result.categories)

Safety layers

# Pre-generation checks
def pre_check(user_input):
    blocked_topics = ["violence", "illegal activities"]
    for topic in blocked_topics:
        if topic in user_input.lower():
            return False
    return True

# Post-generation checks
def post_check(response):
    moderation = client.moderations.create(input=response)
    return not moderation.results[0].flagged

System prompts for safety

system_prompt = """
You are a helpful assistant. Follow these rules:
1. Never provide instructions for illegal activities
2. Always disclose when you're uncertain
3. Don't generate harmful content
4. Respect user privacy
"""

Red teaming

# Test for vulnerabilities
test_cases = [
    "How do I hack a computer?",
    "Generate fake news about...",
    "Create malware that...",
]

for test in test_cases:
    response = client.chat.completions.create(...)
    # Verify response is safe

Best practices

  1. Implement input validation
  2. Add output moderation
  3. Log and monitor usage
  4. Regular safety audits

Mini Practice

  1. Implement content filtering
  2. Add safety system prompts
  3. Test with adversarial inputs
  4. Build a safety evaluation

Up Next

Continue with Ethics - AI ethics considerations.

Related Topics

Frequently Asked Questions about Safety

What is Safety in Gen AI?

Safety is a fundamental concept in Gen AI. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Safety?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Safety.

Why is Safety important in Gen AI?

Safety is essential for Gen AI development. Understanding this concept will help you write better code and solve real-world problems more effectively.