
    7|h                         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       G d de             Zy)    N)AnyDictListMappingOptional)
deprecated)CallbackManagerForLLMRun)LLM)get_from_dict_or_envpre_init)
ConfigDict)enforce_stop_tokenstranslation_textsummary_textgenerated_text)translationsummarizationconversationalztext-generationztext2text-generationz0.0.21z1.0z)langchain_huggingface.HuggingFaceEndpoint)removalalternative_importc                      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	      Z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y)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_tokenforbid)extravaluesreturnc                 b   t        |dd      }	 ddlm}m} |d   } |||      }|d   s5|st	        d       ||	      j                  |
      }|j                  |d<   |d   t        vr't	        d|d    dt        j                          d      ||d<   |S # t        $ r t        d      w xY w)z?Validate that api key and python package exists in environment.r   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_infopipeline_tagVALID_TASKS_DICTkeysImportError)clsr    r   r$   r%   r   r   r*   s           g/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain_community/llms/huggingface_hub.pyvalidate_environmentz#HuggingFaceHub.validate_environment?   s    $8.0J$
 	>Y'G$.F &>$K  #)ABMM# N 
 ",!8!8vf~%55 'v'7 8&&6&;&;&=%>nN   &F8   	H 	s   BB B.c                 `    | j                   xs i }i | j                  | j                  dd|iS )zGet the identifying parameters.)r   r   r   )r   r   r   )self_model_kwargss     r0   _identifying_paramsz"HuggingFaceHub._identifying_paramsd   s@     ))/R
,,		:
}-
 	
    c                      y)zReturn type of llm.r(    )r3   s    r0   	_llm_typezHuggingFaceHub._llm_typem   s     !r6   promptstoprun_managerkwargsc                 t   | j                   xs i }i ||}| j                  j                  ||d| j                        }t	        j
                  |j                               }d|v rt        d|d          t        | j                     }t        |t              r	|d   |   }	n||   }	|t        |	|      }	|	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   )r   r   postr   rA   loadsdecoder)   r,   
isinstancelistr   )
r3   r:   r;   r<   r=   r4   r@   responseresponse_keytexts
             r0   _callzHuggingFaceHub._callr   s    * ))/R000
;;##"*=DII $ 
 ::hoo/0h>x?P>QRSS'		2h%A;|,DL)D 'tT2Dr6   )NN)__name__
__module____qualname____doc__r   r   __annotations__r   r   strr   r   dictr   r   model_configr   r   r1   propertyr   r5   r9   r   r	   rK   r8   r6   r0   r   r      s   " FC!GXc]!LD(3- $(L(4.'1.2hsm2L "$ "4 " "H 
WS#X%6 
 
 !3 ! ! %):>	)) tCy!) 67	)
 ) 
)r6   r   )rA   typingr   r   r   r   r   langchain_core._api.deprecationr   langchain_core.callbacksr	   #langchain_core.language_models.llmsr
   langchain_core.utilsr   r   pydanticr   langchain_community.llms.utilsr   r,   r   r8   r6   r0   <module>r\      se     5 5 6 = 3 ?  >
 &#&',  B
S 
r6   