U
    h9                     @   s   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m	Z	m
Z
 d dlmZ d dlmZmZ d dlmZ d dlmZ d dlmZmZ d dlmZmZ eeZd	Zed
dddG dd deZdS )    N)AnyAsyncIteratorDictIteratorListMappingOptional)
deprecated)AsyncCallbackManagerForLLMRunCallbackManagerForLLMRun)LLM)GenerationChunk)Fieldroot_validator)get_pydantic_field_namespre_init)ztext2text-generationztext-generationZsummarizationZconversationalz0.0.37z1.0z)langchain_huggingface.HuggingFaceEndpoint)ZsinceZremovalZalternative_importc                   @   s  e Zd ZU dZdZee ed< dZee ed< dZ	ee ed< dZ
eed< dZee ed< d	Zee ed
< d	Zee ed< dZee ed< dZee ed< dZeed< dZee ed< eedZe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edZ e!ee"f ed< eedZ#e!ee"f ed< eed< e"ed< e"ed < dZ$ee ed!< G d"d# d#Z%e&d$d%e!ee"f e!ee"f d&d'd(Z'e(e!e!d&d)d*Z)e*e!ee"f d+d,d-Z+e*e,ee"f d+d.d/Z-e*ed+d0d1Z.eee  e"e!ee"f d2d3d4Z/d>eeee  ee0 e"ed5d6d7Z1d?eeee  ee2 e"ed5d8d9Z3d@eeee  ee0 e"e4e5 d5d:d;Z6dAeeee  ee2 e"e7e5 d5d<d=Z8dS )BHuggingFaceEndpointa  
    HuggingFace Endpoint.

    To use this class, you should have installed the ``huggingface_hub`` package, and
    the environment variable ``HUGGINGFACEHUB_API_TOKEN`` set with your API token,
    or given as a named parameter to the constructor.

    Example:
        .. code-block:: python

            # Basic Example (no streaming)
            llm = HuggingFaceEndpoint(
                endpoint_url="http://localhost:8010/",
                max_new_tokens=512,
                top_k=10,
                top_p=0.95,
                typical_p=0.95,
                temperature=0.01,
                repetition_penalty=1.03,
                huggingfacehub_api_token="my-api-key"
            )
            print(llm.invoke("What is Deep Learning?"))

            # Streaming response example
            from langchain_core.callbacks.streaming_stdout import StreamingStdOutCallbackHandler

            callbacks = [StreamingStdOutCallbackHandler()]
            llm = HuggingFaceEndpoint(
                endpoint_url="http://localhost:8010/",
                max_new_tokens=512,
                top_k=10,
                top_p=0.95,
                typical_p=0.95,
                temperature=0.01,
                repetition_penalty=1.03,
                callbacks=callbacks,
                streaming=True,
                huggingfacehub_api_token="my-api-key"
            )
            print(llm.invoke("What is Deep Learning?"))

    Nendpoint_urlrepo_idhuggingfacehub_api_tokeni   max_new_tokenstop_kgffffff?top_p	typical_pg?temperaturerepetition_penaltyFreturn_full_texttruncate)default_factorystop_sequencesseed inference_server_urlx   timeout	streaming	do_sample	watermarkserver_kwargsmodel_kwargsmodelclientasync_clienttaskc                   @   s   e Zd ZdZdS )zHuggingFaceEndpoint.ConfigZforbidN)__name__
__module____qualname__extra r2   r2   Q/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/llms/huggingface_endpoint.pyConfig   s   r4   T)pre)valuesreturnc              
   C   s   t | }|di }t|D ]P}||kr8td| d||krtd| d| d| d ||||< q|| }|rtd| d	||d< d
|krd|krtdd
|krd|krtd|d
p|d|d< |S )z>Build extra kwargs from additional params that were passed in.r)   zFound z supplied twice.z	WARNING! z/ is not default parameter.
                    zL was transferred to model_kwargs.
                    Please make sure that z is what you intended.zParameters za should be specified explicitly. Instead they were passed in as part of `model_kwargs` parameter.r   r   z<Please specify an `endpoint_url` or `repo_id` for the model.zAPlease specify either an `endpoint_url` OR a `repo_id`, not both.r*   )	r   getlist
ValueErrorloggerwarningpopintersectionkeys)clsr6   Zall_required_field_namesr1   
field_nameZinvalid_model_kwargsr2   r2   r3   build_extra   s4    
zHuggingFaceEndpoint.build_extrac              
   C   s   zddl m} W n tk
r,   tdY nX |d p>td}|dk	rz||d W n, tk
r } ztd|W 5 d}~X Y nX dd	l m}m} |f |d
 |d |d|d |d< |f |d
 |d |d|d |d< |S )zCValidate that package is installed and that the API token is valid.r   )loginzfCould not import huggingface_hub python package. Please install it with `pip install huggingface_hub`.r   ZHUGGINGFACEHUB_API_TOKENN)tokenzICould not authenticate with huggingface_hub. Please check your API token.)AsyncInferenceClientInferenceClientr*   r$   )r*   r$   rD   r(   r+   r,   )	Zhuggingface_hubrC   ImportErrorosgetenv	Exceptionr:   rE   rF   )r@   r6   rC   r   erE   rF   r2   r2   r3   validate_environment   sD    


z(HuggingFaceEndpoint.validate_environment)r7   c                 C   s<   | j | j| j| j| j| j| j| j| j| j	| j
| jd| jS )zEGet the default parameters for calling text generation inference API.)r   r   r   r   r   r   r   r   r   r    r&   r'   )r   r   r   r   r   r   r   r   r   r    r&   r'   r)   selfr2   r2   r3   _default_params   s    z#HuggingFaceEndpoint._default_paramsc                 C   s    | j pi }| j| jdd|iS )zGet the identifying parameters.)r   r-   r)   )r)   r   r-   )rN   Z_model_kwargsr2   r2   r3   _identifying_params   s    
z'HuggingFaceEndpoint._identifying_paramsc                 C   s   dS )zReturn type of llm.Zhuggingface_endpointr2   rM   r2   r2   r3   	_llm_type   s    zHuggingFaceEndpoint._llm_type)runtime_stopkwargsr7   c                 K   s"   | j |}|d |pg  |d< |S )Nr   )rO   )rN   rR   rS   paramsr2   r2   r3   _invocation_params   s    
z&HuggingFaceEndpoint._invocation_params)promptstoprun_managerrS   r7   c                 K   s   | j |f|}| jr>d}| j|||f|D ]}||j7 }q*|S |d |d< | jj||dd| jd}zt|	 d d }	W n& t
k
r   t|	 d }	Y nX |d D ],}
|	t|
 d	 |
kr|	d	t|
  }	q|	S d	S )
z1Call out to HuggingFace Hub's inference endpoint.r!   r   rW   inputs
parametersFjsonstreamr-   r   generated_textN)rU   r%   _streamtextr+   postr-   r]   loadsdecodeKeyErrorlenrN   rV   rW   rX   rS   invocation_params
completionchunkresponseZresponse_textstop_seqr2   r2   r3   _call   s,    zHuggingFaceEndpoint._callc                    s   | j |f|}| jrHd}| j|||f|2 z3 d H W }||j7 }q*6 |S |d |d< | jj||dd| jdI d H }zt|	 d d }	W n& t
k
r   t|	 d }	Y nX |d D ],}
|	t|
 d  |
kr|	d t|
  }	q|	S d S )	Nr!   r   rW   rY   Fr\   r   r_   )rU   r%   _astreamra   r,   rb   r-   r]   rc   rd   re   rf   rg   r2   r2   r3   _acall  s4      zHuggingFaceEndpoint._acallc                 k   s   | j |f|}| jj|f|ddiD ]n}d }|d D ]}||kr6|}q6d }	|rd|d || }	n|}	|	rt|	d}
|r||
j |
V  |r& qq&d S Nr^   Tr   )ra   )rU   r+   text_generationindexr   on_llm_new_tokenra   rN   rV   rW   rX   rS   rh   rk   Zstop_seq_foundrl   ra   rj   r2   r2   r3   r`   <  s.    
zHuggingFaceEndpoint._streamc                 K  s   | j |f|}| jj|f|ddiI d H 2 z|3 d H W }d }|d D ]}||krD|}qDd }	|rr|d || }	n|}	|	rt|	d}
|r||
jI d H  |
V  |r, qq,6 d S rp   )rU   r,   rq   rr   r   rs   ra   rt   r2   r2   r3   rn   a  s.    
zHuggingFaceEndpoint._astream)NN)NN)NN)NN)9r.   r/   r0   __doc__r   r   str__annotations__r   r   r   intr   r   floatr   r   r   r   boolr   r   r9   r   r   r    r"   r$   r%   r&   r'   dictr(   r   r   r)   r-   r4   r   rB   r   rL   propertyrO   r   rP   rQ   rU   r   rm   r
   ro   r   r   r`   r   rn   r2   r2   r2   r3   r      s   
+""'
 

  
&  
%  
(  
r   )r]   loggingrH   typingr   r   r   r   r   r   r   Zlangchain_core._api.deprecationr	   Zlangchain_core.callbacksr
   r   Z#langchain_core.language_models.llmsr   Zlangchain_core.outputsr   Zlangchain_core.pydantic_v1r   r   Zlangchain_core.utilsr   r   	getLoggerr.   r;   ZVALID_TASKSr   r2   r2   r2   r3   <module>   s"   $
