All Products
Search
Document Center

Platform For AI:ABTest SDK for Java

Last Updated:Sep 20, 2024

This topic describes how to use ABTest SDK for Java to allocate experiment traffic and retrieve parameter configurations about experiments.

Prerequisites

Add dependencies

To use the SDK in a Maven-based Java project, you must include the following dependencies in the <dependencies> section of your pom.xml file:

  • pai-abtest-sdk

    <dependency>
      <groupId>com.aliyun.openservices.aiservice</groupId>
      <artifactId>pai-abtest-sdk</artifactId>
      <version>1.0.0</version>
    </dependency>
  • okhttp

    <dependency>
        <groupId>com.squareup.okhttp3</groupId>
        <artifactId>okhttp</artifactId>
        <version>4.12.0</version>
    </dependency>

Sample code

package com.aliyun.openservices.paiabtest;

import com.aliyun.openservices.paiabtest.api.ApiClient;
import com.aliyun.openservices.paiabtest.api.Configuration;
import com.aliyun.openservices.paiabtest.model.ExperimentContext;
import com.aliyun.openservices.paiabtest.model.ExperimentResult;

import java.util.HashMap;
import java.util.Map;

public class ExperimentTest {
    static ExperimentClient experimentClient;

    public static void main(String[] args) throws Exception {
        // Set up the experiment client
        String regionId = "cn-beijing";
        String accessId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
        Configuration configuration = new Configuration(regionId, accessId, accessKey);
        ApiClient apiClient = new ApiClient(configuration);
        experimentClient = new ExperimentClient(apiClient);

        // Initiate load experiment data
        experimentClient.init();

        // Set up the experiment context
        ExperimentContext experimentContext = new ExperimentContext();
        experimentContext.setRequestId("<req_id>");
        // Set the user ID
        experimentContext.setUid("<uid>");
        // Define filter parameters for condition matching
        Map<String, Object> filterParams = new HashMap<>();
        filterParams.put("sex", "male");
        experimentContext.setFilterParams(filterParams);

        // Match the experiment
        ExperimentResult experimentResult =  experimentClient.matchExperiment("<DefaultProject>", experimentContext);

        // Print experiment information
        System.out.println(experimentResult.info());
        // Print experiment ID
        System.out.println(experimentResult.getExpId());
        // Print experiment parameters
        System.out.println(experimentResult.getExperimentParams().get("recall_v", "not exist"));
        System.out.println(experimentResult.getExperimentParams().get("rank_v", "not exist"));
        System.out.println(experimentResult.getExperimentParams().get("male_v", "not exist"));
    }
}

Key parameters:

  • regionId: The region ID, such as cn-hangzhou for China (Hangzhou).

  • <req_id>: The custom request ID.

  • <uid>: The ID for experiment traffic allocation, which can be a UserID, a device ID, or other values.

  • filterParams.put("sex", "male"): Parameters for experiment filtering, such as sex and male. Modify the parameters based on your requirements.

  • <DefaultProject>: The name of the ABTest project. Go to the Project Management > Experiment Projects page to view the project name. For more information, see Create an experiment project.