§
    šŠtj^!  ã                   óø   — d dl Zd dlZd dlZd dlmZmZmZmZm	Z	 d dl
mZ d dlmZ d dlmZ d dlmZ  ej        e¦  «        Zddœded	ed
ede	ee                  dedefd„Zdededefd„Z G d„ de¦  «        ZdS )é    N)ÚAnyÚCallableÚListÚMappingÚOptional)ÚCallbackManagerForLLMRun)ÚLLM)Ú
ConfigDict©Úenforce_stop_tokens)ÚstopÚpipelineÚpromptÚargsr   ÚkwargsÚreturnc                óB   —  | |g|¢R i |¤Ž}|�t          ||¦  «        }|S )zîInference function to send to the remote hardware.

    Accepts a pipeline callable (or, more likely,
    a key pointing to the model on the cluster's object store)
    and returns text predictions for each document
    in the batch.
    r   )r   r   r   r   r   Útexts         úb/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/langchain_community/llms/self_hosted.pyÚ_generate_textr      s=   € ð ˆ8�FÐ,˜TÐ,Ð,Ð, VÐ,Ð,€DØÐÝ" 4¨Ñ.Ô.ˆØ€Kó    Údevicec                 ó.  — t          | t          ¦  «        r<t          | d¦  «        5 }t          j        |¦  «        } ddd¦  «         n# 1 swxY w Y   t
          j                             d¦  «        �¤ddl}|j	         
                    ¦   «         }|dk     s||k    rt          d|› d|› d�¦  «        ‚|dk     r!|dk    rt                               d	|¦  «         |                     |¦  «        | _        | j                             | j        ¦  «        | _        | S )
z+Send a pipeline to a device on the cluster.ÚrbNÚtorchr   éÿÿÿÿzGot device==z', device is required to be within [-1, ú)zÃDevice 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.)Ú
isinstanceÚstrÚopenÚpickleÚloadÚ	importlibÚutilÚ	find_specr   ÚcudaÚdevice_countÚ
ValueErrorÚloggerÚwarningr   ÚmodelÚto)r   r   Úfr   Úcuda_device_counts        r   Ú_send_pipeline_to_devicer/   #   ss  € å�(�CÑ Ô ð &Ý�(˜DÑ!Ô!ð 	& Qõ ”{ 1‘~”~ˆHð	&ð 	&ð 	&ñ 	&ô 	&ð 	&ð 	&ð 	&ð 	&ð 	&ð 	&øøøð 	&ð 	&ð 	&ð 	&õ
 „~×Ò Ñ(Ô(Ð4Øˆˆˆà!œJ×3Ò3Ñ5Ô5ÐØ�BŠ;ˆ;˜6Ð%6Ò6Ð6ÝðM˜vð Mð MØ8IðMð Mð Mñô ð ð �AŠ:ˆ:Ð+¨aÒ/Ð/Ý�NŠNðLð "ñô ð ð  Ÿ,š, vÑ.Ô.ˆŒØ!œ×*Ò*¨8¬?Ñ;Ô;ˆŒØ€Os   ¦AÁAÁAc                   ó²  ‡ — e Zd ZU dZdZeed<   dZeed<   eZ	e
ed<   	 dZeed<   	 e
ed<   	 dZee         ed<   	 d	d
gZee         ed<   	 dZeed<   	  ed¬¦  «        Zdefˆ fd„Ze	 	 ddededeee                  dededefd„¦   «         Zedeeef         fd„¦   «         Zedefd„¦   «         Z	 	 ddedeee                  dee         dedef
d„Z ˆ xZ!S )ÚSelfHostedPipelinea­	  Model inference 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 for custom pipeline and inference functions:
        .. code-block:: python

            from langchain_community.llms import SelfHostedPipeline
            from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
            import runhouse as rh

            def load_pipeline():
                tokenizer = AutoTokenizer.from_pretrained("gpt2")
                model = AutoModelForCausalLM.from_pretrained("gpt2")
                return pipeline(
                    "text-generation", model=model, tokenizer=tokenizer,
                    max_new_tokens=10
                )
            def inference_fn(pipeline, prompt, stop = None):
                return pipeline(prompt)[0]["generated_text"]

            gpu = rh.cluster(name="rh-a10x", instance_type="A100:1")
            llm = SelfHostedPipeline(
                model_load_fn=load_pipeline,
                hardware=gpu,
                model_reqs=model_reqs, inference_fn=inference_fn
            )
    Example for <2GB model (can be serialized and sent directly to the server):
        .. code-block:: python

            from langchain_community.llms import SelfHostedPipeline
            import runhouse as rh
            gpu = rh.cluster(name="rh-a10x", instance_type="A100:1")
            my_model = ...
            llm = SelfHostedPipeline.from_pipeline(
                pipeline=my_model,
                hardware=gpu,
                model_reqs=["./", "torch", "transformers"],
            )
    Example passing model path for larger models:
        .. code-block:: python

            from langchain_community.llms import SelfHostedPipeline
            import runhouse as rh
            import pickle
            from transformers import pipeline

            generator = pipeline(model="gpt2")
            rh.blob(pickle.dumps(generator), path="models/pipeline.pkl"
                ).save().to(gpu, path="models")
            llm = SelfHostedPipeline.from_pipeline(
                pipeline="models/pipeline.pkl",
                hardware=gpu,
                model_reqs=["./", "torch", "transformers"],
            )
    NÚpipeline_refÚclientÚinference_fnÚhardwareÚmodel_load_fnÚload_fn_kwargsz./r   Ú
model_reqsFÚallow_dangerous_deserializationÚforbid)Úextrar   c                 óð  •— |                      d¦  «        st          d¦  «        ‚ t          ¦   «         j        di |¤Ž 	 ddl}n# t
          $ r t          d¦  «        ‚w xY w|                     | j        ¬¦  «                             | j	        | j
        ¬¦  «        }| j        pi } |j        di |¤Ž| _        |                     | j        ¬¦  «                             | j	        | j
        ¬¦  «        | _        dS )	zûInit the pipeline with an auxiliary function.

        The load function must be in global scope to be imported
        and run on the server, i.e. in a module and not a REPL or closure.
        Then, initialize the remote inference function.
        r9   aQ  SelfHostedPipeline relies on the pickle module. You will need to set allow_dangerous_deserialization=True if you want to opt-in to allow deserialization of data using pickle.Data can be compromised by a malicious actor if not handled properly to include a malicious payload that when deserialized with pickle can execute arbitrary code. r   NzXCould not import runhouse python package. Please install it with `pip install runhouse`.)Úfn)Úreqs© )Úgetr(   ÚsuperÚ__init__ÚrunhouseÚImportErrorÚfunctionr6   r,   r5   r8   r7   Úremoter2   r4   r3   )Úselfr   ÚrhÚremote_load_fnÚ_load_fn_kwargsÚ	__class__s        €r   rB   zSelfHostedPipeline.__init__—   s1  ø€ ð �zŠzÐ;Ñ<Ô<ð 		Ýð6ñô ð ð 	�‰ŒÔÐ"Ð"˜6Ð"Ð"Ð"ð	Ø!Ð!Ð!Ð!Ð!øåð 	ð 	ð 	ÝðAñô ð ð	øøøð Ÿš¨Ô(:˜Ñ;Ô;×>Ò>ØŒM ¤ð ?ñ 
ô 
ˆð Ô-Ð3°ˆØ1˜NÔ1ÐDÐD°OÐDÐDˆÔà—k’k TÔ%6�kÑ7Ô7×:Ò:ØŒM ¤ð ;ñ 
ô 
ˆŒˆˆs   Á A ÁAr   r   r   r   c                 óœ   — t          |t          ¦  «        st                               d¦  «         ||dœ} | d|t          |ddg|pg z   dœ|¤ŽS )z=Init the SelfHostedPipeline from a pipeline object or string.zÜSerializing pipeline to send to remote hardware. Note, it can be quite slowto serialize and send large models with each execution. Consider sending the pipelineto the cluster and passing the path to the pipeline instead.)r   r   Útransformersr   )r7   r6   r5   r8   r?   )r   r   r)   r*   r/   )Úclsr   r5   r8   r   r   r7   s          r   Úfrom_pipelinez SelfHostedPipeline.from_pipeline¼   s„   € õ ˜(¥CÑ(Ô(ð 	Ý�NŠNðOñô ð ð '/¸&ÐAÐAˆØˆsð 
Ø)Ý2ØØ&¨Ð0°JÐ4DÀ"ÑEð	
ð 
ð
 ð
ð 
ð 	
r   c                 ó   — i d| j         i¥S )zGet the identifying parameters.r5   )r5   ©rG   s    r   Ú_identifying_paramsz&SelfHostedPipeline._identifying_paramsØ   s   € ð
Ø˜4œ=Ð)ð
ð 	
r   c                 ó   — dS )NÚself_hosted_llmr?   rQ   s    r   Ú	_llm_typezSelfHostedPipeline._llm_typeß   s   € à Ð r   r   r   Úrun_managerc                 ó.   —  | j         d| j        ||dœ|¤ŽS )N)r   r   r   r?   )r3   r2   )rG   r   r   rV   r   s        r   Ú_callzSelfHostedPipeline._callã   s6   € ð ˆtŒ{ð 
ØÔ&¨v¸Dð
ð 
ØDJð
ð 
ð 	
r   )Nr   )NN)"Ú__name__Ú
__module__Ú__qualname__Ú__doc__r2   r   Ú__annotations__r3   r   r4   r   r5   r7   r   Údictr8   r   r   r9   Úboolr
   Úmodel_configrB   ÚclassmethodÚintr	   rO   Úpropertyr   rR   rU   r   rX   Ú__classcell__)rK   s   @r   r1   r1   B   s3  ø€ € € € € € ð<ð <ð| €L�#ÐÐÑØ€FˆCÐÐÑØ+€L�(Ð+Ð+Ñ+Ø<Ø€HˆcÐÐÑØ<ØÐÐÑØ<Ø%)€N�H˜T”NÐ)Ð)Ñ)Ø?Ø! 7˜O€J��S”	Ð+Ð+Ñ+ØEà,1Ð# TÐ1Ð1Ñ1ðð �:Øðñ ô €Lð#
 ð #
ð #
ð #
ð #
ð #
ð #
ðJ ð
 +/Øð
ð 
àð
ð ð
ð ˜T #œYÔ'ð	
ð
 ð
ð ð
ð 
ð
ð 
ð 
ñ „[ð
ð6 ð
 W¨S°#¨XÔ%6ð 
ð 
ð 
ñ „Xð
ð ð!˜3ð !ð !ð !ñ „Xð!ð %)Ø:>ð		
ð 	
àð	
ð �t˜C”yÔ!ð	
ð Ð6Ô7ð		
ð
 ð	
ð 
ð	
ð 	
ð 	
ð 	
ð 	
ð 	
ð 	
ð 	
r   r1   )Úimportlib.utilr#   Úloggingr!   Útypingr   r   r   r   r   Úlangchain_core.callbacksr   Ú#langchain_core.language_models.llmsr	   Úpydanticr
   Úlangchain_community.llms.utilsr   Ú	getLoggerrY   r)   r   r   rb   r/   r1   r?   r   r   ú<module>rm      sr  ðØ Ð Ð Ð Ø €€€Ø €€€Ø 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9Ð 9à =Ð =Ð =Ð =Ð =Ð =Ø 3Ð 3Ð 3Ð 3Ð 3Ð 3Ø Ð Ð Ð Ð Ð à >Ð >Ð >Ð >Ð >Ð >à	ˆÔ	˜8Ñ	$Ô	$€ð !%ð	ð ð Øðàðð ðð �4˜”9Ô
ð	ð
 ðð 	ðð ð ð ð( sð °Cð ¸Cð ð ð ð ð>j
ð j
ð j
ð j
ð j
˜ñ j
ô j
ð j
ð j
ð j
r   