使用Microsoft Agent Framework构建C# AI代理
<blockquote><p>本文介绍如何使用Microsoft Agent Framework在C#中构建生产级AI代理,涵盖环境设置、内存管理、工具及多代理工作流。本框架旨在替代AutoGen和Semantic Kernel,提供更好的上下文管理和代理协调。</p>
</blockquote>
<h2 id="what-is-microsoft-agent-framework">What Is Microsoft Agent Framework?</h2>
<p>Microsoft Agent Framework 是微软推出的新框架,用于替代 AutoGen 和 Semantic Kernel,旨在将这两个项目整合为一体,简化开发者的选择。该框架提供了高效的代理模型,具备对话记忆、调用 C# 方法的能力以及跨代理协作的功能。</p>
<p>框架底层的抽象层支持多种接入方式,包括 OpenAI、Azure OpenAI 和 Ollama,并内置线程状态管理、遥测和过滤器等生产级特性,从而减轻开发者在构建复杂应用时的负担。</p>
<h3 id="为什么选择此框架而非-semantic-kernel">为什么选择此框架而非 Semantic Kernel</h3>
<p>虽然 Semantic Kernel 适用于提示链和函数调用,但在跨多轮对话中保持上下文时,它的表现可能不够理想。Agent Framework 提供图形化执行、条件路由和持久线程等功能,简化了开发过程。</p>
<h3 id="首次设置代理">首次设置代理</h3>
<p>要创建第一个代理,确保使用 .NET 8 或更高版本,并安装以下必要包:</p>
<pre><code class="language-shell">dotnet add package Azure.AI.OpenAI --version 2.1.0
dotnet add package Azure.Identity --version 1.17.1
dotnet add package Microsoft.Extensions.AI.OpenAI --version 10.1.1-preview.1.25612.2
dotnet add package Microsoft.Agents.AI.OpenAI --version 1.0.0-preview.251219.1
</code></pre>
<p>以下是一个简单的代理示例:</p>
<pre><code class="language-csharp">using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
using OpenAI;
AIAgent agent = new OpenAIClient("your-api-key")
.GetChatClient("gpt-4o-mini")
.AsIChatClient()
.CreateAIAgent(instructions: "You help developers find accurate technical information.");
var response = await agent.RunAsync("What is C#?");
Console.WriteLine(response);
</code></pre>
<h3 id="代理的记忆管理">代理的记忆管理</h3>
<p>Agent Framework 利用线程管理来维护代理的记忆,每个线程持有独立的对话历史和上下文,确保多轮对话中的一致性:</p>
<pre><code class="language-csharp">AgentThread thread = agent.GetNewThread();
var response1 = await agent.RunAsync("What's the difference between IAsyncEnumerable and Task<List>?", thread);
Console.WriteLine(response1);
var response2 = await agent.RunAsync("Which one should I use for streaming large datasets?", thread);
Console.WriteLine(response2);
</code></pre>
<h3 id="为代理添加工具">为代理添加工具</h3>
<p>代理可以通过简单的 C# 方法使用工具,过程直观,无需复杂代码逻辑:</p>
<pre><code class="language-csharp">
async Task<string> GetWeather( string city) {
await Task.Delay(500);
return $"Sunny, 72°F in {city}";
}
</code></pre>
<p>创建代理后,即可调用以下代码使用这些工具:</p>
<pre><code class="language-csharp">var weatherAgent = chatClient.CreateAIAgent(
name: "WeatherAgent",
instructions: "You provide weather information.",
tools:
);
var response = await weatherAgent.RunAsync("What's the weather in Seattle?");
Console.WriteLine(response);
</code></pre>
<h3 id="多代理工作流">多代理工作流</h3>
<p>Agent Framework 允许定义多个代理以协作完成任务。例如,研究代理和写作代理可共同组成工作流:</p>
<pre><code class="language-csharp">var researchThread = researchAgent.GetNewThread();
var researchResult = await researchAgent.RunAsync("Provide key technical facts about: async/await in C#", researchThread);
Console.WriteLine($"Research: {researchResult}");
var writerThread = writerAgent.GetNewThread();
var documentation = await writerAgent.RunAsync($"Based on this research, write a brief explanation:\n\n{researchResult}", writerThread);
Console.WriteLine($"Documentation: {documentation}");
</code></pre>
<h3 id="关于-microsoftextensionsai">关于 Microsoft.Extensions.AI</h3>
<p>Microsoft.Extensions.AI 提供抽象层以编写与 IChatClient 相关的代码,Agent Framework 则在此基础上扩展功能,支持线程管理和工具编排。</p>
<h3 id="最后思考">最后思考</h3>
<p>Microsoft Agent Framework 为 .NET 开发者构建 AI 代理提供了崭新的途径,消除了整合不同库的麻烦。尽管框架仍处于预览阶段,但其稳定性足以支持大多数用例,适合希望在 .NET 中构建 AI 代理的开发者。</p>
<h2 id="总结">总结</h2>
<p>本文介绍如何在C#中利用Microsoft Agent Framework构建AI代理,强调环境配置和内存管理。该框架在上下文管理和代理协调方面优于AutoGen和Semantic Kernel。建议开发者立即搭建环境,测试多代理工作流以验证性能。</p>
<hr>
<p><strong>原文参考:</strong> Build AI Agents with Microsoft Agent Framework in C# - DEV ...</p>
<p><em>标签:Microsoft Agent Framework, AI代理, C#, .NET, 内存管理</em></p>
<p><em>本内容基于 AI 深度分析生成</em></p><br><br>
来源:https://www.cnblogs.com/chenyishi/p/19749973
頁:
[1]