-
Notifications
You must be signed in to change notification settings - Fork 5.1k
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
Andrea-Oliveri
wants to merge
2
commits into
run-llama:main
Choose a base branch
from
Andrea-Oliveri:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+22
−18
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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( | ||
self, documents: Document | Sequence[Document], **insert_kwargs: Any | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be using python3.8 syntax for now: |
||
) -> 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: | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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