Azure AI Foundry 是微軟提供的一站式 AI 開發與管理平台,讓建構、部署與管理 AI 解決方案都可以在一個平台裡被建立及管理,過去常聽到的 Azure AI Service、Azure OpenAI Service 等等已經整合進來。而其中的 AI Agent 服務,則是專門為了打造「智慧代理(Intelligent Agents)」所設計的核心功能之一。這個服務將模型、工具和框架整合到統一的系統中,包含管理執行緒、協調工具呼叫、強制執行內容安全並與身份、網路和可觀測性系統一起整合,讓開發人員能夠更輕鬆建立和部署智慧代理應用程式。
AI Agent 服務提供超過許多的連接器,可連接到像是 SharePoint、Microsoft Fabric、Azure AI Search 等企業資料來源,也可以使用 Azure Logic Apps 和 Azure Functions 來建立自動化工作流程。並且也支援多種先進模型,包括 GPT-5 系列、GPT-4.1、GPT-4o、Llama 3、Mistral Large 等來自 OpenAI、Meta 和其他廠商的模型,讓開發者能為不同任務選擇最適合的模型。並且也有支援多代理工作流程,不同代理間可以互相呼叫協作,透過狀態層管理上下文、處理錯誤和維護長時間運行的流程。
當然在 semantic kernel 也可以很輕鬆的整合 Azure AI Foundry Agent,並且可以直接使用 semantic kernel 的功能來建立本地的 Plugin,然後將這些 Plugin 加入到 Azure AI Agent 中,讓 Agent 可以呼叫這些 Plugin 來完成任務。今天這篇就來示範如何使用 semantic kernel 來建立一個企業助理(Business Assistant)AI Agent。
這個範例會示範如何使用 semantic kernel 來建立一個企業助理(Business Assistant)AI Agent,這個助理可以協助查詢客戶的帳戶和合約狀態。這個助理會使用一個自訂義的 Plugin 來模擬查詢客戶資料的功能,並且會使用 Azure AI Foundry Agent 來處理請求。除了 semantic kernel本身的套件之外,首先確認已安裝好以下額外的套件
dotnet add package Microsoft.SemanticKernel.Agents.AzureAI --prerelease
dotnet add package Azure.Identity
接著建立一個自訂義的 BusinessAssistant Plugin,這個 Plugin 會模擬查詢客戶合約資料以及業務連絡資訊的功能,並且會有二個方法 GetCustomerInfo
與 GetSalespersonContact
,其中 GetCustomerInfo
會接受一個客戶名稱,然後回傳該客戶的合約資訊,而 GetSalespersonContact
會接受一個業務代表的姓名,然後回傳該業務代表的聯絡資訊。
public class BusinessAssistantPlugin
{
[KernelFunction]
[Description("Query the customer's account and contract status")]
public static string GetCustomerInfo(
[Description("customer name")] string customerName)
{
// 模擬客戶資料查詢
// 在實際應用中,這裡可能會連接到資料庫或其他資料來源
return customerName switch
{
"大大銀行" => "大大銀行的帳號是 #SINO123,合約狀態為有效(到期日 2025/12/31)",
"錢多多金控" => "錢多多金控的帳號是 #TAISHIN001,合約狀態為審核中",
_ => $"找不到名為 {customerName} 的客戶資料。"
};
}
[KernelFunction]
[Description("Query the salesperson's contact information")]
public static string GetSalespersonContact(
[Description("salesperson name")] string name)
{
return name switch
{
"Ian" => "Ian 的聯絡方式:ian@company.com,電話 02-1688-0857",
"Cheryl" => "Cheryl 的聯絡方式:cheryl@company.com,電話 02-9571-1688",
_ => $"找不到 {name} 的聯絡資訊。"
};
}
}
gpt-4.1
。DefaultAzureCredential
來建立 AzureAIClient
,這樣就可以使用本地的開發人員憑證、環境變數或是 Managed Identity 來進行驗證,這樣就不需要在程式碼中寫入金鑰,會比較安全。using Azure.Identity;
using Microsoft.SemanticKernel.Agents.AzureAI;
PersistentAgentsClient client = AzureAIAgent.CreateAgentsClient(Config.Azure_AI_Foundry_EndPoint, new AzureCliCredential());
BusinessAssistantPlugin
,並且會使用 gpt-4.1
作為基礎模型,建立完成後會自動部署到 Azure AI Foundry 中並且取得一個 ID,以後直接使用就可以不需要每次都重新部署。// 定義商務助理 AI 的指令與說明
// 這些指令將會被 Azure AI Agent 使用
string instructions = """
你是一位專業的商務助理 AI,負責協助查詢以下資訊:
1. 根據客戶名稱,查詢客戶的帳戶資訊與合約狀態。
2. 根據業務代表姓名,查詢對應業務代表的聯絡資訊。
請遵守以下原則:
- 回覆時要簡潔清楚、準確不誤導。
- 當使用者輸入不明確或資訊不足時,請主動詢問更多細節。
- 絕對不能憑空編造任何資訊(例如:合約狀態、聯絡方式)。
- 一律透過內部資料庫或指定 API 工具來取得資料,不要自行假設。
- 如果查無資料,請禮貌地說明找不到相關資訊。
若使用者的問題超出你的職責範圍,請清楚說明你能處理的類型,並建議他重新描述需求或洽詢相關單位。
""";
// 建立 PersistentAgentsClient 用於 Azure AI Agents
PersistentAgent definition = await client.Administration.CreateAgentAsync(
Config.Azure_ModelId,
name: "Business Assistant",
description: "這是一位專業的商務助理 AI,專門協助內部人員快速查詢客戶帳戶資訊、合約狀態,以及對應業務聯絡方式。",
instructions: instructions);
KernelPlugin plugin = KernelPluginFactory.CreateFromType<BusinessAssistantPlugin>();
//建立一個新的 Azure AI Agent
AzureAIAgent agent = new(definition, client)
{
Kernel = Kernel.CreateBuilder()
.AddAzureOpenAIChatCompletion(
Config.Azure_ModelId,
Config.Azure_AI_Foundry_EndPoint,
new AzureCliCredential()).Build()
};
agent.Kernel.Plugins.Add(plugin);
// 根據 ID 取得已存在的 Azure AI Agent
PersistentAgent definition = await client.Administration.GetAgentAsync("asst_xxxxxxxx");
AzureAIAgent agent = new(definition, client);
// 每次啟動時重新附掛 Plugin
agent.Kernel.Plugins.Add(plugin);
AzureAIAgentThread
,對話記錄會根據這個 thread 自動管理及持久化。 AzureAIAgentThread agentThread = new(agent.Client);
Console.Write("User > ");
string? userInput;
while ((userInput = Console.ReadLine()) is not null)
{
if (string.IsNullOrWhiteSpace(userInput) || userInput.Equals("exit", StringComparison.OrdinalIgnoreCase))
break;
ChatMessageContent message = new(AuthorRole.User, userInput);
bool isFirst = false;
await foreach (StreamingChatMessageContent response in agent.InvokeStreamingAsync(message, agentThread))
{
if (string.IsNullOrEmpty(response.Content))
{
StreamingFunctionCallUpdateContent? functionCall = response.Items.OfType<StreamingFunctionCallUpdateContent>().SingleOrDefault();
if (!string.IsNullOrEmpty(functionCall?.Name))
{
Console.WriteLine($"\n -- trace {response.Role} - {response.AuthorName ?? "*"}: FUNCTION CALL - {functionCall.Name}");
}
continue;
}
if (!isFirst)
{
Console.Write($"{response.Role} - {response.AuthorName ?? "*"} > ");
isFirst = true;
}
Console.Write($"{response.Content}");
}
Console.WriteLine();
Console.WriteLine($"\n -- trace chat thread with agent: {agent.Name} - {agent.Description},threadId: {agentThread.Id} \n");
Console.Write("User > ");
}
User > 錢多多金控的合約目前是什麼情況
-- trace assistant - Business Assistant: FUNCTION CALL - BusinessAssistantPlugin-GetCustomerInfo
assistant - Business Assistant > 錢多多金控的帳號是 #TAISHIN001,目前合約狀態為「審核中」。
-- trace chat thread with agent: Business Assistant - 這是一位專業的商務助理 AI,專門協助內部人員快速查詢客戶帳戶資訊、合約狀態,以及對應業務聯絡方式。,threadId: thread_D3XJTNtt88wWKOMd5Mf08RIA
User >
雖然在本機(local)環境也能透過 semantic kernel 來建立 AI Agent,但使用 Azure AI Foundry 的 AI Agent 服務,可以利用 Azure 提供的雲端基礎架構,流量增加時能撐得住,適合正式上線或需要多人同時使用的應用場景。並且可以和Azure 生態系統無縫整合,像是可以直接連接到 Azure 上的資料來源,或是使用 Azure 的其他服務來擴展 Agent 的功能。對於需要擴大 AI Agent 應用的企業來說,Azure AI Foundry 提供了較完整的管理、監控和安全性功能,與自行開發相比,能夠節省時間和資源。