U
    h                     @   s   d dl Z d dl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 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root_validator)
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< G dd dZedd	e
eef 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 )Modala|  Modal large language models.

    To use, you should have the ``modal-client`` python package installed.

    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 Modal
            modal = Modal(endpoint_url="")

     endpoint_url)default_factorymodel_kwargsc                   @   s   e Zd ZdZdS )zModal.ConfigZforbidN)__name__
__module____qualname__extra r   r   B/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/llms/modal.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>,   s     z$Modal.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Modal.build_extra)r   c                 C   s   d| j id| jiS )zGet the identifying parameters.r   r   )r   r   selfr   r   r   _identifying_params;   s    zModal._identifying_paramsc                 C   s   dS )zReturn type of llm.modalr   r)   r   r   r   	_llm_typeC   s    zModal._llm_typeN)promptstoprun_managerkwargsr   c           	      K   s   | j pi }||}tj| jddid|i|d}z|| d krJ| }W n tk
rh   tdY nX |d }|dk	rt||}|S )zCall to Modal endpoint.zContent-Typezapplication/jsonr.   )urlheadersjsonz,LangChain requires 'prompt' key in response.N)r   requestspostr   r4   KeyErrorr   )	r*   r.   r/   r0   r1   paramsresponseZresponse_jsontextr   r   r   _callH   s$    
 

zModal._call)NN)r   r   r   __doc__r   str__annotations__r	   dictr   r   r   r   r
   r(   propertyr   r+   r-   r   r   r   r;   r   r   r   r   r      s&   
"  
r   )loggingtypingr   r   r   r   r   r5   Zlangchain_core.callbacksr   Z#langchain_core.language_models.llmsr   Zlangchain_core.pydantic_v1r	   r
   Zlangchain_core.utils.pydanticr   Zlangchain_community.llms.utilsr   	getLoggerr   r#   r   r   r   r   r   <module>   s   
