生成AIエージェントでのSDKでの関数呼出し

このガイドを使用して、OCI Java SDKを使用したアプリケーションへのファンクション・コールの統合について理解します。次のステップは、ファンクション・コール・ツールを作成し、チャットを使用してファンクション・コール・ツールを起動するためのOCI Java SDKの例を示しています。

関数呼び出しツールの作成

次のステップは、OCI Java SDKを使用してエージェント用のファンクション・コール・ツールを作成する方法を示しています。

  1. クライアントを構成します。例:
    GenerativeAiAgentRuntimeClient agentClient = GenerativeAiAgentRuntimeClient.builder()
           .endpoint(endpoint)
           .configuration(clientConfiguration)
           .build(provider);
  2. 関数を定義し、その関数をToolConfigに埋め込みます。たとえば、OCI Object Storageバケットを作成する関数を次に示します。
    private static Function prepareFunction() {
     
            return Function.builder()
                    .name("create_object")
                    .description("A function to create an object storage bucket")
                    .parameters("{\n" +
                            "    \"displayName\": \"Create Object Storage Bucket\",\n" +
                            "    \"description\": \"Create a new bucket in OCI Object Storage.\",\n" +
                            "    \"toolConfig\": {\n" +
                            "        \"toolConfigType\": \"FUNCTION_CALLING_TOOL_CONFIG\",\n" +
                            "        \"function\": {\n" +
                            "            \"name\": \"create_object_storage_bucket\",\n" +
                            "            \"description\": \"Creates a new bucket in OCI Object Storage within a specified compartment.\",\n" +
                            "            \"parameters\": {\n" +
                            "                \"type\": \"object\",\n" +
                            "                \"properties\": {\n" +
                            "                    \"bucket_name\": {\n" +
                            "                        \"type\": \"string\",\n" +
                            "                        \"description\": \"The name of the bucket to create.\"\n" +
                            "                    },\n" +
                            "                    \"compartment_ocid\": {\n" +
                            "                        \"type\": \"string\",\n" +
                            "                        \"description\": \"The OCID of the compartment where the bucket will be created.\"\n" +
                            "                    },\n" +
                            "                    \"storage_tier\": {\n" +
                            "                        \"type\": \"string\",\n" +
                            "                        \"enum\": [\"Standard\", \"Archive\"],\n" +
                            "                        \"description\": \"The storage tier for the bucket.\"\n" +
                            "                    }\n" +
                            "                },\n" +
                            "                \"required\": [\"bucket_name\", \"compartment_ocid\"],\n" +
                            "                \"additionalProperties\": false\n" +
                            "            }\n" +
                            "        }\n" +
                            "    }\n" +
                            "}")
                    .build();
        }
     
    private static ToolConfig createFunctionToolConfig() {
            return FunctionCallingToolConfig.builder()
                    .function(prepareFunction())
                    .build();
    }
  3. ツール・リクエストを作成し、クライアントを使用してツールを作成します。例:
    CreateToolDetails createToolDetails = CreateToolDetails.builder()
                    .agentId(agentId)
                    .compartmentId(COMPARTMENT_ID)
                    .toolConfig(toolConfig)
                    .displayName("tool-sdk")
                    .description("tool description")
                    .build();
     CreateToolRequest toolRequest = CreateToolRequest.builder()
                     .createToolDetails(createToolDetails)
                     .build();
    client.createTool(toolRequest);

エージェントとのチャット

ファンクション・コール・ツールを持つエージェントとチャットするには、まずセッションを作成し、セッションでファンクションを呼び出します。これらのステップの例を次に示します。
  1. セッションを作成します。例:
    GenerativeAiAgentRuntimeClient agentClient = GenerativeAiAgentRuntimeClient.builder()
            .endpoint(endpoint)
            .configuration(clientConfiguration)
            .build(provider);
     
    // Create a new chat session request
    CreateSessionRequest request = CreateSessionRequest.builder()
            .agentEndpointId(<agentEndpointId>)
            .createSessionDetails(CreateSessionDetails.builder()
                    .description("description")
                    .displayName("display_name")
                    .build())
            .build();
    String sessionId = agentClient.createSession(request)
            .getSession()
            .getId();
    

    <agentEndpointId>を、前提条件セクションのエージェント・エンドポイントのOCIDに置き換えます。

  2. セッションを使用して、関数を呼び出すチャット・コールを作成します。この例では、エージェントにメッセージを送信し、ファンクション・コールを含むレスポンスを想定しています。
    String message = "Create an Object Store.";
     
    // Create Chat request
    final ChatDetails chatDetails = ChatDetails.builder()
            .shouldStream(shouldStream)
            .userMessage(message)
            .sessionId(sessionId)
            .build();
    final ChatRequest chatRequest = ChatRequest.builder()
            .chatDetails(chatDetails)
            .agentEndpointId(agentEndpointId)
            .build();
     
    final ChatResult chatResult = agentClient.chat(chatRequest);
     

    エージェントからのレスポンスには、起動するファンクションの詳細を含むrequired-actionsフィールドが含まれます。

    chatResult(message=null, traces=[], usageDetails=null, 
    requiredActions=[FunctionCallingRequiredAction(super=RequiredAction(super=BmcModel
    (__explicitlySet__=[functionCall, actionId])
    actionId=<some-action-id>),
    functionCall=FunctionCall(super=BmcModel(__explicitlySet__=[name, arguments])name=object_storage,
    arguments=
    {"bucket_name": "my_object_store", 
    "compartment_ocid": "ocid1.compartment.oc1..xxx",
    "storage_tier": "Standard"
    }))], 
    toolResults=null)

    レスポンスから、ファンクション・コールに関連付けられているaction-idを抽出します。このIDは、次のステップでアクションを実行するために使用されます。

    String actionId = "<some-action-id>";
    
  3. アクションを実行します。レスポンスには、ファンクション・コールの結果を含む、エージェントからの最終レスポンスが含まれます。例:
    String actionId = chatResponse.getChatResult().getRequiredActions().get(0).getActionId();
    String functionCallOutput = "{"bucket_name": "test_bucket", "compartment_ocid": "<compartment_OCID>",
    "storage_tier":"Standard"}";
     
    PerformedAction performedAction = FunctionCallingPerformedAction.builder()
        .actionId(actionId)
        .functionCallOutput(functionCallOutput)
        .build();
     
    // Execute the chat session with the performed action
    final ChatDetails chatDetails = ChatDetails.builder()
            .shouldStream(shouldStream)
            .userMessage(userMessage)
            .performedActions(List.of(performedAction))
            .sessionId(sessionId)
            .build();
    final ChatRequest chatRequest = ChatRequest.builder()
            .chatDetails(chatDetails)
            .agentEndpointId(agentEndpointId)
            .build();
    ChatResult chatResult = agentClient.chat(chatRequest);
     
    # Chat Result:
    # ChatResult(message=The object store 'my_object_store' has been successfully created 
    in the specified compartment with the 'Standard' storage tier., 
    traces=[], usageDetails=null, toolResults=null)