U
    h                     @   s   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mZ d dlmZ dd	d
d
d
dZeddddG dd deZdS )    N)AnyDictListMappingOptional)
deprecated)CallbackManagerForLLMRun)LLM)get_from_dict_or_envpre_init)enforce_stop_tokensZtranslation_textZsummary_textZgenerated_text)translationZsummarizationZconversationalztext-generationztext2text-generationz0.0.21z1.0z)langchain_huggingface.HuggingFaceEndpoint)ZremovalZalternative_importc                   @   s   e Zd ZU 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< G dd	 d	Zeeed
ddZeeeef dddZeedddZdeeee  ee eedddZdS )HuggingFaceHuba  HuggingFaceHub  models.
    ! This class is deprecated, you should use HuggingFaceEndpoint instead.

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

    Supports `text-generation`, `text2text-generation`, `conversational`, `translation`,
     and `summarization`.

    Example:
        .. code-block:: python

            from langchain_community.llms import HuggingFaceHub
            hf = HuggingFaceHub(repo_id="gpt2", huggingfacehub_api_token="my-api-key")
    clientNrepo_idtaskmodel_kwargshuggingfacehub_api_tokenc                   @   s   e Zd ZdZdS )zHuggingFaceHub.ConfigZforbidN)__name__
__module____qualname__extra r   r   L/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/llms/huggingface_hub.pyConfig:   s   r   )valuesreturnc                 C   s   t |dd}zddlm}m} |d }|||d}|d sb|sFtd||d	j|d
}|j|d< |d tkrtd|d  dt  d||d< W n t	k
r   t	dY nX |S )z?Validate that api key and python package exists in environment.r   ZHUGGINGFACEHUB_API_TOKENr   )HfApiInferenceClientr   )modeltokenr   z1Must specify either `repo_id` or `task`, or both.)r    )r   zGot invalid task z, currently only z are supportedr   zfCould not import huggingface_hub python package. Please install it with `pip install huggingface_hub`.)
r
   huggingface_hubr   r   
ValueError
model_infoZpipeline_tagVALID_TASKS_DICTkeysImportError)clsr   r   r   r   r   r   r#   r   r   r   validate_environment=   s>      


z#HuggingFaceHub.validate_environment)r   c                 C   s    | j pi }| j| jdd|iS )zGet the identifying parameters.)r   r   r   )r   r   r   )self_model_kwargsr   r   r   _identifying_paramsb   s    
z"HuggingFaceHub._identifying_paramsc                 C   s   dS )zReturn type of llm.r!   r   )r)   r   r   r   	_llm_typek   s    zHuggingFaceHub._llm_type)promptstoprun_managerkwargsr   c           
      K   s   | j pi }||}| jj||d| jd}t| }d|krRtd|d  t| j }t	|t
rt|d | }	n|| }	|dk	rt|	|}	|	S )an  Call out to HuggingFace Hub's inference endpoint.

        Args:
            prompt: The prompt to pass into the model.
            stop: Optional list of stop words to use when generating.

        Returns:
            The string generated by the model.

        Example:
            .. code-block:: python

                response = hf("Tell me a joke.")
        )inputs
parameters)jsonr   errorzError raised by inference API: r   N)r   r   postr   r3   loadsdecoder"   r$   
isinstancelistr   )
r)   r-   r.   r/   r0   r*   r2   responseZresponse_keytextr   r   r   _callp   s     
 


zHuggingFaceHub._call)NN)r   r   r   __doc__r   __annotations__r   r   strr   r   dictr   r   r   r   r(   propertyr   r+   r,   r   r   r<   r   r   r   r   r      s,   
$  
r   )r3   typingr   r   r   r   r   Zlangchain_core._api.deprecationr   Zlangchain_core.callbacksr   Z#langchain_core.language_models.llmsr	   Zlangchain_core.utilsr
   r   Zlangchain_community.llms.utilsr   r$   r   r   r   r   r   <module>   s$   	