使用代理作为函数工具

本教程介绍如何使用代理作为函数工具,以便一个代理可以调用另一个代理作为工具。

先决条件

有关先决条件和安装 NuGet 包,请参阅本教程中的 “创建并运行简单代理 ”步骤。

创建和使用代理作为函数工具

可以通过在代理上调用AIAgent并将.AsAIFunction()作为工具提供给另一个代理来使用函数工具。 这样,你就可以撰写代理并生成更高级的工作流。

首先,创建一个函数工具作为 C# 方法,并根据需要使用说明对其进行修饰。 此工具将由公开为函数的代理使用。

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.";

创建使用函数工具的函数 AIAgent

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)]);

现在,创建一个主代理,并调用weatherAgent.AsAIFunction()转换为函数工具,然后提供weatherAgent作为函数工具。

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()]);

正常调用主代理。 它现在可以将天气助手用作工具,并应以法语进行回应。

Console.WriteLine(await agent.RunAsync("What is the weather like in Amsterdam?"));

本教程介绍如何使用代理作为函数工具,以便一个代理可以调用另一个代理作为工具。

先决条件

有关先决条件和安装包,请参阅本教程中的 “创建并运行简单代理 ”步骤。

创建和使用代理作为函数工具

你可以通过在代理上调用ChatAgent并将其作为工具提供给另一个代理的方式来使用.as_tool()作为函数工具。 这样,你就可以撰写代理并生成更高级的工作流。

首先,创建一个函数工具,该工具将由作为函数公开的代理使用。

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."

创建使用函数工具的函数 ChatAgent

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
)

现在,创建一个主代理,并调用weather_agent.as_tool()转换为函数工具,然后提供weather_agent作为函数工具。

main_agent = AzureOpenAIChatClient(credential=AzureCliCredential()).create_agent(
    instructions="You are a helpful assistant who responds in French.",
    tools=weather_agent.as_tool()
)

正常调用主代理。 它现在可以将天气助手用作工具,并应以法语进行回应。

result = await main_agent.run("What is the weather like in Amsterdam?")
print(result.text)

还可以在将代理转换为工具时自定义工具名称、说明和参数名称:

# 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
)

后续步骤