次の方法で共有


Microsoft Agent Framework ワークフローのコア概念 - Executors

このドキュメントでは、Microsoft Agent Framework ワークフロー システムの Executors コンポーネントについて詳しく説明します。

概要

Executor は、ワークフロー内のメッセージを処理する基本的な構成要素です。 型指定されたメッセージを受信し、操作を実行し、出力メッセージまたはイベントを生成できる自律処理ユニットです。

Executor は、 Executor<TInput, TOutput> 基底クラスから継承します。 各 Executor には一意の識別子があり、特定のメッセージの種類を処理できます。

基本的な Executor 構造体

using Microsoft.Agents.AI.Workflows;
using Microsoft.Agents.AI.Workflows.Reflection;

internal sealed class UppercaseExecutor() : Executor<string, string>("UppercaseExecutor")
{
    public async ValueTask<string> HandleAsync(string message, IWorkflowContext context)
    {
        string result = message.ToUpperInvariant();
        return result; // Return value is automatically sent to connected executors
    }
}

値を返さずに手動でメッセージを送信できます。

internal sealed class UppercaseExecutor() : Executor<string>("UppercaseExecutor")
{
    public async ValueTask HandleAsync(string message, IWorkflowContext context)
    {
        string result = message.ToUpperInvariant();
        await context.SendMessageAsync(result); // Manually send messages to connected executors
    }
}

ConfigureRoutes メソッドをオーバーライドすることで、複数の入力型を処理することもできます。

internal sealed class SampleExecutor() : Executor("SampleExecutor")
{
    protected override RouteBuilder ConfigureRoutes(RouteBuilder routeBuilder)
    {
        return routeBuilder
            .AddHandler<string>(this.HandleStringAsync)
            .AddHandler<int>(this.HandleIntAsync);
    }

    /// <summary>
    /// Converts input string to uppercase
    /// </summary>
    public async ValueTask<string> HandleStringAsync(string message, IWorkflowContext context)
    {
        string result = message.ToUpperInvariant();
        return result;
    }

    /// <summary>
    /// Doubles the input integer
    /// </summary>
    public async ValueTask<int> HandleIntAsync(int message, IWorkflowContext context)
    {
        int result = message * 2;
        return result;
    }
}

BindExecutor拡張メソッドを使用して、関数から Executor を作成することもできます。

Func<string, string> uppercaseFunc = s => s.ToUpperInvariant();
var uppercase = uppercaseFunc.BindExecutor("UppercaseExecutor");

Executor は、 Executor 基底クラスから継承します。 各 Executor には一意の識別子があり、 @handler デコレーターで修飾されたメソッドを使用して、特定のメッセージの種類を処理できます。 ハンドラーには、処理できるメッセージの種類を指定するための適切な注釈が必要です。

基本的な Executor 構造体

from agent_framework import (
    Executor,
    WorkflowContext,
    handler,
)

class UpperCase(Executor):

    @handler
    async def to_upper_case(self, text: str, ctx: WorkflowContext[str]) -> None:
        """Convert the input to uppercase and forward it to the next node.

        Note: The WorkflowContext is parameterized with the type this handler will
        emit. Here WorkflowContext[str] means downstream nodes should expect str.
        """
        await ctx.send_message(text.upper())

@executorデコレーターを使用して、関数から Executor を作成できます。

from agent_framework import (
    WorkflowContext,
    executor,
)

@executor(id="upper_case_executor")
async def upper_case(text: str, ctx: WorkflowContext[str]) -> None:
    """Convert the input to uppercase and forward it to the next node.

    Note: The WorkflowContext is parameterized with the type this handler will
    emit. Here WorkflowContext[str] means downstream nodes should expect str.
    """
    await ctx.send_message(text.upper())

複数のハンドラーを定義することで、複数の入力型を処理することもできます。

class SampleExecutor(Executor):

    @handler
    async def to_upper_case(self, text: str, ctx: WorkflowContext[str]) -> None:
        """Convert the input to uppercase and forward it to the next node.

        Note: The WorkflowContext is parameterized with the type this handler will
        emit. Here WorkflowContext[str] means downstream nodes should expect str.
        """
        await ctx.send_message(text.upper())

    @handler
    async def double_integer(self, number: int, ctx: WorkflowContext[int]) -> None:
        """Double the input integer and forward it to the next node.

        Note: The WorkflowContext is parameterized with the type this handler will
        emit. Here WorkflowContext[int] means downstream nodes should expect int.
        """
        await ctx.send_message(number * 2)

WorkflowContext オブジェクト

WorkflowContext オブジェクトは、実行中にハンドラーがワークフローと対話するためのメソッドを提供します。 WorkflowContextは、ハンドラーが出力するメッセージの種類と、生成できる出力の種類でパラメーター化されます。

最も一般的に使用されるメソッドは send_message です。これにより、ハンドラーは接続された Executor にメッセージを送信できます。

from agent_framework import WorkflowContext

class SomeHandler(Executor):

    @handler
    async def some_handler(message: str, ctx: WorkflowContext[str]) -> None:
        await ctx.send_message("Hello, World!")

ハンドラーは、 yield_output を使用して、ワークフロー出力と見なされ、出力イベントとして呼び出し元に返/ストリーミングされる出力を生成できます。

from agent_framework import WorkflowContext

class SomeHandler(Executor):

    @handler
    async def some_handler(message: str, ctx: WorkflowContext[Never, str]) -> None:
        await ctx.yield_output("Hello, World!")

ハンドラーがメッセージを送信せず、出力を生成しない場合、 WorkflowContextに型パラメーターは必要ありません。

from agent_framework import WorkflowContext

class SomeHandler(Executor):

    @handler
    async def some_handler(message: str, ctx: WorkflowContext) -> None:
        print("Doing some work...")

次のステップ