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