Skip to content

Commit

Permalink
raise more legible error if the embedding vector dims don't match
Browse files Browse the repository at this point in the history
  • Loading branch information
asoderlind committed Oct 12, 2023
1 parent e5e5a42 commit 9b6ae46
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion application/vectorstore/faiss.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from langchain.vectorstores import FAISS
from application.core.settings import settings

HUGGINGFACE_MODEL_NAME = "huggingface_sentence-transformers/all-mpnet-base-v2"
class FaissStore(BaseVectorStore):

def __init__(self, path, embeddings_key, docs_init=None):
Expand All @@ -12,9 +13,19 @@ def __init__(self, path, embeddings_key, docs_init=None):
docs_init, self._get_embeddings(settings.EMBEDDINGS_NAME, embeddings_key)
)
else:
embeddings = self._get_embeddings(settings.EMBEDDINGS_NAME, embeddings_key)

Check warning on line 16 in application/vectorstore/faiss.py

View check run for this annotation

Codecov / codecov/patch

application/vectorstore/faiss.py#L16

Added line #L16 was not covered by tests
self.docsearch = FAISS.load_local(
self.path, self._get_embeddings(settings.EMBEDDINGS_NAME, settings.EMBEDDINGS_KEY)
self.path, embeddings
)

# Check that the word_embedding_dimension of the index matches the word_embedding_dimension of the embeddings

Check failure on line 21 in application/vectorstore/faiss.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

application/vectorstore/faiss.py:21:121: E501 Line too long (121 > 120 characters)
if settings.EMBEDDINGS_NAME == HUGGINGFACE_MODEL_NAME:
try:
word_embedding_dimension = embeddings.client[1].word_embedding_dimension
except AttributeError as e:
raise AttributeError("word_embedding_dimension not found in embeddings.client[1]") from e
if word_embedding_dimension != self.docsearch.index.d:
raise ValueError("word_embedding_dimension != docsearch_index_word_embedding_dimension")

Check warning on line 28 in application/vectorstore/faiss.py

View check run for this annotation

Codecov / codecov/patch

application/vectorstore/faiss.py#L22-L28

Added lines #L22 - L28 were not covered by tests

def search(self, *args, **kwargs):
return self.docsearch.similarity_search(*args, **kwargs)
Expand Down

0 comments on commit 9b6ae46

Please sign in to comment.