调用个性化推荐接口,传入用户id以及相关的上下文信息等,可以获得推荐结果列表。
PredictResponse predict(PredictRequest request, Option... opts)
参数 | 类型 | 说明 |
---|---|---|
request | PredictRequest | 请求体,请求体包含的参数说明见下表,具体使用方式见用例示范 |
opts | Option[] | 请求中可选参数,具体使用方式见用例示范;
|
request请求体参数:
参数 | 类型 | 是否必传 | 描述 |
---|---|---|---|
user | object | 是 | 用户信息 |
context | object | 是 | 上下文信息 |
candidateItems | object list | 否 | 跳过召回等特殊场景需要上传候选集 |
parentItem | object | 否 | 相关推荐场景需要上传 |
filterItems | object list | 否 | 需要过滤的物品列表 |
size | int | 否 | 接口返回物品个数(优先级高于栏位配置返回数量) |
推荐服务相关方法使用自定义的PredictRsp类作为响应类型。
参数 | 类型 | 描述 |
---|---|---|
code | int32 | 0或200代表正常,不等同于http status,用于排查业务错误 |
message | string | 状态信息,默认"OK",遇到错误会返回错误信息 |
value | object | |
requestId | string | 推荐请求的requestId |
import volcengine.byteair.ByteairClient; import volcengine.byteair.ByteairClientBuilder; import volcengine.byteair.protocol.VolcengineByteair; import volcengine.core.Option; import volcengine.core.Region; import volcengine.core.metrics.MetricsCollector; import java.util.*; public class Example { public static ByteairClient byteairClient; // 示例省略public static void init() // 某些语法可能在低版本JDK报错,请根据需求替换成等价语法. public static void predict() { // 请求体 VolcengineByteair.PredictUser user = VolcengineByteair.PredictUser.newBuilder() .setUid("uid1") .build(); Map<String, String> stringFilter = new HashMap<>() {{ put("key1", "value1"); put("key2", "value2"); }}; List<String> values = new ArrayList<>() {{ add("value3"); add("value4"); }}; Map<String, VolcengineByteair.StringArray> stringArrayFilter = new HashMap<>() {{ put("key3", VolcengineByteair.StringArray.newBuilder().addAllValues(values).build()); }}; VolcengineByteair.PredictContext context = VolcengineByteair.PredictContext.newBuilder() .setSpm("A$##$B$##$C") .putExtra("extra_key", "extra_value") .setFilter(VolcengineByteair.PredictFilter.newBuilder().putAllStringFilter(stringFilter).putAllStringArrayFilter(stringArrayFilter).build()) .build(); List<VolcengineByteair.PredictCandidateItem> items = new ArrayList<>(); items.add(VolcengineByteair.PredictCandidateItem.newBuilder().setId("item_id1").build()); items.add(VolcengineByteair.PredictCandidateItem.newBuilder().setId("item_id2").build()); VolcengineByteair.PredictRequest predictRequest = VolcengineByteair.PredictRequest.newBuilder() .setUser(user) .setContext(context) .setSize(10) .addAllCandidateItems(items) .build(); Option[] predictOpts = new Option[]{ // 默认default,无需修改. Option.withScene("default"), Option.withRequestId(String.valueOf(UUID.randomUUID())), // 是否开启SPM路由.开启的话需要保证请求体里的SPM存在且绑定了栏位. // server会根据body里的SPM路由到选择的栏位. Option.withHeaders(new HashMap<>() {{ put("Enable-Spm-Route", "true"); }}) }; VolcengineByteair.PredictResponse predictResponse; try { predictResponse = byteairClient.predict(predictRequest, predictOpts); } catch (BizException | NetException e) { System.out.printf("[predict] occur error, msg:%s \n", e.getMessage()); return; } if (!predictResponse.getSuccess()) { System.out.println("[predict] failure"); return; } System.out.println("[predict] success"); } }