o
    Zh                     @   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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)
ConfigDict)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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< edd	Zed
edefddZedee	ef fddZede	fddZ		dde	deee	  dee dede	f
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")
    Nclientrepo_idtaskmodel_kwargshuggingfacehub_api_tokenZforbid)extravaluesreturnc                 C   s   t |dd}zFddlm}m} |d }|||d}|d s1|s#td||d	j|d
}|j|d< |d tvrFtd|d  dt  d||d< W |S  t	yW   t	dw )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#   _/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/llms/huggingface_hub.pyvalidate_environment?   s@   


z#HuggingFaceHub.validate_environmentc                 C   s$   | j pi }i | j| jdd|iS )zGet the identifying parameters.)r   r   r   )r   r   r   )self_model_kwargsr#   r#   r$   _identifying_paramsd   s   
z"HuggingFaceHub._identifying_paramsc                 C   s   dS )zReturn type of llm.r   r#   )r&   r#   r#   r$   	_llm_typem   s   zHuggingFaceHub._llm_typepromptstoprun_managerkwargsc           
      K   s   | j pi }i ||}| jj||d| jd}t| }d|v r+td|d  t| j }t	|t
r<|d | }	n|| }	|durIt|	|}	|	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.")
        )Zinputs
parameters)jsonr   errorzError raised by inference API: r   N)r   r   postr   r/   loadsdecoder   r   
isinstancelistr   )
r&   r*   r+   r,   r-   r'   r.   responseZresponse_keytextr#   r#   r$   _callr   s   



zHuggingFaceHub._call)NN)__name__
__module____qualname____doc__r   r   __annotations__r   r   strr   r   dictr   r   Zmodel_configr   r   r%   propertyr   r(   r)   r   r   r8   r#   r#   r#   r$   r      s@   
 $
r   )r/   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pydanticr   Zlangchain_community.llms.utilsr   r   r   r#   r#   r#   r$   <module>   s(    	