Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import faiss
- import numpy as np
- # Assume we have product embeddings (vectors) for all our products
- product_embeddings = np.random.random((1000000, 128)).astype('float32') # 1M products, 128-dimensional embeddings
- # Create an index
- index = faiss.IndexFlatL2(128) # Using L2 distance
- # Add vectors to the index
- index.add(product_embeddings)
- # Let's say a user is viewing a product with this embedding
- query_embedding = np.random.random((1, 128)).astype('float32')
- # Search for similar products
- k = 5 # Number of similar products to retrieve
- distances, indices = index.search(query_embedding, k)
- print(f"Indices of similar products: {indices}")
- print(f"Distances to similar products: {distances}")
Advertisement
Add Comment
Please, Sign In to add comment