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mZmZ d dlmZmZmZ d dlmZ d dlmZ e eZG d	d
 d
e
ZdS )    N)AnyDictListMappingOptional)CallbackManagerForLLMRun)LLM)Field	SecretStrroot_validator)convert_to_secret_strget_from_dict_or_envpre_init)
get_fields)enforce_stop_tokensc                   @   s^  e Zd ZU dZeed< 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 ed< dZeed< dZee ed< eedZeeef ed< dZee ed< G dd dZeddeeef eeef dddZeeedddZeeeef 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 )'Petalsa  Petals Bloom models.

    To use, you should have the ``petals`` python package installed, and the
    environment variable ``HUGGINGFACE_API_KEY`` set with your API key.

    Any parameters that are valid to be passed to the call can be passed
    in, even if not explicitly saved on this class.

    Example:
        .. code-block:: python

            from langchain_community.llms import petals
            petals = Petals()

    client	tokenizerzbigscience/bloom-petals
model_namegffffff?temperature   max_new_tokensg?top_pNtop_kT	do_sample
max_length)default_factorymodel_kwargshuggingface_api_keyc                   @   s   e Zd ZdZdS )zPetals.ConfigZforbidN)__name__
__module____qualname__extra r#   r#   C/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/llms/petals.pyConfigB   s   r%   )pre)valuesreturnc              
   C   s   dd t |  D }|di }t|D ]P}||kr*||krNtd| dtd| d| d| d	 ||||< q*||d< |S )
z>Build extra kwargs from additional params that were passed in.c                 S   s   h | ]
}|j qS r#   )alias).0fieldr#   r#   r$   	<setcomp>H   s     z%Petals.build_extra.<locals>.<setcomp>r   zFound z supplied twice.z	WARNING! z/ is not default parameter.
                    zJ was transferred to model_kwargs.
                    Please confirm that z is what you intended.)r   r'   getlist
ValueErrorloggerwarningpop)clsr'   Zall_required_field_namesr"   
field_namer#   r#   r$   build_extraE   s    zPetals.build_extrac                 C   s~   t t|dd}zLddlm} ddlm} |d }|||d< |||d< | |d< W n tk
rx   td	Y nX |S )
z?Validate that api key and python package exists in environment.r   ZHUGGINGFACE_API_KEYr   )AutoDistributedModelForCausalLM)AutoTokenizerr   r   r   zpCould not import transformers or petals python package.Please install with `pip install -U transformers petals`.)	r   r   petalsr6   Ztransformersr7   Zfrom_pretrainedZget_secret_valueImportError)r3   r'   r   r6   r7   r   r#   r#   r$   validate_environmentX   s"    

zPetals.validate_environment)r(   c                 C   s(   | j | j| j| j| j| jd}|| jS )z2Get the default parameters for calling Petals API.)r   r   r   r   r   r   )r   r   r   r   r   r   r   )selfZnormal_paramsr#   r#   r$   _default_paramsp   s    zPetals._default_paramsc                 C   s   d| j i| jS )zGet the identifying parameters.r   )r   r<   r;   r#   r#   r$   _identifying_params}   s    zPetals._identifying_paramsc                 C   s   dS )zReturn type of llm.r8   r#   r=   r#   r#   r$   	_llm_type   s    zPetals._llm_type)promptstoprun_managerkwargsr(   c           	      K   sV   | j }||}| j|ddd }| jj|f|}| j|d }|dk	rRt||}|S )zCall the Petals API.pt)Zreturn_tensorsZ	input_idsr   N)r<   r   r   generatedecoder   )	r;   r@   rA   rB   rC   paramsinputsoutputstextr#   r#   r$   _call   s    
zPetals._call)NN)%r   r    r!   __doc__r   __annotations__r   strr   floatr   intr   r   r   r   boolr   r	   dictr   r   r   r
   r%   r   r5   r   r:   propertyr<   r   r>   r?   r   r   rK   r#   r#   r#   r$   r      s@   
"  
r   )loggingtypingr   r   r   r   r   Zlangchain_core.callbacksr   Z#langchain_core.language_models.llmsr   Zlangchain_core.pydantic_v1r	   r
   r   Zlangchain_core.utilsr   r   r   Zlangchain_core.utils.pydanticr   Zlangchain_community.llms.utilsr   	getLoggerr   r0   r   r#   r#   r#   r$   <module>   s   
