Poznámka:
Přístup k této stránce vyžaduje autorizaci. Můžete se zkusit přihlásit nebo změnit adresáře.
Přístup k této stránce vyžaduje autorizaci. Můžete zkusit změnit adresáře.
V tomto kurzu se dozvíte, jak používat agenta jako funkční nástroj, aby jeden agent mohl volat jiného agenta jako nástroj.
Požadavky
Pokyny k požadavkům a instalaci balíčků NuGet najdete v části Vytvoření a spuštění jednoduchého agenta v tomto kurzu.
Vytvoření a použití agenta jako nástroje funkcí
AIAgent můžete použít jako nástroj funkce tím, že zavoláte agenta .AsAIFunction() a poskytnete jej jako nástroj jinému agentovi. To vám umožní vytvářet agenty a vytvářet pokročilejší pracovní postupy.
Nejprve vytvořte nástroj funkce jako metodu jazyka C# a v případě potřeby ho vyzdobit popisy. Tento nástroj bude používat váš agent, který je vystavený jako funkce.
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.";
Vytvořte nástroj AIAgent , který používá nástroj funkce.
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)]);
Teď vytvořte hlavního agenta a nakonfigurujte weatherAgent jako nástroj funkce voláním .AsAIFunction(), abyste převedli weatherAgent na nástroj funkce.
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()]);
Vyvolejte hlavního agenta jako obvykle. Teď může volat agenta počasí jako nástroj a měl by reagovat ve francouzštině.
Console.WriteLine(await agent.RunAsync("What is the weather like in Amsterdam?"));
V tomto kurzu se dozvíte, jak používat agenta jako funkční nástroj, aby jeden agent mohl volat jiného agenta jako nástroj.
Požadavky
Informace o požadavcích a instalaci balíčků najdete v části Vytvoření a spuštění jednoduchého agenta v tomto kurzu.
Vytvoření a použití agenta jako nástroje funkcí
Jako nástroj funkce můžete použít ChatAgent tím, že vyvoláte .as_tool() u agenta a poskytnete ho jako nástroj jinému agentovi. To vám umožní vytvářet agenty a vytvářet pokročilejší pracovní postupy.
Nejprve vytvořte nástroj ve formě funkce, který bude používat váš agent a který je poskytován jako funkce.
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."
Vytvořte ChatAgent, který používá nástroj funkce.
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
)
Teď vytvořte hlavního agenta a nakonfigurujte weather_agent jako nástroj funkce voláním .as_tool(), abyste převedli weather_agent na nástroj funkce.
main_agent = AzureOpenAIChatClient(credential=AzureCliCredential()).create_agent(
instructions="You are a helpful assistant who responds in French.",
tools=weather_agent.as_tool()
)
Vyvolejte hlavního agenta jako obvykle. Teď může volat agenta počasí jako nástroj a měl by reagovat ve francouzštině.
result = await main_agent.run("What is the weather like in Amsterdam?")
print(result.text)
Při převodu agenta na nástroj můžete také přizpůsobit název nástroje, popis a název argumentu:
# 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
)