U
    h                     @   s   d dl Z d dlmZmZmZmZmZmZ d dl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cast)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dZ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dddZdeeee  ee eedddZdS )CerebriumAIaH  CerebriumAI large language models.

    To use, you should have the ``cerebrium`` python package installed.
    You should also have the environment variable ``CEREBRIUMAI_API_KEY``
    set with your API key or pass it as a named argument in the constructor.

    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 CerebriumAI
            cerebrium = CerebriumAI(endpoint_url="", cerebriumai_api_key="my-api-key")

     endpoint_url)default_factorymodel_kwargsNcerebriumai_api_keyc                   @   s   e Zd ZdZdS )zCerebriumAI.ConfigZforbidN)__name__
__module____qualname__extra r   r   H/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/llms/cerebriumai.pyConfig+   s   r   T)pre)valuesreturnc                 C   s   dd t |  D }|di }t|D ]H}||kr*||krNtd| dt| 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>1   s     z*CerebriumAI.build_extra.<locals>.<setcomp>r   zFound z supplied twice.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_extra.   s    zCerebriumAI.build_extrac                 C   s   t t|dd}||d< |S )z?Validate that api key and python package exists in environment.r   ZCEREBRIUMAI_API_KEY)r   r   )r,   r    r   r   r   r   validate_environment@   s
    
z CerebriumAI.validate_environment)r!   c                 C   s   d| j id| jiS )zGet the identifying parameters.r   r   )r   r   selfr   r   r   _identifying_paramsI   s    zCerebriumAI._identifying_paramsc                 C   s   dS )zReturn type of llm.Zcerebriumair   r0   r   r   r   	_llm_typeQ   s    zCerebriumAI._llm_type)promptstoprun_managerkwargsr!   c                 K   s|   t t| j dd}| jpi }d|i||}tj| j||d}|jdkrp|	 }	|	d }
|d k	rlt
|
|}
|
S |  dS )Nzapplication/json)AuthorizationzContent-Typer4   )jsonheaders   resultr   )r   r   r   Zget_secret_valuer   requestspostr   status_coder9   r   raise_for_status)r1   r4   r5   r6   r7   r:   paramspayloadresponsedatatextr   r   r   _callV   s"     


zCerebriumAI._call)NN)r   r   r   __doc__r   str__annotations__r
   dictr   r   r   r   r   r   r   r   r.   r   r/   propertyr   r2   r3   r   r   rF   r   r   r   r   r      s,   
"  
r   )loggingtypingr   r   r   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   r)   r   r   r   r   r   <module>   s    
