
    Ng                         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dS )    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                   V   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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_tokenforbid)extravaluesreturnc                    t          |dd          }	 ddlm}m} |d         } |||          }|d         s;|st	          d           ||	                              |
          }|j        |d<   |d         t          vr3t	          d|d          dt                                           d          ||d<   n# t          $ r t          d          w xY w|S )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           d/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/llms/huggingface_hub.pyvalidate_environmentz#HuggingFaceHub.validate_environment?   sk    $8.0J$
 $
 	>>>>>>>>Y'G$_.  F &> 	9 $K   #U)ABBBMM# N  
 ",!8vf~%555 Nv N N&6&;&;&=&=N N N    &F8 	 	 	H  	
 s   B'B; ;Cc                 >    | j         p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                     dS )zReturn type of llm.r(    )r3   s    r0   	_llm_typezHuggingFaceHub._llm_typem   s
     ! r6   promptstoprun_managerkwargsc                    | j         pi }i ||}| j                            ||d| j                  }t	          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
;##"*==DI $ 
 
 :hoo//00hRx?PRRSSS'	2h%% 	*A;|,DDL)D 'tT2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22:  L "$ "4 " " " X"H 
WS#X%6 
 
 
 X
 !3 ! ! ! X! %):>	) )) 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\      s/    5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 = = = = = = 3 3 3 3 3 3 ? ? ? ? ? ? ? ?       > > > > > >
 &#&',   B  
    S   
  r6   