o
    Ag2                     @  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_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                 C  s   dS )N
yandex_gpt selfr0   r0   f/var/www/html/development/chatbot/venv/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)   r6   r1   r0   r0   r3   _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   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)clsr8   r   r   r   r0   r0   r3   validate_environmentM   s<    z#_BaseYandexGPT.validate_environment)r.   r   )r.   r5   )r8   r   r.   r   )__name__
__module____qualname__r   __annotations__r   r   r   r   r    r"   r$   r&   r'   r)   r*   r,   r-   propertyr4   r7   r   rC   r0   r0   r0   r3   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.")
        rJ   N)completion_with_retryr   r2   rJ   r&   rK   rM   textr0   r0   r3   _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.
        rN   N)acompletion_with_retryr   rP   r0   r0   r3   _acall   s
   
zYandexGPT._acall)NN)
rJ   r   r&   r%   rK   rL   rM   r   r.   r   )
rJ   r   r&   r%   rK   rS   rM   r   r.   r   )rD   rE   rF   __doc__rR   rU   r0   r0   r0   r3   rI   x   s    rI   r2   rJ   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rolerQ   r   completion_optionsmessagesmetadata)grpcgoogle.protobuf.wrappers_pb2rX   rY   4yandex.cloud.ai.foundation_models.v1.text_common_pb2r[   r\   Pyandex.cloud.ai.foundation_models.v1.text_generation.text_generation_service_pb2r]   Uyandex.cloud.ai.foundation_models.v1.text_generation.text_generation_service_pb2_grpcr^   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messagerQ   )r2   rJ   rk   rX   rY   r[   r\   r]   r^   echannel_credentialschannelrequeststubresr0   r0   r3   _make_request   sD   

r   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   rW   rZ   )r]   CompletionResponse)TextGenerationAsyncServiceStub)GetOperationRequest)OperationServiceStubr_   z"operation.api.cloud.yandex.net:443r`   rb   rc   rd   rf   ri      )operation_id)(asynciork   rl   rX   rY   rm   r[   r\   rn   r]   r   ro   r   rp   rq   rr   rs   ,yandex.cloud.operation.operation_service_pb2r   1yandex.cloud.operation.operation_service_pb2_grpcr   rt   ru   aiorv   r'   r   r"   r$   rw   r-   donesleepidGetresponseUnpackry   rz   rQ   )r2   rJ   r   rk   rX   rY   r[   r\   r]   r   r   r   r   r{   operation_api_urlr|   r}   r~   r   	operationoperation_channeloperation_stuboperation_requestcompletion_responser0   r0   r3   _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)rk   r   r*   r   r   r)   r   r   r   loggerloggingWARNING)r   r   min_secondsmax_secondsr0   r0   r3   _create_retry_decorator8  s   
r   rM   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)r   r   r   r0   r3   _completion_with_retryJ  s   z5completion_with_retry.<locals>._completion_with_retryNr   r   r.   r   r0   r   r   rM   retry_decoratorr   r0   r   r3   rO   F  s   rO   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   r0   r3   r   U  s   z6acompletion_with_retry.<locals>._completion_with_retryNr   r0   r   r   r0   r   r3   rT   Q  s
   rT   )r2   rI   rJ   r   r.   r   )r   rI   r.   r   )r   rI   rM   r   r.   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   	getLoggerrD   r   r   rI   r   r   r   rO   rT   r0   r0   r0   r3   <module>   s$     
]
H
2
F
