Модерація

Розроблення AI‑систем з OpenAI API

Francesca Donadoni

Curriculum Manager, DataCamp

Розуміння модерації в OpenAI API

  • Модерація: процес аналізу введення, щоб визначити, чи містить воно контент, що порушує заздалегідь визначені політики або настанови Схема: вхідне повідомлення користувача обробляє OpenAI moderation API та повертає ймовірності належності повідомлення до зловмисних категорій контенту
Розроблення AI‑систем з OpenAI API

Розуміння модерації в OpenAI API

Схема: вхідне повідомлення користувача обробляє OpenAI moderation API і повертає відповідь зі списком розглянутих зловмисних категорій контенту

Розроблення AI‑систем з OpenAI API

Модерація контенту

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‑систем з OpenAI API

Модерація з урахуванням контексту

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‑систем з OpenAI API

Prompt injection

Жінка користується чатботом; уводиться зловмисний підказ

Розроблення AI‑систем з OpenAI API

Prompt injection

 

  • Обмежуйте обсяг тексту у підказках
  • Обмежуйте кількість згенерованих вихідних токенів
  • Використовуйте попередньо відібраний контент як перевірені вхідні та вихідні дані
Розроблення AI‑систем з OpenAI API

Додаємо запобіжники

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‑систем з OpenAI API

Додаємо запобіжники

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‑систем з OpenAI API

Давайте потренуємось!

Розроблення AI‑систем з OpenAI API

Preparing Video For Download...