Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: serialize/deserialize faiss index #23

Merged
merged 2 commits into from
Jan 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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