|
4 | 4 | from google.protobuf import json_format |
5 | 5 |
|
6 | 6 | from pinecone.utils.tqdm import tqdm |
| 7 | +from pinecone.utils import require_kwargs |
7 | 8 | from concurrent.futures import as_completed, Future |
8 | 9 |
|
9 | 10 |
|
|
15 | 16 | parse_upsert_response, |
16 | 17 | parse_update_response, |
17 | 18 | parse_delete_response, |
| 19 | + parse_namespace_description, |
| 20 | + parse_list_namespaces_response, |
18 | 21 | ) |
19 | 22 | from .vector_factory_grpc import VectorFactoryGRPC |
20 | 23 | from .sparse_values_factory import SparseValuesFactory |
|
23 | 26 | FetchResponse, |
24 | 27 | QueryResponse, |
25 | 28 | IndexDescription as DescribeIndexStatsResponse, |
| 29 | + NamespaceDescription, |
| 30 | + ListNamespacesResponse, |
26 | 31 | ) |
27 | 32 | from pinecone.db_control.models.list_response import ListResponse as SimpleListResponse, Pagination |
28 | | -from pinecone.core.grpc.protos.db_data_2025_01_pb2 import ( |
| 33 | +from pinecone.core.grpc.protos.db_data_2025_04_pb2 import ( |
29 | 34 | Vector as GRPCVector, |
30 | 35 | QueryVector as GRPCQueryVector, |
31 | 36 | UpsertRequest, |
|
39 | 44 | DeleteResponse, |
40 | 45 | UpdateResponse, |
41 | 46 | SparseValues as GRPCSparseValues, |
| 47 | + DescribeNamespaceRequest, |
| 48 | + DeleteNamespaceRequest, |
| 49 | + ListNamespacesRequest, |
42 | 50 | ) |
43 | 51 | from pinecone import Vector, SparseValues |
44 | 52 | from pinecone.db_data.query_results_aggregator import QueryNamespacesResults, QueryResultsAggregator |
45 | | -from pinecone.core.grpc.protos.db_data_2025_01_pb2_grpc import VectorServiceStub |
| 53 | +from pinecone.core.grpc.protos.db_data_2025_04_pb2_grpc import VectorServiceStub |
46 | 54 | from .base import GRPCIndexBase |
47 | 55 | from .future import PineconeGrpcFuture |
48 | 56 | from ..db_data.types import ( |
|
54 | 62 | ) |
55 | 63 |
|
56 | 64 |
|
57 | | -__all__ = ["GRPCIndex", "GRPCVector", "GRPCQueryVector", "GRPCSparseValues"] |
| 65 | +__all__ = ["GRPCIndex", "GRPCVector", "GRPCQueryVector", "GRPCSparseValues", "NamespaceDescription", "ListNamespacesResponse"] |
58 | 66 |
|
59 | 67 | _logger = logging.getLogger(__name__) |
60 | 68 | """ :meta private: """ |
@@ -681,6 +689,142 @@ def describe_index_stats( |
681 | 689 | json_response = json_format.MessageToDict(response) |
682 | 690 | return parse_stats_response(json_response) |
683 | 691 |
|
| 692 | + @require_kwargs |
| 693 | + def describe_namespace( |
| 694 | + self, namespace: str, **kwargs |
| 695 | + ) -> NamespaceDescription: |
| 696 | + """ |
| 697 | + The describe_namespace operation returns information about a specific namespace, |
| 698 | + including the total number of vectors in the namespace. |
| 699 | +
|
| 700 | + Examples: |
| 701 | +
|
| 702 | + .. code-block:: python |
| 703 | +
|
| 704 | + >>> index.describe_namespace(namespace='my_namespace') |
| 705 | +
|
| 706 | + Args: |
| 707 | + namespace (str): The namespace to describe. |
| 708 | +
|
| 709 | + Returns: NamespaceDescription object which contains information about the namespace. |
| 710 | + """ |
| 711 | + timeout = kwargs.pop("timeout", None) |
| 712 | + request = DescribeNamespaceRequest(namespace=namespace) |
| 713 | + response = self.runner.run(self.stub.DescribeNamespace, request, timeout=timeout) |
| 714 | + return parse_namespace_description(response) |
| 715 | + |
| 716 | + @require_kwargs |
| 717 | + def delete_namespace( |
| 718 | + self, namespace: str, **kwargs |
| 719 | + ) -> Dict[str, Any]: |
| 720 | + """ |
| 721 | + The delete_namespace operation deletes a namespace from an index. |
| 722 | + This operation is irreversible and will permanently delete all data in the namespace. |
| 723 | +
|
| 724 | + Examples: |
| 725 | +
|
| 726 | + .. code-block:: python |
| 727 | +
|
| 728 | + >>> index.delete_namespace(namespace='my_namespace') |
| 729 | +
|
| 730 | + Args: |
| 731 | + namespace (str): The namespace to delete. |
| 732 | +
|
| 733 | + Returns: Empty dictionary indicating successful deletion. |
| 734 | + """ |
| 735 | + timeout = kwargs.pop("timeout", None) |
| 736 | + request = DeleteNamespaceRequest(namespace=namespace) |
| 737 | + response = self.runner.run(self.stub.DeleteNamespace, request, timeout=timeout) |
| 738 | + return parse_delete_response(response) |
| 739 | + |
| 740 | + @require_kwargs |
| 741 | + def list_namespaces_paginated( |
| 742 | + self, |
| 743 | + limit: Optional[int] = None, |
| 744 | + pagination_token: Optional[str] = None, |
| 745 | + **kwargs, |
| 746 | + ) -> ListNamespacesResponse: |
| 747 | + """ |
| 748 | + The list_namespaces_paginated operation returns a list of all namespaces in a serverless index. |
| 749 | + It returns namespaces in a paginated form, with a pagination token to fetch the next page of results. |
| 750 | +
|
| 751 | + Examples: |
| 752 | +
|
| 753 | + .. code-block:: python |
| 754 | +
|
| 755 | + >>> results = index.list_namespaces_paginated(limit=10) |
| 756 | + >>> [ns.name for ns in results.namespaces] |
| 757 | + ['namespace1', 'namespace2', 'namespace3'] |
| 758 | + >>> results.pagination.next |
| 759 | + eyJza2lwX3Bhc3QiOiI5OTMiLCJwcmVmaXgiOiI5OSJ9 |
| 760 | + >>> next_results = index.list_namespaces_paginated(limit=10, pagination_token=results.pagination.next) |
| 761 | +
|
| 762 | + Args: |
| 763 | + limit (Optional[int]): The maximum number of namespaces to return. If unspecified, the server will use a default value. [optional] |
| 764 | + pagination_token (Optional[str]): A token needed to fetch the next page of results. This token is returned |
| 765 | + in the response if additional results are available. [optional] |
| 766 | +
|
| 767 | + Returns: ListNamespacesResponse object which contains the list of namespaces and pagination information. |
| 768 | + """ |
| 769 | + args_dict = self._parse_non_empty_args( |
| 770 | + [ |
| 771 | + ("limit", limit), |
| 772 | + ("pagination_token", pagination_token), |
| 773 | + ] |
| 774 | + ) |
| 775 | + timeout = kwargs.pop("timeout", None) |
| 776 | + request = ListNamespacesRequest(**args_dict, **kwargs) |
| 777 | + response = self.runner.run(self.stub.ListNamespaces, request, timeout=timeout) |
| 778 | + return parse_list_namespaces_response(response) |
| 779 | + |
| 780 | + @require_kwargs |
| 781 | + def list_namespaces(self, limit: Optional[int] = None, **kwargs): |
| 782 | + """ |
| 783 | + The list_namespaces operation accepts all of the same arguments as list_namespaces_paginated, and returns a generator that yields |
| 784 | + each namespace. It automatically handles pagination tokens on your behalf. |
| 785 | +
|
| 786 | + Args: |
| 787 | + limit (Optional[int]): The maximum number of namespaces to fetch in each network call. If unspecified, the server will use a default value. [optional] |
| 788 | +
|
| 789 | + Returns: |
| 790 | + Returns a generator that yields each namespace. It automatically handles pagination tokens on your behalf so you can |
| 791 | + easily iterate over all results. The ``list_namespaces`` method accepts all of the same arguments as list_namespaces_paginated |
| 792 | +
|
| 793 | + Examples: |
| 794 | +
|
| 795 | + .. code-block:: python |
| 796 | +
|
| 797 | + >>> for namespace in index.list_namespaces(): |
| 798 | + >>> print(namespace.name) |
| 799 | + namespace1 |
| 800 | + namespace2 |
| 801 | + namespace3 |
| 802 | +
|
| 803 | + You can convert the generator into a list by wrapping the generator in a call to the built-in ``list`` function: |
| 804 | +
|
| 805 | + .. code-block:: python |
| 806 | +
|
| 807 | + namespaces = list(index.list_namespaces()) |
| 808 | +
|
| 809 | + You should be cautious with this approach because it will fetch all namespaces at once, which could be a large number |
| 810 | + of network calls and a lot of memory to hold the results. |
| 811 | + """ |
| 812 | + done = False |
| 813 | + while not done: |
| 814 | + try: |
| 815 | + results = self.list_namespaces_paginated(limit=limit, **kwargs) |
| 816 | + except Exception as e: |
| 817 | + raise e |
| 818 | + |
| 819 | + if results.namespaces and len(results.namespaces) > 0: |
| 820 | + for namespace in results.namespaces: |
| 821 | + yield namespace |
| 822 | + |
| 823 | + if results.pagination and results.pagination.next: |
| 824 | + kwargs.update({"pagination_token": results.pagination.next}) |
| 825 | + else: |
| 826 | + done = True |
| 827 | + |
684 | 828 | @staticmethod |
685 | 829 | def _parse_non_empty_args(args: List[Tuple[str, Any]]) -> Dict[str, Any]: |
686 | 830 | return {arg_name: val for arg_name, val in args if val is not None} |
0 commit comments