目录简介MAF简单介绍AI Agent是什么Workflow是什么快速开始创建Agent1Create Agent2多轮对话3函数调用4人工审批的函数调用小结示例源码参考资料简介大家好我是Edison。MAFMicrosoft Agent Framwork已经出来有一段时间最近看到它的commit比较频繁估计是为了在.NET Conf 2025发布1.0.0的正式版。预览版也并不妨碍我们的学习那么从今天开始我们就通过微软官方学习文档来一起进入MAF的世界吧。MAF简单介绍Microsoft Agent Framework (MAF)是微软最新推出的统一的智能体开发框架用于生成适用于 .NET 和 Python 的 AI Agent智能体和 Multi Agent Workflow多智能体工作流。MAF 汇集了 Semantic Kernel 和 AutoGen 两个知名项目的优点并扩展了其优势同时添加了新功能。换句话说它们由同一开发团队构建是今后构建AI Agent的统一基础。简而言之MAF是微软推出的智能体开发的“终极武器”对于.NET技术栈的开发团队而言其意义十分重大它将已有的技术版图组合起来形成一个AI时代的完整技术栈对于Edison所在的团队来说.NET技术栈开发者可以使用MAF快速进入Agent开发领域转型速度飞快。同时在机器学习、模型训练 和 推理服务器Inference Server方面我们也在使用Python来做将各自的优势保留并发挥即可。AI Agent是什么让我们再来看看AI Agent又称为AI智能体是什么它其实是一个使用LLM来处理用户输入、做出决策、调用工具MCP服务器来执行并生成响应以完成某个具体的任务。下图说明了AI Agent中的核心组件及其交互AI Agent适用于需要自主决策、规划试验 和 基于聊天 的用户交互应用程序特别是对于输入任务非结构化且无法提前定义的情况它们特别有用。这一类场景常见于客户支持、教育辅导、研究帮助等领域。需要注意的是AI Agent不适合高度结构化且要求严格遵循预定义规则的任务。也就是说如果应用程序预期有特定类型的输入并且具有明确定义的作业顺序则使用 AI Agent可能会带来不必要的不确定性、延迟和成本。Workflow是什么Workflow即工作流它可以表达预定义的作业序列工作流旨在处理可能涉及多个AI Agent、人工交互 和 与外部系统的集成的复杂且长时间运行的进程。很多时候我们可以显示定义工作流的执行序列从而更好地控制执行路径。下图演示了链接两个AI Agent 和 一个函数方法 的工作流示例工作流提供了一种结构化方法用于管理涉及多个步骤、决策点 和 与各种系统或Agent交互的复杂流程。快速开始创建Agent这里我们创建一个.NET控制台项目安装以下组件包PackageReference IncludeAzure.AI.OpenAI Version2.5.0-beta.1 / PackageReference IncludeMicrosoft.Agents.AI.OpenAI Version1.0.0-preview.251110.1 /假定我们有如下配置文件定义了我们可以用到的LLM API{ OpenAI: { EndPoint: https://api.siliconflow.cn, ApiKey: ******************************, ModelId: Qwen/Qwen2.5-32B-Instruct } }首先我们加载配置文件到应用程序中// Load configuration var config new ConfigurationBuilder() .AddJsonFile($appsettings.json, optional: false, reloadOnChange: true) .Build(); var openAIProvider config.GetSection(OpenAI).GetOpenAIProvider(); var azureOpenAIProvider config.GetSection(AzureOpenAI).GetOpenAIProvider();1Create Agent其次我们创建第一个Hello World的Agentvar jokerAgent new OpenAIClient( new ApiKeyCredential(openAIProvider.ApiKey), new OpenAIClientOptions { Endpoint new Uri(openAIProvider.Endpoint) }) .GetChatClient(openAIProvider.ModelId) .CreateAIAgent(instructions: You are good at telling jokes., name: Joker);然后通过简单的一句RunAsync 或 RunStreamingAsync 即可完成一次交互// Method 1: RunAsync Console.WriteLine(await jokerAgent.RunAsync(Tell me a joke about a pirate.)); // Method 2: RunStreamingAsync await foreach (var update in jokerAgent.RunStreamingAsync(Tell me a joke about a car.)) { Console.Write(update); }最终的结果如下图所示还挺幽默的那么如果是用的AzureOpenAI呢只需要换成下面即可var jokerAgent new AzureOpenAIClient( new Uri(azureOpenAIProvider.Endpoint), new ApiKeyCredential(azureOpenAIProvider.ApiKey)) .GetChatClient(azureOpenAIProvider.ModelId) .CreateAIAgent(instructions: You are good at telling jokes., name: Joker); Console.WriteLine(await jokerAgent.RunAsync(Tell me a joke about a pirate.));2多轮对话我们还可以通过AgentThread这个特性来实现具有“短期记忆”的多轮对话var thread1 jokerAgent.GetNewThread(); var thread2 jokerAgent.GetNewThread(); Console.WriteLine(await jokerAgent.RunAsync(Tell me a joke about a pirate., thread1) Environment.NewLine); Console.WriteLine(await jokerAgent.RunAsync(Now add some emojis to the joke and tell it in the voice of a pirates parrot., thread1) Environment.NewLine); Console.WriteLine(await jokerAgent.RunAsync(Tell me a joke about a robot., thread2) Environment.NewLine); Console.WriteLine(await jokerAgent.RunAsync(Now add some emojis to the joke and tell it in the voice of a robot., thread2) Environment.NewLine);3函数调用此外在MAF中进行函数方法调用也是十分的轻松定义一个简单的函数方法public class WeatherServicePlugin { [Description(Get current weather for a location)] public static async Taskstring GetCurrentWeatherAsync(string location) { return $The weather in {location} is cloudy with a high of 15°C.; } }然后再创建Agent时将工具列表告诉Agent即可var agent new OpenAIClient( new ApiKeyCredential(openAIProvider.ApiKey), new OpenAIClientOptions { Endpoint new Uri(openAIProvider.Endpoint) }) .GetChatClient(openAIProvider.ModelId) .CreateAIAgent( instructions: You are a helpful assistant, tools: [AIFunctionFactory.Create(WeatherServicePlugin.GetCurrentWeatherAsync)]); Console.WriteLine(await agent.RunAsync(Tell me the weather like in Chengdu?));最终显示的结果如下而在实际开发中基于MAF和原有的ASP.NET Core框架将依赖注入和生命周期管理纳入进来可以实现更为强大的模块化力量。4人工审批的函数调用相信我们在使用GitHub Copilot之类的编码智能体时会遇到当它想要执行某个命令行或工具调用时会先征求你的审批同意后它才会执行。其实在Agent开发中人工审批的工具调用也是一个常见的需求而在MAF中它也支持我们快速实现这个需求。下面这个示例演示了如何实现人工审批的工具调用var weatherTool AIFunctionFactory.Create(WeatherServicePlugin.GetCurrentWeatherAsync); var approvalRequiredWeatherTool new ApprovalRequiredAIFunction(weatherTool); var agent new OpenAIClient( new ApiKeyCredential(openAIProvider.ApiKey), new OpenAIClientOptions { Endpoint new Uri(openAIProvider.Endpoint) }) .GetChatClient(openAIProvider.ModelId) .CreateAIAgent(instructions: You are a helpful assistant, tools: [approvalRequiredWeatherTool]); var thread agent.GetNewThread(); var response await agent.RunAsync(Tell me the weather like in Chengdu?, thread); var userInputRequests response.UserInputRequests.ToList(); while(userInputRequests.Count 0) { // Ask the user to approve each function call request. var userInputResponses userInputRequests .OfTypeFunctionApprovalRequestContent() .Select(functionApprovalRequest { Console.WriteLine($The agent would like to invoke the following function, please reply Y to approve: Name {functionApprovalRequest.FunctionCall.Name}); return new ChatMessage(ChatRole.User, [functionApprovalRequest.CreateResponse(Console.ReadLine()?.Equals(Y, StringComparison.OrdinalIgnoreCase) ?? false)]); }) .ToList(); // Pass the user input responses back to the agent for further processing. response await agent.RunAsync(userInputResponses, thread); userInputRequests response.UserInputRequests.ToList(); } Console.WriteLine(Environment.NewLine $Agent: {response});执行结果如下图所示只有当我们人工输入Y确认后它才会调用工具执行。小结本文介绍了MAF的基本功能AI Agent和Workflow的基本概念以及通过一个.NET控制台项目快速创建了一个Agent的代码示例。下一篇我们将继续MAF的学习。示例源码Github:https://github.com/EdisonTalk/MAFD参考资料Microsoft Learn《Agent Framework Tutorials》https://learn.microsoft.com/en-us/agent-framework/tutorials引入地址
MAF快速入门(1)化繁为简的Agent创建范式
发布时间:2026/6/30 3:03:30
目录简介MAF简单介绍AI Agent是什么Workflow是什么快速开始创建Agent1Create Agent2多轮对话3函数调用4人工审批的函数调用小结示例源码参考资料简介大家好我是Edison。MAFMicrosoft Agent Framwork已经出来有一段时间最近看到它的commit比较频繁估计是为了在.NET Conf 2025发布1.0.0的正式版。预览版也并不妨碍我们的学习那么从今天开始我们就通过微软官方学习文档来一起进入MAF的世界吧。MAF简单介绍Microsoft Agent Framework (MAF)是微软最新推出的统一的智能体开发框架用于生成适用于 .NET 和 Python 的 AI Agent智能体和 Multi Agent Workflow多智能体工作流。MAF 汇集了 Semantic Kernel 和 AutoGen 两个知名项目的优点并扩展了其优势同时添加了新功能。换句话说它们由同一开发团队构建是今后构建AI Agent的统一基础。简而言之MAF是微软推出的智能体开发的“终极武器”对于.NET技术栈的开发团队而言其意义十分重大它将已有的技术版图组合起来形成一个AI时代的完整技术栈对于Edison所在的团队来说.NET技术栈开发者可以使用MAF快速进入Agent开发领域转型速度飞快。同时在机器学习、模型训练 和 推理服务器Inference Server方面我们也在使用Python来做将各自的优势保留并发挥即可。AI Agent是什么让我们再来看看AI Agent又称为AI智能体是什么它其实是一个使用LLM来处理用户输入、做出决策、调用工具MCP服务器来执行并生成响应以完成某个具体的任务。下图说明了AI Agent中的核心组件及其交互AI Agent适用于需要自主决策、规划试验 和 基于聊天 的用户交互应用程序特别是对于输入任务非结构化且无法提前定义的情况它们特别有用。这一类场景常见于客户支持、教育辅导、研究帮助等领域。需要注意的是AI Agent不适合高度结构化且要求严格遵循预定义规则的任务。也就是说如果应用程序预期有特定类型的输入并且具有明确定义的作业顺序则使用 AI Agent可能会带来不必要的不确定性、延迟和成本。Workflow是什么Workflow即工作流它可以表达预定义的作业序列工作流旨在处理可能涉及多个AI Agent、人工交互 和 与外部系统的集成的复杂且长时间运行的进程。很多时候我们可以显示定义工作流的执行序列从而更好地控制执行路径。下图演示了链接两个AI Agent 和 一个函数方法 的工作流示例工作流提供了一种结构化方法用于管理涉及多个步骤、决策点 和 与各种系统或Agent交互的复杂流程。快速开始创建Agent这里我们创建一个.NET控制台项目安装以下组件包PackageReference IncludeAzure.AI.OpenAI Version2.5.0-beta.1 / PackageReference IncludeMicrosoft.Agents.AI.OpenAI Version1.0.0-preview.251110.1 /假定我们有如下配置文件定义了我们可以用到的LLM API{ OpenAI: { EndPoint: https://api.siliconflow.cn, ApiKey: ******************************, ModelId: Qwen/Qwen2.5-32B-Instruct } }首先我们加载配置文件到应用程序中// Load configuration var config new ConfigurationBuilder() .AddJsonFile($appsettings.json, optional: false, reloadOnChange: true) .Build(); var openAIProvider config.GetSection(OpenAI).GetOpenAIProvider(); var azureOpenAIProvider config.GetSection(AzureOpenAI).GetOpenAIProvider();1Create Agent其次我们创建第一个Hello World的Agentvar jokerAgent new OpenAIClient( new ApiKeyCredential(openAIProvider.ApiKey), new OpenAIClientOptions { Endpoint new Uri(openAIProvider.Endpoint) }) .GetChatClient(openAIProvider.ModelId) .CreateAIAgent(instructions: You are good at telling jokes., name: Joker);然后通过简单的一句RunAsync 或 RunStreamingAsync 即可完成一次交互// Method 1: RunAsync Console.WriteLine(await jokerAgent.RunAsync(Tell me a joke about a pirate.)); // Method 2: RunStreamingAsync await foreach (var update in jokerAgent.RunStreamingAsync(Tell me a joke about a car.)) { Console.Write(update); }最终的结果如下图所示还挺幽默的那么如果是用的AzureOpenAI呢只需要换成下面即可var jokerAgent new AzureOpenAIClient( new Uri(azureOpenAIProvider.Endpoint), new ApiKeyCredential(azureOpenAIProvider.ApiKey)) .GetChatClient(azureOpenAIProvider.ModelId) .CreateAIAgent(instructions: You are good at telling jokes., name: Joker); Console.WriteLine(await jokerAgent.RunAsync(Tell me a joke about a pirate.));2多轮对话我们还可以通过AgentThread这个特性来实现具有“短期记忆”的多轮对话var thread1 jokerAgent.GetNewThread(); var thread2 jokerAgent.GetNewThread(); Console.WriteLine(await jokerAgent.RunAsync(Tell me a joke about a pirate., thread1) Environment.NewLine); Console.WriteLine(await jokerAgent.RunAsync(Now add some emojis to the joke and tell it in the voice of a pirates parrot., thread1) Environment.NewLine); Console.WriteLine(await jokerAgent.RunAsync(Tell me a joke about a robot., thread2) Environment.NewLine); Console.WriteLine(await jokerAgent.RunAsync(Now add some emojis to the joke and tell it in the voice of a robot., thread2) Environment.NewLine);3函数调用此外在MAF中进行函数方法调用也是十分的轻松定义一个简单的函数方法public class WeatherServicePlugin { [Description(Get current weather for a location)] public static async Taskstring GetCurrentWeatherAsync(string location) { return $The weather in {location} is cloudy with a high of 15°C.; } }然后再创建Agent时将工具列表告诉Agent即可var agent new OpenAIClient( new ApiKeyCredential(openAIProvider.ApiKey), new OpenAIClientOptions { Endpoint new Uri(openAIProvider.Endpoint) }) .GetChatClient(openAIProvider.ModelId) .CreateAIAgent( instructions: You are a helpful assistant, tools: [AIFunctionFactory.Create(WeatherServicePlugin.GetCurrentWeatherAsync)]); Console.WriteLine(await agent.RunAsync(Tell me the weather like in Chengdu?));最终显示的结果如下而在实际开发中基于MAF和原有的ASP.NET Core框架将依赖注入和生命周期管理纳入进来可以实现更为强大的模块化力量。4人工审批的函数调用相信我们在使用GitHub Copilot之类的编码智能体时会遇到当它想要执行某个命令行或工具调用时会先征求你的审批同意后它才会执行。其实在Agent开发中人工审批的工具调用也是一个常见的需求而在MAF中它也支持我们快速实现这个需求。下面这个示例演示了如何实现人工审批的工具调用var weatherTool AIFunctionFactory.Create(WeatherServicePlugin.GetCurrentWeatherAsync); var approvalRequiredWeatherTool new ApprovalRequiredAIFunction(weatherTool); var agent new OpenAIClient( new ApiKeyCredential(openAIProvider.ApiKey), new OpenAIClientOptions { Endpoint new Uri(openAIProvider.Endpoint) }) .GetChatClient(openAIProvider.ModelId) .CreateAIAgent(instructions: You are a helpful assistant, tools: [approvalRequiredWeatherTool]); var thread agent.GetNewThread(); var response await agent.RunAsync(Tell me the weather like in Chengdu?, thread); var userInputRequests response.UserInputRequests.ToList(); while(userInputRequests.Count 0) { // Ask the user to approve each function call request. var userInputResponses userInputRequests .OfTypeFunctionApprovalRequestContent() .Select(functionApprovalRequest { Console.WriteLine($The agent would like to invoke the following function, please reply Y to approve: Name {functionApprovalRequest.FunctionCall.Name}); return new ChatMessage(ChatRole.User, [functionApprovalRequest.CreateResponse(Console.ReadLine()?.Equals(Y, StringComparison.OrdinalIgnoreCase) ?? false)]); }) .ToList(); // Pass the user input responses back to the agent for further processing. response await agent.RunAsync(userInputResponses, thread); userInputRequests response.UserInputRequests.ToList(); } Console.WriteLine(Environment.NewLine $Agent: {response});执行结果如下图所示只有当我们人工输入Y确认后它才会调用工具执行。小结本文介绍了MAF的基本功能AI Agent和Workflow的基本概念以及通过一个.NET控制台项目快速创建了一个Agent的代码示例。下一篇我们将继续MAF的学习。示例源码Github:https://github.com/EdisonTalk/MAFD参考资料Microsoft Learn《Agent Framework Tutorials》https://learn.microsoft.com/en-us/agent-framework/tutorials引入地址