Nuta
Dostęp do tej strony wymaga autoryzacji. Możesz spróbować się zalogować lub zmienić katalog.
Dostęp do tej strony wymaga autoryzacji. Możesz spróbować zmienić katalogi.
W tym samouczku pokazano, jak używać agenta jako narzędzia funkcji, aby jeden agent mógł wywołać innego agenta jako narzędzie.
Wymagania wstępne
Aby uzyskać wymagania wstępne i zainstalować pakiety NuGet, zobacz krok Tworzenie i uruchamianie prostego agenta w tym samouczku.
Tworzenie i używanie agenta jako narzędzia funkcji
Możesz użyć AIAgent jako narzędzia funkcjonalnego, wywołując .AsAIFunction() na agencie i udostępniając go jako narzędzie innemu agentowi. Dzięki temu można tworzyć agentów i tworzyć bardziej zaawansowane przepływy pracy.
Najpierw utwórz narzędzie funkcji jako metodę języka C# i w razie potrzeby udekoruj je opisami. To narzędzie będzie używane przez agenta, który jest udostępniany jako funkcja.
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.";
Utwórz obiekt AIAgent , który używa narzędzia funkcji.
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)]);
Teraz utwórz głównego agenta i udostępnij weatherAgent jako narzędzie funkcji, wywołując .AsAIFunction() w celu przekształcenia weatherAgent w narzędzie funkcji.
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()]);
Wywołaj głównego agenta w zwykły sposób. Teraz może wykorzystać agenta pogody jako narzędzie i powinien odpowiadać po francusku.
Console.WriteLine(await agent.RunAsync("What is the weather like in Amsterdam?"));
W tym samouczku pokazano, jak używać agenta jako narzędzia funkcji, aby jeden agent mógł wywołać innego agenta jako narzędzie.
Wymagania wstępne
Aby uzyskać informacje o wymaganiach wstępnych i instalowanych pakietach, zobacz krok Tworzenie i uruchamianie prostego agenta w tym samouczku.
Tworzenie i używanie agenta jako narzędzia funkcji
Możesz użyć ChatAgent jako narzędzia w roli funkcji, wywołując agenta .as_tool() i przekazując go jako narzędzie innemu agentowi. Dzięki temu można tworzyć agentów i tworzyć bardziej zaawansowane przepływy pracy.
Najpierw utwórz narzędzie do funkcji, które będzie używane przez Twojego agenta i jest eksponowane jako funkcja.
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."
Utwórz ChatAgent, który wykorzystuje narzędzie funkcji.
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
)
Teraz utwórz głównego agenta i udostępnij weather_agent jako narzędzie funkcji, wywołując .as_tool() w celu przekształcenia weather_agent w narzędzie funkcji.
main_agent = AzureOpenAIChatClient(credential=AzureCliCredential()).create_agent(
instructions="You are a helpful assistant who responds in French.",
tools=weather_agent.as_tool()
)
Wywołaj głównego agenta w zwykły sposób. Teraz może wykorzystać agenta pogody jako narzędzie i powinien odpowiadać po francusku.
result = await main_agent.run("What is the weather like in Amsterdam?")
print(result.text)
Możesz również dostosować nazwę narzędzia, opis i nazwę argumentu podczas konwertowania agenta na narzędzie:
# 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
)