(點擊上方圖片觀看本課程影片)
本課程將涵蓋:
完成本課程後,您將能夠:
我們先來檢視如何建置安全的代理應用。安全性表示 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 代理框架實作此概念的程式碼摘錄:
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 驅動系統信賴與可靠性的關鍵。
加入 Microsoft Foundry Discord,與其他學習者交流,參加答疑時間並獲得您有關 AI 代理的問題解答。
免責聲明: 本文件由 AI 翻譯服務 Co-op Translator 進行翻譯。儘管我們努力確保準確性,請注意自動翻譯可能包含錯誤或不準確之處。原始文件的母語版本應被視為權威來源。對於重要資訊,建議尋求專業人工翻譯。我們不對因使用此翻譯而產生的任何誤解或誤譯負責。