Moderatie

AI-systemen ontwikkelen met de OpenAI API

Francesca Donadoni

Curriculum Manager, DataCamp

Moderatie met de OpenAI‑API begrijpen

  • Moderatie: het analyseren van input om te bepalen of die beleid of richtlijnen schendt Een diagram met een gebruikersbericht dat door de OpenAI-moderatie‑API wordt gelezen en als antwoord de kansen geeft dat het bericht in kwaadaardige categorieën valt
AI-systemen ontwikkelen met de OpenAI API

Moderatie met de OpenAI‑API begrijpen

Een diagram met een gebruikersbericht dat door de OpenAI-moderatie‑API wordt gelezen en als antwoord een lijst met beschouwde kwaadaardige categorieën geeft

AI-systemen ontwikkelen met de OpenAI API

Content modereren

moderation_response = client.moderations.create(input="""
...until someone draws an Exploding Kitten.
When that happens, that person explodes. They are now dead.
This process continues until...
""") 

print(moderation_response.results[0].categories.violence)
True
1 https://ek.explodingkittens.com/how-to-play/exploding-kittens
AI-systemen ontwikkelen met de OpenAI API

Moderatie in context

moderation_response = client.moderations.create(input="""
In the deck of cards are some Exploding Kittens. You play the game by putting the deck face down and taking turns drawing cards until someone draws an Exploding Kitten.
When that happens, that person explodes. They are now dead.
This process continues until there’s only 1 player left, who wins the game.
The more cards you draw, the greater your chances of drawing an Exploding Kitten.
""") 

moderation_response.results[0].categories.violence
False
AI-systemen ontwikkelen met de OpenAI API

Promptinjectie

Een vrouw die een chatbot gebruikt met een kwaadaardige promptinjectie

AI-systemen ontwikkelen met de OpenAI API

Promptinjectie

 

  • Beperk de hoeveelheid tekst in prompts
  • Beperk het aantal uitvoertokens
  • Gebruik vooraf geselecteerde content als gevalideerde input en output
AI-systemen ontwikkelen met de OpenAI API

Guardrails toevoegen

user_request = """
In the deck of cards are some Exploding Kittens. You play the game by putting the 
deck face down and taking turns drawing cards until  someone draws an Exploding 
Kitten. When that happens, that person explodes. They are now dead.
This process continues until there’s only 1 player left, who wins the game.
The more cards you draw, the greater your chances of drawing an Exploding Kitten.
"""

messages = [{"role": "system", "content": "Your role is to assess whether the user question is allowed or not. The allowed topics are games of chess only. If the topic is allowed, reply with an answer as normal, otherwise say 'Apologies, but the topic is not_allowed.'",}, {"role": "user", "content": user_request},]
AI-systemen ontwikkelen met de OpenAI API

Guardrails toevoegen

response = client.chat.completions.create(
    model="gpt-4o-mini", 
    messages=messages
)

print(response.choices[0].message.content)
Apologies, but the topic is not allowed.
AI-systemen ontwikkelen met de OpenAI API

Laten we oefenen!

AI-systemen ontwikkelen met de OpenAI API

Preparing Video For Download...