U
    hm                     @   sh   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mZ G dd de	ZdS )	    )AnyDictListMappingOptional)CallbackManagerForLLMRun)LLM)	SecretStr)convert_to_secret_strget_from_dict_or_envpre_initc                   @   sb  e Zd ZU dZ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d< dZe	ed< dZe	ed< dZe	ed< g Ze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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!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 )+NLPCloudaa  NLPCloud large language models.

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

    Example:
        .. code-block:: python

            from langchain_community.llms import NLPCloud
            nlpcloud = NLPCloud(model="finetuned-gpt-neox-20b")
    clientzfinetuned-gpt-neox-20b
model_nameTgpuenlanggffffff?temperature   
max_lengthlength_no_inputremove_inputremove_end_sequence	bad_wordsg      ?top_p2   top_krepetition_penalty   	num_beamsnum_return_sequencesNnlpcloud_api_keyc                   @   s   e Zd ZdZdS )zNLPCloud.ConfigZforbidN)__name__
__module____qualname__extra r&   r&   E/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/llms/nlpcloud.pyConfig6   s   r(   )valuesreturnc                 C   sl   t t|dd|d< z6ddl}|j|d |d  |d |d d|d	< W n tk
rf   td
Y nX |S )z?Validate that api key and python package exists in environment.r!   ZNLPCLOUD_API_KEYr   Nr   r   r   )r   r   r   zXCould not import nlpcloud python package. Please install it with `pip install nlpcloud`.)r
   r   nlpcloudZClientZget_secret_valueImportError)clsr)   r+   r&   r&   r'   validate_environment9   s     


zNLPCloud.validate_environment)r*   c                 C   s2   | j | j| j| j| j| j| j| j| j| j	| j
dS )z4Get the default parameters for calling NLPCloud API.r   r   r   r   r   r   r   r   r   r   r    r/   selfr&   r&   r'   _default_paramsO   s    zNLPCloud._default_paramsc                 C   s    d| j id| jid| ji| jS )zGet the identifying parameters.r   r   r   )r   r   r   r2   r0   r&   r&   r'   _identifying_params`   s
    zNLPCloud._identifying_paramsc                 C   s   dS )zReturn type of llm.r+   r&   r0   r&   r&   r'   	_llm_typej   s    zNLPCloud._llm_type)promptstoprun_managerkwargsr*   c                 K   sb   |rt |dkrtdn|r4t |dkr4|d }nd}| j|}| jj|fd|i|}|d S )al  Call out to NLPCloud's create endpoint.

        Args:
            prompt: The prompt to pass into the model.
            stop: Not supported by this interface (pass in init method)

        Returns:
            The string generated by the model.

        Example:
            .. code-block:: python

                response = nlpcloud("Tell me a joke.")
        r   zXNLPCloud only supports a single stop sequence per generation.Pass in a list of length 1.r   Nend_sequenceZgenerated_text)len
ValueErrorr2   r   Z
generation)r1   r5   r6   r7   r8   r9   paramsresponser&   r&   r'   _callo   s    

zNLPCloud._call)NN)'r"   r#   r$   __doc__r   __annotations__r   strr   boolr   r   floatr   intr   r   r   r   r   r   r   r   r   r    r!   r   r	   r(   r   r   r.   propertyr   r2   r3   r4   r   r>   r&   r&   r&   r'   r   	   sF   
	  
r   N)typingr   r   r   r   r   Zlangchain_core.callbacksr   Z#langchain_core.language_models.llmsr   Zlangchain_core.pydantic_v1r	   Zlangchain_core.utilsr
   r   r   r   r&   r&   r&   r'   <module>   s
   