การกลั่นกรองเนื้อหา

การพัฒนาระบบ AI ด้วย OpenAI API

Francesca Donadoni

Curriculum Manager, DataCamp

ทำความเข้าใจการกลั่นกรองเนื้อหาใน OpenAI API

  • การกลั่นกรองเนื้อหา (Moderation): กระบวนการวิเคราะห์ข้อความนำเข้าเพื่อตรวจสอบว่ามีเนื้อหาที่ละเมิดนโยบายหรือแนวทางที่กำหนดไว้หรือไม่ แผนภาพแสดงข้อความจากผู้ใช้ที่ถูกอ่านโดย 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

ภาพผู้หญิงกำลังใช้งานแชทบอทโดยมีการแทรก prompt อันตราย

การพัฒนาระบบ AI ด้วย OpenAI API

Prompt injection

 

  • จำกัดจำนวนข้อความใน prompt
  • จำกัดจำนวนoutput token ที่สร้างขึ้น
  • ใช้เนื้อหาที่คัดสรรไว้ล่วงหน้าเป็น input และ output ที่ผ่านการตรวจสอบแล้ว
การพัฒนาระบบ AI ด้วย OpenAI API

การเพิ่ม guardrail

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

การเพิ่ม guardrail

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