add serializer methods (#7914)

Description: I have added two methods serializer and deserializer
methods. There was method called save local but it saves the to the
local disk. I wanted the vectorstore in the format using which i can
push it to the sql database's blob field. I have used this while i was
working on something

@rlancemartin, @eyurtsev

---------

Co-authored-by: Harrison Chase <hw.chase.17@gmail.com>
This commit is contained in:
Bal Narendra Sapa 2023-08-03 23:10:35 -04:00 committed by GitHub
parent b786335dd1
commit 47eea32f6a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -674,6 +674,23 @@ class FAISS(VectorStore):
embeddings.embed_query, index, docstore, index_to_docstore_id, **kwargs embeddings.embed_query, index, docstore, index_to_docstore_id, **kwargs
) )
def serialize_to_bytes(self) -> bytes:
"""Serialize FAISS index, docstore, and index_to_docstore_id to bytes."""
return pickle.dumps((self.index, self.docstore, self.index_to_docstore_id))
@classmethod
def deserialize_from_bytes(
cls,
serialized: bytes,
embeddings: Embeddings,
**kwargs: Any,
) -> FAISS:
"""Deserialize FAISS index, docstore, and index_to_docstore_id from bytes."""
index, docstore, index_to_docstore_id = pickle.loads(serialized)
return cls(
embeddings.embed_query, index, docstore, index_to_docstore_id, **kwargs
)
def _select_relevance_score_fn(self) -> Callable[[float], float]: def _select_relevance_score_fn(self) -> Callable[[float], float]:
""" """
The 'correct' relevance function The 'correct' relevance function