
    7|h                         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y)
z-written under MIT Licence, Michael Feil 2023.    N)	getLogger)AnyListOptional)
Embeddings)	BaseModel
ConfigDictmodel_validator)SelfInfinityEmbeddingsLocalc                   F   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y)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}  || j                  | j                  | j
                  | j                  | j                  | j                        | _	        | S # t        $ r t        d      w xY w)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     l/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain_community/embeddings/infinity_local.pyvalidate_environmentz,InfinityEmbeddingsLocal.validate_environmentB   sn    	9 +#zz;;]]**<<
   	> 	s   A A.c                 T   K   | j                   j                          d{    y7 w)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   s      kk$$&&&s   (&(argsc                 R   K    | j                   j                  |  d{    y7 w)zUstop the background worker,
        required to free references to the pytorch model.N)r   	__aexit__)r$   r)   s     r%   r+   z!InfinityEmbeddingsLocal.__aexit__e   s"      $dkk##T***s   '%'textsc                   K   | j                   j                  smt        j                  d       | 4 d{    | j                   j	                  |       d{   \  }}ddd      d{    t        j                  d       S | j                   j	                  |       d{   \  }}|S 7 y7 X7 G# 1 d{  7  sw Y   WxY w7 $w)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;  ? ?&*kk&7&7&> >
A? ? NNAB  #'++"3"3E"::MJ? >? ? ? ? ;sb   2CB.CB4B0B4C*B2+9C$C	%
C0B42C4C:B=;CCtextc                 L   K   | j                  |g       d{   }|d   S 7 	w)zAsync call out to Infinity's embedding endpoint.

        Args:
            text: The text to embed.

        Returns:
            Embeddings for the text.
        Nr   )r4   )r$   r5   r2   s      r%   aembed_queryz$InfinityEmbeddingsLocal.aembed_query   s,       00$88
!} 9s   $"
$c                 t    t         j                  d       t        j                  | j	                  |            S )z,
        This method is async only.
        zQThis method is async only. Please use the async version `await aembed_documents`.)r/   r0   asynciorunr4   )r$   r,   s     r%   embed_documentsz'InfinityEmbeddingsLocal.embed_documents   s2     	E	
 {{400788    c                 t    t         j                  d       t        j                  | j	                  |            S ) zMThis method is async only. Please use the async version `await aembed_query`.)r/   r0   r9   r:   r7   )r$   r5   s     r%   embed_queryz#InfinityEmbeddingsLocal.embed_query   s0    B	
 {{4,,T2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   floatr4   r7   r;   r?   r   r<   r%   r   r      s   * J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4 4U 4r<   )rC   r9   loggingr   typingr   r   r   langchain_core.embeddingsr   pydanticr   r	   r
   typing_extensionsr   __all__r@   r/   r   r   r<   r%   <module>rP      sB    3   & & 0 ; ; "$
%	8	M4i M4r<   