a
    bg.                     @   s*  d dl Z 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 d dl	mZmZmZ d dlmZmZmZmZmZ d d	lmZ d
Z dZ!e"e#Z$G dd deZ%e  e! dfe%e&e&ee edddZ'e%eegef dddZ(e%eedddZ)dS )    N)AnyCallableDictListMappingOptional)CallbackManagerForLLMRun)LLM)convert_to_secret_strget_from_dict_or_envpre_init)
ConfigDict	SecretStr)ConnectTimeoutReadTimeoutRequestException)before_sleep_logretryretry_if_exception_typestop_after_attemptwait_exponentialenforce_stop_tokenszhttps://api-nebula.symbl.aiz/v1/model/generatec                   @   s  e Zd ZU dZdZee ed< dZee	 ed< dZ
ee	 ed< dZee ed< dZee	 ed< dZee ed	< d
Zee ed< dZee ed< dZee ed< dZee ed< dZeee	  ed< dZee ed< e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"eee	  eed!d"d#Z#e$eeee	  e	d$d%d&Z%d*e	eee	  ee& ee	d'd(d)Z'dS )+Nebulaa)  Nebula Service models.

    To use, you should have the environment variable ``NEBULA_SERVICE_URL``,
    ``NEBULA_SERVICE_PATH`` and ``NEBULA_API_KEY`` set with your Nebula
    Service, or pass it as a named parameter to the constructor.

    Example:
        .. code-block:: python

            from langchain_community.llms import Nebula

            nebula = Nebula(
                nebula_service_url="NEBULA_SERVICE_URL",
                nebula_service_path="NEBULA_SERVICE_PATH",
                nebula_api_key="NEBULA_API_KEY",
            )
    Nmodel_kwargsnebula_service_urlnebula_service_pathnebula_api_keymodel   max_new_tokensg333333?temperaturegffffff?top_pg      ?repetition_penalty   top_kstop_sequences
   max_retriesZforbid)extra)valuesreturnc                 C   sr   t |ddt}t |ddt}tt |ddd}|drD|dd	 }|dsVd| }||d< ||d< ||d< |S )
z?Validate that api key and python package exists in environment.r   ZNEBULA_SERVICE_URLr   ZNEBULA_SERVICE_PATHr   ZNEBULA_API_KEYN/)r   DEFAULT_NEBULA_SERVICE_URLDEFAULT_NEBULA_SERVICE_PATHr
   endswith
startswith)clsr*   r   r   r    r3   u/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/llms/symblai_nebula.pyvalidate_environmentC   s.    

zNebula.validate_environment)r+   c                 C   s   | j | j| j| j| jdS )z2Get the default parameters for calling Cohere API.r    r!   r%   r"   r#   r6   selfr3   r3   r4   _default_paramsa   s    zNebula._default_paramsc                 C   s    | j pi }| j| jdd|iS )zGet the identifying parameters.)r   r   r   )r   r   r   )r8   Z_model_kwargsr3   r3   r4   _identifying_paramsl   s    
zNebula._identifying_paramsc                 C   s   dS )zReturn type of llm.Znebular3   r7   r3   r3   r4   	_llm_typev   s    zNebula._llm_type)r&   kwargsr+   c                 K   sL   | j }| jd ur"|d ur"tdn| jd ur8| j|d< n||d< i ||S )Nz2`stop` found in both the input and default params.r&   )r9   r&   
ValueError)r8   r&   r<   paramsr3   r3   r4   _invocation_params{   s    

zNebula._invocation_params)responsestopr+   c                 C   s   | d d }|rt ||}|S )Noutputtextr   )r@   rA   rC   r3   r3   r4   _process_response   s    
zNebula._process_response)promptrA   run_managerr<   r+   c                 K   sL   | j |fi |}| }t| ||| j | j d}|d}| ||S )aa  Call out to Nebula Service endpoint.
        Args:
            prompt: The prompt to pass into the model.
            stop: Optional list of stop words to use when generating.
        Returns:
            The string generated by the model.
        Example:
            .. code-block:: python
                response = nebula("Tell me a joke.")
        )rE   r>   urlr&   )r?   stripcompletion_with_retryr   r   getrD   )r8   rE   rA   rF   r<   r>   r@   _stopr3   r3   r4   _call   s    
zNebula._call)NN)(__name__
__module____qualname____doc__r   r   dict__annotations__r   strr   r   r   r   r    intr!   floatr"   r#   r%   r&   r   r(   r   Zmodel_configr   r   r5   propertyr   r9   r   r:   r;   r?   staticmethodrD   r   rL   r3   r3   r3   r4   r      sL   

		  
r   )r8   rE   rG   r>   r+   c           
      C   s   |pi }d}| j dur | j  }d| d}d|i}| D ]\}}|||< q<tj|||d}	|	jdkrtd|	j d|	j t	|	jS )	zGenerate text from the model.Nzapplication/json)zContent-TypeZApiKeyrE   )headersjson   z Request failed with status code z and message )
r   Zget_secret_valueitemsrequestspoststatus_code	ExceptionrC   rY   loads)
r8   rE   rG   r>   Zapi_keyrX   bodykeyvaluer@   r3   r3   r4   make_request   s$    




rd   )llmr+   c                 C   sN   d}d}| j d ur| j nd}tdt|td||dttttftt	t
jdS )N   r'      Tr$   )
multiplierminmax)reraiserA   waitr   before_sleep)r(   r   r   r   r   r   r   r   r   loggerloggingWARNING)re   Zmin_secondsZmax_secondsr(   r3   r3   r4   _create_retry_decorator   s    
rq   )re   r<   r+   c                    s.   t  }|ttd fdd}|f i |S )z*Use tenacity to retry the completion call.)_kwargsr+   c                     s   t  fi | S )N)rd   )rr   re   r3   r4   _completion_with_retry   s    z5completion_with_retry.<locals>._completion_with_retry)rq   r   )re   r<   Zretry_decoratorrt   r3   rs   r4   rI      s    rI   )*rY   ro   typingr   r   r   r   r   r   r\   Zlangchain_core.callbacksr   Z#langchain_core.language_models.llmsr	   Zlangchain_core.utilsr
   r   r   Zpydanticr   r   r   r   r   tenacityr   r   r   r   r   Zlangchain_community.llms.utilsr   r.   r/   	getLoggerrM   rn   r   rS   rd   rq   rI   r3   r3   r3   r4   <module>   s4    
 
"