Search code examples
pythonfaiss

Why a Faiss index works on macOS but not on Docker (TypeError in method)?


I'm trying to search for vectors using Faiss and access them through an API. All my packages are installed with Conda.

def get_similar_posts(embeddings: list[float]) -> list[str]:
    toSearch = np.array([embeddings])
    toSearch.reshape(1, len(embeddings))

    k = 30

    D, I = index.search(toSearch, k)

    print(D)

    return []

This snippet works perfectly fine on my MacBook Air M1. But as soon as I try to run it using Docker, Faiss returns this:

ERROR:    Exception in ASGI application
Traceback (most recent call last):
[...]
  File "/app/main.py", line 25, in read_root
    print(get_similar_posts(embed))
  File "/app/faiss_index.py", line 28, in get_similar_posts
    D, I = index.search(toSearch, k)
  File "/opt/conda/envs/env/lib/python3.10/site-packages/faiss/__init__.py", line 322, in replacement_search
    self.search_c(n, swig_ptr(x), k, swig_ptr(D), swig_ptr(I))
  File "/opt/conda/envs/env/lib/python3.10/site-packages/faiss/swigfaiss.py", line 7722, in search
    return _swigfaiss.IndexIDMap_search(self, n, x, k, distances, labels)
TypeError: in method 'IndexIDMap_search', argument 3 of type 'faiss::IndexIDMapTemplate< faiss::Index >::component_t const *'

I have tried to rebuild the index again using Float32, but it didn't make any difference. Digging through the Faiss code didn't give me any clues either.


Solution

  • I have fixed it.

    First, as stated in the GitHub, I had to set the type of my array to float32. But that wasn't enough. Faiss was still returning an error. I had to make sure k was an integer.

    Here is the full snippet:

    def get_similar_posts(embeddings: list[float]) -> list[str]:
        toSearch = np.array([embeddings]).astype("float32")
        toSearch.reshape(1, len(embeddings))
    
        k = 30
        k = int(k)
    
        D, I = index.search(toSearch, k)
    
        print(D)
    
        return []
    

    I don't really know the reasons, but it's fixed.