
    3fi              
           d dl Z d dlZd dlmZmZmZmZ d dlmZ dZ	dZ
dZdZ ej                  e      Zded	ed
edeee      fdZddedededefdZ G d de      Z G d de      Zy)    N)AnyCallableListOptional)SelfHostedEmbeddingsz'sentence-transformers/all-mpnet-base-v2zhkunlp/instructor-largez&Represent the document for retrieval: z<Represent the question for retrieving supporting documents: clientargskwargsreturnc                 &     | j                   |i |S )zInference function to send to the remote hardware.

    Accepts a sentence_transformer model_id and
    returns a list of embeddings for each document in the batch.
    )encode)r   r	   r
   s      u/var/www/auto_recruiter/arenv/lib/python3.12/site-packages/langchain_community/embeddings/self_hosted_hugging_face.py_embed_documentsr      s     6==$)&))    model_idinstructdevicec                 f   |sddl }|j                  |       }nddlm}  ||       }t        j
                  j                  d      kddl}|j                  j                         }|dk  s||k\  rt        d| d| d      |dk  r|dkD  rt        j                  d	|       |j                  |      }|S )
zLoad the embedding model.r   N)
INSTRUCTORtorchzGot device==z', device is required to be within [-1, )zDevice has %d GPUs available. Provide device={deviceId} to `from_model_id` to use availableGPUs for execution. deviceId is -1 for CPU and can be a positive integer associated with CUDA device id.)sentence_transformersSentenceTransformerInstructorEmbeddingr   	importlibutil	find_specr   cudadevice_count
ValueErrorloggerwarningto)r   r   r   r   r   r   r   cuda_device_counts           r   load_embedding_modelr&      s    $&::8D2H%~~(4!JJ335B;6%66vh '88I7J!M  A:+a/NNL " 6"Mr   c                        e Zd ZU dZeed<   eZeed<   	 g dZ	e
e   ed<   	 eed<   	 eZeed<   	 dZee   ed	<   	 eZeed
<   	 def fdZ xZS )SelfHostedHuggingFaceEmbeddingsa  HuggingFace embedding models on self-hosted remote hardware.

    Supported hardware includes auto-launched instances on AWS, GCP, Azure,
    and Lambda, as well as servers specified
    by IP address and SSH credentials (such as on-prem, or another cloud
    like Paperspace, Coreweave, etc.).

    To use, you should have the ``runhouse`` python package installed.

    Example:
        .. code-block:: python

            from langchain_community.embeddings import SelfHostedHuggingFaceEmbeddings
            import runhouse as rh
            model_id = "sentence-transformers/all-mpnet-base-v2"
            gpu = rh.cluster(name="rh-a10x", instance_type="A100:1")
            hf = SelfHostedHuggingFaceEmbeddings(model_id=model_id, hardware=gpu)
    r   r   )./r   r   
model_reqshardwaremodel_load_fnNload_fn_kwargsinference_fnr
   c                     |j                  di       }|j                  dt              |d<   |j                  dd      |d<   |j                  dd      |d<   t        |   dd|i| y)	)Initialize the remote inference function.r-   r   r   Fr   r   N )popgetDEFAULT_MODEL_NAMEsuper__init__selfr
   r-   	__class__s      r   r6   z(SelfHostedHuggingFaceEmbeddings.__init__]   so    $4b9%3%7%7
DV%Wz"%3%7%7
E%Jz"#1#5#5h#Bx AA&Ar   )__name__
__module____qualname____doc__r   __annotations__r4   r   strr*   r   r&   r,   r   r-   r   dictr   r.   r6   __classcell__r9   s   @r   r(   r(   ;   st    & K&Hc&DJS	DEM<2M82<%)NHTN)?-L(-7B B Br   r(   c                        e Zd ZU dZeZeed<   	 eZ	eed<   	 e
Zeed<   	 g dZee   ed<   	 def fdZd	ee   d
eee      fdZded
ee   fdZ xZS )'SelfHostedHuggingFaceInstructEmbeddingsa  HuggingFace InstructEmbedding models on self-hosted remote hardware.

    Supported hardware includes auto-launched instances on AWS, GCP, Azure,
    and Lambda, as well as servers specified
    by IP address and SSH credentials (such as on-prem, or another
    cloud like Paperspace, Coreweave, etc.).

    To use, you should have the ``runhouse`` python package installed.

    Example:
        .. code-block:: python

            from langchain_community.embeddings import SelfHostedHuggingFaceInstructEmbeddings
            import runhouse as rh
            model_name = "hkunlp/instructor-large"
            gpu = rh.cluster(name='rh-a10x', instance_type='A100:1')
            hf = SelfHostedHuggingFaceInstructEmbeddings(
                model_name=model_name, hardware=gpu)
    r   embed_instructionquery_instruction)r)   r   r   r*   r
   c                     |j                  di       }|j                  dt              |d<   |j                  dd      |d<   |j                  dd      |d<   t        |   dd|i| y)	r0   r-   r   r   Tr   r   Nr1   )r2   r3   DEFAULT_INSTRUCT_MODELr5   r6   r7   s      r   r6   z0SelfHostedHuggingFaceInstructEmbeddings.__init__   st    $4b9%3%7%7.&
z" &4%7%7
D%Iz"#1#5#5h#Bx AA&Ar   textsr   c                     g }|D ]  }|j                  | j                  |g       ! | j                  | j                  |      }|j	                         S )zCompute doc embeddings using a HuggingFace instruct model.

        Args:
            texts: The list of texts to embed.

        Returns:
            List of embeddings, one for each text.
        )appendrE   r   pipeline_reftolist)r8   rI   instruction_pairstext
embeddingss        r   embed_documentsz7SelfHostedHuggingFaceInstructEmbeddings.embed_documents   sZ      	ED$$d&<&<d%CD	E[[!2!24EF
  ""r   rO   c                 ~    | j                   |g}| j                  | j                  |g      d   }|j                         S )zCompute query embeddings using a HuggingFace instruct model.

        Args:
            text: The text to embed.

        Returns:
            Embeddings for the text.
        r   )rF   r   rL   rM   )r8   rO   instruction_pair	embeddings       r   embed_queryz3SelfHostedHuggingFaceInstructEmbeddings.embed_query   sC     !22D9KK 1 14D3EFqI	!!r   )r:   r;   r<   r=   rH   r   r?   r>   DEFAULT_EMBED_INSTRUCTIONrE   DEFAULT_QUERY_INSTRUCTIONrF   r*   r   r   r6   floatrQ   rU   rA   rB   s   @r   rD   rD   f   s    ( +Hc*6s656s61BJS	BEB B#T#Y #4U3D #" "U "r   rD   )Fr   )r   loggingtypingr   r   r   r   *langchain_community.embeddings.self_hostedr   r4   rH   rV   rW   	getLoggerr:   r"   rX   r   r?   boolintr&   r(   rD   r1   r   r   <module>r_      s      0 0 K> 2 D B  
		8	$*S * * *T%[@Q *3 $  TW B(B&: (BVB".M B"r   