o
    ZhP                     @   sn   d dl mZmZmZmZmZ d dlZd dlmZ d dl	m
Z
mZ d dlmZmZ dZdZG dd	 d	eeZdS )
    )AnyDictListMappingOptionalN)
Embeddings)get_from_dict_or_envpre_init)	BaseModel
ConfigDictz#sentence-transformers/clip-ViT-B-32i   c                   @   s
  e Zd ZU dZeZeed< 	 dZe	ed< 	 dZ
eed< 	 dZeed< 	 d	Zee ed
< 	 d	Zee ed< 	 eZeed< 	 edddZededefddZedeeef fddZdee deee  fddZdee deee  fddZdedee fddZ d	S )DeepInfraEmbeddingsa  Deep Infra's embedding inference service.

    To use, you should have the
    environment variable ``DEEPINFRA_API_TOKEN`` set with your API token, or pass
    it as a named parameter to the constructor.
    There are multiple embeddings models available,
    see https://deepinfra.com/models?type=embeddings.

    Example:
        .. code-block:: python

            from langchain_community.embeddings import DeepInfraEmbeddings
            deepinfra_emb = DeepInfraEmbeddings(
                model_id="sentence-transformers/clip-ViT-B-32",
                deepinfra_api_token="my-api-key"
            )
            r1 = deepinfra_emb.embed_documents(
                [
                    "Alpha is the first letter of Greek alphabet",
                    "Beta is the second letter of Greek alphabet",
                ]
            )
            r2 = deepinfra_emb.embed_query(
                "What is the second letter of Greek alphabet"
            )

    model_idF	normalizez	passage: embed_instructionzquery: query_instructionNmodel_kwargsdeepinfra_api_token
batch_sizeZforbid )extraZprotected_namespacesvaluesreturnc                 C   s   t |dd}||d< |S )z?Validate that api key and python package exists in environment.r   ZDEEPINFRA_API_TOKEN)r   )clsr   r   r   r   _/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/embeddings/deepinfra.pyvalidate_environment;   s
   z(DeepInfraEmbeddings.validate_environmentc                 C   s
   d| j iS )zGet the identifying parameters.r   )r   selfr   r   r   _identifying_paramsD   s   
z'DeepInfraEmbeddings._identifying_paramsinputc              
   C   s   | j pi }d| j dd}ztjd| j ||| jd|d}W n tjjy7 } ztd| d }~ww |j	dkrGtd	|j	|j
f z| }|d
 }W |S  tjjyk } ztd| d|j
 d }~ww )Nzbearer zapplication/json)AuthorizationzContent-Typez'https://api.deepinfra.com/v1/inference/)Zinputsr   )headersjsonz$Error raised by inference endpoint:    z/Error raised by inference API HTTP code: %s, %s
embeddingszError raised by inference API: z.
Response: )r   r   requestspostr   r   
exceptionsRequestException
ValueErrorstatus_codetextr!   JSONDecodeError)r   r   Z_model_kwargsr    resetr#   r   r   r   _embedI   s<   






zDeepInfraEmbeddings._embedtextsc                    sT   g }fdd|D   fddt dt jD }|D ]	}||7 }q|S )aQ  Embed documents using a Deep Infra deployed embedding model.
        For larger batches, the input list of texts is chunked into smaller
        batches to avoid exceeding the maximum request size.

        Args:
            texts: The list of texts to embed.

        Returns:
            List of embeddings, one for each text.
        c                    s   g | ]	} j  | qS r   )r   ).0r*   r   r   r   
<listcomp>v   s    z7DeepInfraEmbeddings.embed_documents.<locals>.<listcomp>c                    s   g | ]} ||j   qS r   )r   )r1   iZinstruction_pairsr   r   r   r2   x   s    r   )rangelenr   r/   )r   r0   r#   chunkschunkr   r4   r   embed_documentsi   s   z#DeepInfraEmbeddings.embed_documentsr*   c                 C   s"   | j  | }| |gd }|S )zEmbed a query using a Deep Infra deployed embedding model.

        Args:
            text: The text to embed.

        Returns:
            Embeddings for the text.
        r   )r   r/   )r   r*   Zinstruction_pairZ	embeddingr   r   r   embed_query   s   	zDeepInfraEmbeddings.embed_query)!__name__
__module____qualname____doc__DEFAULT_MODEL_IDr   str__annotations__r   boolr   r   r   r   dictr   MAX_BATCH_SIZEr   intr   Zmodel_configr	   r   r   propertyr   r   r   r   floatr/   r9   r:   r   r   r   r   r      s0   
  r   )typingr   r   r   r   r   r$   Zlangchain_core.embeddingsr   Zlangchain_core.utilsr   r	   Zpydanticr
   r   r?   rD   r   r   r   r   r   <module>   s    