You need to enable JavaScript to run this app.
导航

推荐接口(predict)

最近更新时间2023.11.22 11:41:21

首次发布时间2022.04.13 17:53:20

调用个性化推荐接口,传入用户id以及相关的上下文信息等,可以获得推荐结果列表。

调用方法

Predict(request *PredictRequest,  opts …option.Option) (*PredictResponse, error)

方法参数

参数

类型

说明

request

PredictRequest

请求体,请求体包含的参数说明见下表,具体使用方式见用例示范

opts

[]option.Option

额外参数可通过
option.WithRequestId(requestId string)自行设置requestId

  • requestId字段很重要,会通过requestId串起个性化推荐、曝光数据上报、行为数据。
  • 每次请求的requestId需要保证唯一
  • 如果未指定requestId,SDK会自动生成唯一的requestId,可通过返回体拿到该requestId

request请求体参数:

参数
类型
是否必传
描述
user
object
用户信息
context
object
上下文信息
candidateItems
object list
跳过召回等特殊场景需要上传候选集
parentItem
object
相关推荐场景需要上传
filterItems
object list
需要过滤的物品列表

方法返回

推荐服务相关方法使用自定义的PredictRsp类作为响应类型

参数
类型
描述
code
int32
0或200代表正常,不等同于http status,用于排查业务错误
message
string
状态信息,默认"OK",遇到错误会返回错误信息
value
object
requestId
string
推荐请求的requestId

示例

import (
   "github.com/google/uuid"
   "github.com/volcengine/volcengine-sdk-go-rec/byteair"
   "github.com/volcengine/volcengine-sdk-go-rec/byteair/protocol"
   "github.com/volcengine/volcengine-sdk-go-rec/core/logs"
   "github.com/volcengine/volcengine-sdk-go-rec/core/option"
)


// 已经初始化好的client.示例省略init()
var client byteair.Client


func Predict() {
   // 请求体
   req := &protocol.PredictRequest{
      User: &protocol.PredictUser{
         Uid: "uid1",
      },
      Context: &protocol.PredictContext{
         Spm:   "A$##$B$##$C",
         Extra: map[string]string{"extra_key": "extra_value"},
      },
      CandidateItems: []*protocol.PredictCandidateItem{&protocol.PredictCandidateItem{
         Id: "item_id1",
      }},
      ParentItem: &protocol.PredictParentItem{
         Id: "item_id2",
      },
   }
   opts := []option.Option{
      // 默认default,无需修改.
      option.WithScene("default"),
      option.WithRequestId(uuid.NewString()),
      // 是否开启SPM路由.开启的话需要保证请求体里的SPM存在且绑定了栏位.
      // server会根据body里的SPM路由到选择的栏位.
      option.WithHeaders(map[string]string{
         "Enable-spm-route": "true",
      }),
   }
   rsp, err := client.Predict(req, opts...)
   if err != nil {
      logs.Error("[predict] occur error, msg: %s", err.Error())
      return
   }
    if !rsp.GetSuccess() {
       logs.Error("[predict] failure")
       return
    }
    logs.Info("[predict] success")
}