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: Add Vertex Vector Search Datapoints deletion, add autogenerated datapoint IDs to document metadata during ingestion #331

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from abc import ABC, abstractmethod
from typing import Any, List, Tuple, Union
from typing import Any, List, Sequence, Tuple, Union

from google.cloud import storage # type: ignore[attr-defined, unused-ignore]
from google.cloud.aiplatform import telemetry
Expand Down Expand Up @@ -60,6 +60,11 @@ def add_to_index(
"""
raise NotImplementedError()

@abstractmethod
def remove_datapoints(self, datapoint_ids: Sequence[str]) -> None:
"""Remove datapoints from the index."""
raise NotImplementedError()

def _postprocess_response(
self, response: List[List[MatchNeighbor]]
) -> List[List[Tuple[str, float]]]:
Expand Down Expand Up @@ -136,6 +141,26 @@ def add_to_index(
is_complete_overwrite=is_complete_overwrite,
)

def remove_datapoints(self, datapoint_ids: Sequence[str]) -> None:
"""Remove datapoints from the index.

Args:
datapoint_ids: Sequence[str]
Required. The list of datapoint ids t
Raises:
ValueError: If the datapoint_ids sequence is empty.
RuntimeError: If there's an error while removing datapoints.
"""
if not datapoint_ids:
raise ValueError("datapoint_ids must not be empty")

try:
self._index.remove_datapoints(datapoint_ids=datapoint_ids)
except Exception as e:
raise RuntimeError(
f"Error removing datapoints from the index: {str(e)}"
) from e

def find_neighbors(
self,
embeddings: List[List[float]],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ def add_texts(
f"{len(metadatas)} != {len(texts)}"
)

# Add generated IDs to metadata
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure modifying things in-place is a good idea. Can we just pass doc_ids from outside maybe?

for metadata, id in zip(metadatas, ids):
metadata["doc_id"] = id

documents = [
Document(page_content=text, metadata=metadata)
for text, metadata in zip(texts, metadatas)
Expand All @@ -207,6 +211,33 @@ def add_texts(

return ids

def delete(self, ids: Optional[List[str]] = None, **kwargs: Any) -> Optional[bool]:
"""
Delete by vector ID.
Args:
ids (Optional[List[str]]): List of ids to delete.
**kwargs (Any): Other keyword arguments (not used,
but included for compatibility).
Returns:
Optional[bool]: True if deletion is successful.
Raises:
ValueError: If ids is None or an empty list.
RuntimeError: If an error occurs during the deletion process.
"""
if ids is None or len(ids) == 0:
raise ValueError("ids must be provided and cannot be an empty list")

try:
# Remove datapoints from the Vertex Vector Search Index
self._searcher.remove_datapoints(datapoint_ids=ids)
# Remove documents from the document storage
self._document_storage.mdelete(ids)

return True

except Exception as e:
raise RuntimeError(f"Error during deletion: {str(e)}") from e

@classmethod
def from_texts(
cls: Type["_BaseVertexAIVectorStore"],
Expand Down
Loading