a
    bg2                     @  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dddZ$ddddddZ%dddddZ&ddddddZ'ddd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!d"Zed#d d$d%Zed&d&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.   m/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/llms/yandex.py	_llm_type>   s    z_BaseYandexGPT._llm_typezDict[str, Any]c                 C  s   | j | j| j| j| jdS )zGet the identifying parameters.r   r!   r#   r%   r(   r3   r/   r.   r.   r1   _identifying_paramsB   s    z"_BaseYandexGPT._identifying_paramsr   )valuesr-   c                 C  s6  t t|ddd}||d< t t|ddd}||d< t|ddd}||d< | dkrj| dkrjtd|d rd	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rtd|d sd|d  d|d  d|d  |d< |d r2|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)clsr5   r   r   r   r.   r.   r1   validate_environmentM   s:    
 
z#_BaseYandexGPT.validate_environment)__name__
__module____qualname__r   __annotations__r   r   r   r   r   r!   r#   r%   r&   r(   r)   r+   r,   propertyr2   r4   r   r<   r.   r.   r.   r1   r      s(   

r   c                   @  s@   e Zd ZdZddddddddd	Zdddd
d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...")
    Nr   r$   z"Optional[CallbackManagerForLLMRun]r   )promptr%   run_managerkwargsr-   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.")
        rC   N)completion_with_retryr   r0   rC   r%   rD   rE   textr.   r.   r1   _call   s    
zYandexGPT._callz'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.
        rF   N)acompletion_with_retryr   rH   r.   r.   r1   _acall   s    
zYandexGPT._acall)NN)NN)r=   r>   r?   __doc__rJ   rL   r.   r.   r.   r1   rB   x   s       rB   r   )r0   rC   r-   c              
   C  s*  zd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0 W n. ty }	 ztd|	W Y d }	~	n
d }	~	0 0 | }
|| 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rolerI   r   Zcompletion_optionsmessagesmetadata)grpcgoogle.protobuf.wrappers_pb2rO   rP   4yandex.cloud.ai.foundation_models.v1.text_common_pb2rR   rS   Pyandex.cloud.ai.foundation_models.v1.text_generation.text_generation_service_pb2rT   Uyandex.cloud.ai.foundation_models.v1.text_generation.text_generation_service_pb2_grpcrU   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messagerI   )r0   rC   r`   rO   rP   rR   rS   rT   rU   echannel_credentialschannelrequeststubresr.   r.   r1   _make_request   s<    

rv   c              
     s0  zdd l }dd l}ddlm}m} z0ddlm}m} ddlm	}m
}	 ddlm}
 W n> ty   ddlm}m} ddlm	}m
}	 ddlm}
 Y n0 ddlm} ddlm} W n. ty } ztd|W Y d }~n
d }~0 0 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 }qtW d   I d H  q1 I d H s0    Y  |	 }|j#$| |j%d j&j'W  d   I d H  S 1 I d H s"0    Y  d S )Nr   rN   rQ   )rT   CompletionResponse)TextGenerationAsyncServiceStub)GetOperationRequest)OperationServiceStubrV   z"operation.api.cloud.yandex.net:443rW   rY   rZ   r[   r\   r^      )Zoperation_id)(asyncior`   ra   rO   rP   rb   rR   rS   rc   rT   rw   rd   rx   re   rf   rg   rh   Z,yandex.cloud.operation.operation_service_pb2ry   Z1yandex.cloud.operation.operation_service_pb2_grpcrz   ri   rj   Zaiork   r&   r   r!   r#   rl   r,   donesleepidZGetresponseZUnpackrn   ro   rI   )r0   rC   r|   r`   rO   rP   rR   rS   rT   rw   rx   ry   rz   rp   Zoperation_api_urlrq   rr   rs   rt   Z	operationZoperation_channelZoperation_stubZoperation_requestZcompletion_responser.   r.   r1   _amake_request   s`    

>r   zCallable[[Any], Any])llmr-   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)r`   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   r   )r   rE   r-   c                   s.   t  }|ddd fdd}|f i |S )z*Use tenacity to retry the completion call.r   _kwargsr-   c                    s   t  fi | S N)rv   r   r   r.   r1   _completion_with_retryJ  s    z5completion_with_retry.<locals>._completion_with_retryr   r   rE   Zretry_decoratorr   r.   r   r1   rG   F  s    rG   c                   s4   t  }|ddd fdd}|f i |I dH S )z0Use tenacity to retry the async completion call.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   r1   rK   Q  s    rK   ))
__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   rB   rv   r   r   rG   rK   r.   r.   r.   r1   <module>   s"    
]H2F