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

大家好,我是Edison。

最近我一直在跟着圣杰的《.NET+AI智能体开发进阶》课程学习MAF开发多智能体工作流,我强烈推荐你也上车跟我一起出发!

上一篇,我们了解了MAF新推出的script执行能力。而就在上周,MAF 1.0.0正式版也发布了(请见devblogs的这篇推文:Microsoft Agent Framework Version 1.0),这意味着breaking change会变得少很多了,我们的Agent也可以上生产了!

本篇,我们来了解下声明式Agent的用法。

1 声明式Agent

在之前的博文中,我们都是将智能体的定义(如系统提示词等)内嵌在代码中,这样其实对非IT技术人员不太友好,是否有一种声明式的方法将代码和定义分开呢?

答案是Yes,我们可以将智能体的核心行为和配置通过声明式文件(如yaml文件)来进行定义,这就使得智能体的定义可以像代码一样被纳入版本控制系统,遵循DevOps的最佳实践,提高了协作效率和部署的一致性 。

2 快速开始:多语言客服助手

这里我们来做一个quick start,体验声明式智能体定义的好处。这个智能体是一个客服助手,可以提供多语言的友好回复。

在开始之前,我们创建了一个控制台应用,并安装了以下NuGet包:

复制代码
<PackageReference Include="Microsoft.Agents.AI.Declarative" Version="1.0.0-rc5" />
<PackageReference Include="Microsoft.Agents.AI.OpenAI" Version="1.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").Get<OpenAIProvider>();
var chatClient = new OpenAIClient(
        new ApiKeyCredential(openAIProvider.ApiKey),
        new OpenAIClientOptions { Endpoint = new Uri(openAIProvider.Endpoint) })
    .GetChatClient(openAIProvider.ModelId)
    .AsIChatClient();

基于yaml文件创建Agent

这里我们读取yaml文件内容,并基于yaml来创建Agent:

复制代码
var 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.Deserialize<CustomerSupportResponse>(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.Deserialize<CustomerSupportResponse>(response2.Text);
Console.WriteLine(responseModel2.Answer);
Console.WriteLine("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
Console.WriteLine();

测试结果如下图所示:

可以看到,无论是中文还是英文,它都按照定义输出了回答的具体内容。

3 小结

本文介绍了声明式智能体的基本概念,并通过一个DEMO案例介绍了如何在MAF框架中使用声明式智能体,通过声明式定义,可以将代码和业务分离,还能使智能体的定义可以像代码一样被纳入版本控制系统,遵循DevOps的最佳实践,提高协作效率和部署的一致性 。

示例源码

GitHub: https://github.com/EdisonTalk/MAFD

参考资料

圣杰,《.NET + AI 智能体开发进阶》(推荐指数:★★★★★)

Microsoft Learn,《Agent Framework Tutorials


作者:爱迪生

出处:https://edisontalk.cnblogs.com

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。

相关推荐
吴佳浩7 小时前
Skill 为什么不同于 Tool?Agent 技能库的自演进与动态加载机制
人工智能·agent·ai编程
吴佳浩7 小时前
Tool 的安全性与执行沙箱:从 Docker 到 gVisor 的防御架构
agent·ai编程·mcp
全栈弄潮儿9 小时前
我的代码审查 Prompt:从“能跑”到“可维护”
aigc·openai·ai编程
AIGCmagic社区10 小时前
具身智能专题:机器人也有Scaling Law?智元GE-Act 2.0用3万小时真机数据给出答案
人工智能·aigc·具身智能
Setsuna_F_Seiei10 小时前
前端转型 Agent 开发 05 之 Agent Hooks 与 Checkpointer(让 Agent 从全自动转变人为可掌控)
前端·agent·ai编程
Web3&Basketball11 小时前
CRM Agent 后训练实战:3 倍更少错误
python·架构·大模型·agent·推理
米小虾12 小时前
你的 harness 技巧有保质期:176 组对照实验显示,上下文管理的收益从 35.7 分跌到 2.7 分
人工智能·agent
Together_CZ13 小时前
DeepSeek-V4.1-Flash:Pushing the Limits of KV Cache Compression——推动 KV 缓存压缩的极限
缓存·llm·compression·kv cache·deepseek·v4.1-flash·推动 kv 缓存压缩的极限
stormzhangV13 小时前
别吹 Jev 了
人工智能·aigc·ai编程
赛博仓鼠15 小时前
秋叶ComfyUI 3.2整合包实测:Python 3.13+Torch 2.13全栈升级,一键跑通MiniMax H3(附完整部署)
ai·ai作画·aigc·音视频