快速开始

安装pyfg

在python 3.11环境下,执行如下命令:

pip install http://tzrec.oss-cn-beijing.aliyuncs.com/third_party/pyfg-0.3.7-cp311-cp311-linux_x86_64.whl

python 3.10 的安装包路径:http://tzrec.oss-cn-beijing.aliyuncs.com/third_party/pyfg-0.3.7-cp310-cp310-linux_x86_64.whl

执行FG

#!/usr/bin/env python
import os
import pyfg

config = {
  "features": [
    {
      "feature_name": "goods_id",
      "feature_type": "id_feature",
      "value_type": "string",
      "expression": "item:goods_id",
      "default_value": "-1024",
      "need_prefix": False,
      "value_dimension": 1
    },
    {
      "feature_name": "color_pair",
      "feature_type": "combo_feature",
      "value_type": "string",
      "expression": ["user:query_color", "item:color"],
      "default_value": "",
      "need_prefix": False,
      "value_dimension": 1
    },
    {
      "feature_name": "current_price",
      "feature_type": "raw_feature",
      "value_type": "double",
      "expression": "item:current_price",
      "default_value": "0",
      "need_prefix": False
    }, 
    {
      "feature_name": "usr_cate1_clk_cnt_1d",
      "feature_type": "lookup_feature",
      "map": "user:usr_cate1_clk_cnt_1d",
      "key": "item:cate1",
      "need_discrete": False,
      "need_key": False,
      "default_value": "0",
      "combiner": "max",
      "need_prefix": False,
      "value_type": "double"
    },
    {
      "feature_name": "recommend_match",
      "feature_type": "overlap_feature",
      "method": "is_contain",
      "query": "user:query_recommend",
      "title": "item:recommend",
      "default_value": "0"
    },
    {
      "feature_name": "query_title_match_ratio",
      "feature_type": "overlap_feature",
      "method": "query_common_ratio",
      "query": "user:query_terms",
      "title": "item:title_terms",
      "default_value": "0"
    },
    {
      "feature_name": "title_term_match_ratio",
      "feature_type": "overlap_feature",
      "method": "title_common_ratio",
      "query": "user:query_terms",
      "title": "item:title_terms",
      "default_value": "0"
    },
    {
      "feature_name": "term_proximity_min_cover",
      "feature_type": "overlap_feature",
      "method": "proximity_min_cover",
      "query": "user:query_terms",
      "title": "item:title_terms",
      "default_value": "0"
    }
  ]
}


if __name__ == '__main__':
    handler = pyfg.FgHandler(config)
    inputs = {
      "goods_id": ["110", "111", "112"],
      "query_color": ["red", "pink", "gray"],
      "color": ["white", "black", "pink"],
      "current_price": [0.5, 0.25, 0.78],
      "usr_cate1_clk_cnt_1d": [
        {"c1": 1, "c2": 13, "c3": 5},
        {"c1": 5, "c2": 3, "c4": 4.5},
        {"c7": 7, "c5": 9, "c3": 5}
      ],
      "cate1": ["c1", "c2", "c3"],
      "query_recommend": ["精品", "品牌", "优质"],
      "recommend": ["精品", "品牌", "严选"],
      "title_terms": [
        "清扬\035男士\035洗发水\035去屑\035清爽\035洗发膏\035男士\035活力\035运动\035100G",
        "康师傅\035茉莉\035蜜茶\035330ml*12\035瓶",
        "雕牌\035洗洁精\035家用\035大桶\035食品级\035洗碗液\035果蔬\035清洗剂\035实惠装\035洗碟精"
      ],
      "query_terms": [
        "清扬\035洗发水",
        "茉莉\035清茶",
        "洗洁精\035家用"
      ]
    }
    outputs, status = handler(inputs)
    print("status:", status.ok())
    print("outputs:", outputs)
    print("------------------------ meta info ---------------------------")
    print("user side inputs:", handler.user_inputs())
    print("item side inputs:", handler.item_inputs())
    print("context side inputs:", handler.context_inputs())
    print("offline table schema:", handler.table_schema())

输出结果如下,其中,value_dimension用来控制输出的维度。

status: True
outputs: {'term_proximity_min_cover': [3.0, 0.0, 2.0], 'title_term_match_ratio': [0.20000000298023224, 0.20000000298023224, 0.20000000298023224], 'query_title_match_ratio': [1.0, 0.5, 1.0], 'recommend_match': [1.0, 1.0, 0.0], 'usr_cate1_clk_cnt_1d': [1.0, 3.0, 5.0], 'current_price': [0.5, 0.25, 0.7799999713897705], 'color_pair': ['red_white', 'pink_black', 'gray_pink'], 'goods_id': ['110', '111', '112']}
------------------------ meta info ---------------------------
user side inputs: {'query_color', 'query_terms', 'query_recommend', 'usr_cate1_clk_cnt_1d'}
item side inputs: {'current_price', 'title_terms', 'recommend', 'cate1', 'color', 'goods_id'}
context side inputs: set()
offline table schema: {'term_proximity_min_cover': 'float', 'title_term_match_ratio': 'float', 'query_title_match_ratio': 'float', 'recommend_match': 'float', 'usr_cate1_clk_cnt_1d': 'double', 'current_price': 'double', 'color_pair': 'string', 'goods_id': 'string'}