o
    Ag                     @   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d
e
eef defddZedeeef fddZedefddZ		ddedeee  dee dedef
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_kwargsforbid)extrabefore)modevaluesreturnc                 C   s   dd t |  D }|di }t|D ]$}||vr9||v r't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   e/var/www/html/development/chatbot/venv/lib/python3.10/site-packages/langchain_community/llms/modal.py	<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   all_required_field_namesr   
field_namer   r   r   build_extra*   s   zModal.build_extrac                 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kwargsc           	      K   s   | j pi }i ||}tj| jddid|i|d}z|| d v r'| }W n ty3   tdw |d }|durAt||}|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responseresponse_jsontextr   r   r   _callJ   s&   


zModal._call)NN)__name__
__module____qualname____doc__r   str__annotations__r   dictr   r   r   r
   model_configr   classmethodr(   propertyr   r+   r-   r   r   r   r<   r   r   r   r   r      s:   
 
r   )loggingtypingr   r   r   r   r   r5   langchain_core.callbacksr   #langchain_core.language_models.llmsr   langchain_core.utils.pydanticr	   pydanticr
   r   r   langchain_community.llms.utilsr   	getLoggerr=   r"   r   r   r   r   r   <module>   s    
