a
    bg99                     @   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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)get_pydantic_field_namespre_init)
ConfigDictFieldmodel_validator)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< dZ$e"ed< dZ%e"ed < dZ&ee ed!< e'd"d#Z(e)d$d%e*e!ee"f e"d&d'd(Z+e,e!e!d&d)d*Z-e.e!ee"f d+d,d-Z/e.e0ee"f d+d.d/Z1e.ed+d0d1Z2eee  e"e!ee"f d2d3d4Z3d>eeee  ee4 e"ed5d6d7Z5d?eeee  ee6 e"ed5d8d9Z7d@eeee  ee4 e"e8e9 d5d:d;Z:dAeeee  ee6 e"e;e9 d5d<d=Z<d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taskZforbid)extrabefore)mode)valuesreturnc              
   C   s   t | }|di }t|D ]P}||v r8td| d||vrtd| d| d| d ||||< q|| }|rtd| d	||d< d
|vrd|vrtdd
|v rd|v 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)clsr2   Zall_required_field_namesr/   
field_nameZinvalid_model_kwargs r>   {/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/llms/huggingface_endpoint.pybuild_extra   s<    
zHuggingFaceEndpoint.build_extrac              
   C   s   zddl m} W n ty*   tdY n0 |d p<td}|durz||d W n. ty } ztd|W Y d}~n
d}~0 0 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%   rB   r)   r,   r-   )	Zhuggingface_hubrA   ImportErrorosgetenv	Exceptionr6   rC   rD   )r<   r2   rA   r   erC   rD   r>   r>   r?   validate_environment   sD    


z(HuggingFaceEndpoint.validate_environment)r3   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*   selfr>   r>   r?   _default_params   s    z#HuggingFaceEndpoint._default_paramsc                 C   s$   | j pi }i | j| jdd|iS )zGet the identifying parameters.)r   r.   r*   )r*   r   r.   )rL   Z_model_kwargsr>   r>   r?   _identifying_params   s    
z'HuggingFaceEndpoint._identifying_paramsc                 C   s   dS )zReturn type of llm.Zhuggingface_endpointr>   rK   r>   r>   r?   	_llm_type   s    zHuggingFaceEndpoint._llm_type)runtime_stopkwargsr3   c                 K   s&   i | j |}|d |pg  |d< |S )Nr    )rM   )rL   rP   rQ   paramsr>   r>   r?   _invocation_params   s    z&HuggingFaceEndpoint._invocation_params)promptstoprun_managerrQ   r3   c                 K   s   | j |fi |}| jrFd}| j|||fi |D ]}||j7 }q2|S |d |d< | jj||dd| jd}zt|	 d d }	W n$ t
y   t|	 d }	Y n0 |d D ],}
|	t|
 d	 |
kr|	d	t|
  }	q|	S d	S )
z1Call out to HuggingFace Hub's inference endpoint.r"   r    rU   inputs
parametersFjsonstreamr.   r   generated_textN)rS   r&   _streamtextr,   postr.   r[   loadsdecodeKeyErrorlenrL   rT   rU   rV   rQ   invocation_params
completionchunkresponseZresponse_textstop_seqr>   r>   r?   _call   s,    zHuggingFaceEndpoint._callc                    s   | j |fi |}| jrPd}| j|||fi |2 z3 d H W }||j7 }q26 |S |d |d< | jj||dd| jdI d H }zt|	 d d }	W n$ t
y   t|	 d }	Y n0 |d D ],}
|	t|
 d  |
kr|	d t|
  }	q|	S d S )	Nr"   r    rU   rW   FrZ   r   r]   )rS   r&   _astreamr_   r-   r`   r.   r[   ra   rb   rc   rd   re   r>   r>   r?   _acall  s0    zHuggingFaceEndpoint._acallc                 k   s   | j |fi |}| jj|fi |ddiD ]n}d }|d D ]}||v r>|}q>d }	|rl|d || }	n|}	|	rt|	d}
|r||
j |
V  |r. qq.d S Nr\   Tr    )r_   )rS   r,   text_generationindexr   on_llm_new_tokenr_   rL   rT   rU   rV   rQ   rf   ri   Zstop_seq_foundrj   r_   rh   r>   r>   r?   r^   >  s.    
zHuggingFaceEndpoint._streamc                 K  s   | j |fi |}| jj|fi |ddiI d H 2 z|3 d H W }d }|d D ]}||v rL|}qLd }	|rz|d || }	n|}	|	rt|	d}
|r||
jI d H  |
V  |r4 qq46 d S rn   )rS   r-   ro   rp   r   rq   r_   rr   r>   r>   r?   rl   c  s.    
zHuggingFaceEndpoint._astream)NN)NN)NN)NN)=__name__
__module____qualname____doc__r   r   str__annotations__r   r   r   intr   r   floatr   r   r   r   boolr   r   r5   r    r   r!   r#   r%   r&   r'   r(   dictr)   r   r   r*   r,   r-   r.   r   Zmodel_configr   classmethodr@   r   rJ   propertyrM   r   rN   rO   rS   r   rk   r
   rm   r   r   r^   r   rl   r>   r>   r>   r?   r      s   
+"'

  
&  
%  
(  
r   ) r[   loggingrF   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.utilsr   r   Zpydanticr   r   r   	getLoggerrs   r7   ZVALID_TASKSr   r>   r>   r>   r?   <module>   s"   $
