a
    bgP                     @   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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_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   v/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/embeddings/deepinfra.py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_paramsD   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 n6 tjjyz } ztd| W Y d }~n
d }~0 0 |j	dkrtd	|j	|j
f z| }|d
 }W n> tjjy } z"td| d|j
 W Y d }~n
d }~0 0 |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   _embedI   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>v       z7DeepInfraEmbeddings.embed_documents.<locals>.<listcomp>c                    s   g | ]} ||j   qS r   )r   )r2   iZinstruction_pairsr   r   r   r3   x   s   r   )rangelenr   r0   )r   r1   r$   chunkschunkr   r6   r   embed_documentsi   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   r0   )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   floatr0   r;   r<   r   r   r   r   r      s    
 r   )typingr   r   r   r   r   r%   Zlangchain_core.embeddingsr   Zlangchain_core.utilsr   r	   Zpydanticr
   r   rA   rF   r   r   r   r   r   <module>   s   