
    Ng.                     f   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j"        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)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                      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'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_retriesforbid)extravaluesreturnc                 0   t          |ddt                    }t          |ddt                    }t          t          |ddd                    }|                    d          r
|dd	         }|                    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        c/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/llms/symblai_nebula.pyvalidate_environmentzNebula.validate_environmentC   s     2  &	
 
 3!!'	
 
 / )9;KTRR
 
 &&s++ 	9!3CRC!8"--c22 	<"%(;";'9#$(;$%#1     c                 D    | 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   s/     #1+ZZ"&"9
 
 	
r<   c                 :    | j         p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#'#;
 
 }-
 	
r<   c                     dS )zReturn type of llm.nebula r?   s    r:   	_llm_typezNebula._llm_typev   s	     xr<   kwargsc                 z    | 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{   s^     %*~/IQRRR ,'+':F#$$'5F#$#&#F##r<   responsestopc                 F    | d         d         }|rt          ||          }|S )Noutputtextr   )rN   rO   rR   s      r:   _process_responsezNebula._process_response   s-    !&) 	3&tT22Dr<   promptrun_managerc                      | j         |fi |}|                                }t          | ||| j         | j                   }|                    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.")
        )rT   rL   urlr(   )rM   stripcompletion_with_retryr   r   getrS   )r@   rT   rO   rU   rI   rL   rN   _stops           r:   _callzNebula._call   s    " )(8888(*FD,DFF	
 
 
 

+,,%%h6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    X: 
c3h 
 
 
 X
 
WS#X%6 
 
 
 X
 3    X
$&tCy1
$=@
$	
$ 
$ 
$ 
$ C xS	/B s    \ %):>	7 77 tCy!7 67	7
 7 
7 7 7 7 7 7r<   r   r@   rT   rW   rL   r.   c                 T   |pi }d}| j         | j                                         }d| d}d|i}|                                D ]
\  }}|||<   t          j        |||          }	|	j        dk    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8*, G
 fD llnn  
US		 }S'===Hs"",x/C , ,$M, ,
 
 	

 :hm$$$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      sy    KK &)_%@#//aK,,MMM#%5~{$STT%fgo>>   r<   rI   c                 j     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2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      s3     ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?  = = = = = = 3 3 3 3 3 3 V V V V V V V V V V * * * * * * * * B B B B B B B B B B              ? > > > > >: 2 		8	$	$N7 N7 N7 N7 N7S N7 N7 N7h -K.IKK!	% %
%% 
% TN	%
 	% % % %D HcUCZ,@    ",v , , , , , , , ,r<   