a
    bg                     @   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 d dlmZmZ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)
get_fields)
ConfigDictFieldmodel_validator)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< eddZedd	ee
eef 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 )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_kwargsZforbid)extrabefore)mode)valuesreturnc                 C   s   dd t |  D }|di }t|D ]H}||vr*||v 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  )alias).0fieldr   r   l/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/llms/modal.py	<setcomp>.       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   i 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_typeE   s    zModal._llm_typeN)promptstoprun_managerkwargsr   c           	      K   s   | j pi }i ||}tj| jddid|i|d}z|| d v rN| }W n tyj   tdY n0 |d }|du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   r3   KeyErrorr   )	r)   r-   r.   r/   r0   paramsresponseZresponse_jsontextr   r   r   _callJ   s"    


zModal._call)NN)__name__
__module____qualname____doc__r   str__annotations__r   dictr   r   r   r
   Zmodel_configr   classmethodr'   propertyr   r*   r,   r   r   r   r:   r   r   r   r   r      s,   
  
r   )loggingtypingr   r   r   r   r   r4   Zlangchain_core.callbacksr   Z#langchain_core.language_models.llmsr   Zlangchain_core.utils.pydanticr	   Zpydanticr
   r   r   Zlangchain_community.llms.utilsr   	getLoggerr;   r"   r   r   r   r   r   <module>   s   
