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.
Les outils permettent à votre agent d’appeler des fonctions personnalisées, telles que l’extraction de données météorologiques, l’interrogation d’une base de données ou l’appel d’une API.
Définissez un outil comme n’importe quelle méthode avec un [Description] attribut :
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 agent avec l’outil :
using System;
using Azure.AI.Projects;
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 AIProjectClient(new Uri(endpoint), new DefaultAzureCredential())
.AsAIAgent(
model: deploymentName,
instructions: "You are a helpful assistant.",
tools: [AIFunctionFactory.Create(GetWeather)]);
Avertissement
DefaultAzureCredential est pratique pour le développement, mais nécessite une considération minutieuse en production. En production, envisagez d’utiliser des informations d’identification spécifiques (par exemple ManagedIdentityCredential) pour éviter les problèmes de latence, la détection involontaire des informations d’identification et les risques de sécurité potentiels liés aux mécanismes de secours.
L’agent appelle automatiquement votre outil en cas de pertinence :
Console.WriteLine(await agent.RunAsync("What is the weather like in Amsterdam?"));
Conseil / Astuce
Consultez cet article pour obtenir un exemple d’application exécutable complet.
Définissez un outil avec le @tool décorateur :
# 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."
Créez un agent avec l’outil :
agent = Agent(
client=client,
name="WeatherAgent",
instructions="You are a helpful weather agent. Use the get_weather tool to answer questions.",
tools=[get_weather],
)
Conseil / Astuce
Consultez l’exemple complet du fichier exécutable complet.
Prochaines étapes
Aller plus loin :
- Vue d’ensemble des outils : en savoir plus sur tous les types d’outils disponibles
- Outils de fonction : modèles d’outils de fonction avancés
- Approbation de l'outil — humain dans la boucle pour les appels d'outil