o
    Zh2                     @  s  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e Z!G dd deZ"G dd de"eZ#d!ddZ$d!ddZ%d"ddZ&d#ddZ'd#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                   @  s   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!d"Zed+d$d%Zed,d(d)ZdS )-_BaseYandexGPT r   	iam_tokenapi_keystr	folder_id	model_urizyandexgpt-lite
model_nameZ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                 C  s   dS )NZ
yandex_gpt selfr.   r.   V/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/llms/yandex.py	_llm_type>   s   z_BaseYandexGPT._llm_typeDict[str, Any]c                 C  s   | j | j| j| j| jdS )zGet the identifying parameters.r   r!   r#   r%   r(   r4   r/   r.   r.   r1   _identifying_paramsB   s   z"_BaseYandexGPT._identifying_paramsvaluesr   c                 C  s2  t t|ddd}||d< t t|ddd}||d< t|ddd}||d< | dkr5| dkr5td|d rWd	d
|d   fg|d< |d rV|d d|d f nd	d|d   fg|d< |d dkru|d dkrutd|d sd|d  d|d  d|d  |d< |d r|d d |S )z.Validate that iam token exists in environment.r   ZYC_IAM_TOKENr   r   Z
YC_API_KEYr   Z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   Zget_secret_value
ValueErrorappend)clsr6   r   r   r   r.   r.   r1   validate_environmentM   s<    z#_BaseYandexGPT.validate_environment)r-   r   )r-   r3   )r6   r   r-   r   )__name__
__module____qualname__r   __annotations__r   r   r   r   r   r!   r#   r%   r&   r(   r)   r+   r,   propertyr2   r5   r   r=   r.   r.   r.   r1   r      sD   
 
r   c                   @  s0   e Zd ZdZ		ddddZ		d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   r-   c                 K  s"   t | |d}|dur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.")
        rD   N)completion_with_retryr   r0   rD   r%   rE   rG   textr.   r.   r1   _call   s   
zYandexGPT._call'Optional[AsyncCallbackManagerForLLMRun]c                   s*   t | |dI dH }|dur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.
        rH   N)acompletion_with_retryr   rJ   r.   r.   r1   _acall   s
   
zYandexGPT._acall)NN)
rD   r   r%   r$   rE   rF   rG   r   r-   r   )
rD   r   r%   r$   rE   rM   rG   r   r-   r   )r>   r?   r@   __doc__rL   rO   r.   r.   r.   r1   rC   x   s    rC   r0   rD   r   r-   c              
   C  s  zBdd l }ddlm}m} zddlm}m} ddlm} ddl	m
} W n ty@   ddlm}m} ddlm} ddlm
} Y nw W n tyS }	 ztd|	d }	~	ww | }
|| j|
}|| j||| jd|| jdd|d	|d
gd}||}|j|| jd}t|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ZrolerK   r   Zcompletion_optionsmessagesmetadata)grpcgoogle.protobuf.wrappers_pb2rR   rS   4yandex.cloud.ai.foundation_models.v1.text_common_pb2rU   rV   Pyandex.cloud.ai.foundation_models.v1.text_generation.text_generation_service_pb2rW   Uyandex.cloud.ai.foundation_models.v1.text_generation.text_generation_service_pb2_grpcrX   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messagerK   )r0   rD   rc   rR   rS   rU   rV   rW   rX   echannel_credentialschannelrequeststubresr.   r.   r1   _make_request   sD   

ry   c              
     s   zVdd l }dd l}ddlm}m} zddlm}m} ddlm	}m
}	 ddlm}
 W n tyI   ddlm}m} ddlm	}m
}	 ddlm}
 Y nw ddlm} ddlm} W n tyh } ztd|d }~ww d	}| }|j| j|4 I d H }|| j||| jd
|| jd
d|d|dgd}|
|}|j|| jdI d H }|j||4 I d H .}||}|js| dI d H  ||j!d}|j"|| jdI d H }|jrW d   I d H  n1 I d H sw   Y  |	 }|j#$| |j%d j&j'W  d   I d H  S 1 I d H s	w   Y  d S )Nr   rQ   rT   )rW   CompletionResponse)TextGenerationAsyncServiceStub)GetOperationRequest)OperationServiceStubrY   z"operation.api.cloud.yandex.net:443rZ   r\   r]   r^   r_   ra      )Zoperation_id)(asynciorc   rd   rR   rS   re   rU   rV   rf   rW   rz   rg   r{   rh   ri   rj   rk   Z,yandex.cloud.operation.operation_service_pb2r|   Z1yandex.cloud.operation.operation_service_pb2_grpcr}   rl   rm   Zaiorn   r&   r   r!   r#   ro   r,   donesleepidZGetresponseZUnpackrq   rr   rK   )r0   rD   r   rc   rR   rS   rU   rV   rW   rz   r{   r|   r}   rs   Zoperation_api_urlrt   ru   rv   rw   Z	operationZoperation_channelZoperation_stubZoperation_requestZcompletion_responser.   r.   r1   _amake_request   sn   

(2r   llmCallable[[Any], Any]c                 C  sD   ddl m} | j}d}tdt| jtd||dt|tt	t
jdS )Nr   )RpcError<   Tr~   )
multiplierminmax)reraiser%   waitr   before_sleep)rc   r   r)   r   r   r(   r   r   r   loggerloggingWARNING)r   r   Zmin_secondsZmax_secondsr.   r.   r1   _create_retry_decorator8  s   
r   rG   r   c                   s(   t  }|d fdd}|di |S )	z*Use tenacity to retry the completion call._kwargsr   r-   c                    s   t  fi | S N)ry   r   r   r.   r1   _completion_with_retryJ  s   z5completion_with_retry.<locals>._completion_with_retryNr   r   r-   r   r.   r   r   rG   Zretry_decoratorr   r.   r   r1   rI   F  s   rI   c                   s0   t  }|d fdd}|di |I dH S )	z0Use tenacity to retry the async completion call.r   r   r-   c                    s   t  fi | I d H S r   )r   r   r   r.   r1   r   U  s   z6acompletion_with_retry.<locals>._completion_with_retryNr   r.   r   r   r.   r   r1   rN   Q  s
   rN   )r0   rC   rD   r   r-   r   )r   rC   r-   r   )r   rC   rG   r   r-   r   ))
__future__r   r   typingr   r   r   r   r   r   Zlangchain_core.callbacksr	   r
   Z#langchain_core.language_models.llmsr   Z langchain_core.load.serializabler   Zlangchain_core.utilsr   r   r   Zpydanticr   tenacityr   r   r   r   r   Zlangchain_community.llms.utilsr   	getLoggerr>   r   r   rC   ry   r   r   rI   rN   r.   r.   r.   r1   <module>   s$     
]
H
2
F
