SDK源码地址:https://github.com/volcengine/volcengine-sdk-python-rec,支持Python 3 及其以上版本。pip地址:https://pypi.org/project/volcengine-sdk-rec/。
pip install volcengine-sdk-rec
构建client,示例:
import uuid from datetime import datetime from byteair import ClientBuilder, Client from byteair.protocol.volcengine_byteair_pb2 import * from core import Region, Option, NetException, BizException, metrics # 必传,租户id. TENANT_ID = "xxx" # 必传,应用id. APPLICATION_ID = "xxx" # 必传,密钥AK. AK = "xxx" # 必传,密钥SK. SK = "xxx" # 必传,域名. HOST = "xxx" client: Client = ClientBuilder() \ .tenant_id(TENANT_ID) \ .application_id(APPLICATION_ID) \ .ak(AK) \ .sk(SK) \ .hosts([HOST]) \ .build() # metrics上报初始化.建议开启,方便火山侧排查问题. metrics.init((MetricsOption.with_metrics_domain(HOST),)) ''' 私有化部署初始化 # 必传,租户id. TENANT_ID = "xxx" # 必传,应用id. APPLICATION_ID = "xxx" # 必传,域名. HOST = "xxx" # 由火山侧给出 TOKEN = "xxx" client: Client = ClientBuilder() \ .tenant_id("xxx") \ .application_id("xxx") \ .use_air_auth() \ .token('xxx') \ .hosts([HOST]) \ .schema('http') \ .build() '''
初始化完成后,可以使用client进行数据同步和推荐服务的调用。client不需要重复创建,最好设置为全局变量,以免产生不必要的资源泄漏。
client封装了以下4个函数,分别用于请求数据服务的同步接口(write)和同步完成通知接口(done),推荐服务的推荐接口(predict)和下发回传接口(callback)。
write_data(self, data_list: list, topic: str, *opts: Option) done(self, date_list: Optional[list], topic: str, *opts: Option) predict(self, request: PredictRequest, *opts: CoreOption) callback(self, request: CallbackRequest, *opts: CoreOption)