Skip to content

Commit

Permalink
feat: serialize/deserialize faiss index (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
fynnfluegge authored Jan 14, 2024
1 parent 8baa7ec commit b836cdf
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions codeqai/vector_store.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os

import inquirer
from langchain.embeddings.base import Embeddings
from langchain.schema import Document
Expand All @@ -17,10 +19,11 @@ def __init__(self, name: str, embeddings: Embeddings):
def load_documents(self):
spinner = yaspin(text="💾 Loading vector store...", color="green")
spinner.start()
self.db = FAISS.load_local(
index_name=self.name,
folder_path=get_cache_path(),
embeddings=self.embeddings,
with open(os.path.join(get_cache_path(), f"{self.name}.faiss"), "rb") as file:
index = file.read()

self.db = FAISS.deserialize_from_bytes(
embeddings=self.embeddings, serialized=index
)
self.vector_cache = load_vector_cache(f"{self.name}.json")
spinner.stop()
Expand All @@ -31,8 +34,11 @@ def index_documents(self, documents: list[Document]):
spinner = yaspin(text="💾 Indexing vector store...", color="green")
spinner.start()
self.db = FAISS.from_documents(documents, self.embeddings)
self.db.save_local(index_name=self.name, folder_path=get_cache_path())

index = self.db.serialize_to_bytes()
with open(
os.path.join(get_cache_path(), f"{self.name}.faiss"), "wb"
) as binary_file:
binary_file.write(index)
# Create vector cache
index_to_docstore_id = self.db.index_to_docstore_id
for i in range(len(documents)):
Expand Down

0 comments on commit b836cdf

Please sign in to comment.