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.
Prima di aggiungere tool, esaminare le procedure consigliate per l'approvazione sicura dei tool per decidere quali operazioni necessitano di conferma umana.
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.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)]);
Avvertimento
DefaultAzureCredential è utile per lo sviluppo, ma richiede un'attenta considerazione nell'ambiente di produzione. Nell'ambiente di produzione prendere in considerazione l'uso di credenziali specifiche ,ad esempio ManagedIdentityCredential, per evitare problemi di latenza, probe di credenziali indesiderate e potenziali rischi per la sicurezza dai meccanismi di fallback.
L'agente chiamerà automaticamente il tuo strumento quando pertinente.
Console.WriteLine(await agent.RunAsync("What is the weather like in Amsterdam?"));
Tip
Vedere qui per un'applicazione di esempio eseguibile completa.
L'esempio completo definisce uno strumento con l'elemento @tool Decorator, crea un agente con lo strumento ed esegue l'agente:
import asyncio
from agent_framework import Agent, tool
from agent_framework.foundry import FoundryChatClient
from azure.identity import AzureCliCredential
# This read-only sample tool can run without approval.
@tool(approval_mode="never_require")
def get_weather(location: str) -> str:
"""Get the weather for a given location."""
return f"The weather in {location} is sunny."
async def main() -> None:
agent = Agent(
client=FoundryChatClient(
project_endpoint="https://your-account.services.ai.azure.com/api/projects/your-project",
model="gpt-6-luna",
credential=AzureCliCredential(),
),
instructions="Use the weather tool to answer questions.",
tools=[get_weather],
)
print(await agent.run("What's the weather like in Seattle?"))
if __name__ == "__main__":
asyncio.run(main())
Tip
Vedere l'esempio completo per il file eseguibile completo.
Definire uno strumento usando functool:
import (
"context"
"fmt"
"github.com/microsoft/agent-framework-go/tool"
"github.com/microsoft/agent-framework-go/tool/functool"
)
var weatherTool = functool.MustNew(functool.Config{
Name: "weather",
Description: "Get the current weather for a given location",
}, func(_ context.Context, location string) (string, error) {
return fmt.Sprintf("The weather in %s is cloudy with a high of 15°C.", location), nil
})
Creare un agente con lo strumento :
a := foundryprovider.NewAgent(
endpoint,
token,
foundryprovider.ModelDeployment(model),
foundryprovider.AgentConfig{
Instructions: "You are a helpful assistant",
Config: agent.Config{
Tools: []tool.Tool{weatherTool},
},
},
)
L'agente chiamerà automaticamente il tuo strumento quando pertinente.
resp, err := a.RunText(ctx, "What is the weather like in Amsterdam?").Collect()
fmt.Println(resp, err)
Tip
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 degli strumenti - human-in-the-loop per le chiamate agli strumenti