1
1
import logging
2
- from typing import Optional , Dict , Union , List , Tuple , Any , TypedDict , cast
2
+ from typing import Optional , Dict , Union , List , cast
3
3
4
4
from google .protobuf import json_format
5
5
6
6
from tqdm .autonotebook import tqdm
7
7
8
+ from pinecone .utils import parse_non_empty_args
8
9
from .utils import (
9
10
dict_to_proto_struct ,
10
11
parse_fetch_response ,
11
12
parse_query_response ,
12
13
parse_stats_response ,
14
+ parse_sparse_values_arg ,
13
15
)
14
16
from .vector_factory_grpc import VectorFactoryGRPC
17
+ from .base import GRPCIndexBase
18
+ from .future import PineconeGrpcFuture
19
+ from .sparse_vector import SparseVectorTypedDict
20
+ from .config import GRPCClientConfig
15
21
16
22
from pinecone .core .openapi .data .models import (
17
23
FetchResponse ,
36
42
)
37
43
from pinecone import Vector as NonGRPCVector
38
44
from pinecone .core .grpc .protos .vector_service_pb2_grpc import VectorServiceStub
39
- from .base import GRPCIndexBase
40
- from .future import PineconeGrpcFuture
41
45
42
- from .config import GRPCClientConfig
43
46
from pinecone .config import Config
44
47
from grpc ._channel import Channel
45
48
49
52
_logger = logging .getLogger (__name__ )
50
53
51
54
52
- class SparseVectorTypedDict (TypedDict ):
53
- indices : List [int ]
54
- values : List [float ]
55
-
56
-
57
55
class GRPCIndex (GRPCIndexBase ):
58
56
"""A client for interacting with a Pinecone index via GRPC API."""
59
57
@@ -152,7 +150,7 @@ def upsert(
152
150
153
151
vectors = list (map (VectorFactoryGRPC .build , vectors ))
154
152
if async_req :
155
- args_dict = self . _parse_non_empty_args ([("namespace" , namespace )])
153
+ args_dict = parse_non_empty_args ([("namespace" , namespace )])
156
154
request = UpsertRequest (vectors = vectors , ** args_dict , ** kwargs )
157
155
future = self .runner .run (self .stub .Upsert .future , request , timeout = timeout )
158
156
return PineconeGrpcFuture (future )
@@ -178,7 +176,7 @@ def upsert(
178
176
def _upsert_batch (
179
177
self , vectors : List [GRPCVector ], namespace : Optional [str ], timeout : Optional [int ], ** kwargs
180
178
) -> UpsertResponse :
181
- args_dict = self . _parse_non_empty_args ([("namespace" , namespace )])
179
+ args_dict = parse_non_empty_args ([("namespace" , namespace )])
182
180
request = UpsertRequest (vectors = vectors , ** args_dict )
183
181
return self .runner .run (self .stub .Upsert , request , timeout = timeout , ** kwargs )
184
182
@@ -285,7 +283,7 @@ def delete(
285
283
else :
286
284
filter_struct = None
287
285
288
- args_dict = self . _parse_non_empty_args (
286
+ args_dict = parse_non_empty_args (
289
287
[
290
288
("ids" , ids ),
291
289
("delete_all" , delete_all ),
@@ -322,7 +320,7 @@ def fetch(
322
320
"""
323
321
timeout = kwargs .pop ("timeout" , None )
324
322
325
- args_dict = self . _parse_non_empty_args ([("namespace" , namespace )])
323
+ args_dict = parse_non_empty_args ([("namespace" , namespace )])
326
324
327
325
request = FetchRequest (ids = ids , ** args_dict , ** kwargs )
328
326
response = self .runner .run (self .stub .Fetch , request , timeout = timeout )
@@ -388,8 +386,8 @@ def query(
388
386
else :
389
387
filter_struct = None
390
388
391
- sparse_vector = self . _parse_sparse_values_arg (sparse_vector )
392
- args_dict = self . _parse_non_empty_args (
389
+ sparse_vector = parse_sparse_values_arg (sparse_vector )
390
+ args_dict = parse_non_empty_args (
393
391
[
394
392
("vector" , vector ),
395
393
("id" , id ),
@@ -456,8 +454,8 @@ def update(
456
454
set_metadata_struct = None
457
455
458
456
timeout = kwargs .pop ("timeout" , None )
459
- sparse_values = self . _parse_sparse_values_arg (sparse_values )
460
- args_dict = self . _parse_non_empty_args (
457
+ sparse_values = parse_sparse_values_arg (sparse_values )
458
+ args_dict = parse_non_empty_args (
461
459
[
462
460
("values" , values ),
463
461
("set_metadata" , set_metadata_struct ),
@@ -506,7 +504,7 @@ def list_paginated(
506
504
507
505
Returns: SimpleListResponse object which contains the list of ids, the namespace name, pagination information, and usage showing the number of read_units consumed.
508
506
"""
509
- args_dict = self . _parse_non_empty_args (
507
+ args_dict = parse_non_empty_args (
510
508
[
511
509
("prefix" , prefix ),
512
510
("limit" , limit ),
@@ -585,36 +583,10 @@ def describe_index_stats(
585
583
filter_struct = dict_to_proto_struct (filter )
586
584
else :
587
585
filter_struct = None
588
- args_dict = self . _parse_non_empty_args ([("filter" , filter_struct )])
586
+ args_dict = parse_non_empty_args ([("filter" , filter_struct )])
589
587
timeout = kwargs .pop ("timeout" , None )
590
588
591
589
request = DescribeIndexStatsRequest (** args_dict )
592
590
response = self .runner .run (self .stub .DescribeIndexStats , request , timeout = timeout )
593
591
json_response = json_format .MessageToDict (response )
594
592
return parse_stats_response (json_response )
595
-
596
- @staticmethod
597
- def _parse_non_empty_args (args : List [Tuple [str , Any ]]) -> Dict [str , Any ]:
598
- return {arg_name : val for arg_name , val in args if val is not None }
599
-
600
- @staticmethod
601
- def _parse_sparse_values_arg (
602
- sparse_values : Optional [Union [GRPCSparseValues , SparseVectorTypedDict ]],
603
- ) -> Optional [GRPCSparseValues ]:
604
- if sparse_values is None :
605
- return None
606
-
607
- if isinstance (sparse_values , GRPCSparseValues ):
608
- return sparse_values
609
-
610
- if (
611
- not isinstance (sparse_values , dict )
612
- or "indices" not in sparse_values
613
- or "values" not in sparse_values
614
- ):
615
- raise ValueError (
616
- "Invalid sparse values argument. Expected a dict of: {'indices': List[int], 'values': List[float]}."
617
- f"Received: { sparse_values } "
618
- )
619
-
620
- return GRPCSparseValues (indices = sparse_values ["indices" ], values = sparse_values ["values" ])
0 commit comments