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

Allow batching of embedding generation when running BaseIndex.refresh_ref_docs to achieve significant speed improvement #16196

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
40 changes: 22 additions & 18 deletions llama-index-core/llama_index/core/indices/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,17 +203,23 @@ def insert_nodes(self, nodes: Sequence[BaseNode], **insert_kwargs: Any) -> None:
self._insert(nodes, **insert_kwargs)
self._storage_context.index_store.add_index_struct(self._index_struct)

def insert(self, document: Document, **insert_kwargs: Any) -> None:
def insert(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updating base class methods has some larger impacts -- need to check every class that subclasses BaseIndex and ensure that if its implementing this method, that it matches

self, documents: Document | Sequence[Document], **insert_kwargs: Any
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be using python3.8 syntax for now: documents: Union[Document, Sequence[Document]]

) -> None:
"""Insert a document."""
with self._callback_manager.as_trace("insert"):
if isinstance(documents, Document):
documents = [documents]

nodes = run_transformations(
[document],
documents,
self._transformations,
show_progress=self._show_progress,
)

self.insert_nodes(nodes, **insert_kwargs)
self.docstore.set_document_hash(document.get_doc_id(), document.hash)
for doc in documents:
self.docstore.set_document_hash(doc.get_doc_id(), doc.hash)

@abstractmethod
def _delete_node(self, node_id: str, **delete_kwargs: Any) -> None:
Expand Down Expand Up @@ -298,13 +304,7 @@ def update_ref_doc(self, document: Document, **update_kwargs: Any) -> None:
delete_kwargs (Dict): kwargs to pass to delete

"""
with self._callback_manager.as_trace("update"):
self.delete_ref_doc(
document.get_doc_id(),
delete_from_docstore=True,
**update_kwargs.pop("delete_kwargs", {}),
)
self.insert(document, **update_kwargs.pop("insert_kwargs", {}))
self.refresh_ref_docs([document], **update_kwargs)

def refresh(
self, documents: Sequence[Document], **update_kwargs: Any
Expand Down Expand Up @@ -332,19 +332,23 @@ def refresh_ref_docs(
"""
with self._callback_manager.as_trace("refresh"):
refreshed_documents = [False] * len(documents)
for i, document in enumerate(documents):
existing_doc_hash = self._docstore.get_document_hash(
document.get_doc_id()
)
for i, doc in enumerate(documents):
existing_doc_hash = self._docstore.get_document_hash(doc.get_doc_id())
if existing_doc_hash is None:
self.insert(document, **update_kwargs.pop("insert_kwargs", {}))
refreshed_documents[i] = True
elif existing_doc_hash != document.hash:
self.update_ref_doc(
document, **update_kwargs.pop("update_kwargs", {})
elif existing_doc_hash != doc.hash:
self.delete_ref_doc(
doc.get_doc_id(),
delete_from_docstore=True,
**update_kwargs.pop("delete_kwargs", {}),
)
refreshed_documents[i] = True

documents = [
doc for i, doc in enumerate(documents) if refreshed_documents[i]
]
self.insert(documents, **update_kwargs.pop("insert_kwargs", {}))

return refreshed_documents

@property
Expand Down