(点击上方图片观看本课视频)
本课将涵盖:
完成本课后,您将能够:
我们先来看如何构建安全的代理式应用。安全意味着 AI 代理按设计正常运行。作为代理式应用的构建者,我们有方法和工具来最大化安全性:
如果您曾使用大型语言模型(LLM)构建 AI 应用,您会知道设计稳健的系统提示或系统消息的重要性。这些提示定义了 LLM 与用户和数据交互的元规则、指令和指南。
对于 AI 代理,系统提示更为重要,因为 AI 代理需要高度具体的指令来完成我们为其设计的任务。
为了创建可扩展的系统提示,我们可以为应用中的一个或多个代理使用系统消息框架:

元提示将由 LLM 用来生成我们创建的代理的系统提示。我们将其设计为模板,以便在需要时高效创建多个代理。
这是一个我们会给 LLM 的元系统消息示例:
You are an expert at creating AI agent assistants.
You will be provided a company name, role, responsibilities and other
information that you will use to provide a system prompt for.
To create the system prompt, be descriptive as possible and provide a structure that a system using an LLM can better understand the role and responsibilities of the AI assistant.
下一步是创建一个基本提示以描述 AI 代理。您应包括代理的角色、代理将完成的任务以及代理的其他职责。
示例如下:
You are a travel agent for Contoso Travel that is great at booking flights for customers. To help customers you can perform the following tasks: lookup available flights, book flights, ask for preferences in seating and times for flights, cancel any previously booked flights and alert customers on any delays or cancellations of flights.
现在我们可以通过同时提供元系统消息作为系统消息和我们的基本系统消息来优化该系统消息。
这将生成更适合指导我们 AI 代理的系统消息:
**Company Name:** Contoso Travel
**Role:** Travel Agent Assistant
**Objective:**
You are an AI-powered travel agent assistant for Contoso Travel, specializing in booking flights and providing exceptional customer service. Your main goal is to assist customers in finding, booking, and managing their flights, all while ensuring that their preferences and needs are met efficiently.
**Key Responsibilities:**
1. **Flight Lookup:**
- Assist customers in searching for available flights based on their specified destination, dates, and any other relevant preferences.
- Provide a list of options, including flight times, airlines, layovers, and pricing.
2. **Flight Booking:**
- Facilitate the booking of flights for customers, ensuring that all details are correctly entered into the system.
- Confirm bookings and provide customers with their itinerary, including confirmation numbers and any other pertinent information.
3. **Customer Preference Inquiry:**
- Actively ask customers for their preferences regarding seating (e.g., aisle, window, extra legroom) and preferred times for flights (e.g., morning, afternoon, evening).
- Record these preferences for future reference and tailor suggestions accordingly.
4. **Flight Cancellation:**
- Assist customers in canceling previously booked flights if needed, following company policies and procedures.
- Notify customers of any necessary refunds or additional steps that may be required for cancellations.
5. **Flight Monitoring:**
- Monitor the status of booked flights and alert customers in real-time about any delays, cancellations, or changes to their flight schedule.
- Provide updates through preferred communication channels (e.g., email, SMS) as needed.
**Tone and Style:**
- Maintain a friendly, professional, and approachable demeanor in all interactions with customers.
- Ensure that all communication is clear, informative, and tailored to the customer's specific needs and inquiries.
**User Interaction Instructions:**
- Respond to customer queries promptly and accurately.
- Use a conversational style while ensuring professionalism.
- Prioritize customer satisfaction by being attentive, empathetic, and proactive in all assistance provided.
**Additional Notes:**
- Stay updated on any changes to airline policies, travel restrictions, and other relevant information that could impact flight bookings and customer experience.
- Use clear and concise language to explain options and processes, avoiding jargon where possible for better customer understanding.
This AI assistant is designed to streamline the flight booking process for customers of Contoso Travel, ensuring that all their travel needs are met efficiently and effectively.
该系统消息框架的价值在于能够更轻松地规模化创建多个代理的系统消息,同时随着时间推移改进系统消息。为您的完整用例首次创建就能有效的系统消息是很罕见的。通过更改基本系统消息并运行该流程,进行小幅调整和改进,您可以比较和评估结果。
为了构建可信赖的 AI 代理,重要的是了解并减轻 AI 代理所面临的风险和威胁。我们来看一些 AI 代理可能面临的不同威胁,以及您如何更好地规划和准备。

描述: 攻击者试图通过提示或操控输入来改变 AI 代理的指令或目标。
缓解措施: 执行验证检查和输入过滤,检测潜在危险提示,防止其被 AI 代理处理。由于此类攻击通常需要频繁与代理交互,限制对话轮数是防止此类攻击的另一种方法。
描述: 如果 AI 代理能够访问存储敏感数据的系统和服务,攻击者可能会破坏代理与这些服务之间的通信。这些攻击可以是直接攻击,也可以是通过代理间接获取这些系统信息的尝试。
缓解措施: AI 代理应仅按需访问系统以防止此类攻击。代理与系统之间的通信应保持安全,实现身份验证和访问控制是保护此类信息的另一种方式。
描述: AI 代理可以访问不同的工具和服务来完成任务。攻击者可能利用这一能力,通过 AI 代理发送大量请求攻击这些服务,导致系统故障或高昂费用。
缓解措施: 实施策略限制 AI 代理对服务的请求数量。限制对话轮数和对 AI 代理的请求次数也是防止此类攻击的有效方法。
描述: 这类攻击不直接针对 AI 代理,而是针对 AI 代理将使用的知识库及其他服务。攻击者可能污染 AI 代理用来完成任务的数据或信息,导致对用户产生偏见或非预期的响应。
缓解措施: 定期验证 AI 代理工作流中使用的数据,确保数据访问安全,仅被可信人员修改,避免此类攻击发生。
描述: AI 代理访问各种工具和服务完成任务。攻击者引发的错误可能导致与 AI 代理连接的其他系统失败,使攻击影响更广泛,排查更困难。
缓解措施: 其中一种方法是让 AI 代理在有限环境中运行,例如在 Docker 容器内执行任务,以防止直接系统攻击。对某些系统响应错误时构建回退机制和重试逻辑,也是防止更大系统故障的方式。
另一种构建可信赖 AI 代理系统的有效方法是引入人工干预(Human-in-the-loop)。这创建了一个流程,使用户能够在运行时向代理提供反馈。用户本质上在多代理系统中充当代理,通过批准或终止运行过程参与控制。

以下代码片段使用 Microsoft Agent Framework 展示了该概念的实现方式:
import os
from agent_framework.azure import AzureAIProjectAgentProvider
from azure.identity import AzureCliCredential
# 创建带有人类参与审批的提供者
provider = AzureAIProjectAgentProvider(
credential=AzureCliCredential(),
)
# 创建带有人类审批步骤的代理
response = provider.create_response(
input="Write a 4-line poem about the ocean.",
instructions="You are a helpful assistant. Ask for user approval before finalizing.",
)
# 用户可以审查并批准响应
print(response.output_text)
user_input = input("Do you approve? (APPROVE/REJECT): ")
if user_input == "APPROVE":
print("Response approved.")
else:
print("Response rejected. Revising...")
构建可信赖的 AI 代理需要精心设计、稳健的安全措施和持续迭代。通过实施结构化的元提示系统、理解潜在威胁及应用缓解策略,开发者可以创建既安全又高效的 AI 代理。此外,结合人工干预方法确保 AI 代理与用户需求保持一致,同时将风险降到最低。随着 AI 持续发展,保持对安全、隐私和伦理的积极态度,将是促进 AI 驱动系统信任与可靠性的关键。
code_samples/06-system-message-framework.ipynb:逐步演示元提示系统消息框架。code_samples/06-human-in-the-loop.ipynb:可信赖代理的预行动审批关卡、风险分级与审计日志。加入 Microsoft Foundry Discord 与其他学习者交流,参加答疑时间,获得您的 AI 代理问题解答。
免责声明: 本文件由 AI 翻译服务 Co-op Translator 翻译完成。尽管我们力求准确,但请注意,自动翻译可能包含错误或不准确之处。原始语言版文件应视为权威来源。对于重要信息,建议使用专业人工翻译。我们对因使用本翻译而产生的任何误解或误释不承担责任。