pinecone_text.dense.base_dense_ecoder

 1from typing import List, Union, Any, Optional
 2from abc import ABC, abstractmethod
 3from functools import cached_property
 4
 5
 6class BaseDenseEncoder(ABC):
 7    def __init__(self, *args: Any, **kwargs: Any) -> None:
 8        self._dimension: Optional[int] = None
 9
10    @cached_property
11    def dimension(self) -> int:
12        if self._dimension is None:
13            return len(self.encode_documents("hello"))
14        return self._dimension
15
16    @abstractmethod
17    def encode_documents(
18        self, texts: Union[str, List[str]]
19    ) -> Union[List[float], List[List[float]]]:
20        """
21        encode documents to a dense vector (for upsert to pinecone)
22
23        Args:
24            texts: a single or list of documents to encode as a string
25        """
26        pass  # pragma: no cover
27
28    @abstractmethod
29    def encode_queries(
30        self, texts: Union[str, List[str]]
31    ) -> Union[List[float], List[List[float]]]:
32        """
33        encode queries to a dense vector
34
35        Args:
36            texts: a single or list of queries to encode as a string
37        """
38        pass  # pragma: no cover
class BaseDenseEncoder(abc.ABC):
 7class BaseDenseEncoder(ABC):
 8    def __init__(self, *args: Any, **kwargs: Any) -> None:
 9        self._dimension: Optional[int] = None
10
11    @cached_property
12    def dimension(self) -> int:
13        if self._dimension is None:
14            return len(self.encode_documents("hello"))
15        return self._dimension
16
17    @abstractmethod
18    def encode_documents(
19        self, texts: Union[str, List[str]]
20    ) -> Union[List[float], List[List[float]]]:
21        """
22        encode documents to a dense vector (for upsert to pinecone)
23
24        Args:
25            texts: a single or list of documents to encode as a string
26        """
27        pass  # pragma: no cover
28
29    @abstractmethod
30    def encode_queries(
31        self, texts: Union[str, List[str]]
32    ) -> Union[List[float], List[List[float]]]:
33        """
34        encode queries to a dense vector
35
36        Args:
37            texts: a single or list of queries to encode as a string
38        """
39        pass  # pragma: no cover

Helper class that provides a standard way to create an ABC using inheritance.

@abstractmethod
def encode_documents( self, texts: Union[str, List[str]]) -> Union[List[float], List[List[float]]]:
17    @abstractmethod
18    def encode_documents(
19        self, texts: Union[str, List[str]]
20    ) -> Union[List[float], List[List[float]]]:
21        """
22        encode documents to a dense vector (for upsert to pinecone)
23
24        Args:
25            texts: a single or list of documents to encode as a string
26        """
27        pass  # pragma: no cover

encode documents to a dense vector (for upsert to pinecone)

Arguments:
  • texts: a single or list of documents to encode as a string
@abstractmethod
def encode_queries( self, texts: Union[str, List[str]]) -> Union[List[float], List[List[float]]]:
29    @abstractmethod
30    def encode_queries(
31        self, texts: Union[str, List[str]]
32    ) -> Union[List[float], List[List[float]]]:
33        """
34        encode queries to a dense vector
35
36        Args:
37            texts: a single or list of queries to encode as a string
38        """
39        pass  # pragma: no cover

encode queries to a dense vector

Arguments:
  • texts: a single or list of queries to encode as a string