U
    h:                     @   sj   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
 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)	BaseModel)get_from_dict_or_envpre_initz#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< G dd dZeeedddZeeeef dddZee eee  dddZee eee  dddZeee d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_sizec                   @   s   e Zd ZdZdS )zDeepInfraEmbeddings.ConfigZforbidN)__name__
__module____qualname__extra r   r   L/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/embeddings/deepinfra.pyConfig9   s   r   )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   r   validate_environment<   s      z(DeepInfraEmbeddings.validate_environment)r   c                 C   s
   d| j iS )zGet the identifying parameters.r   )r   selfr   r   r   _identifying_paramsE   s    z'DeepInfraEmbeddings._identifying_params)inputr   c              
   C   s   | j pi }d| j dd}z(tjd| j ||| jd|d}W n4 tjjk
rx } ztd| W 5 d }~X Y nX |j	dkrtd	|j	|j
f z| }|d
 }W n< tjjk
r } ztd| d|j
 W 5 d }~X Y nX |S )Nzbearer zapplication/json)AuthorizationzContent-Typez'https://api.deepinfra.com/v1/inference/)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   _embedJ   s4    



 

zDeepInfraEmbeddings._embed)textsr   c                    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>w   s     z7DeepInfraEmbeddings.embed_documents.<locals>.<listcomp>c                    s   g | ]} ||j   qS r   )r   )r5   iZinstruction_pairsr   r   r   r6   y   s   r   )rangelenr   r3   )r   r4   r'   chunkschunkr   r8   r   embed_documentsj   s    z#DeepInfraEmbeddings.embed_documents)r.   r   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   r3   )r   r.   Zinstruction_pairZ	embeddingr   r   r   embed_query   s    	zDeepInfraEmbeddings.embed_query) r   r   r   __doc__DEFAULT_MODEL_IDr   str__annotations__r   boolr   r   r   r   dictr   MAX_BATCH_SIZEr   intr   r
   r   r   propertyr   r   r    r   floatr3   r=   r>   r   r   r   r   r      s    
 r   )typingr   r   r   r   r   r(   Zlangchain_core.embeddingsr   Zlangchain_core.pydantic_v1r   Zlangchain_core.utilsr	   r
   r@   rE   r   r   r   r   r   <module>   s   