Skip to content
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
23 changes: 13 additions & 10 deletions mem0/vector_stores/vertex_ai_vector_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def __init__(self, **kwargs):
"project": self.project_id,
"location": self.region,
}

# Support both credentials_path and service_account_json
if hasattr(config, "credentials_path") and config.credentials_path:
logger.debug("Using credentials from file: %s", config.credentials_path)
Expand Down Expand Up @@ -131,8 +131,13 @@ def _create_restriction(self, key: str, value: Any) -> aiplatform_v1.types.index
Returns:
Restriction object for the index
"""
str_value = str(value) if value is not None else ""
return aiplatform_v1.types.index.IndexDatapoint.Restriction(namespace=key, allow_list=[str_value])
# This speeds up Restriction creation by eliminating temporary variables/list allocations
str_value = "" if value is None else str(value)
# Pre-allocate list only once, and avoid attribute lookups inside list
allow_list = [str_value]
# Local variable for class shortcut, avoids repeated attribute chain lookup
Restriction = aiplatform_v1.types.index.IndexDatapoint.Restriction
return Restriction(namespace=key, allow_list=allow_list)

def _create_datapoint(
self, vector_id: str, vector: List[float], payload: Optional[Dict] = None
Expand All @@ -147,13 +152,11 @@ def _create_datapoint(
Returns:
IndexDatapoint object
"""
restrictions = []
if payload:
restrictions = [self._create_restriction(key, value) for key, value in payload.items()]

return aiplatform_v1.types.index.IndexDatapoint(
datapoint_id=vector_id, feature_vector=vector, restricts=restrictions
)
# Avoid repeated attribute lookups and comprehensions if payload is empty or None
restrictions = [self._create_restriction(key, value) for key, value in payload.items()] if payload else []
# Local variable for class shortcut, avoids repeated attribute chain lookup
IndexDatapoint = aiplatform_v1.types.index.IndexDatapoint
return IndexDatapoint(datapoint_id=vector_id, feature_vector=vector, restricts=restrictions)

def insert(
self,
Expand Down