并发编排

重要

代理框架中的代理编排功能处于实验阶段。 它们处于积极开发阶段,在升级到预览版或候选发布阶段之前可能会发生重大变化。

并发模式使多个代理能够并行处理同一任务。 每个代理独立处理输入,并收集并聚合其结果。 此方法非常适合多种观点或解决方案很有价值的情况,例如集思广益、群体推理以及其他投票系统。

常见用例

多个代理生成问题的不同解决方案,并收集其响应以进一步分析或选择:

图解

学习内容

  • 如何定义具有不同专业知识的多个代理
  • 如何协调这些代理程序使其在单个任务上同时工作
  • 如何收集和处理结果

定义您的代理

代理是可以处理任务的专用实体。 在这里,我们定义了两个代理:一个物理专家和一个化学专家。

小窍门

在此,使用了 ChatCompletionAgent,但您可以使用任何 代理类型

using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Agents;
using Microsoft.SemanticKernel.Agents.Orchestration;
using Microsoft.SemanticKernel.Agents.Orchestration.Concurrent;
using Microsoft.SemanticKernel.Agents.Runtime.InProcess;

// Create a kernel with an AI service
Kernel kernel = ...;

ChatCompletionAgent physicist = new ChatCompletionAgent{
    Name = "PhysicsExpert",
    Instructions = "You are an expert in physics. You answer questions from a physics perspective."
    Kernel = kernel,
};

ChatCompletionAgent chemist = new ChatCompletionAgent{
    Name = "ChemistryExpert",
    Instructions = "You are an expert in chemistry. You answer questions from a chemistry perspective."
    Kernel = kernel,
};

设置并发编排

ConcurrentOrchestration 允许并行运行多个代理。 将代理对象列表传递给成员。

ConcurrentOrchestration orchestration = new (physicist, chemist);

启动运行时

需要运行时才能管理代理的执行。 在这里,我们在调用业务流程之前使用 InProcessRuntime 并启动它。

InProcessRuntime runtime = new InProcessRuntime();
await runtime.StartAsync();

调用编排

现在可以使用特定任务调用编排。 编排将并发运行给定任务上的所有代理。

var result = await orchestration.InvokeAsync("What is temperature?", runtime);

收集结果

可以异步收集来自所有代理的结果。 请注意,不保证结果的顺序。

string[] output = await result.GetValueAsync(TimeSpan.FromSeconds(20));
Console.WriteLine($"# RESULT:\n{string.Join("\n\n", output.Select(text => $"{text}"))}");

可选:停止运行时

处理完成后,停止运行时以清理资源。

await runtime.RunUntilIdleAsync();

示例输出

# RESULT:
Temperature is a fundamental physical quantity that measures the average kinetic energy ...

Temperature is a measure of the average kinetic energy of the particles ...

小窍门

此处提供了完整的示例代码

定义您的代理

代理是可以处理任务的专用实体。 在这里,我们定义了两个代理:一个物理专家和一个化学专家。

小窍门

此处 ChatCompletionAgent 与 Azure OpenAI 一起使用,但可以使用任何 代理类型模型服务

from semantic_kernel.agents import Agent, ChatCompletionAgent
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion

def get_agents() -> list[Agent]:
    physics_agent = ChatCompletionAgent(
        name="PhysicsExpert",
        instructions="You are an expert in physics. You answer questions from a physics perspective.",
        service=AzureChatCompletion(),
    )
    chemistry_agent = ChatCompletionAgent(
        name="ChemistryExpert",
        instructions="You are an expert in chemistry. You answer questions from a chemistry perspective.",
        service=AzureChatCompletion(),
    )
    return [physics_agent, chemistry_agent]

设置并发编排

ConcurrentOrchestration 允许并行运行多个代理。 将代理对象列表传递给成员。

from semantic_kernel.agents import ConcurrentOrchestration

agents = get_agents()
concurrent_orchestration = ConcurrentOrchestration(members=agents)

启动运行时

需要运行时才能管理代理的执行。 在这里,我们在调用业务流程之前使用 InProcessRuntime 并启动它。

from semantic_kernel.agents.runtime import InProcessRuntime

runtime = InProcessRuntime()
runtime.start()

调用编排

现在可以使用特定任务调用编排。 编排将并发运行给定任务上的所有代理。

orchestration_result = await concurrent_orchestration.invoke(
    task="What is temperature?",
    runtime=runtime,
)

收集结果

可以异步收集来自所有代理的结果。 请注意,不保证结果的顺序。

value = await orchestration_result.get(timeout=20)
# For the concurrent orchestration, the result is a list of chat messages
for item in value:
    print(f"# {item.name}: {item.content}")

可选:停止运行时

处理完成后,停止运行时以清理资源。

await runtime.stop_when_idle()

示例输出

# PhysicsExpert: Temperature is a physical quantity that represents the average kinetic energy of the particles in a substance...
# ChemistryExpert: Temperature is a fundamental concept in chemistry and physics, representing a measure of the average kinetic energy...

小窍门

此处提供了完整的示例代码。

注释

代理编排在 Java SDK 中尚不可用。

后续步骤