Commit 4361ceba authored by 赵威's avatar 赵威

add api

parent 06526cd7
import json
import os
import traceback
import faiss
import numpy as np
import pandas as pd
from gm_rpcd.all import bind
from utils.cache import redis_client_db
from utils.message import send_msg_to_dingtalk
from utils.personas import get_user_portrait_tag3_from_redis
MODEL_PATH = os.path.join(os.getcwd(), "_models")
INDEX_PATH = os.path.join(MODEL_PATH, "faiss_personas_vector.index")
FAISS_TAGS_INDEX = faiss.read_index(INDEX_PATH)
TAG_EMBEDDING_DICT = redis_client_db.hgetall("personas_tags_embedding")
@bind("strategy_embedding/personas_vector/match")
def match_tractate_by_device(device_id, n=10):
try:
business_tags = get_user_portrait_tag3_from_redis(device_id, tags_num=5).get("business_tags", [])
business_tags = ["假体下巴", "你好", "假体隆胸"]
res = []
vectors = []
for tag in business_tags:
lst = json.loads(TAG_EMBEDDING_DICT.get(bytes(tag, "utf-8"), b"[]"))
if lst:
vectors.append(np.array(lst).astype("float32"))
if vectors:
D, I = FAISS_TAGS_INDEX.search(np.array([np.average(vectors, axis=0)]).astype("float32"), n)
res = I.tolist()[0]
return res
except Exception as e:
send_msg_to_dingtalk(str(traceback.format_exc()))
return []
import json import json
import os import os
import random
import sys import sys
sys.path.append(os.path.realpath(".")) sys.path.append(os.path.realpath("."))
......
...@@ -33,3 +33,4 @@ keras==2.4.3 ...@@ -33,3 +33,4 @@ keras==2.4.3
protobuf==3.13.0 protobuf==3.13.0
ipython ipython
prompt-toolkit==2.0.10
import json
from utils.cache import redis_client2
def get_user_portrait_tag3_redis_key(device_id):
return "doris:user_portrait:tag3:device_id:" + str(device_id)
def get_user_portrait_tag3_from_redis(device_id, limit_score=0, tags_num=5):
def items_gt_score(d, tags_num=tags_num):
new_d = dict(sorted(d.items(), key=lambda x: x[1], reverse=True)[:tags_num])
res = {tag: float(score) for tag, score in new_d.items() if float(score) >= limit_score}
return list(res.keys())
portrait_key = get_user_portrait_tag3_redis_key(device_id)
if redis_client2.exists(portrait_key):
user_portrait = json.loads(redis_client2.get(portrait_key))
first_demands = items_gt_score(user_portrait.get("first_demands", {}))
second_demands = items_gt_score(user_portrait.get("second_demands", {}))
first_solutions = items_gt_score(user_portrait.get("first_solutions", {}))
second_solutions = items_gt_score(user_portrait.get("second_solutions", {}))
first_positions = items_gt_score(user_portrait.get("first_positions", {}))
second_positions = items_gt_score(user_portrait.get("second_positions", {}))
projects = items_gt_score(user_portrait.get("projects", {}))
anecdote_tags = items_gt_score(user_portrait.get("anecdote_tags", {}))
business_tags = items_gt_score(user_portrait.get("business_tags", {}))
return {
"first_demands": first_demands,
"second_demands": second_demands,
"first_solutions": first_solutions,
"second_solutions": second_solutions,
"first_positions": first_positions,
"second_positions": second_positions,
"projects": projects,
"anecdote_tags": anecdote_tags,
"business_tags": business_tags
}
return {}
...@@ -112,7 +112,7 @@ def clicked_tractate_ids_item2vec_model(id, n=5): ...@@ -112,7 +112,7 @@ def clicked_tractate_ids_item2vec_model(id, n=5):
try: try:
return TRACTATE_CLICK_IDS_MODEL.wv.most_similar(id, topn=n) return TRACTATE_CLICK_IDS_MODEL.wv.most_similar(id, topn=n)
except Exception as e: except Exception as e:
send_msg_to_dingtalk(str(traceback.format_exc())) # send_msg_to_dingtalk(str(traceback.format_exc()))
return [] return []
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment