U
    h                     @   sp   d Z ddlZddlmZ ddlmZmZmZmZ ddl	m
Z
 ddlmZmZ dgZeeZG dd dee
ZdS )	z-written under MIT Licence, Michael Feil 2023.    N)	getLogger)AnyDictListOptional)
Embeddings)	BaseModelroot_validatorInfinityEmbeddingsLocalc                   @   s  e Zd ZU dZeed< dZee ed< dZe	ed< dZ
eed< d	Zeed
< dZeed< dZeed< G dd dZedddeedddZddddZeddddZee eee  dddZ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 )%r
   a  Optimized Infinity embedding models.

    https://github.com/michaelfeil/infinity
    This class deploys a local Infinity instance to embed text.
    The class requires async usage.

    Infinity is a class to interact with Embedding Models on https://github.com/michaelfeil/infinity


    Example:
        .. code-block:: python

            from langchain_community.embeddings import InfinityEmbeddingsLocal
            async with InfinityEmbeddingsLocal(
                model="BAAI/bge-small-en-v1.5",
                revision=None,
                device="cpu",
            ) as embedder:
                embeddings = await engine.aembed_documents(["text1", "text2"])
    modelNrevision    
batch_sizeautodeviceZtorchbackendTmodel_warmupenginec                   @   s   e Zd ZdZdS )zInfinityEmbeddingsLocal.ConfigZforbidN)__name__
__module____qualname__extra r   r   Q/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/embeddings/infinity_local.pyConfig<   s   r   F)preZskip_on_failure)valuesreturnc                 C   sr   zddl m} W n tk
r,   tdY nX td|  ||d |d |d |d |d	 |d
 d|d< |S )z?Validate that api key and python package exists in environment.r   )AsyncEmbeddingEnginezrPlease install the `pip install 'infinity_emb[optimum,torch]>=0.0.24'` package to use the InfinityEmbeddingsLocal.z*Using InfinityEmbeddingsLocal with kwargs r   r   r   r   r   r   )Zmodel_name_or_pathr   r   r   r   r   r   )Zinfinity_embr   ImportErrorloggerdebug)clsr   r   r   r   r   validate_environment?   s     

z,InfinityEmbeddingsLocal.validate_environment)r   c                    s   | j  I dH  dS )aQ  start the background worker.
        recommended usage is with the async with statement.

        async with InfinityEmbeddingsLocal(
            model="BAAI/bge-small-en-v1.5",
            revision=None,
            device="cpu",
        ) as embedder:
            embeddings = await engine.aembed_documents(["text1", "text2"])
        N)r   
__aenter__)selfr   r   r   r$   W   s    z"InfinityEmbeddingsLocal.__aenter__)argsr   c                    s   | j j| I dH  dS )zUstop the background worker,
        required to free references to the pytorch model.N)r   	__aexit__)r%   r&   r   r   r   r'   d   s    z!InfinityEmbeddingsLocal.__aexit__)textsr   c              
      sl   | j jsRtd | 4 I dH  | j |I dH \}}W 5 Q I dH R X td n| j |I dH \}}|S )zAsync call out to Infinity's embedding endpoint.

        Args:
            texts: The list of texts to embed.

        Returns:
            List of embeddings, one for each text.
        zeStarting Infinity engine on the fly. This is not recommended.Please start the engine before using it.Nz$Stopped infinity engine after usage.)r   Zrunningr    warningembed)r%   r(   
embeddings_r   r   r   aembed_documentsi   s    	&z(InfinityEmbeddingsLocal.aembed_documents)textr   c                    s   |  |gI dH }|d S )zAsync call out to Infinity's embedding endpoint.

        Args:
            text: The text to embed.

        Returns:
            Embeddings for the text.
        Nr   )r-   )r%   r.   r+   r   r   r   aembed_query   s    	z$InfinityEmbeddingsLocal.aembed_queryc                 C   s   t d t| |S )z,
        This method is async only.
        zQThis method is async only. Please use the async version `await aembed_documents`.)r    r)   asynciorunr-   )r%   r(   r   r   r   embed_documents   s    z'InfinityEmbeddingsLocal.embed_documentsc                 C   s   t d t| |S ) zMThis method is async only. Please use the async version `await aembed_query`.)r    r)   r0   r1   r/   )r%   r.   r   r   r   embed_query   s    z#InfinityEmbeddingsLocal.embed_query)r   r   r   __doc__str__annotations__r   r   r   intr   r   r   boolr   r   r   r	   r   r#   r$   r'   r   floatr-   r/   r2   r4   r   r   r   r   r
      s"   


)r5   r0   loggingr   typingr   r   r   r   Zlangchain_core.embeddingsr   Zlangchain_core.pydantic_v1r   r	   __all__r   r    r
   r   r   r   r   <module>   s   