Moderacja

Tworzenie systemów AI z OpenAI API

Francesca Donadoni

Curriculum Manager, DataCamp

Moderacja w API OpenAI

  • Moderacja: proces analizowania danych wejściowych w celu wykrycia treści naruszających zdefiniowane zasady lub wytyczne Diagram przedstawiający wiadomość użytkownika odczytywaną przez API moderacji OpenAI, które zwraca prawdopodobieństwa przynależności wiadomości do poszczególnych kategorii złośliwych treści
Tworzenie systemów AI z OpenAI API

Moderacja w API OpenAI

Diagram przedstawiający wiadomość użytkownika odczytywaną przez API moderacji OpenAI, które zwraca odpowiedź z listą rozpatrywanych kategorii złośliwych treści

Tworzenie systemów AI z OpenAI API

Moderowanie treści

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
Tworzenie systemów AI z OpenAI API

Moderacja w kontekście

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
Tworzenie systemów AI z OpenAI API

Wstrzykiwanie promptów

Kobieta korzystająca z chatbota, do którego wstrzyknięto złośliwy prompt

Tworzenie systemów AI z OpenAI API

Wstrzykiwanie promptów

 

  • Ograniczanie ilości tekstu w promptach
  • Ograniczanie liczby generowanych tokenów wyjściowych
  • Używanie wstępnie wybranych treści jako zweryfikowanego wejścia i wyjścia
Tworzenie systemów AI z OpenAI API

Dodawanie zabezpieczeń

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},]
Tworzenie systemów AI z OpenAI API

Dodawanie zabezpieczeń

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

print(response.choices[0].message.content)
Apologies, but the topic is not allowed.
Tworzenie systemów AI z OpenAI API

Czas na ćwiczenia!

Tworzenie systemów AI z OpenAI API

Preparing Video For Download...