pinecone.data.features.inference.inference_request_builder

 1from enum import Enum
 2from typing import Optional, Union, List, Dict, Any
 3
 4from pinecone.core.openapi.inference.models import (
 5    EmbedRequest,
 6    EmbedRequestInputs,
 7    Document,
 8    RerankRequest,
 9)
10from pinecone.utils import convert_enum_to_string
11
12
13class EmbedModel(Enum):
14    Multilingual_E5_Large = "multilingual-e5-large"
15    Pinecone_Sparse_English_V0 = "pinecone-sparse-english-v0"
16
17
18class RerankModel(Enum):
19    Bge_Reranker_V2_M3 = "bge-reranker-v2-m3"
20    Cohere_Rerank_3_5 = "cohere-rerank-3.5"
21    Pinecone_Rerank_V0 = "pinecone-rerank-v0"
22
23
24class InferenceRequestBuilder:
25    @staticmethod
26    def embed_request(
27        model: Union[EmbedModel, str],
28        inputs: Union[str, List[Dict], List[str]],
29        parameters: Optional[Dict[str, Any]] = None,
30    ) -> EmbedRequest:
31        model = convert_enum_to_string(model)
32        embeddings_inputs: List[EmbedRequestInputs] = []
33        if isinstance(inputs, str):
34            embeddings_inputs = [EmbedRequestInputs(text=inputs)]
35        elif isinstance(inputs, list) and len(inputs) > 0:
36            if isinstance(inputs[0], str):
37                embeddings_inputs = [EmbedRequestInputs(text=i) for i in inputs]
38            elif isinstance(inputs[0], dict):
39                embeddings_inputs = [EmbedRequestInputs(**i) for i in inputs]
40            else:
41                raise Exception("Invalid type for variable 'inputs'")
42        else:
43            raise Exception("Invalid type for variable 'inputs'")
44
45        if parameters:
46            return EmbedRequest(model=model, inputs=embeddings_inputs, parameters=parameters)
47        else:
48            return EmbedRequest(model=model, inputs=embeddings_inputs)
49
50    @staticmethod
51    def rerank(
52        model: Union[RerankModel, str],
53        query: str,
54        documents: Union[List[str], List[Dict[str, Any]]],
55        rank_fields: List[str] = ["text"],
56        return_documents: bool = True,
57        top_n: Optional[int] = None,
58        parameters: Optional[Dict[str, Any]] = None,
59    ) -> RerankRequest:
60        if isinstance(model, RerankModel):
61            model = model.value
62        else:
63            model = str(model)
64
65        if isinstance(documents, list) and len(documents) > 0:
66            if isinstance(documents[0], str):
67                documents = [Document(text=doc) for doc in documents]
68            elif isinstance(documents[0], dict):
69                documents = [Document(**doc) for doc in documents]
70            else:
71                raise Exception("Invalid type for variable 'documents'")
72        else:
73            raise Exception("Invalid type or value for variable 'documents'")
74
75        args: Dict[str, Any] = {
76            "model": model,
77            "query": query,
78            "documents": documents,
79            "rank_fields": rank_fields,
80            "return_documents": return_documents,
81        }
82        if top_n is not None:
83            args["top_n"] = top_n
84        if parameters is not None:
85            args["parameters"] = parameters
86
87        return RerankRequest(**args)
class EmbedModel(enum.Enum):
14class EmbedModel(Enum):
15    Multilingual_E5_Large = "multilingual-e5-large"
16    Pinecone_Sparse_English_V0 = "pinecone-sparse-english-v0"

Create a collection of name/value pairs.

Example enumeration:

>>> class Color(Enum):
...     RED = 1
...     BLUE = 2
...     GREEN = 3

Access them by:

  • attribute access::
>>> Color.RED
<Color.RED: 1>
  • value lookup:
>>> Color(1)
<Color.RED: 1>
  • name lookup:
>>> Color['RED']
<Color.RED: 1>

Enumerations can be iterated over, and know how many members they have:

>>> len(Color)
3
>>> list(Color)
[<Color.RED: 1>, <Color.BLUE: 2>, <Color.GREEN: 3>]

Methods can be added to enumerations, and members can have their own attributes -- see the documentation for details.

Multilingual_E5_Large = <EmbedModel.Multilingual_E5_Large: 'multilingual-e5-large'>
Pinecone_Sparse_English_V0 = <EmbedModel.Pinecone_Sparse_English_V0: 'pinecone-sparse-english-v0'>
Inherited Members
enum.Enum
name
value
class RerankModel(enum.Enum):
19class RerankModel(Enum):
20    Bge_Reranker_V2_M3 = "bge-reranker-v2-m3"
21    Cohere_Rerank_3_5 = "cohere-rerank-3.5"
22    Pinecone_Rerank_V0 = "pinecone-rerank-v0"

Create a collection of name/value pairs.

Example enumeration:

>>> class Color(Enum):
...     RED = 1
...     BLUE = 2
...     GREEN = 3

Access them by:

  • attribute access::
>>> Color.RED
<Color.RED: 1>
  • value lookup:
>>> Color(1)
<Color.RED: 1>
  • name lookup:
>>> Color['RED']
<Color.RED: 1>

Enumerations can be iterated over, and know how many members they have:

>>> len(Color)
3
>>> list(Color)
[<Color.RED: 1>, <Color.BLUE: 2>, <Color.GREEN: 3>]

Methods can be added to enumerations, and members can have their own attributes -- see the documentation for details.

Bge_Reranker_V2_M3 = <RerankModel.Bge_Reranker_V2_M3: 'bge-reranker-v2-m3'>
Cohere_Rerank_3_5 = <RerankModel.Cohere_Rerank_3_5: 'cohere-rerank-3.5'>
Pinecone_Rerank_V0 = <RerankModel.Pinecone_Rerank_V0: 'pinecone-rerank-v0'>
Inherited Members
enum.Enum
name
value
class InferenceRequestBuilder:
25class InferenceRequestBuilder:
26    @staticmethod
27    def embed_request(
28        model: Union[EmbedModel, str],
29        inputs: Union[str, List[Dict], List[str]],
30        parameters: Optional[Dict[str, Any]] = None,
31    ) -> EmbedRequest:
32        model = convert_enum_to_string(model)
33        embeddings_inputs: List[EmbedRequestInputs] = []
34        if isinstance(inputs, str):
35            embeddings_inputs = [EmbedRequestInputs(text=inputs)]
36        elif isinstance(inputs, list) and len(inputs) > 0:
37            if isinstance(inputs[0], str):
38                embeddings_inputs = [EmbedRequestInputs(text=i) for i in inputs]
39            elif isinstance(inputs[0], dict):
40                embeddings_inputs = [EmbedRequestInputs(**i) for i in inputs]
41            else:
42                raise Exception("Invalid type for variable 'inputs'")
43        else:
44            raise Exception("Invalid type for variable 'inputs'")
45
46        if parameters:
47            return EmbedRequest(model=model, inputs=embeddings_inputs, parameters=parameters)
48        else:
49            return EmbedRequest(model=model, inputs=embeddings_inputs)
50
51    @staticmethod
52    def rerank(
53        model: Union[RerankModel, str],
54        query: str,
55        documents: Union[List[str], List[Dict[str, Any]]],
56        rank_fields: List[str] = ["text"],
57        return_documents: bool = True,
58        top_n: Optional[int] = None,
59        parameters: Optional[Dict[str, Any]] = None,
60    ) -> RerankRequest:
61        if isinstance(model, RerankModel):
62            model = model.value
63        else:
64            model = str(model)
65
66        if isinstance(documents, list) and len(documents) > 0:
67            if isinstance(documents[0], str):
68                documents = [Document(text=doc) for doc in documents]
69            elif isinstance(documents[0], dict):
70                documents = [Document(**doc) for doc in documents]
71            else:
72                raise Exception("Invalid type for variable 'documents'")
73        else:
74            raise Exception("Invalid type or value for variable 'documents'")
75
76        args: Dict[str, Any] = {
77            "model": model,
78            "query": query,
79            "documents": documents,
80            "rank_fields": rank_fields,
81            "return_documents": return_documents,
82        }
83        if top_n is not None:
84            args["top_n"] = top_n
85        if parameters is not None:
86            args["parameters"] = parameters
87
88        return RerankRequest(**args)
@staticmethod
def embed_request( model: Union[EmbedModel, str], inputs: Union[str, List[Dict], List[str]], parameters: Optional[Dict[str, Any]] = None) -> pinecone.core.openapi.inference.model.embed_request.EmbedRequest:
26    @staticmethod
27    def embed_request(
28        model: Union[EmbedModel, str],
29        inputs: Union[str, List[Dict], List[str]],
30        parameters: Optional[Dict[str, Any]] = None,
31    ) -> EmbedRequest:
32        model = convert_enum_to_string(model)
33        embeddings_inputs: List[EmbedRequestInputs] = []
34        if isinstance(inputs, str):
35            embeddings_inputs = [EmbedRequestInputs(text=inputs)]
36        elif isinstance(inputs, list) and len(inputs) > 0:
37            if isinstance(inputs[0], str):
38                embeddings_inputs = [EmbedRequestInputs(text=i) for i in inputs]
39            elif isinstance(inputs[0], dict):
40                embeddings_inputs = [EmbedRequestInputs(**i) for i in inputs]
41            else:
42                raise Exception("Invalid type for variable 'inputs'")
43        else:
44            raise Exception("Invalid type for variable 'inputs'")
45
46        if parameters:
47            return EmbedRequest(model=model, inputs=embeddings_inputs, parameters=parameters)
48        else:
49            return EmbedRequest(model=model, inputs=embeddings_inputs)
@staticmethod
def rerank( model: Union[RerankModel, str], query: str, documents: Union[List[str], List[Dict[str, Any]]], rank_fields: List[str] = ['text'], return_documents: bool = True, top_n: Optional[int] = None, parameters: Optional[Dict[str, Any]] = None) -> pinecone.core.openapi.inference.model.rerank_request.RerankRequest:
51    @staticmethod
52    def rerank(
53        model: Union[RerankModel, str],
54        query: str,
55        documents: Union[List[str], List[Dict[str, Any]]],
56        rank_fields: List[str] = ["text"],
57        return_documents: bool = True,
58        top_n: Optional[int] = None,
59        parameters: Optional[Dict[str, Any]] = None,
60    ) -> RerankRequest:
61        if isinstance(model, RerankModel):
62            model = model.value
63        else:
64            model = str(model)
65
66        if isinstance(documents, list) and len(documents) > 0:
67            if isinstance(documents[0], str):
68                documents = [Document(text=doc) for doc in documents]
69            elif isinstance(documents[0], dict):
70                documents = [Document(**doc) for doc in documents]
71            else:
72                raise Exception("Invalid type for variable 'documents'")
73        else:
74            raise Exception("Invalid type or value for variable 'documents'")
75
76        args: Dict[str, Any] = {
77            "model": model,
78            "query": query,
79            "documents": documents,
80            "rank_fields": rank_fields,
81            "return_documents": return_documents,
82        }
83        if top_n is not None:
84            args["top_n"] = top_n
85        if parameters is not None:
86            args["parameters"] = parameters
87
88        return RerankRequest(**args)