pinecone .data .dataclasses .vector
1from typing import List, Optional 2from .sparse_values import SparseValues 3from .utils import DictLike 4from ..types import VectorTypedDict, VectorMetadataTypedDict 5 6from dataclasses import dataclass, field 7 8 9@dataclass 10class Vector(DictLike): 11 id: str 12 values: List[float] = field(default_factory=list) 13 metadata: Optional[VectorMetadataTypedDict] = None 14 sparse_values: Optional[SparseValues] = None 15 16 def __post_init__(self): 17 if self.sparse_values is None and len(self.values) == 0: 18 raise ValueError("The values and sparse_values fields cannot both be empty") 19 20 def to_dict(self) -> VectorTypedDict: 21 vector_dict: VectorTypedDict = {"id": self.id, "values": self.values} 22 if self.metadata is not None: 23 vector_dict["metadata"] = self.metadata 24 if self.sparse_values is not None: 25 vector_dict["sparse_values"] = self.sparse_values.to_dict() 26 return vector_dict 27 28 @staticmethod 29 def from_dict(vector_dict: VectorTypedDict) -> "Vector": 30 passed_sparse_values = vector_dict.get("sparse_values") 31 if passed_sparse_values is not None: 32 parsed_sparse_values = SparseValues.from_dict(passed_sparse_values) 33 else: 34 parsed_sparse_values = None 35 36 return Vector( 37 id=vector_dict["id"], 38 values=vector_dict["values"], 39 metadata=vector_dict.get("metadata"), 40 sparse_values=parsed_sparse_values, 41 )
10@dataclass 11class Vector(DictLike): 12 id: str 13 values: List[float] = field(default_factory=list) 14 metadata: Optional[VectorMetadataTypedDict] = None 15 sparse_values: Optional[SparseValues] = None 16 17 def __post_init__(self): 18 if self.sparse_values is None and len(self.values) == 0: 19 raise ValueError("The values and sparse_values fields cannot both be empty") 20 21 def to_dict(self) -> VectorTypedDict: 22 vector_dict: VectorTypedDict = {"id": self.id, "values": self.values} 23 if self.metadata is not None: 24 vector_dict["metadata"] = self.metadata 25 if self.sparse_values is not None: 26 vector_dict["sparse_values"] = self.sparse_values.to_dict() 27 return vector_dict 28 29 @staticmethod 30 def from_dict(vector_dict: VectorTypedDict) -> "Vector": 31 passed_sparse_values = vector_dict.get("sparse_values") 32 if passed_sparse_values is not None: 33 parsed_sparse_values = SparseValues.from_dict(passed_sparse_values) 34 else: 35 parsed_sparse_values = None 36 37 return Vector( 38 id=vector_dict["id"], 39 values=vector_dict["values"], 40 metadata=vector_dict.get("metadata"), 41 sparse_values=parsed_sparse_values, 42 )
Vector( id: str, values: List[float] = <factory>, metadata: Optional[Dict[str, Union[str, int, float, List[str], List[int], List[float]]]] = None, sparse_values: Optional[pinecone.data.dataclasses.sparse_values.SparseValues] = None)
21 def to_dict(self) -> VectorTypedDict: 22 vector_dict: VectorTypedDict = {"id": self.id, "values": self.values} 23 if self.metadata is not None: 24 vector_dict["metadata"] = self.metadata 25 if self.sparse_values is not None: 26 vector_dict["sparse_values"] = self.sparse_values.to_dict() 27 return vector_dict
@staticmethod
def
from_dict( vector_dict: pinecone.data.types.vector_typed_dict.VectorTypedDict) -> Vector:
29 @staticmethod 30 def from_dict(vector_dict: VectorTypedDict) -> "Vector": 31 passed_sparse_values = vector_dict.get("sparse_values") 32 if passed_sparse_values is not None: 33 parsed_sparse_values = SparseValues.from_dict(passed_sparse_values) 34 else: 35 parsed_sparse_values = None 36 37 return Vector( 38 id=vector_dict["id"], 39 values=vector_dict["values"], 40 metadata=vector_dict.get("metadata"), 41 sparse_values=parsed_sparse_values, 42 )