本教學課程說明如何使用代理程式作為函式工具,以便一個代理程式可以呼叫另一個代理程式作為工具。
先決條件
如需必要條件和安裝 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,並將 c1 作為工具提供給另一個代理程式,以將.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
)