
    7|h                        d Z ddlmZ ddlZddlZddlmZmZmZm	Z	m
Z
 ddlmZ ddlmZmZmZ ddlmZmZmZmZ ddlmZmZmZmZmZ  ej8                  e      Z G d	 d
ee      ZddZ ddZ!ddZ"y)z*Wrapper around YandexGPT embedding models.    )annotationsN)AnyCallableDictListSequence)
Embeddings)convert_to_secret_strget_from_dict_or_envpre_init)	BaseModel
ConfigDictField	SecretStr)before_sleep_logretryretry_if_exception_typestop_after_attemptwait_exponentialc                  2   e Zd ZU dZdZded<   	 dZde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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ed<    ed d!"      Zed'd#       Zd(d$Zd)d%Zy&)*YandexGPTEmbeddingsa4  YandexGPT Embeddings 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`.

    Example:
        .. code-block:: python

            from langchain_community.embeddings.yandex import YandexGPTEmbeddings
            embeddings = YandexGPTEmbeddings(iam_token="t1.9eu...", folder_id=<folder-id>)
     r   	iam_tokenapi_keyquery_model_uri)defaultaliasstr	model_uridoc_model_uri	folder_idztext-search-docdoc_model_nameztext-search-queryquery_model_name
model_namelatestmodel_versionzllm.api.cloud.yandex.net:443url   intmax_retriesg        floatsleep_intervalFbooldisable_request_loggingr   grpc_metadataT )populate_by_nameprotected_namespacesc                   t        t        |ddd            }||d<   t        t        |ddd            }||d<   t        |ddd      }||d<   |j                         dk(  r|j                         dk(  rt        d      |d   r;d	d
|d   j                          fg|d<   |d   r6|d   j	                  d|d   f       nd	d|d   j                          fg|d<   |j                  d      s*|d   dk(  rt        d      d|d    d|d    d|d    |d<   |j                  d      s*|d   dk(  rt        d      d|d    d|d    d|d    |d<   |d   r|d   j	                  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    z0'doc_model_uri' or 'folder_id' must be provided.zemb:///r"   r&   r   z,'model_uri' or 'folder_id' must be provided.r$   r.   )zx-data-logging-enabledfalse)r
   r   get_secret_value
ValueErrorappendget)clsvaluesr   r   r!   s        d/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain_community/embeddings/yandex.pyvalidate_environmentz(YandexGPTEmbeddings.validate_environmentL   s    * nbI
	 ({' L"E
 $y(nbQ	'{##%+	0J0J0LPR0RVWW+ GF;,?,P,P,R+S"TU'F?# k"'..vk?R/ST !HVI->-O-O-Q,R"ST'F?# zz/*k"b( !STT,-Qv6F/G.H&Q`JaIbc ?# zz+&k"b( !OPP,-Qvl/C.DAf_F]E^_ ; +,?#**     c                    t        | |      S )zEmbed documents using a YandexGPT embeddings models.

        Args:
            texts: The list of texts to embed.

        Returns:
            List of embeddings, one for each text.
        )texts_embed_with_retry)selfrD   s     r@   embed_documentsz#YandexGPTEmbeddings.embed_documents|   s     !U33rB   c                &    t        | |gd      d   S )zEmbed a query using a YandexGPT embeddings models.

        Args:
            text: The text to embed.

        Returns:
            Embeddings for the text.
        T)rD   embed_queryr   rE   )rG   texts     r@   rJ   zYandexGPTEmbeddings.embed_query   s     !dVFqIIrB   N)r?   r   returnr   )rD   	List[str]rL   zList[List[float]])rK   r   rL   zList[float])__name__
__module____qualname____doc__r   __annotations__r   r   r   r    r!   r"   r$   r&   r'   r*   r,   r.   r   model_configr   rA   rH   rJ   r0   rB   r@   r   r      s    * Iy.GY.2->?Is?!M3Is +NC+ $7?QRJR"!M3!-C-K<NE$$)T)Pt"ML- -^
4	JrB   r   c           
         ddl m} d}d}t        dt        | j                        t        d||      t        |      t        t        t        j                              S )Nr   )RpcError   <   T)
multiplierminmax)reraisestopwaitr   before_sleep)grpcrU   r   r   r*   r   r   r   loggerloggingWARNING)llmrU   min_secondsmax_secondss       r@   _create_retry_decoratorrf      sM    KK0M&2%fgoo> rB   c                @     t               }|d fd       } |di |S )z)Use tenacity to retry the embedding call.c                     t        fi | S )N)_make_request)_kwargsrc   s    r@   _completion_with_retryz1_embed_with_retry.<locals>._completion_with_retry   s    S,G,,rB   )rj   r   rL   r   r0   )rf   )rc   kwargsretry_decoratorrk   s   `   r@   rF   rF      s/    -c2O- - "+F++rB   c                <   	 dd l }	 ddlm} ddlm} g }|j                         }|j                  | j                  |      }	|j                  d      r| j                  }
n| j                  }
|D ]t  } ||
|      } ||	      }|j                  || j                         }|j#                  t%        |j&                               t)        j*                  | j,                         v |S # t
        $ r ddlm} ddlm} Y w xY w# t        $ r}t        d      |d }~ww xY w)Nr   )TextEmbeddingRequest)EmbeddingsServiceStubzkPlease install YandexCloud SDK  with `pip install yandexcloud`             or upgrade it to recent version.rJ   )r   rK   )metadata)r_   Dyandex.cloud.ai.foundation_models.v1.embedding.embedding_service_pb2ro   Iyandex.cloud.ai.foundation_models.v1.embedding.embedding_service_pb2_grpcrp   ModuleNotFoundErrorByandex.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    TextEmbeddingr/   r<   list	embeddingtimesleepr,   )rG   rD   rl   r_   ro   rp   eresultchannel_credentialschannelr   rK   requeststubress                  r@   ri   ri      s   	 F668!!$((,?@Gzz- NN	&&	 (&F$W-  43E3E Fd3==)*

4&&'( M9 # 			  .
 	s4   D C& &C>;D =C>>D 	D
DD)rc   r   rL   zCallable[[Any], Any])rc   r   rl   r   rL   r   )rG   r   rD   rM   )#rQ   
__future__r   ra   r}   typingr   r   r   r   r   langchain_core.embeddingsr	   langchain_core.utilsr
   r   r   pydanticr   r   r   r   tenacityr   r   r   r   r   	getLoggerrN   r`   r   rf   rF   ri   r0   rB   r@   <module>r      sf    0 "   6 6 0 V V < <  
		8	$zJ)Z zJz,'rB   