-
Notifications
You must be signed in to change notification settings - Fork 85
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
Implement deletion protection #367
Changes from all commits
09c32e2
d31b072
2c48823
5818072
343751c
1378390
24e2f3e
fb4c78e
afcd31b
1e42790
824802d
6b26b2d
08bbac2
628c8a2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import time | ||
import warnings | ||
import logging | ||
from typing import Optional, Dict, Any, Union, List, Tuple, cast, NamedTuple | ||
from typing import Optional, Dict, Any, Union, List, Tuple, Literal | ||
|
||
from .index_host_store import IndexHostStore | ||
|
||
|
@@ -23,12 +23,14 @@ | |
ConfigureIndexRequest, | ||
ConfigureIndexRequestSpec, | ||
ConfigureIndexRequestSpecPod, | ||
DeletionProtection, | ||
IndexSpec, | ||
ServerlessSpec as ServerlessSpecModel, | ||
PodSpec as PodSpecModel, | ||
PodSpecMetadataConfig, | ||
) | ||
from pinecone.models import ServerlessSpec, PodSpec, IndexList, CollectionList | ||
from pinecone.core.openapi.shared import API_VERSION | ||
from pinecone.models import ServerlessSpec, PodSpec, IndexModel, IndexList, CollectionList | ||
from .langchain_import_warnings import _build_langchain_attribute_error_message | ||
|
||
from pinecone.data import Index | ||
|
@@ -229,6 +231,7 @@ def __init__( | |
config=self.config, | ||
openapi_config=self.openapi_config, | ||
pool_threads=pool_threads, | ||
api_version=API_VERSION, | ||
) | ||
|
||
self.index_host_store = IndexHostStore() | ||
|
@@ -258,6 +261,7 @@ def create_index( | |
spec: Union[Dict, ServerlessSpec, PodSpec], | ||
metric: Optional[str] = "cosine", | ||
timeout: Optional[int] = None, | ||
deletion_protection: Optional[Literal["enabled", "disabled"]] = "disabled", | ||
): | ||
"""Creates a Pinecone index. | ||
|
||
|
@@ -278,6 +282,7 @@ def create_index( | |
:type timeout: int, optional | ||
:param timeout: Specify the number of seconds to wait until index gets ready. If None, wait indefinitely; if >=0, time out after this many seconds; | ||
if -1, return immediately and do not wait. Default: None | ||
:param deletion_protection: If enabled, the index cannot be deleted. If disabled, the index can be deleted. Default: "disabled" | ||
|
||
### Creating a serverless index | ||
|
||
|
@@ -291,7 +296,8 @@ def create_index( | |
name="my_index", | ||
dimension=1536, | ||
metric="cosine", | ||
spec=ServerlessSpec(cloud="aws", region="us-west-2") | ||
spec=ServerlessSpec(cloud="aws", region="us-west-2"), | ||
deletion_protection="enabled" | ||
) | ||
``` | ||
|
||
|
@@ -310,7 +316,8 @@ def create_index( | |
spec=PodSpec( | ||
environment="us-east1-gcp", | ||
pod_type="p1.x1" | ||
) | ||
), | ||
deletion_protection="enabled" | ||
) | ||
``` | ||
""" | ||
|
@@ -320,6 +327,11 @@ def create_index( | |
def _parse_non_empty_args(args: List[Tuple[str, Any]]) -> Dict[str, Any]: | ||
return {arg_name: val for arg_name, val in args if val is not None} | ||
|
||
if deletion_protection in ["enabled", "disabled"]: | ||
dp = DeletionProtection(deletion_protection) | ||
else: | ||
raise ValueError("deletion_protection must be either 'enabled' or 'disabled'") | ||
Comment on lines
+330
to
+333
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. The generated |
||
|
||
if isinstance(spec, dict): | ||
if "serverless" in spec: | ||
index_spec = IndexSpec(serverless=ServerlessSpecModel(**spec["serverless"])) | ||
|
@@ -376,6 +388,7 @@ def _parse_non_empty_args(args: List[Tuple[str, Any]]) -> Dict[str, Any]: | |
dimension=dimension, | ||
metric=metric, | ||
spec=index_spec, | ||
deletion_protection=dp, | ||
), | ||
) | ||
|
||
|
@@ -454,7 +467,7 @@ def list_indexes(self) -> IndexList: | |
index name, dimension, metric, status, and spec. | ||
|
||
:return: Returns an `IndexList` object, which is iterable and contains a | ||
list of `IndexDescription` objects. It also has a convenience method `names()` | ||
list of `IndexModel` objects. It also has a convenience method `names()` | ||
which returns a list of index names. | ||
|
||
```python | ||
|
@@ -498,7 +511,7 @@ def describe_index(self, name: str): | |
"""Describes a Pinecone index. | ||
|
||
:param name: the name of the index to describe. | ||
:return: Returns an `IndexDescription` object | ||
:return: Returns an `IndexModel` object | ||
which gives access to properties such as the | ||
index name, dimension, metric, host url, status, | ||
and spec. | ||
|
@@ -530,13 +543,14 @@ def describe_index(self, name: str): | |
host = description.host | ||
self.index_host_store.set_host(self.config, name, host) | ||
|
||
return description | ||
return IndexModel(description) | ||
|
||
def configure_index( | ||
self, | ||
name: str, | ||
replicas: Optional[int] = None, | ||
pod_type: Optional[str] = None, | ||
deletion_protection: Optional[Literal["enabled", "disabled"]] = None, | ||
): | ||
"""This method is used to scale configuration fields for your pod-based Pinecone index. | ||
|
||
|
@@ -561,15 +575,28 @@ def configure_index( | |
|
||
""" | ||
api_instance = self.index_api | ||
config_args: Dict[str, Any] = {} | ||
|
||
if deletion_protection is None: | ||
description = self.describe_index(name=name) | ||
dp = DeletionProtection(description.deletion_protection) | ||
Comment on lines
+579
to
+581
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. If the user does not specify a value for |
||
elif deletion_protection in ["enabled", "disabled"]: | ||
dp = DeletionProtection(deletion_protection) | ||
else: | ||
raise ValueError("deletion_protection must be either 'enabled' or 'disabled'") | ||
|
||
pod_config_args: Dict[str, Any] = {} | ||
if pod_type: | ||
config_args.update(pod_type=pod_type) | ||
pod_config_args.update(pod_type=pod_type) | ||
if replicas: | ||
config_args.update(replicas=replicas) | ||
configure_index_request = ConfigureIndexRequest( | ||
spec=ConfigureIndexRequestSpec(pod=ConfigureIndexRequestSpecPod(**config_args)) | ||
) | ||
api_instance.configure_index(name, configure_index_request=configure_index_request) | ||
pod_config_args.update(replicas=replicas) | ||
|
||
if pod_config_args != {}: | ||
spec = ConfigureIndexRequestSpec(pod=ConfigureIndexRequestSpecPod(**pod_config_args)) | ||
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. I hate the arrangement of this code. Blame the black formatter. I need to update the configs to shorten the line length to something more reasonable. |
||
req = ConfigureIndexRequest(deletion_protection=dp, spec=spec) | ||
else: | ||
req = ConfigureIndexRequest(deletion_protection=dp) | ||
|
||
api_instance.configure_index(name, configure_index_request=req) | ||
|
||
def create_collection(self, name: str, source: str): | ||
"""Create a collection from a pod-based index | ||
|
@@ -634,7 +661,6 @@ def describe_collection(self, name: str): | |
print(description.source) | ||
print(description.status) | ||
print(description.size) | ||
print(description.) | ||
``` | ||
""" | ||
api_instance = self.index_api | ||
|
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.
This is docstrings code.