U
    ™ÀÂhw  ã                   @   s^   d dl mZmZmZ d dlmZ d dlmZ eeeeee  dœdd„Z	G dd„ deeƒZ
d	S )
é    )ÚAnyÚCallableÚList)Ú
Embeddings)ÚSelfHostedPipeline)ÚpipelineÚargsÚkwargsÚreturnc                 O   s
   | ||ŽS )z©Inference 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.
    © )r   r   r	   r   r   úN/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/embeddings/self_hosted.pyÚ_embed_documents   s    r   c                   @   sh   e Zd ZU dZeZeed< dZe	ed< G dd„ dƒZ
ee eee  dœdd	„Zeee d
œdd„ZdS )ÚSelfHostedEmbeddingsa„  Custom 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 using a model load function:
        .. code-block:: python

            from langchain_community.embeddings import SelfHostedEmbeddings
            from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
            import runhouse as rh

            gpu = rh.cluster(name="rh-a10x", instance_type="A100:1")
            def get_pipeline():
                model_id = "facebook/bart-large"
                tokenizer = AutoTokenizer.from_pretrained(model_id)
                model = AutoModelForCausalLM.from_pretrained(model_id)
                return pipeline("feature-extraction", model=model, tokenizer=tokenizer)
            embeddings = SelfHostedEmbeddings(
                model_load_fn=get_pipeline,
                hardware=gpu
                model_reqs=["./", "torch", "transformers"],
            )
    Example passing in a pipeline path:
        .. code-block:: python

            from langchain_community.embeddings import SelfHostedHFEmbeddings
            import runhouse as rh
            from transformers import pipeline

            gpu = rh.cluster(name="rh-a10x", instance_type="A100:1")
            pipeline = pipeline(model="bert-base-uncased", task="feature-extraction")
            rh.blob(pickle.dumps(pipeline),
                path="models/pipeline.pkl").save().to(gpu, path="models")
            embeddings = SelfHostedHFEmbeddings.from_pipeline(
                pipeline="models/pipeline.pkl",
                hardware=gpu,
                model_reqs=["./", "torch", "transformers"],
            )
    Úinference_fnNÚinference_kwargsc                   @   s   e Zd ZdZdS )zSelfHostedEmbeddings.ConfigZforbidN)Ú__name__Ú
__module__Ú__qualname__Úextrar   r   r   r   ÚConfigD   s   r   )Útextsr
   c                 C   s6   t tdd„ |ƒƒ}|  | j|¡}t|t ƒs2| ¡ S |S )zÊCompute doc embeddings using a HuggingFace transformer model.

        Args:
            texts: The list of texts to embed.s

        Returns:
            List of embeddings, one for each text.
        c                 S   s   |   dd¡S )NÚ
ú )Úreplace)Úxr   r   r   Ú<lambda>P   ó    z6SelfHostedEmbeddings.embed_documents.<locals>.<lambda>)ÚlistÚmapÚclientÚpipeline_refÚ
isinstanceÚtolist)Úselfr   Ú
embeddingsr   r   r   Úembed_documentsG   s
    	
z$SelfHostedEmbeddings.embed_documents)Útextr
   c                 C   s0   |  dd¡}|  | j|¡}t|tƒs,| ¡ S |S )z³Compute query embeddings using a HuggingFace transformer model.

        Args:
            text: The text to embed.

        Returns:
            Embeddings for the text.
        r   r   )r   r   r    r!   r   r"   )r#   r&   r$   r   r   r   Úembed_queryV   s
    	
z SelfHostedEmbeddings.embed_query)r   r   r   Ú__doc__r   r   r   Ú__annotations__r   r   r   r   ÚstrÚfloatr%   r'   r   r   r   r   r      s   
-r   N)Útypingr   r   r   Zlangchain_core.embeddingsr   Z$langchain_community.llms.self_hostedr   r+   r   r   r   r   r   r   Ú<module>   s   	