首頁(yè)
人工智能平臺(tái) PAI
操作指南
AI開(kāi)發(fā)
A/B實(shí)驗(yàn)(ABTest)
A/B實(shí)驗(yàn)SDK參考
A/B實(shí)驗(yàn)Java SDK
A/B實(shí)驗(yàn)Java SDK
更新時(shí)間:
本文為您介紹如何使用A/B實(shí)驗(yàn)的Java SDK進(jìn)行實(shí)驗(yàn)分流,并獲取實(shí)驗(yàn)相關(guān)的參數(shù)配置信息。
前提條件
已在代碼運(yùn)行環(huán)境設(shè)置環(huán)境變量。具體操作,請(qǐng)參見(jiàn)在Linux、macOS和Windows系統(tǒng)配置環(huán)境變量。
已創(chuàng)建實(shí)驗(yàn)。具體操作,請(qǐng)參見(jiàn)新建實(shí)驗(yàn)。
添加依賴項(xiàng)
使用Java編寫(xiě)客戶端代碼時(shí),在Maven工程中使用A/B實(shí)驗(yàn)SDK,必須在pom.xml文件<dependencies>中添加如下依賴,示例如下所示:
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>
使用示例
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 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);
// init load experiment data
experimentClient.init();
// set up experiment context
ExperimentContext experimentContext = new ExperimentContext();
experimentContext.setRequestId("<req_id>");
// set uid
experimentContext.setUid("<uid>");
// set filter params for condition filter match
Map<String, Object> filterParams = new HashMap<>();
filterParams.put("sex", "male");
experimentContext.setFilterParams(filterParams);
// match experiment
ExperimentResult experimentResult = experimentClient.matchExperiment("<DefaultProject>", experimentContext);
// print experiment info
System.out.println(experimentResult.info());
// print exp id
System.out.println(experimentResult.getExpId());
// print exp params
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"));
}
}
其中:
regionId:地域ID,例如華東1(杭州)配置為cn-hangzhou。
<req_id>:自定義配置請(qǐng)求ID。
<uid>:實(shí)驗(yàn)分流ID標(biāo)識(shí)。可以是用戶側(cè)的UserID,也可以是設(shè)備ID等。
filterParams.put("sex", "male"):其中sex和male為實(shí)驗(yàn)分流參數(shù),請(qǐng)根據(jù)實(shí)際情況進(jìn)行修改。
<DefaultProject>:A/B實(shí)驗(yàn)項(xiàng)目名稱。請(qǐng)前往A/B實(shí)驗(yàn)的新建實(shí)驗(yàn)項(xiàng)目。
頁(yè)面,查看項(xiàng)目名稱。具體操作,請(qǐng)參見(jiàn)
文檔內(nèi)容是否對(duì)您有幫助?