访问我们的内容审核指导手册,查看使用Claude实现内容审核的示例。
本指南主要关注在您的应用程序中审核用户生成的内容。如果您正在寻找有关审核与Claude互动的指导,请参阅我们的
防护指南。
使用Claude构建前的准备
决定是否使用Claude进行内容审核
以下是一些关键指标,表明您应该使用像Claude这样的LLM而不是传统的ML或基于规则的方法进行内容审核:
Anthropic已经训练所有Claude模型做到诚实、有帮助和无害。这可能导致Claude会审核被认为特别危险的内容(符合我们的
可接受使用政策),无论使用什么提示。例如,一个想允许用户发布露骨性内容的成人网站可能会发现,即使在提示中指定不要审核露骨性内容,Claude仍然会将露骨内容标记为需要审核。我们建议在构建审核解决方案之前先查看我们的AUP。
生成需要审核的内容示例
在开发内容审核解决方案之前,首先创建应该被标记的内容示例和不应该被标记的内容示例。确保您包括边缘案例和可能难以被内容审核系统有效处理的具有挑战性的场景。之后,审查您的示例以创建明确定义的审核类别列表。
例如,社交媒体平台生成的示例可能包括以下内容:
有效审核这些示例需要对语言有细微的理解。在评论This movie was great, I really enjoyed it. The main actor really killed it!
中,内容审核系统需要认识到”killed it”是一个比喻,而不是表示实际的暴力行为。相反,尽管没有明确提及暴力,评论Delete this post now or you better hide. I am coming after you and your family.
应该被内容审核系统标记。
unsafe_categories
列表可以根据您的特定需求进行定制。例如,如果您希望防止未成年人在您的网站上创建内容,您可以在列表中添加”Underage Posting”。
如何使用Claude审核内容
选择合适的Claude模型
在选择模型时,考虑数据的大小很重要。如果成本是一个考虑因素,像Claude Haiku 3这样的较小模型是一个极好的选择,因为它具有成本效益。以下是对一个每月接收十亿帖子的社交媒体平台进行文本审核的成本估算:
-
内容大小
- 每月帖子数:10亿
- 每个帖子的字符数:100
- 总字符数:1000亿
-
估计的令牌数
- 输入令牌:286亿(假设每3.5个字符1个令牌)
- 被标记的消息百分比:3%
- 每个被标记消息的输出令牌:50
- 总输出令牌:15亿
-
Claude Haiku 3估计成本
- 输入令牌成本:2,860 MTok * $0.25/MTok = $715
- 输出令牌成本:1,500 MTok * $1.25/MTok = $1,875
- 月度成本:$715 + $1,875 = $2,590
-
Claude Sonnet 4估计成本
- 输入令牌成本:2,860 MTok * $3.00/MTok = $8,580
- 输出令牌成本:1,500 MTok * $15.00/MTok = $22,500
- 月度成本:$8,580 + $22,500 = $31,080
实际成本可能与这些估计不同。这些估计基于
批处理部分中突出显示的提示。通过从响应中删除
explanation
字段,可以进一步减少输出令牌。
构建强大的提示
为了使用Claude进行内容审核,Claude必须了解您应用程序的审核要求。让我们首先编写一个提示,让您定义您的审核需求:
在这个例子中,moderate_message
函数包含一个评估提示,其中包括不安全内容类别和我们希望评估的消息。提示要求Claude根据我们定义的不安全类别评估消息是否应该被审核。
然后解析模型的评估,以确定是否存在违规。如果存在违规,Claude还会返回被违反的类别列表,以及为什么该消息不安全的解释。
评估您的提示
内容审核是一个分类问题。因此,您可以使用我们的分类指导手册中概述的相同技术来确定内容审核系统的准确性。
另一个考虑因素是,您可以不将内容审核视为二元分类问题,而是创建多个类别来表示各种风险级别。创建多个风险级别允许您调整审核的严格程度。例如,您可能希望自动阻止被认为是高风险的用户查询,而具有许多中等风险查询的用户则被标记为需要人工审核。
这段代码实现了一个assess_risk_level
函数,该函数使用Claude评估消息的风险级别。该函数接受消息和不安全类别列表作为输入。
在函数内部,为Claude生成一个提示,包括要评估的消息、不安全类别和评估风险级别的具体指示。提示指示Claude以JSON对象的形式响应,该对象包括风险级别、违反的类别和可选的解释。
这种方法通过分配风险级别实现了灵活的内容审核。它可以无缝集成到更大的系统中,根据评估的风险级别自动过滤内容或标记评论以进行人工审核。例如,在执行此代码时,评论Delete this post now or you better hide. I am coming after you and your family.
由于其危险威胁而被识别为高风险。相反,评论Stay away from the 5G cellphones!! They are using 5G to control you.
被归类为中等风险。
部署您的提示
一旦您对解决方案的质量有信心,就该将其部署到生产环境中了。以下是在生产中使用内容审核时应遵循的一些最佳实践:
-
向用户提供清晰的反馈: 当用户输入因内容审核而被阻止或响应被标记时,提供信息丰富且具有建设性的反馈,帮助用户理解为什么他们的消息被标记以及如何适当地重新表述。在上面的编码示例中,这是通过Claude响应中的explanation
标签完成的。
-
分析被审核的内容: 跟踪被审核系统标记的内容类型,以识别趋势和潜在的改进领域。
-
持续评估和改进: 使用精确度和召回率跟踪等指标定期评估内容审核系统的性能。使用这些数据迭代地改进您的审核提示、关键词和评估标准。
提高性能
在复杂场景中,除了标准的提示工程技术外,考虑额外的策略来提高性能可能会有所帮助。以下是一些高级策略:
定义主题并提供示例
除了在提示中列出不安全类别外,还可以通过提供与每个类别相关的定义和短语来进一步改进。
import anthropic
import json
client = anthropic.Anthropic()
unsafe_category_definitions = {
'Child Exploitation': 'Content that depicts child nudity or that enables, encourages, excuses, or depicts the sexual abuse of children.',
'Conspiracy Theories': """Content that promotes or endorses unfounded, false, or misleading theories about events, situations, or individuals,
which can incite harm or undermine public trust in institutions or facts.""",
'Hate': """Content that is hateful toward people on the basis of their protected characteristics
(race, color, ethnicity, national origin, disability, religious affiliation, caste, sexual orientation, sex, gender identity and serious disease),
or content that perpetuates negative stereotypes.""",
'Indiscriminate Weapons': 'Content that enables the creation of weapons of mass destruction.',
'Intellectual Property': 'Content that violates the intellectual property rights of third parties.',
'Non-Violent Crimes': 'Content that enables, encourages, or excuses the commission of non-violent crimes.',
'Privacy': 'Content that contains sensitive, personal information about private individuals.',
'Self-Harm': 'Content that enables, encourages, or excuses acts of intentional self-harm.',
'Sex Crimes': 'Content that enables, encourages, or excuses the commission of sex crimes.',
'Sexual Content': 'Content that contains erotic descriptions or explicit depictions of sex acts.',
'Specialized Advice': """Content that contains financial, medical, or legal advice.
Financial advice includes guidance on investments, stocks, bonds, or any financial planning.""",
'Violent Crimes': 'Content that enables, encourages, or excuses the commission of violent crimes.',
}
def moderate_message_with_definitions(message, unsafe_category_definitions):
unsafe_category_str = '\n'.join(f'{category}: {definition}'
for category, definition in unsafe_category_definitions.items()
)
assessment_prompt = f"""Determine whether the following message warrants moderation, based on the unsafe categories outlined below.
Message:
<message>{message}</message>
Unsafe Categories and Their Definitions:
<categories>
{unsafe_category_str}
</categories>
It's important that you remember all unsafe categories and their definitions.
Respond with ONLY a JSON object, using the format below:
{{
"violation": <Boolean field denoting whether the message should be moderated>,
"categories": [Comma-separated list of violated categories],
"explanation": [Optional. Only include if there is a violation.]
}}"""
response = client.messages.create(
model="claude-3-haiku-20240307",
max_tokens=200,
temperature=0,
messages=[
{"role": "user", "content": assessment_prompt}
]
)
assessment = json.loads(response.content[0].text)
contains_violation = assessment['violation']
violated_categories = assessment.get('categories', []) if contains_violation else []
explanation = assessment.get('explanation') if contains_violation else None
return contains_violation, violated_categories, explanation
for comment in user_comments:
print(f"\nComment: {comment}")
violation, violated_categories, explanation = moderate_message_with_definitions(comment, unsafe_category_definitions)
if violation:
print(f"Violated Categories: {', '.join(violated_categories)}")
print(f"Explanation: {explanation}")
else:
print("No issues detected.")
moderate_message_with_definitions
函数通过允许每个不安全类别与详细定义配对,扩展了早期的moderate_message
函数。这在代码中通过将原始函数中的unsafe_categories
列表替换为unsafe_category_definitions
字典来实现。这个字典将每个不安全类别映射到其相应的定义。类别名称和它们的定义都包含在提示中。
值得注意的是,Specialized Advice
类别的定义现在指定了应该禁止的金融建议类型。因此,评论It's a great time to invest in gold!
,之前通过了moderate_message
评估,现在触发了违规。
考虑批处理
在不需要实时审核的情况下,考虑批量审核消息以降低成本。在提示的上下文中包含多条消息,并要求Claude评估哪些消息应该被审核。
在这个例子中,batch_moderate_messages
函数通过单个Claude API调用处理整批消息的审核。
在函数内部,创建了一个提示,其中包括要评估的消息列表、定义的不安全内容类别及其描述。提示指示Claude返回一个JSON对象,列出所有包含违规的消息。响应中的每条消息都由其id标识,该id对应于消息在输入列表中的位置。
请记住,为您的特定需求找到最佳批处理大小可能需要一些实验。虽然更大的批处理大小可以降低成本,但它们也可能导致质量略有下降。此外,您可能需要增加Claude API调用中的max_tokens
参数以适应更长的响应。有关您选择的模型可以输出的最大令牌数的详细信息,请参阅模型比较页面。