o
    Zh                     @   s|   d Z ddlZddlmZ ddlmZmZmZ ddlm	Z	 ddl
mZmZmZ ddl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ListOptional)
Embeddings)	BaseModel
ConfigDictmodel_validator)Self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< 	 edddZedddefddZd%ddZdedd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ee deee  fd!d"Zdedee f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engineZforbid )extraZprotected_namespacesafter)modereturnc                 C   sN   zddl m} W n ty   tdw || j| j| j| j| j| j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model_name_or_pathr   r   r   r   r   )
Zinfinity_embr   ImportErrorr   r   r   r   r   r   r   )selfr   r   r   d/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/embeddings/infinity_local.pyvalidate_environmentB   s    z,InfinityEmbeddingsLocal.validate_environmentc                    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__)r   r   r   r   r   X   s   z"InfinityEmbeddingsLocal.__aenter__argsc                    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!   e   s   z!InfinityEmbeddingsLocal.__aexit__textsc              	      s   | j js8td | 4 I dH  | j |I dH \}}W d  I dH  n1 I dH s,w   Y  td |S | 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   runningloggerwarningZembed)r   r"   
embeddings_r   r   r   aembed_documentsj   s   	(
z(InfinityEmbeddingsLocal.aembed_documentstextc                    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      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   r+   ) zMThis method is async only. Please use the async version `await aembed_query`.)r$   r%   r,   r-   r*   )r   r)   r   r   r   embed_query   s   z#InfinityEmbeddingsLocal.embed_query)r   N)__name__
__module____qualname____doc__str__annotations__r   r   r   intr   r   r   boolr   r   r   Zmodel_configr	   r
   r   r   r!   r   floatr(   r*   r.   r0   r   r   r   r   r      s:   
 

)r4   r,   loggingr   typingr   r   r   Zlangchain_core.embeddingsr   Zpydanticr   r   r	   Ztyping_extensionsr
   __all__r1   r$   r   r   r   r   r   <module>   s    