モデレーション

OpenAI API で開発する AI システム

Francesca Donadoni

Curriculum Manager, DataCamp

OpenAI APIのモデレーションを理解する

  • モデレーション: 入力内容が事前定義されたポリシーやガイドラインに違反するかどうかを分析するプロセス ユーザーメッセージをOpenAIモデレーションAPIが読み取り、悪意あるコンテンツカテゴリに属する確率をレスポンスとして出力する図
OpenAI API で開発する AI システム

OpenAI APIのモデレーションを理解する

ユーザーメッセージをOpenAIモデレーションAPIが読み取り、対象となる悪意あるコンテンツカテゴリの一覧をレスポンスとして出力する図

OpenAI API で開発する AI システム

コンテンツのモデレーション

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
OpenAI API で開発する AI システム

文脈に応じたモデレーション

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
OpenAI API で開発する AI システム

プロンプトインジェクション

悪意のあるプロンプトが注入されたチャットボットを使用する女性

OpenAI API で開発する AI システム

プロンプトインジェクション

 

  • プロンプト内のテキスト量制限する
  • 生成される出力トークン数制限する
  • 事前選定済みコンテンツを検証済みの入出力として使用する
OpenAI API で開発する AI システム

ガードレールの追加

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},]
OpenAI API で開発する AI システム

ガードレールの追加

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

print(response.choices[0].message.content)
Apologies, but the topic is not allowed.
OpenAI API で開発する AI システム

では、練習しましょう!

OpenAI API で開発する AI システム

Preparing Video For Download...