このページは機械翻訳したものです。

機能ツールを使用したエージェント

カスタム関数ツールを使用してエージェントを作成する方法を学習します。

気象エージェントの例

この例では、気象エージェントにカスタム機能ツールが装備されています。これは関数呼び出しエージェントとも呼ばれます。

関数ツールを使用すると、エージェントはカスタム記述のローカル定義関数をツールとして使用できます。ファンクション・ツールは、ローカル処理、容易な認証、および既存の機能機能とのシームレスな統合により、企業のユースケースに柔軟で特に役立ちます。


情報:ファンクション・ツールはローカルで実行されます。

OCIサーバー側でリモート・エージェントに送信されるのは、ファンクション定義(ファンクション名、ファンクション・パラメータおよびその説明)です。OCIサーバーはファンクション実装にアクセスしません。


Python

weather_agent.py

from typing import Dict, Any
from oci.addons.adk import Agent, AgentClient, tool

@tool
def get_weather(location: str) -> Dict[str, Any]:
    """Get the weather for a given location.

    Args:
      location(str): The location for which weather is queried
    """
    return {"location": location, "temperature": 72, "unit": "F"}


def main():
    client = AgentClient(
        auth_type="api_key",
        profile="DEFAULT",
        region="us-chicago-1"
    )

    agent = Agent(
        client=client,
        agent_endpoint_id="ocid1...",
        instructions="Perform weather queries using the given tools",
        tools=[get_weather]
    )

    agent.setup()

    input = "Is it cold in Seattle?"
    response = agent.run(input)

    response.pretty_print()

if __name__ == "__main__":
    main()

Java

WeatherAgent.java

package demos.singleTurnSingleTool.WeatherAgent;

import com.oracle.bmc.ConfigFileReader;
import com.oracle.bmc.adk.agent.Agent;
import com.oracle.bmc.adk.agent.RunOptions;
import com.oracle.bmc.adk.client.AgentClient;
import com.oracle.bmc.adk.tools.FunctionTool;
import com.oracle.bmc.adk.run.RunResponse;
import com.oracle.bmc.auth.BasicAuthenticationDetailsProvider;
import com.oracle.bmc.auth.SessionTokenAuthenticationDetailsProvider;

import java.lang.reflect.Method;
import java.util.Arrays;

public class WeatherAgent {
    @Tool(name = "getWeather", description = "Get weather of a given location")
    public static Map<String, String> getWeather(
            @Param(description = "The location") String location) {
        Map<String, String> weather = new HashMap<>();
        weather.put("location", location);
        weather.put("temperature", "72");
        weather.put("unit", "F");
        return weather;
    }

  public static void main(String[] args) throws Exception {
    final String configLocation = "~/.oci/config";
    final String configProfile = "DEFAULT";

    BasicAuthenticationDetailsProvider authProvider =
            new SessionTokenAuthenticationDetailsProvider(
                    ConfigFileReader.parse(configLocation, configProfile));

    AgentClient agentClient = AgentClient.builder()
        .authProvider(authProvider)
        .region("us-chicago-1")
        .build();

    FunctionTool getWeatherTool =
        FunctionTool.fromMethod(WeatherAgent.class, "getWeather", String.class);

    Agent agent = Agent.builder()
        .client(agentClient)
        .agentEndpointId("YOUR_AGENT_ENDPOINT_ID")
        .instructions("Perform weather queries using the given tools.")
        .tools(Arrays.asList(getWeatherTool))
        .build();

    agent.setup();
    final String input = "Is it cold in Seattle";
    final Integer maxStep = 3;
    RunOptions runOptions = RunOptions.builder().maxSteps(maxStep).build();
    RunResponse response = agent.run(input, runOptions);
    response.prettyPrint();
  }
}

プレーン・ファンクションget_weatherを記述し、それを@toolでデコレーションし、標準のdocstringを使用してファンクションの説明とパラメータの説明を記述します。

ファンクションは、文字列に変換できる任意のものを返すことができます。ここでは、データベース、キャッシュまたはAPIエンドポイントから取得したJSONオブジェクトをシミュレートするDictを返します。

次に、Agentオブジェクトの作成時に、その関数をtoolsリストに渡します。これにより、関数がツールとしてエージェントにローカルに登録されます。

agent.setup()メソッドを実行すると、ADKはローカル・エージェント設定をリモート・エージェント・インスタンスに適用します。これには、エージェント指示とファンクション・ツールが含まれます。ADKは、サーバー上でカスタム関数ツールを設定するために必要な関数JSONスキーマを自動的に生成します。

agent.run()メソッドを実行すると、ADKは、get_weather関数の呼出しを含む、関数呼出しに必要なクライアントとサーバーの相互作用を処理します。

機能

リモート・エージェントは、get_weatherツールの使用を決定すると、クライアントが必要とするアクションを含むレスポンスで応答します。この場合、クライアントに必要なアクションは、get_weatherファンクション・ツールを引数location=Seattleで起動することです。

リモート・エージェントは、自然言語問合せIs it cold in Seattle?に基づいて、get_weatherツールを使用するインテント分類および引数スロット入力を適用します。

ADKは、必要なアクションを解析し、登録されているローカル関数を特定し、指定された引数でこの関数を実行し、関数コールの出力を取得して、出力をリモート・エージェントに返します。エージェントは、その出力を使用して、ユーザーの質問に対する回答を生成できます。