EcomShoppingBuddy / utilities.py
RomyMy's picture
first logic
4eaf3da
raw
history blame
1.29 kB
from redis import Redis
from redis.commands.search.field import VectorField
from redis.commands.search.field import TextField
from redis.commands.search.field import TagField
from redis.commands.search.result import Result
import numpy as np
def load_vectors(client:Redis, product_metadata, vector_dict):
p = client.pipeline(transaction=False)
for index in product_metadata.keys():
#hash key
key='product:'+ str(index)+ ':' + product_metadata[index]['primary_key']
#hash values
item_metadata = product_metadata[index]
item_keywords_vector = np.array(vector_dict[index], dtype=np.float32).tobytes()
item_metadata['item_vector']=item_keywords_vector
# HSET
p.hset(key,mapping=item_metadata)
p.execute()
def create_flat_index (redis_conn, number_of_vectors, vector_dimensions=512, distance_metric='L2'):
redis_conn.ft().create_index([
VectorField('item_vector', "FLAT", {"TYPE": "FLOAT32", "DIM": vector_dimensions, "DISTANCE_METRIC": distance_metric, "INITIAL_CAP": number_of_vectors, "BLOCK_SIZE":number_of_vectors }),
TagField("product_type"),
TextField("item_name"),
TextField("item_keywords"),
TagField("country")
])