MAF快速入门(22)声明式Agent实战

发布时间:2026/7/3 2:49:19
MAF快速入门(22)声明式Agent实战 在之前的博文中我们都是将智能体的定义如系统提示词等内嵌在代码中这样其实对非IT技术人员不太友好是否有一种声明式的方法将代码和定义分开呢答案是Yes我们可以将智能体的核心行为和配置通过声明式文件如yaml文件来进行定义这就使得智能体的定义可以像代码一样被纳入版本控制系统遵循DevOps的最佳实践提高了协作效率和部署的一致性 。2 快速开始多语言客服助手这里我们来做一个quick start体验声明式智能体定义的好处。这个智能体是一个客服助手可以提供多语言的友好回复。在开始之前我们创建了一个控制台应用并安装了以下NuGet包PackageReference IncludeMicrosoft.Agents.AI.Declarative Version1.0.0-rc5 / PackageReference IncludeMicrosoft.Agents.AI.OpenAI Version1.0.0 /CustomerSupportAgent.prompt.yml首先我们创建一个yaml文件内容如下kind: Prompt name: CustomerSupportAgent description: 多语言客服助手 instructions: | 你是一个专业的客服助手。 你的任务是 1. 使用用户的语言回答问题 2. 提供清晰、礼貌、专业的回复 3. 判断问题类型订单/退款/技术/其他 4. 判断用户情绪正面/中性/负面 注意 - 回答要简洁 - 如果是负面情绪语气要更安抚 - 不要编造信息 model: options: temperature: 0.3 topP: 0.9 outputSchema: properties: language: type: string description: 用户使用的语言 required: true category: type: string description: 问题分类order/refund/technical/other required: true sentiment: type: string description: 情绪positive/neutral/negative required: true answer: type: string description: 客服回复内容 required: true可以看到上面的内容其实实现了我们之前对智能体的系统提示词以及模型参数的设置甚至还定义了模型的输出格式是一个包含4个字段的强类型schema。这里为了正确解析到回答的内容我们定义一个模型类public sealed record CustomerSupportResponse( [property: JsonPropertyName(language)] string Language, [property: JsonPropertyName(category)] string Category, [property: JsonPropertyName(sentiment)] string Sentiment, [property: JsonPropertyName(answer)] string Answer);准备工作创建ChatClient其次我们创建ChatClient老面孔了var config new ConfigurationBuilder() .AddJsonFile($appsettings.json, optional: false, reloadOnChange: true) .Build(); var openAIProvider config.GetSection(OpenAI).GetOpenAIProvider(); var chatClient new OpenAIClient( new ApiKeyCredential(openAIProvider.ApiKey), new OpenAIClientOptions { Endpoint new Uri(openAIProvider.Endpoint) }) .GetChatClient(openAIProvider.ModelId) .AsIChatClient();基于yaml文件创建Agent这里我们读取yaml文件内容并基于yaml来创建Agentvar text await File.ReadAllTextAsync(CustomerSupportAgent.prompt.yml); var agentFactory new ChatClientPromptAgentFactory(chatClient); var agent await agentFactory.CreateFromYamlAsync(text); Console.WriteLine(✅ 基于YAML定义创建Agent成功); Console.WriteLine();测试回答问题既然是多语言客服助手我们就来测试一个中文一个英文吧var session await agent.CreateSessionAsync(); Console.WriteLine(━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━); Console.WriteLine($开始测试使用中文回答); var question1 用中文回答为什么我的订单货还没有到。; Console.WriteLine($ 用户: {question1}); Console.WriteLine(); Console.WriteLine(━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━); Console.WriteLine($ Agent:); var response1 await agent.RunAsync(question1, session); var responseModel1 JsonSerializer.DeserializeCustomerSupportResponse(response1.Text); Console.WriteLine(responseModel1.Answer); Console.WriteLine(━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━); Console.WriteLine(); Console.WriteLine($开始测试使用英文回答); var question2 When my order can be handled?; Console.WriteLine($ 用户: {question2}); Console.WriteLine(); Console.WriteLine(━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━); Console.WriteLine($ Agent:); var response2 await agent.RunAsync(question2, session); var responseModel2 JsonSerializer.DeserializeCustomerSupportResponse(response2.Text); Console.WriteLine(responseModel2.Answer); Console.WriteLine(━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━); Console.WriteLine();测试结果如下图所示