モデレーション

OpenAI API を使った AI システムの開発

Francesca Donadoni

Curriculum Manager, DataCamp

OpenAI API におけるモデレーションを理解する

  • モデレーション:入力を分析し、事前に定められた方針やガイドラインに違反する内容が含まれているかを判断するプロセス OpenAIのモデレーションAPIが入力されたユーザーメッセージを読み取り、その応答としてユーザーメッセージが各種の悪意あるコンテンツカテゴリに属する確率を出力する図
OpenAI API を使った AI システムの開発

OpenAI API におけるモデレーションを理解する

OpenAI moderation 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...