最近在折腾 AI Agent,体验了一下 Microsoft Agent Framework 里的“声明式 Agent”,感觉挺实用的,尤其适合团队协作。
下面是完整代码,可以直接参考:
using Azure.AI.OpenAI;using Microsoft.Agents.AI;using Microsoft.Extensions.AI;using System.ClientModel;var arr = File.ReadAllLines("C:/gpt/azure_gpt5.4_mini.txt");var endpoint = arr[1];var deploymentName = arr[0];var credential = new ApiKeyCredential(arr[2]);IChatClient chatClient = new AzureOpenAIClient( new Uri(endpoint), credential) .GetChatClient(deploymentName) .AsIChatClient();var text = """ 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 """;var agentFactory = new ChatClientPromptAgentFactory(chatClient);var agent = await agentFactory.CreateFromYamlAsync(text);Console.WriteLine(await agent!.RunAsync("用中文回答为什么我的订单货还没有到。"));await foreach (var update in agent!.RunStreamingAsync("用日语回答,我的退单多会处理。")){ Console.Write(update);}
csproj语言件是:
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net10.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> </PropertyGroup><ItemGroup><PackageReference Include="Azure.AI.OpenAI" Version="2.9.0-beta.1" /><PackageReference Include="Azure.Identity" Version="1.19.0" /><PackageReference Include="Microsoft.Agents.AI.Declarative" Version="1.0.0-rc4" /><PackageReference Include="Microsoft.Agents.AI.OpenAI" Version="1.0.0-rc4" /></ItemGroup></Project>
先看效果更直观。我写了一个简单的多语言客服助手,只需要输入一句话,比如:“用中文回答为什么我的订单货还没有到。”
它返回的不只是普通文本,而是结构化结果,比如语言、问题类型(订单)、情绪,以及一段专业客服回复。
再换一句:“用日语回答,我的退单多会处理。”
它会用日语流式输出回复,一边生成一边展示,很适合做对话界面。
重点是,这个 Agent 的能力并不是靠写一堆业务代码实现的,而是用一段 YAML 定义出来的。包括它的角色、行为规则、模型参数,甚至输出格式,都是“声明”出来的。
可以看到,代码其实很薄,核心逻辑都在那段 YAML 里。
这也是声明式 Agent 最有意思的地方。你不再需要把 Prompt 写死在代码里,而是把 Agent 的“人格、规则和输出”全部配置化。要改语气、改规则、加字段,直接改 YAML 就行,不用改代码发版。
简单来说,它把“写 Prompt”这件事,从工程代码里抽出来,变成了一个可以被团队共同维护的配置文件。
如果你正在做 AI 应用,这种方式真的值得试一试。
声明:来自硅基-桂迹,仅代表创作者观点。链接:https://eyangzhen.com/6957.html