共用方式為


使用函數工具搭配代理程式

本教學課程步驟示範如何搭配代理程式使用函式工具,其中代理程式是以 Azure OpenAI 聊天完成服務為基礎。

這很重要

並非所有代理程式類型都支援函數工具。 有些可能只支援自訂內建工具,而不允許呼叫端提供自己的函式。 此步驟使用 ChatClientAgent,它確實支援函式工具。

先決條件

如需必要條件和安裝 NuGet 套件,請參閱本教學課程中的 建立並執行簡單代理程式 步驟。

使用函數工具建立代理程式

函數工具只是您希望客服專員能夠在需要時呼叫的自訂程式碼。 您可以使用方法從 AIFunctionFactory.Create 方法建立 AIFunction 實例,將任何 C# 方法變成函式工具。

如果您需要向代理程式提供有關函數或其參數的額外描述,以便代理程式可以更準確地在不同函數之間進行選擇,您可以使用 System.ComponentModel.DescriptionAttribute 方法及其參數上的屬性。

這是一個簡單的函數工具的示例,該工具可以偽造獲取給定位置的天氣。 它會以說明屬性裝飾,以向代理程式提供有關其本身及其位置參數的其他說明。

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

建立代理程式時,您現在可以透過將工具清單傳遞給 CreateAIAgent 方法,將函數工具提供給代理程式。

using System;
using Azure.AI.OpenAI;
using Azure.Identity;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
using OpenAI;

AIAgent agent = new AzureOpenAIClient(
    new Uri("https://<myresource>.openai.azure.com"),
    new AzureCliCredential())
     .GetChatClient("gpt-4o-mini")
     .CreateAIAgent(instructions: "You are a helpful assistant", tools: [AIFunctionFactory.Create(GetWeather)]);

現在您可以正常執行代理程式,且代理程式將能夠在需要時呼叫 GetWeather 函式工具。

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

這很重要

並非所有代理程式類型都支援函數工具。 有些可能只支援自訂內建工具,而不允許呼叫端提供自己的函式。 此步驟使用透過聊天用戶端建立的代理程式,這些用戶端支援功能工具。

先決條件

如需必要條件和安裝 Python 套件,請參閱本教學課程中的 建立並執行簡式代理程式 步驟。

使用函數工具建立代理程式

函數工具只是您希望客服專員能夠在需要時呼叫的自訂程式碼。 您可以在建立代理程式時將任何 Python 函數傳遞給代理程式的 tools 參數,將其轉換為函數工具。

如果需要向代理提供有關函數或其參數的額外描述,以便其能夠更準確地在不同函數之間進行選擇,可以使用Python的類型註解和 Annotated Pydantic的 Field 來提供描述。

這是一個簡單的函數工具的示例,該工具可以偽造獲取給定位置的天氣。 它使用類型註釋,向代理程式提供有關函數及其位置參數的其他描述。

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

您也可以使用 ai_function 裝飾器來明確指定函數的名稱和描述:

from typing import Annotated
from pydantic import Field
from agent_framework import ai_function

@ai_function(name="weather_tool", description="Retrieves weather information for any location")
def get_weather(
    location: Annotated[str, Field(description="The location to get the weather for.")],
) -> str:
    return f"The weather in {location} is cloudy with a high of 15°C."

如果您未在name裝飾器中指定descriptionai_function參數,框架將自動使用函數的名稱和說明字串作為後備。

建立代理程式時,您現在可以將函數工具傳遞給 tools 參數,以將函數工具提供給代理程式。

import asyncio
from agent_framework.azure import AzureOpenAIChatClient
from azure.identity import AzureCliCredential

agent = AzureOpenAIChatClient(credential=AzureCliCredential()).create_agent(
    instructions="You are a helpful assistant",
    tools=get_weather
)

現在您可以正常執行代理程式,且代理程式將能夠在需要時呼叫 get_weather 函式工具。

async def main():
    result = await agent.run("What is the weather like in Amsterdam?")
    print(result.text)

asyncio.run(main())

建立具有多個函數工具的類別

您也可以建立包含多個函式工具作為方法的類別。 這對於將相關函數組織在一起或當您想要在它們之間傳遞狀態時非常有用。


class WeatherTools:
    def __init__(self):
        self.last_location = None

    def get_weather(
        self,
        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."

    def get_weather_details(self) -> int:
        """Get the detailed weather for the last requested location."""
        if self.last_location is None:
            return "No location specified yet."
        return f"The detailed weather in {self.last_location} is cloudy with a high of 15°C, low of 7°C, and 60% humidity."

建立代理程式時,您現在可以將類別的所有方法提供為函數:

tools = WeatherTools()
agent = AzureOpenAIChatClient(credential=AzureCliCredential()).create_agent(
    instructions="You are a helpful assistant",
    tools=[tools.get_weather, tools.get_weather_details]
)

您也可以使用與以前相同的 ai_function 裝飾器來裝飾功能。

後續步驟