중요합니다
에이전트 프레임워크의 에이전트 오케스트레이션 기능은 실험적 단계에 있습니다. 현재 개발 중이며 미리 보기 또는 릴리스 후보 단계로 넘어가기 전에 크게 변경될 수 있습니다.
동시 오케스트레이션을 사용하면 여러 에이전트가 동일한 작업에서 병렬로 작업할 수 있습니다. 각 에이전트는 입력을 독립적으로 처리하고 결과를 수집 및 집계합니다. 이 접근 방식은 브레인스토밍, 앙상블 추론 또는 투표 시스템과 같은 다양한 관점이나 솔루션이 중요한 시나리오에 적합합니다.
패턴을 사용하는 시기 또는 패턴을 피해야 하는 경우와 같은 패턴에 대한 자세한 내용은 동시 오케스트레이션을 참조하세요.
일반 사용 예
여러 에이전트가 문제에 대해 서로 다른 솔루션을 생성하고 추가 분석 또는 선택을 위해 응답이 수집됩니다.
학습 내용
- 다양한 전문 지식을 갖춘 여러 에이전트를 정의하는 방법
- 단일 작업에서 동시에 작동하도록 이러한 에이전트를 오케스트레이션하는 방법
- 결과를 수집하고 처리하는 방법
에이전트를 정의하세요
에이전트는 작업을 처리할 수 있는 특수 엔터티입니다. 여기서는 물리학 전문가와 화학 전문가라는 두 가지 에이전트를 정의합니다.
팁 (조언)
여기서 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에서 아직 사용할 수 없습니다.