Remarque
L’accès à cette page nécessite une autorisation. Vous pouvez essayer de vous connecter ou de modifier des répertoires.
L’accès à cette page nécessite une autorisation. Vous pouvez essayer de modifier des répertoires.
Ce tutoriel vous montre comment utiliser un agent comme outil de fonction afin qu’un agent puisse appeler un autre agent en tant qu’outil.
Prerequisites
Pour connaître les prérequis et l’installation des packages NuGet, consultez l’étape Créer et exécuter un agent simple dans ce tutoriel.
Créer et utiliser un agent en tant qu’outil de fonction
Vous pouvez utiliser un AIAgent outil de fonction en appelant .AsAIFunction() l’agent et en le fournissant en tant qu’outil à un autre agent. Cela vous permet de composer des agents et de créer des flux de travail plus avancés.
Tout d’abord, créez un outil de fonction en tant que méthode C# et décorez-le avec des descriptions si nécessaire. Cet outil sera utilisé par votre agent qui sera exposé en tant que fonction.
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.";
Créez un AIAgent qui utilise l’outil fonctionnel.
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)]);
À présent, créez un agent principal et fournissez l’outil fonctionnel weatherAgent en appliquant .AsAIFunction() pour convertir weatherAgent en outil fonctionnel.
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()]);
Appelez l’agent principal comme d'habitude. Il peut maintenant appeler l’agent météo en tant qu’outil et devrait répondre en français.
Console.WriteLine(await agent.RunAsync("What is the weather like in Amsterdam?"));
Ce tutoriel vous montre comment utiliser un agent comme outil de fonction afin qu’un agent puisse appeler un autre agent en tant qu’outil.
Prerequisites
Pour connaître les prérequis et l’installation des packages, consultez l’étape Créer et exécuter un agent simple dans ce tutoriel.
Créer et utiliser un agent en tant qu’outil de fonction
Vous pouvez utiliser un ChatAgent comme outil fonctionnel en appelant .as_tool() sur l'agent et en le fournissant comme outil à un autre agent. Cela vous permet de composer des agents et de créer des flux de travail plus avancés.
Tout d’abord, créez un outil fonctionnel qui sera utilisé par votre agent exposé comme une fonction.
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."
Créez un ChatAgent qui utilise l'outil fonctionnel.
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
)
À présent, créez un agent principal et fournissez l’outil fonctionnel weather_agent en appliquant .as_tool() pour convertir weather_agent en outil fonctionnel.
main_agent = AzureOpenAIChatClient(credential=AzureCliCredential()).create_agent(
instructions="You are a helpful assistant who responds in French.",
tools=weather_agent.as_tool()
)
Appelez l’agent principal comme d'habitude. Il peut maintenant appeler l’agent météo en tant qu’outil et devrait répondre en français.
result = await main_agent.run("What is the weather like in Amsterdam?")
print(result.text)
Vous pouvez également personnaliser le nom, la description et le nom de l’argument de l’outil lors de la conversion d’un agent en un outil :
# 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
)