
    Ng2                    ,   d dl mZ d dlZd dlmZmZmZmZmZm	Z	 d dl
mZ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 d d	lmZmZmZmZmZ d d
lmZ  ej        e           Z! G d de          Z" G d de"e          Z#ddZ$ddZ%ddZ&ddZ'ddZ(dS )    )annotationsN)AnyCallableDictListOptionalSequence)AsyncCallbackManagerForLLMRunCallbackManagerForLLMRun)LLM)Serializable)convert_to_secret_strget_from_dict_or_envpre_init)	SecretStr)before_sleep_logretryretry_if_exception_typestop_after_attemptwait_exponential)enforce_stop_tokensc                  6   e Zd ZU dZded<   	 dZded<   	 dZded<   	 dZded<   	 dZded	<   	 d
Z	ded<   	 dZ
ded<   	 dZded<   	 dZded<   	 dZded<   	 dZded<   	 dZded<   	 dZded<   	 dZded<   ed'd!            Zed(d#            Zed)d&            ZdS )*_BaseYandexGPT r   	iam_tokenapi_keystr	folder_id	model_urizyandexgpt-lite
model_namelatestmodel_versiong333333?floattemperaturei  int
max_tokensNOptional[List[str]]stopzllm.api.cloud.yandex.net:443url   max_retriesg      ?sleep_intervalFbooldisable_request_loggingzOptional[Sequence]grpc_metadatareturnc                    dS )N
yandex_gpt selfs    [/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/llms/yandex.py	_llm_typez_BaseYandexGPT._llm_type>   s    |    Dict[str, Any]c                D    | j         | j        | j        | j        | j        dS )zGet the identifying parameters.r   r$   r&   r(   r+   r;   r4   s    r6   _identifying_paramsz"_BaseYandexGPT._identifying_paramsB   s.     +/I+
 
 	
r8   valuesr   c                   t          t          |ddd                    }||d<   t          t          |ddd                    }||d<   t          |ddd          }||d<   |                                dk    r'|                                dk    rt          d          |d         rOd	d
|d                                          fg|d<   |d         r#|d                             d|d         f           n#d	d|d                                          fg|d<   |d         dk    r|d         dk    rt          d          |d         s d|d          d|d          d|d          |d<   |d         r|d                             d           |S )z.Validate that iam token exists in environment.r   YC_IAM_TOKENr   r   
YC_API_KEYr   YC_FOLDER_IDz7Either 'YC_API_KEY' or 'YC_IAM_TOKEN' must be provided.authorizationzBearer r/   zx-folder-idzApi-Key r   z3Either 'model_uri' or 'folder_id' must be provided.zgpt:///r    r"   r.   )zx-data-logging-enabledfalse)r   r   get_secret_value
ValueErrorappend)clsr=   r   r   r   s        r6   validate_environmentz#_BaseYandexGPT.validate_environmentM   s    * nbII
 
	 ({' L"EE
 
 $y(nbQQ	'{##%%++	0J0J0L0LPR0R0RVWWW+ 		 "TF;,?,P,P,R,R"T"TU'F?# k" U'..vk?R/STTT !"SVI->-O-O-Q-Q"S"ST'F?# +"$$)<)B)BRSSSk" 	_,__vl/C__f_F]__ ; +, 	?#**   r8   )r0   r   )r0   r9   )r=   r   r0   r   )__name__
__module____qualname__r   __annotations__r   r   r   r    r"   r$   r&   r(   r)   r+   r,   r.   r/   propertyr7   r<   r   rI   r3   r8   r6   r   r      s        I.G.I I&J&&&&!M!!!!KJJ= !%D$$$$9-C----K<N$$)))))P(,M,,,,   X 
 
 
 X
 ' ' ' X' ' 'r8   r   c                  .    e Zd ZdZ	 	 dddZ	 	 dddZdS )	YandexGPTaV  Yandex large language models.

    To use, you should have the ``yandexcloud`` python package installed.

    There are two authentication options for the service account
    with the ``ai.languageModels.user`` role:
        - You can specify the token in a constructor parameter `iam_token`
        or in an environment variable `YC_IAM_TOKEN`.
        - You can specify the key in a constructor parameter `api_key`
        or in an environment variable `YC_API_KEY`.

    To use the default model specify the folder ID in a parameter `folder_id`
    or in an environment variable `YC_FOLDER_ID`.

    Or specify the model URI in a constructor parameter `model_uri`

    Example:
        .. code-block:: python

            from langchain_community.llms import YandexGPT
            yandex_gpt = YandexGPT(iam_token="t1.9eu...", folder_id="b1g...")
    Npromptr   r(   r'   run_manager"Optional[CallbackManagerForLLMRun]kwargsr   r0   c                L    t          | |          }|t          ||          }|S )at  Call the Yandex GPT model and return the output.

        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 = YandexGPT("Tell me a joke.")
        rQ   )completion_with_retryr   r5   rQ   r(   rR   rT   texts         r6   _callzYandexGPT._call   s0    * %T&999&tT22Dr8   'Optional[AsyncCallbackManagerForLLMRun]c                \   K   t          | |           d{V }|t          ||          }|S )a  Async call the Yandex GPT model and return the output.

        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.
        rV   N)acompletion_with_retryr   rX   s         r6   _acallzYandexGPT._acall   sF        ,D@@@@@@@@@&tT22Dr8   )NN)
rQ   r   r(   r'   rR   rS   rT   r   r0   r   )
rQ   r   r(   r'   rR   r[   rT   r   r0   r   )rJ   rK   rL   __doc__rZ   r^   r3   r8   r6   rP   rP   x   s`         4 %):>	    : %)?C	      r8   rP   r5   rQ   r   r0   c           	     f   	 dd l }ddlm}m} 	 ddlm}m} ddlm} ddl	m
} n$# t          $ r ddlm}m} ddlm} ddlm
} Y nw xY wn"# t          $ r}	t          d          |	d }	~	ww xY w|                                }
|                    | j        |
          } || j         | || j                   || j                             |d	|
          g          } ||          }|                    || j                  }t1          |          d         j        d         j        j        S )Nr   DoubleValue
Int64ValueCompletionOptionsMessage)CompletionRequest)TextGenerationServiceStubkPlease install YandexCloud SDK  with `pip install yandexcloud`             or upgrade it to recent version.valuer$   r&   userrolerY   r   completion_optionsmessagesmetadata)grpcgoogle.protobuf.wrappers_pb2rb   rc   4yandex.cloud.ai.foundation_models.v1.text_common_pb2re   rf   Pyandex.cloud.ai.foundation_models.v1.text_generation.text_generation_service_pb2rg   Uyandex.cloud.ai.foundation_models.v1.text_generation.text_generation_service_pb2_grpcrh   ModuleNotFoundError:yandex.cloud.ai.foundation_models.v1.foundation_models_pb2Byandex.cloud.ai.foundation_models.v1.foundation_models_service_pb2Gyandex.cloud.ai.foundation_models.v1.foundation_models_service_pb2_grpcImportErrorssl_channel_credentialssecure_channelr)   r   r$   r&   
Completionr/   listalternativesmessagerY   )r5   rQ   ru   rb   rc   re   rf   rg   rh   echannel_credentialschannelrequeststubress                  r6   _make_requestr      s@   HHHHHHHH	                   # 
	 
	 
	                   
	    .
 
 	
 6688!!$(,?@@G.,,#$*:;;;!z888
 
 
 'vF3334  G %$W--D
//'D,>/
?
?C99Q<$Q'/44s7   A	 $ A	 AA	 AA	 	
A(A##A(c           
       K   	 dd l }dd l}ddlm}m} 	 ddlm}m} ddlm	}m
}	 ddlm}
 n&# t          $ r ddlm}m} ddlm	}m
}	 ddlm}
 Y nw xY wddlm} ddlm} n"# t*          $ r}t+          d          |d }~ww xY wd	}|                                }|j                            | j        |          4 d {V } || j         | || j        
           || j        
                     |d|          g          } |
|          }|                    || j                   d {V }|j                            ||          4 d {V 	 } ||          }|j        sU|                     d           d {V   ||j!                  }|"                    || j                   d {V }|j        U	 d d d           d {V  n# 1 d {V swxY w Y    |	            }|j#        $                    |           |j%        d         j&        j'        cd d d           d {V  S # 1 d {V swxY w Y   d S )Nr   ra   rd   )rg   CompletionResponse)TextGenerationAsyncServiceStub)GetOperationRequest)OperationServiceStubri   z"operation.api.cloud.yandex.net:443rj   rl   rm   rn   rp   rs      )operation_id)(asyncioru   rv   rb   rc   rw   re   rf   rx   rg   r   ry   r   rz   r{   r|   r}   ,yandex.cloud.operation.operation_service_pb2r   1yandex.cloud.operation.operation_service_pb2_grpcr   r~   r   aior   r)   r   r$   r&   r   r/   donesleepidGetresponseUnpackr   r   rY   )r5   rQ   r   ru   rb   rc   re   rf   rg   r   r   r   r   r   operation_api_urlr   r   r   r   	operationoperation_channeloperation_stuboperation_requestcompletion_responses                           r6   _amake_requestr      s&     &HHHHHHHH	                     # 	 	 	                     	 	UTTTTT	
 	
 	
 	
 	
 	
 	
    .
 
 	
 =6688x&&tx1DEE @ @ @ @ @ @ @##n00'Kd.>???%:DO<<<      g67778
 
 
 .-g66//'D<N/OOOOOOOO	8**2
 
 
	 
	 
	 
	 
	 
	 
	 
	112CDDNn mmA&&&&&&&&&$7$7Y\$R$R$R!"0"4"4%!/ #5 # #      	  n  	
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
	 1022!!"5666"/2:?3@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @so   A , A  AA AA 
A>)A99A>=BH.A(G>H.
G	H.G	=H..
H8;H8llmCallable[[Any], Any]c           
         ddl m} | j        }d}t          dt	          | j                  t          d||          t          |          t          t          t          j                            S )Nr   )RpcError<   Tr   )
multiplierminmax)reraiser(   waitr   before_sleep)ru   r   r,   r   r   r+   r   r   r   loggerloggingWARNING)r   r   min_secondsmax_secondss       r6   _create_retry_decoratorr   8  st    $KK00MMM&22%fgo>>   r8   rT   r   c                N     t                     }|d fd            } |di |S )z*Use tenacity to retry the completion call._kwargsr   r0   c                     t          fi | S N)r   r   r   s    r6   _completion_with_retryz5completion_with_retry.<locals>._completion_with_retryJ  s    S,,G,,,r8   r   r   r0   r   r3   r   r   rT   retry_decoratorr   s   `   r6   rW   rW   F  sQ    -c22O- - - - - _- "!++F+++r8   c                ^    K   t                     }|d fd            } |di | d{V S )z0Use tenacity to retry the async completion call.r   r   r0   c                 .   K   t          fi |  d {V S r   )r   r   s    r6   r   z6acompletion_with_retry.<locals>._completion_with_retryU  s/      #C337333333333r8   Nr   r3   r   r   s   `   r6   r]   r]   Q  sg      -c22O4 4 4 4 4 _4 ('11&111111111r8   )r5   rP   rQ   r   r0   r   )r   rP   r0   r   )r   rP   rT   r   r0   r   ))
__future__r   r   typingr   r   r   r   r   r	   langchain_core.callbacksr
   r   #langchain_core.language_models.llmsr    langchain_core.load.serializabler   langchain_core.utilsr   r   r   pydanticr   tenacityr   r   r   r   r   langchain_community.llms.utilsr   	getLoggerrJ   r   r   rP   r   r   r   rW   r]   r3   r8   r6   <module>r      s   " " " " " "  @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @        4 3 3 3 3 3 9 9 9 9 9 9 V V V V V V V V V V                    ? > > > > >		8	$	$Z Z Z Z Z\ Z Z ZzE E E E E E E EP/5 /5 /5 /5dC@ C@ C@ C@L   , , , ,2 2 2 2 2 2r8   