API Endpoint Calling with SDKs in Generative AI Agents

Use this guide to become familiar with using the OCI Java SDK to integrate API endpoint calling into an application. The following steps show an OCI Java SDK example for creating an API endpoint calling tool and then using chat to invoke the tool.

IAM Policies

Ensure that you add policies to give the appropriate permissions to access the OCI service's API operations that you want the agent to call.

In the example, the API endpoint calling tool interacts with Object Storage buckets. You can use the following policies to enable access to Object Storage in all compartments in the tenancy or restrict access to a specific compartment.

// To enable access to all compartments in the tenancy
allow any-user to manage object-family in tenancy where any {request.principal.type='genaiagent'} 
// To enable access to a specific compartment in the tenancy
allow any-user to manage object-family in compartment <compartment-name> where any {request.principal.type='genaiagent'} 

Replace <compartment-name> with the compartment that you want to use.

Creating an API Endpoint Calling Tool

The following steps show how to use OCI Java SDK to create an API endpoint calling tool for an agent.

  1. Configure a client to interact with Generative AI Agents. For example:
    // Configure your client
    GenerativeAiAgentRuntimeClient agentClient = GenerativeAiAgentRuntimeClient.builder()
           .endpoint(endpoint)
           .configuration(clientConfiguration)
           .build(provider);

    In the code, replace endpoint and clientConfiguration with the appropriate values for your setup.

  2. Create the HttpEndpointToolConfig for your tool. For example:
    private static ToolConfig createHttpEndpointToolConfig() {
        return HttpEndpointToolConfig.builder()
                .apiSchema(ApiSchemaInlineInputLocation.builder()
                        .content("{\"openapi\":\"3.0.3\",\"info\":{\"title\":\"OCIObjectStorageAPI\",\"version\":\"1.0.0\",\"description\":\"API for interacting with Oracle Cloud Infrastructure (OCI) Object Storage.\"},\"servers\":[{\"url\":\"https://<namespace>.objectstorage.us-chicago-1.oci.customer-oci.com\"}],\"paths\":{\"/n/<namespace>/b/{bucketName}/o\":{\"get\":{\"summary\":\"List objects in a bucket\",\"description\":\"Retrieves a list of objects stored in the specified bucket.\",\"operationId\":\"listObjects\",\"parameters\":[{\"name\":\"bucketName\",\"in\":\"path\",\"required\":true,\"description\":\"The name of the bucket.\",\"schema\":{\"type\":\"string\"}},{\"name\":\"prefix\",\"in\":\"query\",\"required\":false,\"description\":\"Filter objects by prefix.\",\"schema\":{\"type\":\"string\"}},{\"name\":\"limit\",\"in\":\"query\",\"required\":false,\"description\":\"Maximum number of objects to return.\",\"schema\":{\"type\":\"integer\",\"format\":\"int32\"}},{\"name\":\"start\",\"in\":\"query\",\"required\":false,\"description\":\"Pagination token to start listing from.\",\"schema\":{\"type\":\"string\"}}],\"responses\":{\"200\":{\"description\":\"A list of objects in the bucket.\",\"content\":{\"application/json\":{\"schema\":{\"type\":\"object\",\"properties\":{\"objects\":{\"type\":\"array\",\"items\":{\"type\":\"object\",\"properties\":{\"name\":{\"type\":\"string\"},\"size\":{\"type\":\"integer\",\"format\":\"int64\"},\"md5\":{\"type\":\"string\"},\"timeCreated\":{\"type\":\"string\",\"format\":\"date-time\"},\"etag\":{\"type\":\"string\"}}}}}}}}},\"400\":{\"description\":\"Bad request.\"},\"401\":{\"description\":\"Unauthorized request.\"},\"404\":{\"description\":\"Bucket not found.\"},\"500\":{\"description\":\"Internal server error.\"}}},\"/n/<namespace>/b/{bucketName}/o/{objectName}\":{\"delete\":{\"summary\":\"Delete an object from a bucket\",\"description\":\"Deletes a specified object from the given bucket.\",\"operationId\":\"deleteObject\",\"parameters\":[{\"name\":\"bucketName\",\"in\":\"path\",\"required\":true,\"description\":\"The name of the bucket.\",\"schema\":{\"type\":\"string\"}},{\"name\":\"objectName\",\"in\":\"path\",\"required\":true,\"description\":\"The name of the object to delete.\",\"schema\":{\"type\":\"string\"}},{\"name\":\"versionId\",\"in\":\"query\",\"required\":false,\"description\":\"The version ID of the object (if versioning is enabled).\",\"schema\":{\"type\":\"string\"}}],\"responses\":{\"204\":{\"description\":\"Object deleted successfully.\"},\"400\":{\"description\":\"Bad request.\"},\"401\":{\"description\":\"Unauthorized request.\"},\"404\":{\"description\":\"Object not found.\"},\"500\":{\"description\":\"Internal server error.\"}}}}}}")
                        .build())
                .httpEndpointAuthConfig(HttpEndpointAuthConfig.builder()
            .httpEndpointAuthSources(
                    Collections.singletonList(
                            HttpEndpointAuthSource.builder()
                                    .httpEndpointAuthScope(
                                            HttpEndpointAuthSource.HttpEndpointAuthScope.Agent)
                                    .httpEndpointAuthScopeConfig(
                                            HttpEndpointOciAuthScopeConfig.builder().build())
                                    .build()))
            .build());
                .subnetId("ocid1.subnet.oc1.us-chicago-1.aaaaaaaal7lmb2rnugbljkchadorxub7463ggill2onyt74v3l5dqhgsojcq")
                .build();
    }
    

    In the code:

    • apiSchema defines the OpenAPI specification for the HTTP API you want to interact with.
    • httpEndpointAuthConfig specifies the authentication configuration for the HTTP endpoint. In the example, we're calling an OCI service, so authentication is set to OCI Resource Principal, and the <namespace> is the Object Storage namespace of the tenancy.
    • subnetId is the ID of the subnet that we want to use to make API endpoint calls.
  3. Create a CreateToolDetails request to define your tool.
    // Create a CreateToolDetails request
    CreateToolDetails createToolDetails = CreateToolDetails.builder()
                    .agentId(agentId)
                    .compartmentId(COMPARTMENT_ID)
                    .toolConfig(createHttpEndpointToolConfig())
                    .displayName("tool-sdk")
                    .description("tool description")
                    .build();
    
    // Build the CreateToolRequest
    CreateToolRequest toolRequest = CreateToolRequest.builder()
                     .createToolDetails(createToolDetails)
                     .build();
    
    // Create the tool
    client.createTool(toolRequest);

    In the code:

    • Replace agentId and COMPARTMENT_ID with the appropriate values for your setup.
    • For toolConfig, pass the HttpEndpointToolConfig you created in step 2.

Chatting with an Agent

To initiate a conversation with the agent and use the HTTP endpoint tool, follow these steps:

  1. Create a session to start a conversation. For example:
    // 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();

    In the code, replace agentEndpointId with the OCID of the agent endpoint that you want to use.

  2. Invoke the HTTP endpoint tool by sending a chat request.
    // Create a chat request
    String message = "List all the objects under mydepartment bucket";
    final ChatDetails chatDetails = ChatDetails.builder()
            .shouldStream(shouldStream)
            .userMessage(message)
            .sessionId(sessionId)
            .build();
    final ChatRequest chatRequest = ChatRequest.builder()
            .chatDetails(chatDetails)
            .agentEndpointId(agentEndpointId)
            .build();
    
    // Send the chat request
    agentClient.chat(chatRequest);

    In this example, the message "List all the objects under mydepartment bucket triggers the HTTP endpoint tool to interact with the API and retrieves the list of objects.