Kiểm duyệt

Phát triển hệ thống AI với OpenAI API

Francesca Donadoni

Curriculum Manager, DataCamp

Hiểu kiểm duyệt trong OpenAI API

  • Moderation: quy trình phân tích đầu vào để xác định có nội dung vi phạm chính sách/hướng dẫn hay không Sơ đồ: thông điệp người dùng đi vào OpenAI moderation API và trả về xác suất thuộc các danh mục nội dung độc hại
Phát triển hệ thống AI với OpenAI API

Hiểu kiểm duyệt trong OpenAI API

Sơ đồ: thông điệp người dùng vào OpenAI moderation API và phản hồi liệt kê các danh mục nội dung độc hại được xét

Phát triển hệ thống AI với OpenAI API

Kiểm duyệt nội dung

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
Phát triển hệ thống AI với OpenAI API

Kiểm duyệt theo ngữ cảnh

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
Phát triển hệ thống AI với OpenAI API

Prompt injection

Một phụ nữ dùng chatbot với prompt độc hại bị chèn vào

Phát triển hệ thống AI với OpenAI API

Prompt injection

 

  • Giới hạn lượng văn bản trong prompt
  • Giới hạn số token đầu ra tạo ra
  • Dùng nội dung đã chọn trước làm đầu vào và đầu ra đã kiểm định
Phát triển hệ thống AI với OpenAI API

Thêm 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},]
Phát triển hệ thống AI với OpenAI API

Thêm 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.
Phát triển hệ thống AI với OpenAI API

Let's practice!

Phát triển hệ thống AI với OpenAI API

Preparing Video For Download...