pinecone.data.sparse_vector_factory
1from collections.abc import Mapping 2from typing import Union, Dict 3 4from ..utils import convert_to_list 5 6from .errors import ( 7 SparseValuesTypeError, 8 SparseValuesMissingKeysError, 9 SparseValuesDictionaryExpectedError, 10) 11 12from pinecone.core.openapi.data.models import SparseValues 13 14 15class SparseValuesFactory: 16 @staticmethod 17 def build(input: Union[Dict, SparseValues]) -> SparseValues: 18 if input is None: 19 return input 20 if isinstance(input, SparseValues): 21 return input 22 if not isinstance(input, Mapping): 23 raise SparseValuesDictionaryExpectedError(input) 24 if not {"indices", "values"}.issubset(input): 25 raise SparseValuesMissingKeysError(input) 26 27 indices = SparseValuesFactory._convert_to_list(input.get("indices"), int) 28 values = SparseValuesFactory._convert_to_list(input.get("values"), float) 29 30 if len(indices) != len(values): 31 raise ValueError("Sparse values indices and values must have the same length") 32 33 try: 34 return SparseValues(indices=indices, values=values) 35 except TypeError as e: 36 raise SparseValuesTypeError() from e 37 38 @staticmethod 39 def _convert_to_list(input, expected_type): 40 try: 41 converted = convert_to_list(input) 42 except TypeError as e: 43 raise SparseValuesTypeError() from e 44 45 SparseValuesFactory._validate_list_items_type(converted, expected_type) 46 return converted 47 48 @staticmethod 49 def _validate_list_items_type(input, expected_type): 50 if len(input) > 0 and not isinstance(input[0], expected_type): 51 raise SparseValuesTypeError()
class
SparseValuesFactory:
16class SparseValuesFactory: 17 @staticmethod 18 def build(input: Union[Dict, SparseValues]) -> SparseValues: 19 if input is None: 20 return input 21 if isinstance(input, SparseValues): 22 return input 23 if not isinstance(input, Mapping): 24 raise SparseValuesDictionaryExpectedError(input) 25 if not {"indices", "values"}.issubset(input): 26 raise SparseValuesMissingKeysError(input) 27 28 indices = SparseValuesFactory._convert_to_list(input.get("indices"), int) 29 values = SparseValuesFactory._convert_to_list(input.get("values"), float) 30 31 if len(indices) != len(values): 32 raise ValueError("Sparse values indices and values must have the same length") 33 34 try: 35 return SparseValues(indices=indices, values=values) 36 except TypeError as e: 37 raise SparseValuesTypeError() from e 38 39 @staticmethod 40 def _convert_to_list(input, expected_type): 41 try: 42 converted = convert_to_list(input) 43 except TypeError as e: 44 raise SparseValuesTypeError() from e 45 46 SparseValuesFactory._validate_list_items_type(converted, expected_type) 47 return converted 48 49 @staticmethod 50 def _validate_list_items_type(input, expected_type): 51 if len(input) > 0 and not isinstance(input[0], expected_type): 52 raise SparseValuesTypeError()
@staticmethod
def
build( input: Union[Dict, pinecone.core.openapi.data.model.sparse_values.SparseValues]) -> pinecone.core.openapi.data.model.sparse_values.SparseValues:
17 @staticmethod 18 def build(input: Union[Dict, SparseValues]) -> SparseValues: 19 if input is None: 20 return input 21 if isinstance(input, SparseValues): 22 return input 23 if not isinstance(input, Mapping): 24 raise SparseValuesDictionaryExpectedError(input) 25 if not {"indices", "values"}.issubset(input): 26 raise SparseValuesMissingKeysError(input) 27 28 indices = SparseValuesFactory._convert_to_list(input.get("indices"), int) 29 values = SparseValuesFactory._convert_to_list(input.get("values"), float) 30 31 if len(indices) != len(values): 32 raise ValueError("Sparse values indices and values must have the same length") 33 34 try: 35 return SparseValues(indices=indices, values=values) 36 except TypeError as e: 37 raise SparseValuesTypeError() from e