Von Bedeutung
エージェント フレームワークのエージェント オーケストレーション機能は、試験段階にあります。 これらはアクティブな開発中であり、プレビューまたはリリース候補ステージに進む前に大幅に変更される可能性があります。
同時実行パターンにより、複数のエージェントが同じタスクで並列に作業できるようになります。 各エージェントは入力を個別に処理し、その結果が収集および集計されます。 このアプローチは、ブレーンストーミング、アンサンブル推論、投票システムなど、多様な視点やソリューションが価値のあるシナリオに適しています。
一般的なユース ケース
複数のエージェントが問題に対して異なる解決策を生成し、さらに分析または選択するために応答が収集されます。
ここでは、次の内容について学習します
- さまざまな専門知識を持つ複数のエージェントを定義する方法
- 1 つのタスクで同時に動作するようにこれらのエージェントを調整する方法
- 結果を収集して処理する方法
エージェントの定義
エージェントは、タスクを処理できる特殊なエンティティです。 ここでは、物理専門家と化学専門家の 2 つのエージェントを定義します。
ヒント
ここでは 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 ...
ヒント
完全なサンプル コードについては、こちらを参照してください。
エージェントの定義
エージェントは、タスクを処理できる特殊なエンティティです。 ここでは、物理専門家と化学専門家の 2 つのエージェントを定義します。
ヒント
この 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 ではまだ使用できません。