Skip to content
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
14 changes: 11 additions & 3 deletions backend/src/vectorstores/faiss.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import time
import logging
from typing import Optional, Union
from dotenv import load_dotenv
Expand Down Expand Up @@ -74,13 +75,13 @@ def faiss_db(self) -> Optional[FAISS]:

@retry(
stop=stop_after_attempt(5),
wait=wait_exponential(multiplier=2, min=10, max=120),
wait=wait_exponential(multiplier=2, min=60, max=600),
retry=retry_if_exception(
lambda e: "RESOURCE_EXHAUSTED" in str(e) or "429" in str(e)
),
reraise=True,
)
def _add_to_db(self, documents: list[Document]) -> None:
def _embed_and_add(self, documents: list[Document]) -> None:
if self._faiss_db is None:
self._faiss_db = FAISS.from_documents(
documents=documents,
Expand All @@ -90,6 +91,13 @@ def _add_to_db(self, documents: list[Document]) -> None:
else:
self._faiss_db.add_documents(documents)

def _add_to_db(self, documents: list[Document], batch_size: int = 100) -> None:
for i in range(0, len(documents), batch_size):
batch = documents[i : i + batch_size]
self._embed_and_add(batch)
if i + batch_size < len(documents):
time.sleep(1)

def add_md_docs(
self, folder_paths: list[str], chunk_size: int = 500, return_docs: bool = False
) -> Optional[list[Document]]:
Expand Down Expand Up @@ -229,7 +237,7 @@ def get_documents(self) -> list[Document]:

@retry(
stop=stop_after_attempt(5),
wait=wait_exponential(multiplier=2, min=10, max=120),
wait=wait_exponential(multiplier=2, min=60, max=600),
retry=retry_if_exception(
lambda e: "RESOURCE_EXHAUSTED" in str(e) or "429" in str(e)
),
Expand Down
Loading