前一篇文章,我試圖勾勒我對 AI Agent 的理解,本篇內容讓我用 Semantic Kernel 來實作一個基本款的辦公室 AI Agent 應用,展示 AI Agent 的能力。
在開始之前,讓我再一次重覆一下 agent 的組成元素,如圖所示,需要 LLM 模型、Plugin Function(也就是 Tools)以及Memory記憶。
這個 AI Agent 被設計為一個辦公助手,能夠協助以下關鍵任務:
首先專案需要以下套件
var builder = Kernel.CreateBuilder()
.AddAzureOpenAIChatCompletion(
endpoint: Config.aoai_endpoint,
deploymentName: Config.aoai_deployment,
apiKey: Config.aoai_apiKey);
var kernel = builder.Build();
public class OfficePlugin
{
[KernelFunction, Description("Get customer information by name")]
public string GetCustomerInfo([Description("customer name")] string customerName)
{
var customerInfo = new Dictionary<string, string>
{
{ "Name", customerName },
{ "Address", "123 Main St, Anytown, USA" },
{ "Phone", "555-1234" },
{ "Email", "Lee@mail.com" }
};
return System.Text.Json.JsonSerializer.Serialize(customerInfo);
}
[KernelFunction, Description("send e-mail to customer")]
public string SendEmail([Description("email address")] string mailTo
, [Description("email recipient")] string name
, [Description("email subject")] string subject
, [Description("email content")] string content)
{
return $"email sent to {mailTo} with subject {subject} and content {content} successfully";
}
}
const string newsAgentName = "OfficeAgent";
string newsAgentNameInstructions =
"""
You are an office agent, specializing in handling office tasks.
The goal is to manage calendar scheduling, client information inquiries, and email sending based on the user's requests.
You will focus on these tasks and will not perform any unrelated tasks.
""";
// Define the agent
ChatCompletionAgent agent = new()
{
Name = newsAgentName,
Instructions = newsAgentNameInstructions,
Kernel = kernel,
Arguments = new KernelArguments(new OpenAIPromptExecutionSettings() { ToolCallBehavior = ToolCallBehavior.AutoInvokeKernelFunctions }),
};
KernelPlugin plugin = KernelPluginFactory.CreateFromType<OfficePlugin>();
agent.Kernel.Plugins.Add(plugin);
// 建立AgentGroupChat 對話記錄物件
AgentGroupChat chat = new();
// 模擬連續對話過程
await InvokeAgentAsync("Hello");
await InvokeAgentAsync("告訴我Lee的基本資料");
await InvokeAgentAsync("發送郵件給Lee,提醒關於明天下午2點會議的通知");
async Task InvokeAgentAsync(string input)
{
//使用者prompt加入對話記錄
ChatMessageContent message = new(AuthorRole.User, input);
chat.AddChatMessage(message);
await foreach (ChatMessageContent response in chat.InvokeAsync(agent))
{
Console.WriteLine($"{response.AuthorName}: {response.Content}");
}
}
/*
agent 輸出:
OfficeAgent: Hi there! How can I assist you today?
OfficeAgent: 以下是Lee的基本資料:
- 名字: Lee
- 地址: 123 Main St, Anytown, USA
- 电话: 555-1234
- 邮箱: Lee@mail.com
还有什么我可以帮忙的吗?
OfficeAgent: 郵件已經成功發送給Lee,提醒他明天下午2點的會議。
還有其他需要幫忙的嗎?
*/
這個 AI Agent 透過 Semantic Kernel 進行實作,展現了以下能力:
在接下來的文章裡,我將會再示範不同的 AI Agent 設計,包含進階的 multi Agent
。