次の方法で共有


エージェントを関数ツールとして使用する

このチュートリアルでは、あるエージェントが別のエージェントをツールとして呼び出すことができるように、エージェントを関数ツールとして使用する方法について説明します。

[前提条件]

前提条件と Nuget パッケージのインストールについては、このチュートリアルの 「エージェントの作成と実行 」の手順を参照してください。

エージェントを関数ツールとして作成して使用する

AIAgentを関数ツールとして使用するには、エージェントで.AsAIFunction()を呼び出し、別のエージェントにツールとして提供します。 これにより、エージェントを作成し、より高度なワークフローを構築できます。

まず、C# メソッドとして関数ツールを作成し、必要に応じて説明で装飾します。 このツールは、関数として公開されているエージェントによって使用されます。

using System.ComponentModel;

[Description("Get the weather for a given location.")]
static string GetWeather([Description("The location to get the weather for.")] string location)
    => $"The weather in {location} is cloudy with a high of 15°C.";

AIAgent を作成し、関数ツールを使用します。

using System;
using Azure.AI.OpenAI;
using Azure.Identity;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
using OpenAI;

AIAgent weatherAgent = new AzureOpenAIClient(
    new Uri("https://<myresource>.openai.azure.com"),
    new AzureCliCredential())
     .GetChatClient("gpt-4o-mini")
     .CreateAIAgent(
        instructions: "You answer questions about the weather.",
        name: "WeatherAgent",
        description: "An agent that answers questions about the weather.",
        tools: [AIFunctionFactory.Create(GetWeather)]);

次に、メイン エージェントを作成し、weatherAgentを呼び出して関数ツールとして.AsAIFunction()を提供し、weatherAgentを関数ツールに変換します。

AIAgent agent = new AzureOpenAIClient(
    new Uri("https://<myresource>.openai.azure.com"),
    new AzureCliCredential())
     .GetChatClient("gpt-4o-mini")
     .CreateAIAgent(instructions: "You are a helpful assistant who responds in French.", tools: [weatherAgent.AsAIFunction()]);

メイン エージェントを通常どおりに呼び出します。 これで気象エージェントをツールとして呼び出すようになり、フランス語で応答する必要があります。

Console.WriteLine(await agent.RunAsync("What is the weather like in Amsterdam?"));

このチュートリアルでは、あるエージェントが別のエージェントをツールとして呼び出すことができるように、エージェントを関数ツールとして使用する方法について説明します。

[前提条件]

前提条件とパッケージのインストールについては、このチュートリアルの 「エージェントの作成と実行 」の手順を参照してください。

エージェントを関数ツールとして作成して使用する

ChatAgentを関数ツールとして使用するには、エージェントで.as_tool()を呼び出し、別のエージェントにツールとして提供します。 これにより、エージェントを作成し、より高度なワークフローを構築できます。

まず、我々のエージェントが関数として公開する機能ツールを作成します。

from typing import Annotated
from pydantic import Field

def get_weather(
    location: Annotated[str, Field(description="The location to get the weather for.")],
) -> str:
    """Get the weather for a given location."""
    return f"The weather in {location} is cloudy with a high of 15°C."

関数ツールを活用したChatAgentを作成します。

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

weather_agent = AzureOpenAIChatClient(credential=AzureCliCredential()).create_agent(
    name="WeatherAgent",
    description="An agent that answers questions about the weather.",
    instructions="You answer questions about the weather.",
    tools=get_weather
)

次に、メイン エージェントを作成し、weather_agentを呼び出して関数ツールとして.as_tool()を提供し、weather_agentを関数ツールに変換します。

main_agent = AzureOpenAIChatClient(credential=AzureCliCredential()).create_agent(
    instructions="You are a helpful assistant who responds in French.",
    tools=weather_agent.as_tool()
)

メイン エージェントを通常どおりに呼び出します。 これで気象エージェントをツールとして呼び出すようになり、フランス語で応答する必要があります。

result = await main_agent.run("What is the weather like in Amsterdam?")
print(result.text)

また、エージェントをツールに変換するときに、ツール名、説明、引数名をカスタマイズすることもできます。

# Convert agent to tool with custom parameters
weather_tool = weather_agent.as_tool(
    name="WeatherLookup",
    description="Look up weather information for any location",
    arg_name="query", 
    arg_description="The weather query or location"
)

main_agent = AzureOpenAIChatClient(credential=AzureCliCredential()).create_agent(
    instructions="You are a helpful assistant who responds in French.",
    tools=weather_tool
)

次のステップ