Skip to content

Commit

Permalink
feedback updates
Browse files Browse the repository at this point in the history
  • Loading branch information
rbs333 committed Jul 31, 2024
1 parent 05ee76c commit 304b96f
Show file tree
Hide file tree
Showing 4 changed files with 18,956 additions and 16 deletions.
59 changes: 48 additions & 11 deletions backend/productsearch/api/routes/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ async def get_products(
gender: str = "",
category: str = "",
index: AsyncSearchIndex = Depends(redis_helpers.get_async_index),
):
"""_summary_
) -> ProductSearchResponse:
"""Fetch and return products based on gender and category fields
Args:
limit (int, optional): _description_. Defaults to 20.
Expand All @@ -56,7 +56,7 @@ async def get_products(
category (str, optional): _description_. Defaults to "".
Returns:
ProductSearchResponse model
ProductSearchResponse: Pydantic model containing products and total count
"""
# Build query
filter_expression = (Tag("gender") == gender) & (Tag("category") == category)
Expand All @@ -79,12 +79,30 @@ async def find_products_by_image(
similarity_request: SimilarityRequest,
index: AsyncSearchIndex = Depends(redis_helpers.get_async_index),
) -> ProductVectorSearchResponse:
"""Fetch and return products based on image similarity
Args:
SimilarityRequest:
number_of_results (int): Number of results to return
search_type (str): Search type
gender (str): filter criteria
category (str): filter criteria
Returns:
ProductSearchResponse:
total (int): Total number of results
products (list[VectorSearchProduct]): List of products
product_id (str): Product ID
name (str): Product name
gender (str): fashion tag
category (str): fashion tag
img_url (str): Image URL for displaying in FE
text_vector (str): Text vector for similarity computation
img_vector (str): Image vector for similarity computation
"""
# Fetch paper key and the vector from the HASH, cast to numpy array
product_key = f"{index.schema.index.prefix}:{similarity_request.product_id}"
product_img_vector = np.frombuffer(
await index.client.hget(product_key, "img_vector"), dtype=np.float32
)
product = await index.fetch(similarity_request.product_id)
product_img_vector = np.frombuffer(product["img_vector"], dtype=np.float32)

# Build filter expression
filter_expression = (Tag("gender") == similarity_request.gender) & (
Expand Down Expand Up @@ -119,11 +137,30 @@ async def find_products_by_text(
similarity_request: SimilarityRequest,
index: AsyncSearchIndex = Depends(redis_helpers.get_async_index),
) -> ProductVectorSearchResponse:
"""Fetch and return products based on image similarity
Args:
SimilarityRequest:
number_of_results (int): Number of results to return
search_type (str): Search type
gender (str): filter criteria
category (str): filter criteria
Returns:
ProductSearchResponse:
total (int): Total number of results
products (list[VectorSearchProduct]): List of products
product_id (str): Product ID
name (str): Product name
gender (str): fashion tag
category (str): fashion tag
img_url (str): Image URL for displaying in FE
text_vector (str): Text vector for similarity computation
img_vector (str): Image vector for similarity computation
"""
# Fetch paper key and the vector from the HASH, cast to numpy array
product_key = f"{index.schema.index.prefix}:{similarity_request.product_id}"
product_text_vector = np.frombuffer(
await index.client.hget(product_key, "text_vector"), dtype=np.float32
)
product = await index.fetch(similarity_request.product_id)
product_text_vector = np.frombuffer(product["text_vector"], dtype=np.float32)

# Build filter expression
filter_expression = (Tag("gender") == similarity_request.gender) & (
Expand Down
4 changes: 0 additions & 4 deletions backend/productsearch/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
API_DOCS = "/api/docs"
OPENAPI_DOCS = "/api/openapi.json"
INDEX_NAME = "products"
INDEX_TYPE = os.environ.get("VECSIM_INDEX_TYPE", "HNSW") # TODO: change
REDIS_HOST = os.environ.get("REDIS_HOST", "redis-vector-db")
REDIS_PORT = os.environ.get("REDIS_PORT", 6379)
REDIS_DB = os.environ.get("REDIS_DB", 0)
Expand All @@ -27,11 +26,8 @@
else:
REDIS_URL = f"redis://{REDIS_HOST}:{REDIS_PORT}"

os.environ["REDIS_DATA_URL"] = REDIS_URL
os.environ["REDIS_OM_URL"] = REDIS_URL
API_V1_STR = "/api/v1"
DATA_LOCATION = os.environ.get("DATA_LOCATION", "../data")
DEFAULT_RETURN_FIELDS = ["product_id", "product_pk", "vector_score"]
DEPLOYMENT_ENV = os.environ.get("DEPLOYMENT", "dev")

WRITE_CONCURRENCY = os.environ.get("WRITE_CONCURRENCY", 150)
Loading

0 comments on commit 304b96f

Please sign in to comment.