幸运的桃子 發表於 2025-7-17 15:55:00

Semantic Kernel Agent Orchestration编排

<h4>一、多代理编排核心价值</h4>
<p>Semantic Kernel的Agent Orchestration框架解决了传统单代理系统的局限性:</p>
<pre><code>// 统一调用接口示例(适用于所有模式)
InProcessRuntime runtime = new();
await runtime.StartAsync();

// 任意编排模式通用执行流程
OrchestrationResult&lt;string&gt; result = await orchestration.InvokeAsync(task, runtime);
string output = await result.GetValueAsync();</code></pre>
<p>通过协调具有​<strong>​不同专业技能​</strong>​的代理(如物理专家、化学专家),构建出能处理复杂工作流的协作系统。​<strong>​实验性提示​</strong>​:当前功能仍处实验阶段,API可能变更。</p>
<h4>二、五种编排模式详解</h4>
<h6>1. 并发编排 (Concurrent)</h6>
<p>​<strong>​原理​</strong>​:向所有代理广播任务,独立收集结果<br>
​<strong>​适用场景​</strong>​:并行分析、多方案决策</p>
<pre><code>// 定义不同领域的专家代理
ChatCompletionAgent physicist = new() {
    Name = "物理专家",
    Instructions = "你是一名物理学专家,从物理角度回答问题",
    Kernel = kernel
};

ChatCompletionAgent chemist = new() {
    Name = "化学专家",
    Instructions = "你是一名化学专家,从化学角度分析问题",
    Kernel = kernel
};

// 创建并发编排
ConcurrentOrchestration orchestration = new(physicist, chemist);

// 执行任务(获取温度定义的多元视角)
var result = await orchestration.InvokeAsync("温度是什么?", runtime);
string[] outputs = await result.GetValueAsync();

/* 输出示例:
物理专家:温度是衡量粒子平均动能的物理量...
化学专家:温度反映了分子热运动的剧烈程度...
*/</code></pre>
<h6>2. 顺序编排 (Sequential)</h6>
<p>​<strong>​原理​</strong>​:按预设顺序传递处理结果,形成流水线<br>
​<strong>​适用场景​</strong>​:多阶段文档处理、供应链管理</p>
<pre><code>// 创建营销处理流水线
ChatCompletionAgent analyst = new() {
    Name = "市场分析师",
    Instructions = "你是一名市场分析师,从产品描述中提取:\n-核心功能\n-目标用户\n-独特卖点"
};

ChatCompletionAgent writer = new() {
    Name = "文案撰写人",
    Instructions = "根据分析结果撰写150字营销文案,只需输出文案正文"
};

ChatCompletionAgent editor = new() {
    Name = "内容编辑",
    Instructions = "优化文案语法,提升表达清晰度,保持风格统一"
};

// 构建顺序链
SequentialOrchestration orchestration = new(analyst, writer, editor);

// 执行产品文案生成
var result = await orchestration.InvokeAsync(
    "一款环保不锈钢水杯,可保持饮品低温24小时", runtime);

/* 输出示例:
市场分析师:核心功能:环保材料、24小时保冷...
文案撰写人:全新环保不锈钢水杯,让您的冷饮持续冰爽...
内容编辑:【最终文案】创新环保不锈钢水杯...
*/</code></pre>
<h6>3. 群聊编排 (Group Chat)</h6>
<p>​<strong>​原理​</strong>​:通过管理器协调多代理对话<br>
​<strong>​适用场景​</strong>​:头脑风暴、争议解决</p>
<pre><code>// 构建创意团队
ChatCompletionAgent writer = new() {
    Name = "文案专员",
    Instructions = "你是有十年经验的文案专家,擅长简洁幽默的表达,每次仅提出一个方案"
};

ChatCompletionAgent director = new() {
    Name = "艺术总监",
    Instructions = "你是一位崇尚David Ogilvy的创意总监,负责审核文案质量"
};

// 创建轮询式群聊(5轮对话上限)
GroupChatOrchestration orchestration = new(
    new RoundRobinGroupChatManager { MaximumInvocationCount = 5 },
    writer, director
);

// 发起创意任务
var result = await orchestration.InvokeAsync(
    "为经济型电动SUV设计广告标语", runtime);

/* 输出示例:
文案专员:“电动驾趣,触手可及”
艺术总监:建议加入环保元素...
文案专员:“绿色动力,轻松驰骋”
*/</code></pre>
<h6>4. 移交编排 (Handoff)</h6>
<p>​<strong>​原理​</strong>​:根据上下文动态转移控制权<br>
​<strong>​适用场景​</strong>​:多级客服系统、专家转接</p>
<pre><code>// 构建客服代理链
OrchestrationHandoffs handoffs = OrchestrationHandoffs
    .StartWith(triageAgent)
    .Add(triageAgent, statusAgent, returnAgent, refundAgent)
    .Add(statusAgent, triageAgent, "遇到非订单问题转接")
    .Add(returnAgent, triageAgent, "遇到非退货问题转接");

// 模拟用户咨询队列
Queue&lt;string&gt; queries = new(new[] {
    "我想查询订单状态", "订单号123", "需要退货", "订单号456", "商品破损"
});

// 创建移交编排(支持人工介入)
HandoffOrchestration orchestration = new(handoffs) {
    InteractiveCallback = () =&gt; ValueTask.FromResult(
      new ChatMessageContent(AuthorRole.User, queries.Dequeue()))
};

// 处理用户请求
var result = await orchestration.InvokeAsync("我需要订单帮助", runtime);

/* 输出示例:
工单代理:请问您需要什么帮助?
订单代理:请提供订单号
退货代理:请描述退货原因...
*/</code></pre>
<h6>5. Magentic编排</h6>
<p>​<strong>​原理​</strong>​:管理器动态协调代理分工<br>
​<strong>​适用场景​</strong>​:研究分析、跨领域协作</p>
<pre><code>// 创建研究+执行代理
ChatCompletionAgent researcher = new() {
    Name = "研究专员",
    Instructions = "你负责收集信息,不执行计算或量化分析",
    Kernel = researchKernel // 使用gpt-4o-search-preview模型
};

AzureAIAgent coder = new() {
    Name = "代码专员",
    Instructions = "你使用代码处理数据,需提供详细分析过程",
    Tools = { new CodeInterpreterToolDefinition() }// 代码解释器
};

// 配置Magentic管理器
StandardMagenticManager manager = new(...) {
    MaximumInvocationCount = 5// 最大调用次数
};

// 构建复杂任务协作
MagenticOrchestration orchestration = new(manager, researcher, coder);

// 执行综合研究任务
var result = await orchestration.InvokeAsync(@"
比较ResNet-50、BERT-base和GPT-2在Azure Standard_NC6s_v3 VM上训练24小时的
能耗与CO2排放量,按图像分类/文本分类/文本生成任务给出建议", runtime);

/* 输出示例:
研究专员:根据论文A,ResNet-50训练能耗约45kWh...
代码专员:排放量计算代码:
    emissions = energy * 0.387 // 微软区域排放因子
表格结果:
| 模型       | 任务类型       | 能耗(kWh) | CO2(kg) |
|----------|--------------|----------|---------|
| ResNet-50| 图像分类      | 42       | 16.25   |
*/</code></pre>
<h4>三、开发准备</h4>
<ol>
<li>安装必要包:</li>
</ol>
<pre><code>dotnet add package Microsoft.SemanticKernel.Agents.Orchestration --prerelease
dotnet add package Microsoft.SemanticKernel.Agents.Runtime.InProcess --prerelease</code></pre>
<ol start="2">
<li>统一架构优势:</li>
</ol>
<pre><code>// 所有模式共享相同调用接口
await orchestration.InvokeAsync(task, runtime);
await result.GetValueAsync();
await runtime.RunUntilIdleAsync(); </code></pre>
<blockquote>
<p>​<strong>​实验性声明​</strong>​:本文所述功能仍处开发阶段,API可能变更,生产环境慎用。完整示例代码详见官方GitHub仓库。</p>
</blockquote>
<p><em>&nbsp;</em></p><br><br>
来源:https://www.cnblogs.com/chenyishi/p/18989861
頁: [1]
查看完整版本: Semantic Kernel Agent Orchestration编排