Spaces:
Sleeping
Sleeping
File size: 1,044 Bytes
7fdb8e9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
from loguru import logger
from typing_extensions import Annotated
from typing import Generator
from .base import VectorBaseDocument
def batch(list_: list, size: int) -> Generator[list, None, None]:
yield from (list_[i : i + size] for i in range(0, len(list_), size))
def load_to_vector_db(
documents: Annotated[list, "documents"],
) -> Annotated[bool, "successful"]:
logger.info(f"Loading {len(documents)} documents into the vector database.")
grouped_documents = VectorBaseDocument.group_by_class(documents)
for document_class, documents in grouped_documents.items():
logger.info(f"Loading documents into {document_class.get_collection_name()}")
for documents_batch in batch(documents, size=4):
try:
document_class.bulk_insert(documents_batch)
except Exception as e:
logger.error(
f"Failed to insert documents into {document_class.get_collection_name()}: {e}"
)
return False
return True
|