yangxiaohui před 1 rokem
rodič
revize
bf008abd42
1 změnil soubory, kde provedl 29 přidání a 17 odebrání
  1. 29 17
      ad_recommend.py

+ 29 - 17
ad_recommend.py

@@ -301,23 +301,24 @@ def predict_mid_video_res_with_model(now_date, mid, video_id, abtest_param, abte
     model_key = abtest_param.get('model_key', 'ad_out_v1')
     user_key_name = f"{config_.KEY_NAME_PREFIX_AD_OUT_MODEL_SCORE_USER}{model_key}:{mid}"
     item_key_name = f"{config_.KEY_NAME_PREFIX_AD_OUT_MODEL_SCORE_ITEM}{model_key}:{video_id}"
-    config_key_prefix = f"{config_.KEY_NAME_PREFIX_AD_OUT_MODEL_CONFIG}{model_key}:{abtest_id}:{abtest_config_tag}"
-    threshold_key = f"{config_key_prefix}:threshold"
-    use_backup_key = f"{config_key_prefix}:use_backup"
-    item_threshold_key = f"{config_key_prefix}:item_threshold"
-    user_threshold_key = f"{config_key_prefix}:user_threshold"
-    miss_threshold_key = f"{config_key_prefix}:miss_threshold"
-
     user_score = redis_helper.get_data_from_redis(key_name=user_key_name)
     item_score = redis_helper.get_data_from_redis(key_name=item_key_name)
+
+    config_key_prefix = f"{config_.KEY_NAME_PREFIX_AD_OUT_MODEL_CONFIG}{model_key}:{abtest_id}:{abtest_config_tag}"
+    config_key = f"{config_key_prefix}:config"
+    config_str = redis_helper.get_data_from_redis(key_name=config_key)
+    config = {}
+    if config_str is not None:
+        config = json.loads(config_str)
+
     hit_strategy = 'model'
-    # 如果离线分数为空,则走基线逻辑
+    threshold = config.get('threshold', None)
     if user_score is not None and item_score is not None:
         offline_score = float(user_score) + float(item_score)
     else:
-        use_backup = redis_helper.get_data_from_redis(key_name=use_backup_key)
+        use_backup = config.get('use_backup', False)
         # 如果离线分数为空 & 兜底策略开启,走兜底策略
-        if use_backup == 'true':
+        if use_backup:
             result = predict_mid_video_res(
                 now_date=now_date,
                 mid=mid,
@@ -336,15 +337,15 @@ def predict_mid_video_res_with_model(now_date, mid, video_id, abtest_param, abte
         else:
             if item_score is not None:
                 offline_score = float(item_score)
-                threshold_key = item_threshold_key
+                threshold = config.get('item_threshold', None)
                 hit_strategy = 'item'
             elif user_score is not None:
                 offline_score = float(user_score)
-                threshold_key = user_threshold_key
+                threshold = config.get('user_threshold', None)
                 hit_strategy = 'user'
             else:  # item_score and user_score all None
                 offline_score = 0.0
-                threshold_key = miss_threshold_key
+                threshold = config.get('miss_threshold', None)
                 hit_strategy = 'miss'
 
     online_features = {
@@ -354,14 +355,22 @@ def predict_mid_video_res_with_model(now_date, mid, video_id, abtest_param, abte
     }
 
     final_score, online_score = get_final_score(online_features, offline_score)
-    threshold = redis_helper.get_data_from_redis(key_name=threshold_key)
+
+    rank_score = 0.0
+    if config.get('use_rank_score', False):
+        rank_score_key = f"rank:score1:{video_id}"
+        rank_score = redis_helper.get_data_from_redis(key_name=rank_score_key)
+        if rank_score is not None:
+            rank_score = float(rank_score)
+    rank_score_bias = config.get('rank_score_bias', 0.0)
+    merge_score = config.get('final_score_w', 1.0) * final_score + config.get('rank_score_w', 1.0) * (rank_score + rank_score_bias)
+
     if threshold is None:
         threshold = 0.0
         hit_strategy = 'error_' + hit_strategy
-    else:
-        threshold = float(threshold)
+
     # 跳出率阈值判断
-    if final_score < threshold:
+    if merge_score < threshold:
         # 小于阈值,出广告
         ad_predict = 2
     else:
@@ -371,6 +380,9 @@ def predict_mid_video_res_with_model(now_date, mid, video_id, abtest_param, abte
         'user_score': user_score,
         'item_score': item_score,
         'final_score': final_score,
+        'merge_score': merge_score,
+        'rank_score': rank_score,
+        'rank_score_bias': rank_score_bias,
         'online_score': online_score,
         'threshold': threshold,
         'ad_predict': ad_predict,