datadabllp

efficient vector search

Jul 19th, 2024
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.49 KB | None | 0 0
  1. import faiss
  2. import numpy as np
  3.  
  4. # Assume we have a large number of document embeddings
  5. num_docs = 1000000
  6. embedding_dim = 128
  7. embeddings = np.random.random((num_docs, embedding_dim)).astype('float32')
  8.  
  9. # Create an index
  10. index = faiss.IndexFlatL2(embedding_dim)
  11. index.add(embeddings)
  12.  
  13. # Perform a search
  14. k = 5  # number of nearest neighbors
  15. query = np.random.random((1, embedding_dim)).astype('float32')
  16. D, I = index.search(query, k)
  17.  
  18. print(f"Distances: {D}")
  19. print(f"Indices: {I}")
  20.  
Advertisement
Add Comment
Please, Sign In to add comment