U
    h"                     @   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 d dlmZ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)	SecretStr)convert_to_secret_strget_from_dict_or_envpre_init)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< G d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_retriesc                   @   s   e Zd ZdZdS )zNebula.ConfigZforbidN)__name__
__module____qualname__extra r,   r,   K/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/llms/symblai_nebula.pyConfig?   s   r.   )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   r,   r,   r-   validate_environmentB   s.    

zNebula.validate_environment)r0   c                 C   s   | j | j| j| j| jdS )z2Get the default parameters for calling Cohere API.r   r    r$   r!   r"   r9   selfr,   r,   r-   _default_params`   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   )r;   Z_model_kwargsr,   r,   r-   _identifying_paramsk   s    
zNebula._identifying_paramsc                 C   s   dS )zReturn type of llm.Znebular,   r:   r,   r,   r-   	_llm_typeu   s    zNebula._llm_type)r%   kwargsr0   c                 K   sH   | j }| jd k	r"|d k	r"tdn| jd k	r8| j|d< n||d< ||S )Nz2`stop` found in both the input and default params.r%   )r<   r%   
ValueError)r;   r%   r?   paramsr,   r,   r-   _invocation_paramsz   s    

zNebula._invocation_params)responsestopr0   c                 C   s   | d d }|rt ||}|S )Noutputtextr   )rC   rD   rF   r,   r,   r-   _process_response   s    
zNebula._process_response)promptrD   run_managerr?   r0   c                 K   sH   | j |f|}| }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.")
        )rH   rA   urlr%   )rB   stripcompletion_with_retryr   r   getrG   )r;   rH   rD   rI   r?   rA   rC   _stopr,   r,   r-   _call   s    
zNebula._call)NN)'r(   r)   r*   __doc__r   r   dict__annotations__r   strr   r   r
   r   r   intr    floatr!   r"   r$   r%   r   r'   r.   r   r   r8   propertyr   r<   r   r=   r>   rB   staticmethodrG   r   rO   r,   r,   r,   r-   r      sJ   

	
 	  
r   )r;   rH   rJ   rA   r0   c           
      C   s   |pi }d}| j dk	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ApiKeyrH   )headersjson   z Request failed with status code z and message )
r   Zget_secret_valueitemsrequestspoststatus_code	ExceptionrF   rY   loads)
r;   rH   rJ   rA   Zapi_keyrX   bodykeyvaluerC   r,   r,   r-   make_request   s     



rd   )llmr0   c                 C   sN   d}d}| j d k	r| j nd}tdt|td||dttttftt	t
jdS )N   r&      Tr#   )Z
multiplierminmax)reraiserD   waitr   Zbefore_sleep)r'   r   r   r   r   r   r   r   r   loggerloggingWARNING)re   Zmin_secondsZmax_secondsr'   r,   r,   r-   _create_retry_decorator   s    
ro   )re   r?   r0   c                    s*   t  }|ttd fdd}|f |S )z*Use tenacity to retry the completion call.)_kwargsr0   c                     s   t  f| S )N)rd   )rp   re   r,   r-   _completion_with_retry   s    z5completion_with_retry.<locals>._completion_with_retry)ro   r   )re   r?   Zretry_decoratorrr   r,   rq   r-   rL      s    rL   ))rY   rm   typingr   r   r   r   r   r   r\   Zlangchain_core.callbacksr   Z#langchain_core.language_models.llmsr	   Zlangchain_core.pydantic_v1r
   Zlangchain_core.utilsr   r   r   r   r   r   Ztenacityr   r   r   r   r   Zlangchain_community.llms.utilsr   r3   r4   	getLoggerr(   rl   r   rS   rd   ro   rL   r,   r,   r,   r-   <module>   s4    
 
"