
    i3                        d dl Z d dlZd dlmZmZmZmZmZmZ d dl	m
Z
 d dlmZ ddlmZmZ d dlmZ d dlmZmZ d d	lmZ dd
lmZmZmZ erd dlmZmZ ddlm Z! ddlm"Z"m#Z#  e jH                  e%      Z&	  G d de      Z'y)    N)OptionalDictListUnionAnyTYPE_CHECKING)	ApiClient)InferenceApi   )EmbeddingsListRerankResult)API_VERSION)setup_openapi_clientPluginAware)require_kwargs)InferenceRequestBuilder
EmbedModelRerankModel)ConfigOpenApiConfigurationModel)	ModelInfoModelInfoListc                       e Zd ZdZeZeZ	 d"dddddeddf fd	Z	e
d#d
       Ze
d$d       Ze
defd       Ze
d%d       Z	 d&deeef   deeee   ee   f   deeeef      defdZdgdddfdeeef   dedeee   eeeef      f   dee   dedee   deeeef      defdZeddddee   dee   ddfd       Zededd fd!       Z xZS )'	Inferencea  
    The ``Inference`` class configures and uses the Pinecone Inference API to generate embeddings and
    rank documents.

    It is generally not instantiated directly, but rather accessed through a parent ``Pinecone`` client
    object that is responsible for managing shared configurations.

    .. code-block:: python

        from pinecone import Pinecone

        pc = Pinecone()
        embeddings = pc.inference.embed(
            model="text-embedding-3-small",
            inputs=["Hello, world!"],
            parameters={"input_type": "passage", "truncate": "END"}
        )


    :param config: A ``pinecone.config.Config`` object, configured and built in the ``Pinecone`` class.
    :type config: ``pinecone.config.Config``, required
    configr   openapi_configr   pool_threadsreturnNc                     || _         	 || _        	 || _        	 t        t        t
        ||| j                  t              | _        d | _        	 t        | )          y )N)api_client_klass	api_klassr   r   r   api_version)_config_openapi_config_pool_threadsr   r	   r
   r   _Inference__inference_api_modelsuper__init__)selfr   r   r   kwargs	__class__s        n/var/www/catia.catastroantioquia-mas.com/valormas/lib/python3.12/site-packages/pinecone/inference/inference.pyr+   zInference.__init__6   sa     -)3&")++# 
 26    c                     | j                   S ):meta private:)r%   r,   s    r/   r   zInference.configT   s    
 ||r0   c                 R    t        j                  dt        d       | j                  S )r2   zThe `openapi_config` property has been renamed to `_openapi_config`. It is considered private and should not be used directly. This warning will become an error in a future version of the Pinecone Python SDK.   
stacklevel)warningswarnDeprecationWarningr&   r3   s    r/   r   zInference.openapi_config[   s*     	 _	

 ###r0   c                 R    t        j                  dt        d       | j                  S )r2   zThe `pool_threads` property has been renamed to `_pool_threads`. It is considered private and should not be used directly. This warning will become an error in a future version of the Pinecone Python SDK.r5   r6   )r8   r9   r:   r'   r3   s    r/   r   zInference.pool_threadse   s*     	 [	

 !!!r0   c                     | j                   ?ddlm}  || j                  | j                  | j
                  | j                        | _         | j                   S )a  
        Model is a resource that describes models available in the Pinecone Inference API.

        Curently you can get or list models.

        .. code-block:: python
            pc = Pinecone()

            # List all models
            models = pc.inference.model.list()

            # List models, with model type filtering
            models = pc.inference.model.list(type="embed")
            models = pc.inference.model.list(type="rerank")

            # List models, with vector type filtering
            models = pc.inference.model.list(vector_type="dense")
            models = pc.inference.model.list(vector_type="sparse")

            # List models, with both type and vector type filtering
            models = pc.inference.model.list(type="rerank", vector_type="dense")

            # Get details on a specific model
            model = pc.inference.model.get("text-embedding-3-small")

        r   r   )inference_apir   r   r   )r)   resources.sync.modelr   r(   r%   r&   r'   )r,   ModelResources     r/   modelzInference.modelo   sK    8 ;;D'"22||#33!//	DK {{r0   r@   inputs
parametersc                     t        j                  |||      }| j                  j                  |      }t	        |      S )a  
        Generates embeddings for the provided inputs using the specified model and (optional) parameters.

        :param model: The model to use for generating embeddings.
        :type model: str, required
        :param inputs: A list of items to generate embeddings for.
        :type inputs: list, required
        :param parameters: A dictionary of parameters to use when generating embeddings.
        :type parameters: dict, optional

        :return: ``EmbeddingsList`` object with keys ``data``, ``model``, and ``usage``. The ``data`` key contains a list of
          ``n`` embeddings, where ``n`` = len(inputs). Precision of returned embeddings is either
          float16 or float32, with float32 being the default. ``model`` key is the model used to generate the embeddings.
          ``usage`` key contains the total number of tokens used at request-time.

        Example:

        .. code-block:: python

            >>> pc = Pinecone()
            >>> inputs = ["Who created the first computer?"]
            >>> outputs = pc.inference.embed(model="multilingual-e5-large", inputs=inputs, parameters={"input_type": "passage", "truncate": "END"})
            >>> print(outputs)
            EmbeddingsList(
                model='multilingual-e5-large',
                data=[
                    {'values': [0.1, ...., 0.2]},
                ],
                usage={'total_tokens': 6}
            )

        )r@   rA   rB   )embed_request)r   rD   r(   embedr   )r,   r@   rA   rB   request_bodyresps         r/   rE   zInference.embed   sB    L /<<:
 ##)))Ed##r0   textTquery	documentsrank_fieldsreturn_documentstop_nc           	          t        j                  |||||||      }| j                  j                  |      }	t        |	      S )a}  
        Rerank documents with associated relevance scores that represent the relevance of each document
        to the provided query using the specified model.

        :param model: The model to use for reranking.
        :type model: str, required
        :param query: The query to compare with documents.
        :type query: str, required
        :param documents: A list of documents or strings to rank.
        :type documents: list, required
        :param rank_fields: A list of document fields to use for ranking. Defaults to ["text"].
        :type rank_fields: list, optional
        :param return_documents: Whether to include the documents in the response. Defaults to True.
        :type return_documents: bool, optional
        :param top_n: How many documents to return. Defaults to len(documents).
        :type top_n: int, optional
        :param parameters: A dictionary of parameters to use when ranking documents.
        :type parameters: dict, optional

        :return: ``RerankResult`` object with keys ``data`` and ``usage``. The ``data`` key contains a list of
          ``n`` documents, where ``n`` = ``top_n`` and type(n) = Document. The documents are sorted in order of
          relevance, with the first being the most relevant. The ``index`` field can be used to locate the document
          relative to the list of documents specified in the request. Each document contains a ``score`` key
          representing how close the document relates to the query.

        Example:

        .. code-block:: python

            >>> pc = Pinecone()
            >>> pc.inference.rerank(
                    model="bge-reranker-v2-m3",
                    query="Tell me about tech companies",
                    documents=[
                        "Apple is a popular fruit known for its sweetness and crisp texture.",
                        "Software is still eating the world.",
                        "Many people enjoy eating apples as a healthy snack.",
                        "Acme Inc. has revolutionized the tech industry with its sleek designs and user-friendly interfaces.",
                        "An apple a day keeps the doctor away, as the saying goes.",
                ],
                top_n=2,
                return_documents=True,
            )
            RerankResult(
                model='bge-reranker-v2-m3',
                data=[{
                    index=3,
                    score=0.020924192,
                    document={
                        text='Acme Inc. has revolutionized the tech industry with its sleek designs and user-friendly interfaces.'
                    }
                },{
                    index=1,
                    score=0.00034464317,
                    document={
                        text='Software is still eating the world.'
                    }
                }],
                usage={'rerank_units': 1}
            )

        )r@   rI   rJ   rK   rL   rM   rB   )rerank_request)r   rerankr(   r   )
r,   r@   rI   rJ   rK   rL   rM   rB   rO   rG   s
             r/   rP   zInference.rerank   sP    P 177#-!
 ##**.*ID!!r0   typevector_typerR   rS   r   c                <    | j                   j                  ||      S )a  
        List all available models.

        :param type: The type of model to list. Either "embed" or "rerank".
        :type type: str, optional

        :param vector_type: The type of vector to list. Either "dense" or "sparse".
        :type vector_type: str, optional

        :return: A list of models.

        Example:

        .. code-block:: python

            pc = Pinecone()

            # List all models
            models = pc.inference.list_models()

            # List models, with model type filtering
            models = pc.inference.list_models(type="embed")
            models = pc.inference.list_models(type="rerank")

            # List models, with vector type filtering
            models = pc.inference.list_models(vector_type="dense")
            models = pc.inference.list_models(vector_type="sparse")

            # List models, with both type and vector type filtering
            models = pc.inference.list_models(type="rerank", vector_type="dense")

        rQ   )r@   list)r,   rR   rS   s      r/   list_modelszInference.list_models  s    H zzDkBBr0   
model_namer   c                 :    | j                   j                  |      S )a  
        Get details on a specific model.

        :param model_name: The name of the model to get details on.
        :type model_name: str, required

        :return: A ModelInfo object.

        .. code-block:: python

            >>> pc = Pinecone()
            >>> pc.inference.get_model(model_name="pinecone-rerank-v0")
            {
                "model": "pinecone-rerank-v0",
                "short_description": "A state of the art reranking model that out-performs competitors on widely accepted benchmarks. It can handle chunks up to 512 tokens (1-2 paragraphs)",
                "type": "rerank",
                "supported_parameters": [
                    {
                        "parameter": "truncate",
                        "type": "one_of",
                        "value_type": "string",
                        "required": false,
                        "default": "END",
                        "allowed_values": [
                            "END",
                            "NONE"
                        ]
                    }
                ],
                "modality": "text",
                "max_sequence_length": 512,
                "max_batch_size": 100,
                "provider_name": "Pinecone",
                "supported_metrics": []
            }
        )rW   )r@   get)r,   rW   s     r/   	get_modelzInference.get_model<  s    L zz~~~44r0   )r   )r    r   )r    r   )r    r?   )N)__name__
__module____qualname____doc__EmbedModelEnumr   RerankModelEnumr   intr+   propertyr   r   r   r@   r   strr   r   r   r   r   rE   boolr   rP   r   rV   rZ   __classcell__)r.   s   @r/   r   r      s    .  J!K 	 / 	 
<   $ $ "c " " $ $T 04	*$^S()*$ c4:tCy01*$ T#s(^,	*$
 
*$b #)!%#/3R"_c)*R" R" cDc3h$889	R"
 #YR" R" }R" T#s(^,R" 
R"h '+$#C}#C:B3-#C	#C #CJ %5C %5K %5 %5r0   r   )(loggingr8   typingr   r   r   r   r   r   pinecone.openapi_supportr	   $pinecone.core.openapi.inference.apisr
   modelsr   r   pinecone.core.openapi.inferencer   pinecone.utilsr   r   r   inference_request_builderr   r   r_   r   r`   pinecone.configr   r   r>   r   r?   r   r   	getLoggerr[   loggerr    r0   r/   <module>rr      s^      B B . = 0 7 < )  <<0			8	$ G5 G5r0   