
    Ng              
           d dl mZmZmZ d dlmZ d dlmZ d dlm	Z	 dedededeee
                  fd	Z G d
 de	e          ZdS )    )AnyCallableList)
Embeddings)
ConfigDict)SelfHostedPipelinepipelineargskwargsreturnc                      | |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.
     )r	   r
   r   s      f/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/embeddings/self_hosted.py_embed_documentsr   	   s     8T$V$$$    c                       e Zd ZU dZeZeed<   	 dZe	ed<   	  e
d          Zdee         deee                  fd	Zd
edee         f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_kwargsforbid)extratextsr   c                     t          t          d |                    }|                     | j        |          }t	          |t                     s|                                S |S )zCompute 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                 .    |                      dd          S )N
 )replace)xs    r   <lambda>z6SelfHostedEmbeddings.embed_documents.<locals>.<lambda>R   s    199T3#7#7 r   )listmapclientpipeline_ref
isinstancetolist)selfr   
embeddingss      r   embed_documentsz$SelfHostedEmbeddings.embed_documentsI   s_     S77??@@[[!2E::
*d++ 	'$$&&&r   textc                     |                     dd          }|                     | j        |          }t          |t                    s|                                S |S )zCompute 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'   s      r   embed_queryz SelfHostedEmbeddings.embed_queryX   sW     ||D#&&[[!2D99
*d++ 	'$$&&&r   )__name__
__module____qualname____doc__r   r   r   __annotations__r   r   r   model_configr   strfloatr(   r+   r   r   r   r   r      s         + +Z .L(---N c   ?:  LT#Y 4U3D     U      r   r   N)typingr   r   r   langchain_core.embeddingsr   pydanticr   $langchain_community.llms.self_hostedr   r3   r   r   r   r   r   <module>r8      s    & & & & & & & & & & 0 0 0 0 0 0       C C C C C C%s %3 %# %$tE{BS % % % %S S S S S-z S S S S Sr   