From 47eea32f6a70f6330d6c5cf54e2037a5d5d490eb Mon Sep 17 00:00:00 2001 From: Bal Narendra Sapa <61614290+balnarendrasapa@users.noreply.github.com> Date: Thu, 3 Aug 2023 23:10:35 -0400 Subject: [PATCH] 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 --- libs/langchain/langchain/vectorstores/faiss.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/libs/langchain/langchain/vectorstores/faiss.py b/libs/langchain/langchain/vectorstores/faiss.py index 31930beaff..0e29daf01b 100644 --- a/libs/langchain/langchain/vectorstores/faiss.py +++ b/libs/langchain/langchain/vectorstores/faiss.py @@ -674,6 +674,23 @@ class FAISS(VectorStore): 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]: """ The 'correct' relevance function