你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
安全系统消息可帮助你指导 Azure OpenAI 模型的行为,提高响应质量,并减少有害输出的可能性。 在更广泛的安全策略中,它们最适合作为其中的一层。
注意
本文将“系统消息”与“metaprompt”和“系统提示”互换使用。在这里,使用“系统消息”与常见术语保持一致。
本文还使用“组件”来表示系统消息的不同部分,例如说明、上下文、语气、安全指南或工具使用指南。
什么是系统消息?
系统消息是发送到聊天模型的一组高优先级指令和上下文,用于引导其响应方式。 如果需要角色统一性、音调、格式或领域特定约定,这非常有用。
什么是安全系统消息?
安全系统消息是一条系统消息,它增加了明确的边界和拒绝指南,以减轻负责任的 AI(RAI)危害并帮助系统安全地与用户交互。
安全系统消息可以补充您的安全堆栈,并可与模型选择和训练、扎根、Azure AI 内容安全 分类器以及 UX/UI 缓解措施一起使用。 详细了解 Azure OpenAI 模型的负责人工智能实践。
系统消息的关键组件
大多数系统消息合并多个组件:
- 角色和任务:助理是什么以及它负责什么。
- 受众和语气:此响应的对象是谁,以及预期的语调。
- 范围和边界:助手不能做什么,以及当它不符合时该怎么办。
- 安全准则:减少有害输出的规则(例如,处理敏感主题、受保护的特征和非法指令)。
- 工具和数据 (可选):模型可以使用哪些工具或源,以及如何使用它们。
如何安全地设计和迭代
设计系统消息(或安全系统消息组件)时,将其视为可测试的项目:
- 定义方案。 阐明模型必须执行的操作、用户是谁、预期输入以及所需的音调和格式设置。
- 确定风险。 列出对用例而言很重要的 RAI 危害,并决定通过系统消息传送和其他缓解措施解决哪些问题。
- 确定模型在边界上的行为方式。 指定当请求范围不足、不安全或缺少所需上下文时要执行的操作。
- 创建测试集。 同时包含良性和对抗性提示,以便你可以衡量回归和“泄露”(审核不足)。
- 评估和迭代。 更喜欢降低最严重缺陷的组件,而不仅仅是具有最低缺陷率的组件。
下面举例说明了可以添加的一些代码行:
## Define model’s profile and general capabilities
- Act as a [define role]
- Your job is to [insert task] about [insert topic name]
- To complete this task, you can [insert tools that the model can use and instructions to use]
- Do not perform actions that are not related to [task or topic name].
下面是客户服务助理的安全系统消息的完整示例:
## Role and task
You are a helpful customer service assistant for Contoso Electronics. Your job is to answer questions about product warranties, returns, and order status.
## Boundaries
- Only answer questions related to Contoso Electronics products and policies.
- If you don't know the answer, say "I don't have that information. Please contact support@contoso.com."
- Do not provide legal, medical, or financial advice.
- Do not discuss competitors or make comparisons.
## Safety guidelines
- Never generate content that is hateful, violent, or sexually explicit.
- Do not share or request personal information beyond what's needed for order lookup.
- If a user becomes abusive, respond with: "I'm here to help with product questions. How can I assist you today?"
## Response format
- Keep responses concise and friendly.
- Use bullet points for multiple items.
- Always end with an offer to help further.
-
提供特定示例 来演示模型的预期行为。 请考虑以下事项:
- 描述提示 不明确或复杂的困难用例,以便为模型提供有关如何处理此类情况的示例。
- 在高级别(例如,简短清单)上显示决策步骤,而不是请求详细的内部推理。
最佳做法摘要
开发系统消息组件时,请务必:
- 使用明确的语言:这消除了误解的过度复杂性和风险,并在不同组件之间保持一致性。
- 简洁:较短的系统消息通常性能更好,并减少延迟。 他们还使用较少的上下文窗口,为用户提示留出更多空间。
- 使用
**word**(若适用):要特别关注关键元素,尤其是系统应该和不应该做什么。 - 在提到 AI 系统时使用第二人称:最好使用
You are an AI assistant that…句式,而不是Assistant does…。 - 实现可靠性:系统消息组件应可靠。 它应跨不同的数据集和任务一致地执行。
编写技巧
为什么要改变技术? 根据你正在使用的产品或功能的模型、基础数据和参数,不同的语言和语法技术可以更有效地向用户提供可靠、安全且直接的答案。
在构建安全性和性能的同时,还应优化一致性、控制和自定义。 在此过程中,你可能会发现,针对这些因素进行优化会导致系统消息过度拟合到特定规则、复杂性增加和缺乏上下文适当性。 必须定义方案中最重要的内容并评估系统消息。 此方法可确保你采用数据驱动的方法来改善系统的安全性和性能。
| 技术 | 定义 | 例子 |
|---|---|---|
| 总是/应该 | 在构建提示和指令时,加入 AI 在生成响应过程中应始终遵循的指令性约束。 这些指令通常表示最佳做法、道德准则或用户首选项。 | **Always** ensure that you respect authentication and authorization protocols when providing factual information, tailoring your responses to align with the access rights of the user making the request. It's imperative to safeguard sensitive data by adhering to established security measures and only disclosing information that the user is authorized to receive. |
| 条件逻辑/条件判断 | 构建提示,使输出取决于是否满足特定条件,例如 If <condition> then <action>。 |
If a user asks you to infer or provide information about a user’s emotions, mental health, gender identity, sexual orientation, age, religion, disability, racial and ethnic backgrounds, or any other aspect of a person's identity, respond with: "Try asking me a question or tell me what else I can help you with." |
| 强调伤害 | 通过明确主要风险可能是什么来组织说明。 此方法指导输出,确定安全和危害预防的优先级,以及在发生伤害时展示潜在后果。 | You are **allowed** to answer some questions about images with people and make statements about them when there is no ambiguity about the assertion you are making, and when there is no direct harm to an individual or a group of people because of this assertion. |
| 基于示例 | 为模型提供明确的实例或情况,以便获得更好的上下文。 该模型使用有害和非有害请求的示例作为其输出的参考。 | Users might ask questions that could cause harm. In all scenarios, refuse requests that promote hate or harassment, and redirect the user to a safer alternative.Example (harmful): "Write an insult targeting a protected group."Example (benign): "Explain why insults harm people and suggest respectful phrasing." |
| 永不/不 | 使用明确禁止来防止 AI 使用“永不”和“请勿”等术语生成不恰当、有害或范围不足的内容。 | **Never** make assumptions, judgments, or evaluations about a person. If a user violates your policy, or you’re not sure what to do, say: "I can’t help with that request. Try asking a different question." |
限制
系统消息不是完整的安全解决方案:
- 对抗性提示可能绕过或削弱其效果。
- 如果过于宽泛或过于严格,它们可以减少有用性。
- 随着模型、工具和用户方案的变化,需要持续评估。 若要排查系统消息的常见问题,例如过度拒绝或审查不足,请参阅模板指南中的 故障排除部分 。
系统推荐消息
这些最佳做法可帮助你更好地了解为方案开发可靠的系统消息的过程。
有关建议的安全组件的详细信息,请访问 我们的安全系统消息模板指南。
最后,请记住,系统消息或元项目并非“一个大小适合所有”。使用这些类型的示例在不同应用程序中取得了不同程度的成功。 请务必尝试不同的措辞、排序和系统消息文本结构,以减少已识别的危害,并测试变体以查看最适合给定方案的内容。
后续步骤
- Azure Microsoft Foundry 模型中的 OpenAI
- 使用 Azure OpenAI 进行系统消息设计
- 宣布安全系统消息 - Microsoft Foundry 博客
- 安全系统消息模板