
    Ng                         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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                      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          Z ed          defd            ZddZdeddfdZdee         deee                  fdZdedee         fdZdee         deee                  fdZdedee         f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devicetorchbackendTmodel_warmupengineforbid )extraprotected_namespacesafter)modereturnc                     	 ddl m} n# t          $ r t          d          w xY w || j        | j        | j        | j        | j        | j                  | _	        | 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.)model_name_or_pathr   r   r   r   r   )
infinity_embr    ImportErrorr   r   r   r   r   r   r   )selfr    s     i/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/embeddings/infinity_local.pyvalidate_environmentz,InfinityEmbeddingsLocal.validate_environmentB   s    	9999999 	 	 	>  	 +*#z;]*<
 
 
 s   	 #c                 H   K   | j                                          d{V  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$   s    r%   r(   z"InfinityEmbeddingsLocal.__aenter__X   s4       k$$&&&&&&&&&&&    argsc                 4   K    | j         j        |  d{V  dS )zUstop the background worker,
        required to free references to the pytorch model.N)r   	__aexit__)r$   r*   s     r%   r,   z!InfinityEmbeddingsLocal.__aexit__e   s2       $dk#T**********r)   textsc                 p  K   | j         j        st                              d           | 4 d{V  | j                             |           d{V \  }}ddd          d{V  n# 1 d{V swxY w Y   t                              d           n#| j                             |           d{V \  }}|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warningembed)r$   r-   
embeddings_s       r%   aembed_documentsz(InfinityEmbeddingsLocal.aembed_documentsj   s[      {" 	;NN;    ? ? ? ? ? ? ? ?&*k&7&7&>&> > > > > > >
A? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? NNABBBB"&+"3"3E":":::::::MJs   $A''
A14A1textc                 N   K   |                      |g           d{V }|d         S )zAsync call out to Infinity's embedding endpoint.

        Args:
            text: The text to embed.

        Returns:
            Embeddings for the text.
        Nr   )r5   )r$   r6   r3   s      r%   aembed_queryz$InfinityEmbeddingsLocal.aembed_query   s9        00$88888888
!}r)   c                     t                               d           t          j        |                     |                    S )z,
        This method is async only.
        zQThis method is async only. Please use the async version `await aembed_documents`.)r0   r1   asynciorunr5   )r$   r-   s     r%   embed_documentsz'InfinityEmbeddingsLocal.embed_documents   s@     	E	
 	
 	
 {40077888r)   c                     t                               d           t          j        |                     |                    S ) zMThis method is async only. Please use the async version `await aembed_query`.)r0   r1   r:   r;   r8   )r$   r6   s     r%   embed_queryz#InfinityEmbeddingsLocal.embed_query   s>    B	
 	
 	
 {4,,T22333r)   )r   N)__name__
__module____qualname____doc__str__annotations__r   r   r   intr   r   r   boolr   r   r	   model_configr
   r   r&   r(   r,   r   floatr5   r8   r<   r?   r   r)   r%   r   r      s         * JJJG"Hhsm"""5J0FCAGSG%L$/FC* :  L
 _'"""d    #"*' ' ' '+S +T + + + +
DI $tE{:K    .
s 
tE{ 
 
 
 
9T#Y 94U3D 9 9 9 94 4U 4 4 4 4 4 4r)   )rC   r:   loggingr   typingr   r   r   langchain_core.embeddingsr   pydanticr   r	   r
   typing_extensionsr   __all__r@   r0   r   r   r)   r%   <module>rP      s    3 3        & & & & & & & & & & 0 0 0 0 0 0 ; ; ; ; ; ; ; ; ; ; " " " " " "$
%	8		M4 M4 M4 M4 M4i M4 M4 M4 M4 M4r)   