도구를 사용하면 에이전트가 날씨 데이터 가져오기, 데이터베이스 쿼리 또는 API 호출과 같은 사용자 지정 함수를 호출할 수 있습니다.
[Description] 특성을 가진 메서드를 도구로 정의합니다.
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.";
도구를 사용하여 에이전트를 만듭니다.
using System;
using Azure.AI.OpenAI;
using Azure.Identity;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
var endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT")
?? throw new InvalidOperationException("Set AZURE_OPENAI_ENDPOINT");
var deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o-mini";
AIAgent agent = new AzureOpenAIClient(new Uri(endpoint), new AzureCliCredential())
.GetChatClient(deploymentName)
.AsAIAgent(instructions: "You are a helpful assistant.", tools: [AIFunctionFactory.Create(GetWeather)]);
에이전트는 관련되는 경우 자동으로 도구를 호출합니다.
Console.WriteLine(await agent.RunAsync("What is the weather like in Amsterdam?"));
팁 (조언)
전체 실행 가능한 파일에 대한 전체 샘플을 참조하세요.
데코레이터를 사용하여 도구를 @tool 정의합니다.
# NOTE: approval_mode="never_require" is for sample brevity.
# Use "always_require" in production for user confirmation before tool execution.
@tool(approval_mode="never_require")
def get_weather(
location: Annotated[str, Field(description="The location to get the weather for.")],
) -> str:
"""Get the weather for a given location."""
conditions = ["sunny", "cloudy", "rainy", "stormy"]
return f"The weather in {location} is {conditions[randint(0, 3)]} with a high of {randint(10, 30)}°C."
도구를 사용하여 에이전트를 만듭니다.
agent = client.as_agent(
name="WeatherAgent",
instructions="You are a helpful weather agent. Use the get_weather tool to answer questions.",
tools=get_weather,
)
팁 (조언)
전체 실행 가능한 파일에 대한 전체 샘플을 참조하세요.
다음 단계:
더 자세히 살펴보기: