classYandexGPTEmbedding(BaseEmbedding):""" A class representation for generating embeddings using the Yandex Cloud API. Args: api_key (Optional[str]): An API key for Yandex Cloud. model_name (str): The name of the model to be used for generating embeddings. The class ensures that this model is supported. Defaults to "general:embedding". embed_batch_size (int): The batch size for embedding. Defaults to DEFAULT_EMBED_BATCH_SIZE. callback_manager (Optional[CallbackManager]): Callback manager for hooks. Example: . code-block:: python from llama_index.embeddings.yandexgpt import YandexGPTEmbedding embeddings = YandexGPTEmbedding( api_key="your-api-key", folder_id="your-folder-id", ) """api_key:str=Field(description="The YandexGPT API key.")folder_id:str=Field(description="The folder id for YandexGPT API.")retries:int=6sleep_interval:float=0.1def__init__(self,api_key:Optional[str]=None,folder_id:Optional[str]=None,model_name:str="general:embedding",embed_batch_size:int=DEFAULT_EMBED_BATCH_SIZE,callback_manager:Optional[CallbackManager]=None,**kwargs:Any,)->None:ifnotapi_key:raiseValueError("You must provide an API key or IAM token to use YandexGPT. ""You can either pass it in as an argument or set it `YANDEXGPT_API_KEY`.")ifnotfolder_id:raiseValueError("You must provide catalog_id to use YandexGPT. ""You can either pass it in as an argument or set it `YANDEXGPT_CATALOG_ID`.")api_key=get_from_param_or_env("api_key",api_key,"YANDEXGPT_KEY")folder_id=get_from_param_or_env("folder_id",folder_id,"YANDEXGPT_CATALOG_ID")super().__init__(model_name=model_name,api_key=api_key,folder_id=folder_id,embed_batch_size=embed_batch_size,callback_manager=callback_manager,**kwargs,)def_getModelUri(self,is_document:bool=False)->str:"""Construct the model URI based on whether the text is a document or a query."""returnf"emb://{self.folder_id}/text-search-{'doc'ifis_documentelse'query'}/latest"@classmethoddefclass_name(cls)->str:"""Return the class name."""return"YandexGPTEmbedding"def_embed(self,text:str,is_document:bool=False)->List[float]:""" Embeds text using the YandexGPT Cloud API synchronously. Args: text: The text to embed. is_document: Whether the text is a document (True) or a query (False). Returns: A list of floats representing the embedding. Raises: YException: If an error occurs during embedding. """payload={"modelUri":self._getModelUri(is_document),"text":text}header={"Content-Type":"application/json","Authorization":f"Api-Key {self.api_key}","x-data-logging-enabled":"false",}try:forattemptinRetrying(stop=stop_after_attempt(self.retries),wait=wait_fixed(self.sleep_interval),):withattempt:response=requests.post(DEFAULT_YANDEXGPT_API_BASE,json=payload,headers=header)response=response.json()if"embedding"inresponse:returnresponse["embedding"]raiseYException(f"No embedding found, result returned: {response}")exceptRetryError:raiseYException(f"Error computing embeddings after {self.retries} retries. Result returned:\n{response}")asyncdef_aembed(self,text:str,is_document:bool=False)->List[float]:""" Embeds text using the YandexGPT Cloud API asynchronously. Args: text: The text to embed. is_document: Whether the text is a document (True) or a query (False). Returns: A list of floats representing the embedding. Raises: YException: If an error occurs during embedding. """payload={"modelUri":self._getModelUri(is_document),"text":text}header={"Content-Type":"application/json","Authorization":f"Api-Key {self.api_key}","x-data-logging-enabled":"false",}try:forattemptinRetrying(stop=stop_after_attempt(self.retries),wait=wait_fixed(self.sleep_interval),):withattempt:asyncwithaiohttp.ClientSession()assession:asyncwithsession.post(DEFAULT_YANDEXGPT_API_BASE,json=payload,headers=header)asresponse:result=awaitresponse.json()if"embedding"inresult:returnresult["embedding"]raiseYException(f"No embedding found, result returned: {result}")exceptRetryError:raiseYException(f"Error computing embeddings after {self.retries} retries. Result returned:\n{result}")def_get_text_embedding(self,text:str)->List[float]:"""Get text embedding sync."""returnself._embed(text,is_document=True)def_get_text_embeddings(self,texts:List[str])->List[List[float]]:"""Get list of texts embeddings sync."""embeddings=[]fortextintexts:embeddings.append(self._embed(text,is_document=True))time.sleep(self.sleep_interval)returnembeddingsdef_get_query_embedding(self,text:str)->List[float]:"""Get query embedding sync."""returnself._embed(text,is_document=False)asyncdef_aget_text_embedding(self,text:str)->List[float]:"""Get query text async."""returnawaitself._aembed(text,is_document=True)asyncdef_aget_text_embeddings(self,texts:List[str])->List[List[float]]:"""Get list of texts embeddings async."""embeddings=[]fortextintexts:embeddings.append(awaitself._aembed(text,is_document=True))awaitasyncio.sleep(self.sleep_interval)returnembeddingsasyncdef_aget_query_embedding(self,text:str)->List[float]:"""Get query embedding async."""returnawaitself._aembed(text,is_document=False)