
    7|h.                     V   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jD                  e#      Z$ G d de      Z%e  e! dfde%de&de&dee   def
dZ'de%deegef   fdZ(de%dedefdZ)y)    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                      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      Zededefd       Zedee	ef   fd       Zede e	ef   fd       Z!ede	fd       Z"deee	      dedefdZ#e$ded eee	      de	fd!       Z%	 	 d%d"e	d eee	      d#ee&   dede	f
d$Z'y)&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_retriesforbid)extravaluesreturnc                     t        |ddt              }t        |ddt              }t        t        |ddd            }|j	                  d      r|dd	 }|j                  d      sd|z   }||d<   ||d<   ||d<   |S )
z?Validate that api key and python package exists in environment.r   NEBULA_SERVICE_URLr   NEBULA_SERVICE_PATHr   NEBULA_API_KEYN/)r   DEFAULT_NEBULA_SERVICE_URLDEFAULT_NEBULA_SERVICE_PATHr   endswith
startswith)clsr-   r   r   r   s        f/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain_community/llms/symblai_nebula.pyvalidate_environmentzNebula.validate_environmentC   s     2  &	
 3!!'	
 / )9;KTR
 &&s+!3CR!8"--c2"%(;";'9#$(;$%#1     c                 v    | j                   | j                  | j                  | j                  | j                  dS )z2Get the default parameters for calling Cohere API.r"   r#   r'   r$   r%   r>   selfs    r:   _default_paramszNebula._default_paramsa   s7     #11++ZZZZ"&"9"9
 	
r<   c                 \    | j                   xs i }| j                  | j                  dd|iS )zGet the identifying parameters.)r   r   r   )r   r   r   )r@   _model_kwargss     r:   _identifying_paramszNebula._identifying_paramsl   s?     ))/R"&"9"9#'#;#;
 }-
 	
r<   c                      y)zReturn type of llm.nebula r?   s    r:   	_llm_typezNebula._llm_typev   s     r<   kwargsc                     | j                   }| j                  |t        d      | j                  | j                  |d<   n||d<   i ||S )Nz2`stop` found in both the input and default params.r(   )rA   r(   
ValueError)r@   r(   rI   paramss       r:   _invocation_paramszNebula._invocation_params{   sd     %%*~/IQRR  ,'+':':F#$'5F#$#&#F##r<   responsestopc                 2    | d   d   }|rt        ||      }|S )Noutputtextr   )rN   rO   rR   s      r:   _process_responsezNebula._process_response   s%    !&)&tT2Dr<   promptrun_managerc                      | j                   |fi |}|j                         }t        | ||| j                   | j                         }|j                  d      }| j                  ||      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.")
        )rT   rL   urlr(   )rM   stripcompletion_with_retryr   r   getrS   )r@   rT   rO   rU   rI   rL   rN   _stops           r:   _callzNebula._call   sw    " )((88(**+D,D,D+EF	
 

+,%%h66r<   )NN)(__name__
__module____qualname____doc__r   r   dict__annotations__r   strr   r   r   r    r"   intr#   floatr$   r%   r'   r(   r   r*   r   model_configr   r   r;   propertyr   rA   r   rD   rH   rM   staticmethodrS   r	   r\   rG   r<   r:   r   r      s   $ L#'L(4.'(,,)-#-*.NHY'.E8C=$'NHSM'#&K%&!E8E?!*--E8C=*.NHT#Y'.!#K##L $ 4  : 
c3h 
 
 
WS#X%6 
 
 3  
$&tCy1
$=@
$	
$ C xS	/B s   %):>	77 tCy!7 67	7
 7 
7r<   r   r@   rT   rW   rL   r.   c                    |xs i }d}| j                   | j                   j                         }d| d}d|i}|j                         D ]
  \  }}|||<    t        j                  |||      }	|	j
                  dk7  r%t        d|	j
                   d|	j                         t        j                  |	j                        S )	zGenerate text from the model.Nzapplication/json)zContent-TypeApiKeyrT   )headersjson   z Request failed with status code z and message )
r   get_secret_valueitemsrequestspoststatus_code	ExceptionrR   rl   loads)
r@   rT   rW   rL   api_keyrk   bodykeyvaluerN   s
             r:   make_requestry      s     \rFG&%%668*IG
 fD lln 
US	 }}S'=Hs".x/C/C.DHMM?,
 	

 ::hmm$$r<   llmc           
          d}d}| j                   | j                   nd}t        dt        |      t        d||      t	        t
        t        t        f      t        t        t        j                              S )N   r)      Tr&   )
multiplierminmax)reraiserO   waitr   before_sleep)r*   r   r   r   r   r   r   r   r   loggerloggingWARNING)rz   min_secondsmax_secondsr*   s       r:   _create_retry_decoratorr      sd    KK &)__%@#//aK,M#%5~{$ST%fgoo> r<   rI   c                 X     t               }|dt        dt        f fd       } |di |S )z*Use tenacity to retry the completion call._kwargsr.   c                      t        fi | S )N)ry   )r   rz   s    r:   _completion_with_retryz5completion_with_retry.<locals>._completion_with_retry   s    C+7++r<   rG   )r   r   )rz   rI   retry_decoratorr   s   `   r:   rY   rY      s=    -c2O,# ,# , , "+F++r<   )*rl   r   typingr   r   r   r   r   r   rp   langchain_core.callbacksr	   #langchain_core.language_models.llmsr
   langchain_core.utilsr   r   r   pydanticr   r   r   r   r   tenacityr   r   r   r   r   langchain_community.llms.utilsr   r5   r6   	getLoggerr]   r   r   rc   ry   r   rY   rG   r<   r:   <module>r      s      ? ?  = 3 V V * B B  ?: 2 			8	$N7S N7h --.I-JK!	%
%% 
% TN	%
 	%D HcUCZ,@ ",v , , ,r<   