Nota
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare ad accedere o modificare le directory.
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare a modificare le directory.
Gli strumenti consentono all'agente di chiamare funzioni personalizzate, ad esempio il recupero di dati meteo, l'esecuzione di query su un database o la chiamata di un'API.
Definire uno strumento come qualsiasi metodo con un [Description] attributo :
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.";
Creare un agente con lo strumento :
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)]);
L'agente chiamerà automaticamente il tuo strumento quando pertinente.
Console.WriteLine(await agent.RunAsync("What is the weather like in Amsterdam?"));
Suggerimento
Vedere l'esempio completo per il file eseguibile completo.
Definire uno strumento con l'elemento @tool decorator:
# 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."
Creare un agente con lo strumento :
agent = client.as_agent(
name="WeatherAgent",
instructions="You are a helpful weather agent. Use the get_weather tool to answer questions.",
tools=get_weather,
)
Suggerimento
Vedere l'esempio completo per il file eseguibile completo.
Passaggi successivi
Approfondimento:
- Panoramica degli strumenti : informazioni su tutti i tipi di strumenti disponibili
- Strumenti per le funzioni : modelli avanzati di strumenti per le funzioni
- Approvazione dello strumento — intervento umano durante le chiamate agli strumenti