이 자습서에서는 한 에이전트가 다른 에이전트를 도구로 호출할 수 있도록 에이전트를 함수 도구로 사용하는 방법을 보여줍니다.
필수 조건
필수 구성 요소 및 NuGet 패키지 설치는 이 자습서의 간단한 에이전트 만들기 및 실행 단계를 참조하세요.
함수 도구로 에이전트 만들기 및 사용
에이전트에서 AIAgent을(를) 호출하고 이를 다른 에이전트에 도구로 제공하여 함수 도구로 사용할 수 있습니다. 이렇게 하면 에이전트를 작성하고 고급 워크플로를 빌드할 수 있습니다.
먼저 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 함수 도구를 사용하는 API를 만듭니다.
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
)