次の方法で共有


手順 6: エージェントをホストする

エージェントを構築したら、ユーザーや他のエージェントがエージェントと対話できるように、エージェントをホストする必要があります。

ホスティング オプション

Option Description 最適な対象者
A2A プロトコル エージェント間プロトコルを使用してエージェントを公開する マルチエージェント システム
OpenAI互換性のあるエンドポイント チャットの完了または応答 API を使用してエージェントを公開する OpenAI と互換性のあるクライアント
Azure Functions (Durable) 永続的な Azure Functions としてエージェントを実行する サーバーレスで実行時間の長いタスク
AG-UI プロトコル Web ベースの AI エージェント アプリケーションを構築する Web フロントエンド

ASP.NET Core でのホスティング

Agent Framework には、AI エージェントを ASP.NET Core アプリケーションに統合できるホスティング ライブラリが用意されています。 これらのライブラリを使用すると、さまざまなプロトコルを使用したエージェントの登録、構成、公開が簡単になります。

エージェントの概要で説明されているように、AIAgentはエージェント フレームワークの基本的な概念です。 ユーザー入力を処理し、意思決定を行い、ツールを呼び出し、アクションを実行して応答を生成するための追加作業を実行する "LLM ラッパー" を定義します。 ASP.NET Core アプリケーションから AI エージェントを公開することは簡単ではありません。 ホスティング ライブラリは、依存関係挿入コンテナーに AI エージェントを登録することでこれを解決し、アプリケーション サービスで解決して使用できるようにします。 また、ツールやセッション ストレージなどのエージェントの依存関係を同じコンテナーから管理することもできます。 エージェントは、使用するプロトコルに関係なく、アプリケーション インフラストラクチャと共にホストできます。 同様に、ワークフローをホストし、アプリケーションの一般的なインフラストラクチャを活用できます。

コア ホスティング ライブラリ

Microsoft.Agents.AI.Hosting ライブラリは、ASP.NET Core で AI エージェントをホストするための基盤です。 AI エージェントとワークフローを登録して構成する IHostApplicationBuilder の拡張機能を提供します。 ASP.NET Core では、 IHostApplicationBuilder は、ホストされているアプリケーションとサービスのビルダー、構成、ログ記録、有効期間などを管理する基本的な種類です。

エージェントまたはワークフローを構成する前に、依存関係挿入コンテナーに IChatClient を登録します。 次の例では、 chat-modelという名前のキー付きシングルトンとして登録されています。

// endpoint is of 'https://<your-own-foundry-endpoint>.openai.azure.com/' format
// deploymentName is 'gpt-4o-mini' for example

IChatClient chatClient = new AzureOpenAIClient(
        new Uri(endpoint),
        new DefaultAzureCredential())
    .GetChatClient(deploymentName)
    .AsIChatClient();
builder.Services.AddSingleton(chatClient);

AddAIAgent

依存関係の挿入を使用して AI エージェントを登録します。

var pirateAgent = builder.AddAIAgent(
    "pirate",
    instructions: "You are a pirate. Speak like a pirate",
    description: "An agent that speaks like a pirate.",
    chatClientServiceKey: "chat-model");

AddAIAgent() メソッドは、エージェントを構成するための拡張メソッドを提供するIHostedAgentBuilderを返します。 たとえば、エージェントにツールを追加できます。

var pirateAgent = builder.AddAIAgent("pirate", instructions: "You are a pirate. Speak like a pirate")
    .WithAITool(new MyTool()); // MyTool is a custom type derived from AITool

セッション ストア (会話データのストレージ) を構成することもできます。

var pirateAgent = builder.AddAIAgent("pirate", instructions: "You are a pirate. Speak like a pirate")
    .WithInMemorySessionStore();

AddWorkflow

複数のエージェントを調整するワークフローを登録します。 ワークフローは基本的に、各ノードが AIAgentであり、エージェントが相互に通信する "グラフ" です。

この例では、2 つのエージェントが順番に動作します。 ユーザー入力は最初に agent-1 に送信され、応答が生成され、 agent-2に送信されます。 その後、ワークフローによって最終的な応答が出力されます。 同時エージェント ワークフローを作成する BuildConcurrent メソッドもあります。

builder.AddAIAgent("agent-1", instructions: "you are agent 1!");
builder.AddAIAgent("agent-2", instructions: "you are agent 2!");

var workflow = builder.AddWorkflow("my-workflow", (sp, key) =>
{
    var agent1 = sp.GetRequiredKeyedService<AIAgent>("agent-1");
    var agent2 = sp.GetRequiredKeyedService<AIAgent>("agent-2");
    return AgentWorkflowBuilder.BuildSequential(key, [agent1, agent2]);
});

ワークフローを AIAgent として公開する

ワークフローでプロトコル統合 (A2A や OpenAI など) を使用するには、スタンドアロン エージェントに変換します。 現時点では、ワークフローは独自に同様の統合機能を提供しないため、この変換手順が必要です。

var workflowAsAgent = builder
    .AddWorkflow("science-workflow", (sp, key) => { ... })
    .AddAsAIAgent();  // Now the workflow can be used as an agent

実装の詳細

ホスティング ライブラリは、外部通信プロトコルと Agent Framework の内部 AIAgent 実装をブリッジするプロトコル アダプターとして機能します。 ホスティング統合ライブラリを使用する場合、ライブラリは依存関係の挿入から登録済みの AIAgent を取得し、それをプロトコル固有のミドルウェアでラップして受信要求と送信応答を変換し、 AIAgent を呼び出して要求を処理します。 このアーキテクチャでは、エージェント実装のプロトコルに依存しません。

たとえば、A2A プロトコル アダプターで ASP.NET Core ホスティング ライブラリを使用するとします。

// Register the agent
var pirateAgent = builder.AddAIAgent("pirate",
    instructions: "You are a pirate. Speak like a pirate",
    description: "An agent that speaks like a pirate.");

// Expose via a protocol (e.g. A2A)
builder.Services.AddA2AServer();
var app = builder.Build();
app.MapA2AServer();
app.Run();

ヒント

サーバーレス ホスティングの例については、 Durable Azure Functions のサンプル を参照してください。

Azure Functions ホスティング パッケージをインストールします。

pip install agent-framework-azurefunctions --pre

エージェントを作成します。

from agent_framework.azure import AgentFunctionApp, AzureOpenAIChatClient
from azure.identity import AzureCliCredential


# 1. Instantiate the agent with the chosen deployment and instructions.
def _create_agent() -> Any:
    """Create the Joker agent."""

    return AzureOpenAIChatClient(credential=AzureCliCredential()).as_agent(
        name="Joker",
        instructions="You are good at telling jokes.",
    )

エージェントを AgentFunctionAppに登録します。

# 2. Register the agent with AgentFunctionApp so Azure Functions exposes the required triggers.
app = AgentFunctionApp(agents=[_create_agent()], enable_health_check=True, max_poll_retries=50)

Azure Functions Core Tools を使用してローカルで実行する:

func start

次に、次を呼び出します。

curl -X POST http://localhost:7071/api/agents/Joker/run \
  -H "Content-Type: text/plain" \
  -d "Tell me a short joke about cloud computing."

ヒント

完全な実行可能ファイルの 完全なサンプル と、その他のパターンについては Azure Functions ホスティングのサンプル を参照してください。

次のステップ

より深く進む:

こちらも参照ください